So I'm trying to use GetPixel() function to get which colors is the texture as a value of x and y. So far it works well but it only return 1 color, being unable to get the rest of the colors of the copied texture.
Let's consider this texture, my one is quite similar, I'll update as soon as I can. But it will work as example, basically, GetPixel() is only returning the red value while the rest are like doesn't exist at all. I leave here the code:
EDITED CODE
public Texture2D oldTexture = GetDistortedGradient(1f);
public Texture2D GetTexture(){
var texture = new Texture2D(width, height);
var pixels = new Color[width * height];
for(var x = 0; x < width; x++){
for(var y = 0; y < height; y++){
Color newPixels = oldTexture.GetPixel(x, y);
if(newPixels == hotest){
pixels[x + y * width] = Color.red;
}
else if (newPixels == coldest){
pixels[x + y * width] = Color.blue;
}
else{
pixels[x + y * width] = Color.white;
}
}
}
texture.SetPixels(pixels);
texture.Apply();
return texture;
}
What I'm trying to do is whenever that texture is "color1" set the new texture as a red color on the specified x and y coordinate, then check again and if the texture is "color2" on the news x and y, set to blue, and so on. However, this is only returning color1 for the correctly x, y coordinates and after that it jumps to the else function, like if color2 never existed on the old texture. Btw, as this is an example, the oldTexture is generated procedurally but the colors are set in the inspector, so I just take the color I've set, but i don't why it only keep returning 1 of many colors, when basically it check for all colors. Am I missing anything or am I not understanding GetPixel() function and how it work? Any help will be appreciated!
UPDATED WITH OLDTEXTURE CODE
public Texture2D GetDistortedGradient(float noiseStrength)
{
var texture = new Texture2D(heatMap.Width, heatMap.Height);
var pixels = new Color[heatMap.Width * heatMap.Height];
for (var x = 0; x < heatMap.Width; x++)
{
for (var y = 0; y < heatMap.Height; y++)
{
float noise = GetNoise(x, y);
int shift = Mathf.RoundToInt(noise * noiseStrength);
pixels[x + y * heatMap.Width] = GetGradientColor(x, y + shift);
}
}
//And apply texture ofc
}
//And here the colors to get
Color GetGradientColor(int x, int y) {
if (y <= southPoleCap || y >= northPoleCap)
return coldest;
else if (y > southPoleCap && y <= southArcticCap || y < northPoleCap && y >= northArcticCap)
return colder;
else if (y > southArcticCap && y <= southArcticOuter || y < northArcticCap && y >= northArcticOuter)
return cold;
else if (y > southArcticOuter && y <= southMiddleOuter || y < northArcticOuter && y >= northMiddleOuter)
return template;
else if (y > southMiddleOuter && y <= southTropic || y < northMiddleOuter && y >= northTropic)
return hot;
else if (y > southTropic && y <= southMiddleInner || y >= northMiddleInner && y < northTropic)
return hotter;
else
return hottest;
}
I'm not at home right now, so I can't give the new code I've got for that, but that's the proper base, just few really small changes So, once the GetDistortedGradient texture is done, it should go to the GetTexture and create based on whatever colors there's on the distorted texture
Color32
andoldTexture.GetPixels32()
instead? \$\endgroup\$oldTexture
here? It might be relevant if the texture is generated in such a way that its pixels aren't readable by the time this script runs. \$\endgroup\$