Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is unity_ColorSpaceGrey

Discussion in 'Shaders' started by MaT227, Jun 16, 2014.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    I just discovered unity_ColorSpaceGrey but what is it ?
    How can I use it ?
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    It's a gamma correct value of grey that is 'correct' for the given color space.
     
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thank you for your answer, can I use this value to correct my colors according to the color space ? How ? For the moment I am using precompiler directive and those functions.

    Code (CSharp):
    1. inline float3 toGamma(float3 srcColor)
    2. { return pow(srcColor, 1 / 2.2); }
    3.  
    4. inline float3 toLinear(float3 srcColor)
    5. { return pow(srcColor, 2.2); }
     
  4. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Any idea ?
     
  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    The grey valid is just a precomputed value of 0.5 that has been run through that function (but only if in linear mode). The trick is you only want to apply the translation if in linear color space mode.
     
  6. monkeyscience

    monkeyscience

    Joined:
    Dec 13, 2011
    Posts:
    705
    Has anyone noticed problems with ColorSpaceGrey on Android? It doesn't seem to come through in gamma space for me.

    btw, if you want to convert to linear or gamma in a shader, using ColorSpaceGrey without branches:
    Code (csharp):
    1.  
    2. //Linear - unity_ColorSpaceGrey.r is 0.19
    3. //Gamma - unity_ColorSpaceGrey.r is 0.5          
    4. //linear ? 1 : 0 = (Grey - 0.5) / (0.19 - 0.5) = one clever MAD instruction
    5. #define IS_LINEAR ((-3.22581*unity_ColorSpaceGrey.r) + 1.6129)
    6. #define IS_GAMMA  (( 3.22581*unity_ColorSpaceGrey.r) - 0.6129)
    7. ...
    8. half toLinearAuto1(half a)    { return a*(IS_GAMMA*rsqrt(a) + IS_LINEAR); }
    9. half3 toLinearAuto3(half3 a) { return a*(IS_GAMMA.xxx*rsqrt(a) + IS_LINEAR.xxx); }
    10.  
     
    Chman and Farfarer like this.
  7. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Nice tips, thank you very much ! With such code, you don't have to use keywords and preprocessor macros ?
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Very cool, monkeyscience, I'm always baffled that Unity doesn't set a keyword for gamma/linear space settings.
     
  9. monkeyscience

    monkeyscience

    Joined:
    Dec 13, 2011
    Posts:
    705
    Yeah it's a shame but a keyword for it means shaderCount *= 2; I'm baffled by mobile GPUs still not supporting sRGB sampling. What is this, 1995? All my ಠ___ಠ.
     
  10. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Yeah, I think for their mobile demo Unity used colour*colour and sqrt(colour), which is a neat workaround. It means a gamma of 2.0 rather than 2.2 but it's a lot cheaper than the pow() you need otherwise.

    Native sRGB sampling would be nice, though :p
     
  11. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    I think if they pledge to support GL-ES 3 they should provide sRGB-sampling for spec compliance. That not all-mobile-GPUs-ever-produced-in-the-last-7-years support sRGB sampling OTOH is surely understandable. ;)