Loading 64*5 from a 64*64 Color[][] Array

Hello,

New try with a new project design highlighting the problem.
Therefore i deleted my old post.

With the following code i try to make a plane with a user specific material on it.
To do this I have a image that contains all the tiles to make this material.
For this example i created this img:

102818-bgimgtest.png

If u add this image in a new project and add a plane and a empty material the following code will run after some settings:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class test : MonoBehaviour {
    public int size_x;
    public int size_z;
    public float tileSize = 1.0f;
    public int tileResolution;
    public Texture2D terrainTiles;
    int[,] textureTile;
    int number;

    void Loadmap()
    {
        //toekomst map uit database halen
        textureTile = new int[size_z, size_x];
        for (int z = 0; z < size_z; z++)
        {
            for (int x = 0; x < size_x; x++)
            {
                if (z == 0)
                {
                    textureTile[z, x] = 0;
                }
                else
                { 
                    textureTile[z, x] = 2;
                }
            }
        }
    }

    void Start()
    {
        int textWidth = size_x * tileResolution;
        int textHeight = size_z * tileResolution;
        Texture2D texture = new Texture2D(textWidth, textHeight);
        Texture2D normal = new Texture2D(textWidth, textHeight, TextureFormat.ARGB32, true);

        Loadmap();
        Color[][] tiles = chopUpTiles();

        for (int z = 0; z < size_z; z++)
        {
            for (int x = 0; x < size_x; x++)
            {
                Color[] p = tiles[textureTile[z, x]];

                texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, p);


                if (z > 0)//borderchecker
                {
                    if (!(textureTile[z - 1, x] == textureTile[z, x]))
                    {
                        Color[] t = tiles[textureTile[z, x] + 1];
                        Color[] v = tiles[textureTile[z - 1, x] + 1];

                        texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, 5, t);
                        texture.SetPixels(x * tileResolution, z * tileResolution - 5, tileResolution, 5, v);
                    }
                }
            }
        }

        texture.filterMode = FilterMode.Bilinear;
        texture.wrapMode = TextureWrapMode.Clamp;




        texture.Apply();


        MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
        mesh_renderer.sharedMaterials[0].mainTexture = texture;

    }

    Color[][] chopUpTiles()
    {
        int numTiles_x = terrainTiles.width / tileResolution;
        int numTiles_z = terrainTiles.height / tileResolution;

        Color[][] tiles = new Color[numTiles_x * numTiles_z][];

        for (int z = 0; z < numTiles_z; z++)
        {
            for (int x = 0; x < numTiles_x; x++)
            {
                tiles[z * numTiles_x + x] = terrainTiles.GetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution);
            }
        }
        return tiles;
    }

    
}

the settings are:

  • The image needs to be Read/Write Enabled in the inspector

  • ( edit forgot this) Set the img on clamp and Mobile defuse in the inspector.

  • drag the code on the plane

this enables the settings. select the plane and fill the public variables with info :slight_smile:

  • Size_x = 2

  • Size_z = 2

  • Tile Size = 1

  • Tile Resolution = 64

  • Drag the image on Terrain Tiles

Also set the empty material to the Plane.

If u run the project you can see it adds the border (right side of the image) correctly but it shows the bottom border of the image on the green and the blue square while the blue one should show the top one.

I know i should address the Color for the top part but i can’t figure out how to do so.
Got that part from a tutorial i followed before i went in a different direction.

Please help me out.

Nivey

I just thought of another option.
instead of loading the desired array in another array i could paint it on a material 64*64 and then grab the desired field from there with getpixels. This will have the same amount of code lines but needs another texture in code.

I think i go for this or is this bad too?
@MaxGuernseyIII I read you got tons of experience and since i only work with c# and unity for 3 months, I do know other programming languages for many years now, it would not be wise to go against your idea. So I’m curious what your thoughts are for this approach?
Also thanks for telling me i can address the color array from its first array instead of loading a second.

Regards Nivey