Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Color gradients in linear color space?

Discussion in 'General Graphics' started by battou, Mar 4, 2015.

  1. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    I've done atmosphere shader that go from blue to orange on sunset, in gamma space its ok, from orange through some pale colors to blue, but in linear its going through som asidish pink and purple to blue.( Any way to fix it?
     
  2. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    Use GammaToLinearSpace (from UnityCG.cginc)
     
  3. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    Thanks. ) But can you be more specific? Im new to shaders. I pass lerp coef in it?
     
  4. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    You can use the helper functions in the cg include to convert values from gamma space to linear space.
    float3 gammaSpaceColor = GammaToLinearSpace(linearSpaceColor);

    Ideally you should only use linear space but if you are targeting devices that doesn't support it, you have to make sure your shaders in gamma space look as close to the linear space version as possible. Linear space should be your starting point with gamma space being the fallback for less capable devices.
     
  5. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    But the issue is not with my pc cant handle linear color space, I have GTX770, its with the fact that lerping from orange to blue goes through different colors in gamma and linear color spaces in editor. Its like gamma goes upthe swatch through green and linear goes down through red and violet. Will post screenshots when return home.
     
  6. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
  7. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    So there are no easy way to lerp like in gamma? Or this helper function will do what I want? Im a bit confused.
     
  8. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    The color space you choose with affect the output and the helper function is, well... just a helper. You should do all your math in linear space and only do the gamma conversion as the final step.
     
  9. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    Will try. Thanks.)