1
\$\begingroup\$


I am using Unity and I can choose between two color space mode in the settings: Gamma or Linear Space.

I am trying to build a Custom Lighting Surface shader but I am facing some problems with those Color Space. Because the render is not the same depending of the Color Space.

If I render the lightDir, Normal or viewDir I can see that they are different depending of the Color Space I use.

I made some test and the result I have in Linear Space is great but how can I obtain the same result in Gamma Space ? Are there some transformations ? On what component should I apply those transformations ?

Thank you very much !

\$\endgroup\$
4
\$\begingroup\$

First of all, you don't do your render in gamma space to get the same results as in linear, but to have "gamma correct" final image. The topic is a bit complicated, so see the link in the end of the answer if you want to learn about it in-depth. The process of applying gamma correction to your scene follows: (I've seen this in a presentation from a graphics programmer at Naugty Dog, the creators of Uncharted.)
You have to sample your diffuse color in shader (be it texture or material color), then take the float4 color = pow(diffuse,2.2f); //2.2f is the default gamma value of it then apply your lighting calculations. After all your lighting is calculated and applied to the color, you have to take the power of the inverse of your gamma eg. 1.0f/2.2f for your final color.
So in the end you have the following process (taken from the presentation):

  • Account for gamma when reading textures: color = pow( tex2D( Sampler, Uv ), 2.2 )‏
  • Do your lighting calculations
  • Account for gamma on color output: finalcolor = pow( color, 1/2.2 )‏

For full explanations, please see this presentation

\$\endgroup\$
  • 1
    \$\begingroup\$ You may have to implement blending yourself if you do this. By converting from gamma-correct color to linear at the end of a shader, fixed-function blending, which occurs after the shader executes will be done in linear space :-\ \$\endgroup\$ – Andon M. Coleman May 5 '14 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.