Unity GetPixels and GetPixel return differently

Viewed 119
/// <summary>
    /// generates a heightmap from given greyscale texture.
    /// </summary>
    /// <param name="texture"> If the texture * scale is smaller than heightmap, then the texture will get repeated over the heightmap. </param>
    /// <param name="heightMapResolution"> the resolution of the heightmap generated </param>
    /// <param name="scale"> how the height map will get scaled in each direction along the texture. set to Vector3.one for no scaling.</param>
    public static float[,] ConvertGrayScaleTextureToHeightMap(Texture2D texture, int heightMapResolution, Vector3 scale)
    {
        float[,] heightMap = new float[heightMapResolution, heightMapResolution];
        Color[] colorMap = texture.GetPixels();

        for (int y = 0; y < heightMapResolution; y++)
        {
            for (int x = 0; x < heightMapResolution; x++)
            {
                if (texture.GetPixel(x, y) != colorMap[y * heightMapResolution + x])
                {
                    Debug.LogError("wtf at (" + x + "," + y + ").");
                    Debug.LogError("res: " + heightMapResolution + "\n texture width: " + texture.width + "\n texture height: " + texture.height + "\n 1-D accessed at: " + (y * heightMapResolution + x));
                    return heightMap;
                }
            }
        }

        return heightMap;
    }

This is what the code looks like. Debug returns "wtf at (305,1). res: 513 texture width: 1024 texture height: 1024 1-D accessed at: 818"

Am I making an extremely stupid mistake? GetPixel returns correctly when I translate to heightmap data but I get a spikey landscape when I use GetPixels.

I wanted to use GetPixels as it is faster. What am I missing here?

0 Answers
Related