Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Vector3 vs Color

Discussion in 'Shader Graph' started by CodeKiwi, Aug 21, 2022.

  1. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    upload_2022-8-21_11-7-45.png

    I noticed that an unlit shader graph seems to show different colors depending on data type. The left cube is using Color(0.5, 0.5, 0.5) (displays correctly) while the right cube uses Vector3(0.5, 0.5, 0.5) (incorrect). The vector3 value seems to display as (0.74, 0.74, 0.74). I'm using Unity 2021.2.5f1 with URP.

    ColorTest unlit shader. Color(0.5, 0.5, 0.5)
    ColorTest.png

    VectorTest unlite shader. Vector3(0.5, 0.5, 0.5)
    upload_2022-8-21_11-11-40.png

    Is this a bug or am I just missing something?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,863
    I don't have a thorough answer, but I immediately assume Color is literal, while Vector3 is going through a gamma correction or a perceptive luminance calculation, instead of a simple linear relationship.
     
    CodeKiwi likes this.
  3. Ben_at_Work

    Ben_at_Work

    Joined:
    Mar 16, 2022
    Posts:
    58
    colorSpace_vector3b.PNG

    Whenever I hit things like this I'll toss on the Colorspace Conversion node to find out what's going on.

    colorSpace_vector3.PNG
    1. Color
    2. Vector3 (Linear to RGB)
    3. Vector3 (RGB to Linear)
     
    CodeKiwi likes this.
  4. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    Thanks @halley and @Ben_at_Work. That does seem to be the issue. The Colorspace Conversion node looks useful, I'll have to try it in the future.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Technically they’re both “correct”.

    This is on the right track … except it’s backwards. Color is going through a color conversion, and Vector3 is not! The thing you’re missing is when using linear color space rendering, which the URP and HDRP default to, the final image goes through a linear to gamma conversion before being displayed on screen.

    Here’s the documentation for the Color node.
    https://docs.unity3d.com/Packages/com.unity.shadergraph@10.2/manual/Color-Node.html

    Here’s the code example for that node:
    Code (CSharp):
    1. float4 _Color = IsGammaSpace() ? float4(1, 2, 3, 4) : float4(SRGBToLinear(float3(1, 2, 3)), 4);
    Basically if a color conversion is needed, it does the gamma (sRGB) to linear conversion. If you were using gamma color space rendering, the color and vector3 nodes would end up looking exactly the same.
     
    CodeKiwi likes this.