- Home /
GetPixel Returning Inaccurate Color
I have a script that simply assigns a float value to the alpha channel of a pixel in a texture, however when calling get pixel on the exact same position the value is slightly off which is actually causing a big problem in my case. The code is all fairly simple:
texture.SetPixel(x, y, new Color (0, 0, 0, originalValue));
float returnedValue = texture.GetPixel(x, y).a;
print("0v: " +originalValue + " Rv: " +returnedValue);
Here are the results:
GetPixel seems to be rounding numbers to seemingly arbitrary values. Once again these values have to be exactly the same, I can't really work around that in any way. Please tell me there is a fix for this. Is there?
Answer by Graham-Dunnett · Jun 06, 2014 at 10:40 AM
If you want the data to be exactly the same you'll need to create a texture that uses a floating point format for RGBA. That probably means you need to create a RenderTexture
in the ARGBFloat
format. To understand what you see, multiply your originalValue
by 255, and remove the fractional part, to get the value that will be stored in your 32-bit RGBA texture. Then divide that number by 255 and you'll see the returnedValue
. There's nothing arbitrary going on.
Ah, I knew that the conversion from Color to Color32 had to convert from the 0...1 system to the 0...255 system, but I didn't know that it removed the fractional part of the values as well. So, just to confirm this, I have to use render textures if I don't want the fractional part removed?