Search Unity

[SOLVED] UnityCG.cginc Luminance values

Discussion in 'Shaders' started by H4ppyTurtle, Jan 29, 2020.

  1. H4ppyTurtle

    H4ppyTurtle

    Joined:
    Aug 20, 2019
    Posts:
    18
    Hello everyone,
    I need to calculate the luminance of a cubemap. Now I am a bit confused about the included Luminance function in UnityCG.cginc: https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html. I can't find any reference why the RGB values in gamma or linear space are chosen like they are.

    Code (CSharp):
    1. #ifdef UNITY_COLORSPACE_GAMMA
    2. #define unity_ColorSpaceLuminance half4(0.22, 0.707, 0.071, 0.0)
    3. #else // Linear values
    4. #define unity_ColorSpaceLuminance half4(0.0396819152, 0.458021790, 0.00609653955, 1.0)
    5. #endif
    The only function I know a reference for is the LinearRgbToLuminance one, it is the ITU-R Recommendation 709. But this function is not even mentioned on the manual page for UnityCG.cginc.

    Code (CSharp):
    1. // Converts color to luminance (grayscale)
    2. inline half Luminance(half3 rgb)
    3. {
    4.     return dot(rgb, unity_ColorSpaceLuminance.rgb);
    5. }
    6.  
    7. // Convert rgb to luminance
    8. // with rgb in linear space with sRGB primaries and D65 white point
    9. half LinearRgbToLuminance(half3 linearRgb)
    10. {
    11.     return dot(linearRgb, half3(0.2126729f,  0.7151522f, 0.0721750f));
    12. }
    Could someone help me get clarity on this? I am currently working on my thesis with Unity, so I would like to reference an official source why the luminance calculation is done this way and not differently.
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    It's a color to grayscale conversion using some common values that give a decently natural look to our eyes. You can see in the Grayscale Conversion section of this Nvidia GPUGems page where they go over it and explain it some more:
    https://developer.nvidia.com/sites/all/modules/custom/gpugems/books/GPUGems/gpugems_ch22.html
    The Linear space value is simply this gamma space values mentioned above, passed through Unity's "GammeToLinearSpace" function in the UnityCG.cginc file (using the commented out "Precise version" inside it, which it references this page for the formula:
    http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html?m=1
     
    Last edited: Jan 29, 2020
    H4ppyTurtle likes this.
  3. H4ppyTurtle

    H4ppyTurtle

    Joined:
    Aug 20, 2019
    Posts:
    18
    Ah yes, that was the piece of information I was missing. Thank you very much!