I need help i have to read a 2d texture put it into an array then put it on to cubes need help reading colours

public GameObject redCube;
public GameObject blueCube;
public GameObject yellowCube;
public GameObject greenCube;

    public Texture2D importTexture;
    private Color[] pixels;

    // Use this for initialization
    void Start () {
    CreateWorld();
    

}

void CreateWorld() 
{

    pixels = importTexture.GetPixels(0, 0, importTexture.width, importTexture.height);
  

    for (int x = 0; x < importTexture.width; x++)
    {
        for (int z = 0; z < importTexture.height; z++)
        {
            Color color = pixels[(x * importTexture.width) + z];

            if (color == Color.red)
            {
                Instantiate(redCube, new Vector3(x * 2.0f, 0.0f, z * 2.0f), Quaternion.identity);
            }
            else if (color == Color.blue)
                Instantiate(blueCube, new Vector3(x * 2.0f, 0.0f, z * 2.0f), Quaternion.identity);
        }
    }
    
}

}

Is this a surface shader or a regular vert/frag shader? From memory surface shaders don't work well with Blit: https://forum.unity3d.com/threads/shaders-not-working-in-graphics-blit-when-using-my-own-rendertexture.98161/

1 Answer

1

What exactly isn’t working for you?
Is it just getting the correct color from the pixel array?
This should work:

 Color color =  pixels[importTexture.width * z + x];

Hope this helps!

It took some time, but I figured out! Yes, it is surface shader, so it does not work, but it can be quite easily converted in fragment shader and it starts to work perfect! Thanks you! :) If someone will encounter the same problem - let me know, I will describe solution in more details.

Thanks Artaani, I got it working! There's a problem with PerlinRidged though. It shows up fine, but it generates data quite differently and is no longer infinite when offset. Is this error my doing or from the frag computation? The PerlinRidged function and supporting function are the same in both shaders. [89524-testingunitypackage.zip|89524] [89523-screen-shot-2017-03-07-at-35539-pm.png|89523]