Every object on scene seen by this camera have the Unlit/Color shader, so the camera only see their pure material color ;
Every object on scene chose on Start() a unique Color, apply it to its Material, then adds it to a List ;
I transfer pixels from the RenderTexture to a Texture2D with Texture2D.ReadPixels(), then I transfer all the Texture2D pixels in a Color array with Texture2D.GetPixels();
I do a foreach loop on every Color in the Color array, and see if the pixel color matches a Color in the List ;
And now my problem :
The colors match when the Objects on scene chose pure colors like Color.red or Color.white, but never with more subtle tints like Yellow or anything else ;
I found that the problem resids in a conversion from Linear to Gamma space : a material color (0.5, 0.5, 0.5) gives a color (0.216, 0.216, 0.216) in the Color array.
Problem is : Iāve done everything to make the colors match. I have checked that my project Lighting is in Linear space, that the Texture2D is in Linear space, etc.
Even if the floats in the colors are the same, the comparaison do not match
So now I have a Material.color of rgba(0.5, 0.5, 0.5, 1), I convert it so it is stored in the Color as (0.216, 0.216, 0.216, 1), and it is identified in the Texture2D pixels as (0.216, 0.216, 0.216, 1), but the comparaison says itās not the same color, so I canāt match them even if they have the exact same values
Please⦠has someone any idea on how to resolve this problem ?
Iām not really sure what the problem is, but maybe I can help you debug.
Iām sure youāve probably already noticed, but Color does have a .gamma and a .linear property that can convert back and forth between gamma space and linear color space.
I assume this means you have two colors which you believe should be the same when you compare them, but you arenāt getting that result from the comparison. Just to see if maybe thereās a little floating point difference to ruin the comparison, check the difference between the two colors.
What I mean by that is if you have color āaā and color ābā, make āColor c = b - aā. Components of colors can go negative, so you can check the components of c. If all components of c are 0, then it is not a floating point problem and youāll have to try to find out how to test between the color spaces.
Iāll keep helping where I can if you keep me posted.
1. When you define (create) a Color, itās already in āGamma spaceā.
In my projet, the color was converted because I assumed that you created colors in Linear, and then it was converted to Gamma, but it is actually the opposite :
If you set your RenderTexture and Texture2D to āLinearā, it will convert the color from Gamma back to Linear.
Solution : you stick to āsRGBā (constructor default setting) and it will not convert anything.
2. Color create floats inaccuracy, use Color32 instead.
Even when sticking with sRGB (Gamma) colors, a (0.5, 0.5, 0.5, 1) defined Color was read as (0.502, 0.502, 0.502, 1) in the texture pixel.
So I changed everything to Color32 and the bytes channels stay accurate
(128, 128, 128, 256)