- Home /
Getpixels and Getpixels32 doubles and clips RGBA pixel values
I'm writing a texture to image using
for (int y = 0; y < local_texture.height; y++)
{
for (int x = 0; x < local_texture.width; x++)
{
//first find the index!
int location = XYToArray(x, y);
int height = X_height_list[location];
float color_r = Mathf.FloorToInt((float)height / 256); //max height is 65536 add check later
float color_g = height - (color_r * 256);
float color_b = 128;// Mathf.RoundToInt(height - ((color_r * 65536)+ (color_g * 256)));
Color32 color = (new Color((float)color_r/256, (float)color_g / 256, (float)color_b / 256, 1));
local_texture.SetPixel(x, y, color);
}
}
local_texture.Apply();
This works very nicely, and I save it with
byte[] bytes = heightmap.EncodeToPNG();
File.WriteAllBytes(Directory.GetParent(Application.dataPath) + "Captures/" + "heightmap.png", bytes);
I then load it again later with
LoadedMap = new Texture2D(arrayside, arrayside);
LoadedMap.LoadImage(System.IO.File.ReadAllBytes(Directory.GetParent(Application.dataPath) + "Captures/" + "heightmap.png"));
Debug.Log("loaded image");
Color32[] pixels = LoadedMap.GetPixels32();
If I write the LoadedMap Texture2D back to an image via "byte[] bytesout = LoadedMap.EncodeToPNG();" I get an exact copy.
But if I read "Color colorheight = pixels[(cx * arrayside) + cy];" then for many pixels colorheight.r goes up to 1 while no pixel should be higher than 0.7. It seems the 'clip' occurs for all pixels higher than around 0.5 so maybe it's doubling the RGBA values?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612211358im_/https://answers.unity.com/themes/thub/images/avi.jpg)