- Home /
Question by
danielniemiec44_unity · Oct 15, 2021 at 11:55 AM ·
colortexture2dsetpixels
Texture randomly generated terrain
I have generated terrain (a plane) using perlin noise and I'm trying to texture this plane using selected texture and using color map but it shows me only one color.
private Texture2D BuildTexture(float[,] heightMap) {
int tileDepth = heightMap.GetLength (0);
int tileWidth = heightMap.GetLength (1);
Color[] colorMap = new Color[tileDepth * tileWidth];
for (int zIndex = 0; zIndex < tileDepth; zIndex+=500) {
for (int xIndex = 0; xIndex < tileWidth; xIndex+=500) {
TerrainType terrainType = ChooseTerrainType (heightMap, zIndex, xIndex);
//float height = heightMap [zIndex, xIndex];
Texture2D texture = terrainType.texture;
for(int z = 0;z < 500 && z + zIndex < tileDepth;z++){
for(int x = 0;x < 500 && x + xIndex < tileWidth;x++){
int colorIndex = (z + zIndex) * tileWidth + (x + zIndex);
colorMap[colorIndex] = texture.GetPixel(x, z);
//Debug.Log(texture.GetPixel(x, z));
}
}
//colorMap[colorIndex] = terrainType.color;
}
}
// create a new texture and set its pixel colors
Texture2D tileTexture = new Texture2D (tileWidth, tileDepth);
tileTexture.wrapMode = TextureWrapMode.Clamp;
Debug.Log(string.Join(", ", colorMap));
tileTexture.SetPixels (colorMap);
tileTexture.Apply ();
return tileTexture;
}
screen.png
(353.9 kB)
Comment
Your answer
Follow this Question
Related Questions
Convert 2D array to a 1D array 2 Answers
Changing color of Texture2d with transparency 1 Answer
Convert a byte array to color data 0 Answers
For different texture sizes, which is faster? SetPixel or SetPixels? 2 Answers
How can I efficiently determine that certain parts of a texture have a specific color? 1 Answer