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

Using only Vertex Colors result in faded / too light colors

Discussion in 'Shaders' started by artofcode, Jul 21, 2018.

  1. artofcode

    artofcode

    Joined:
    Mar 18, 2013
    Posts:
    8
    I pass a vertex color to the mesh and use this as the only source of color (no Texture), but if I have a standard material with the same color value on a second mesh, it's a totally different color. Not sure what I'm doing wrong here. This is what it looks like:

    upload_2018-7-21_3-3-49.png

    The dark orange is using the 'Standard' shader, the mesh around it is using the above shader. Both use the same RGBA values.

    The shader doesn't do much, as you can see below. Is there anything I forgot to do here or am doing horribly wrong?

    Code (CSharp):
    1.  
    2. Shader "Custom/VertexColor Shader"
    3. {
    4.  
    5.     SubShader
    6.     {
    7.         Tags { "RenderType" = "Opaque" }
    8.         LOD 200
    9.        
    10.         CGPROGRAM
    11.         #pragma surface surf Lambert vertex:vert
    12.         #pragma target 3.0
    13.         struct Input
    14.         {
    15.             float4 vertColor;
    16.         };
    17.  
    18.         void vert (inout appdata_full v, out Input o)
    19.         {
    20.             UNITY_INITIALIZE_OUTPUT(Input, o);
    21.             o.vertColor = v.color;
    22.         }
    23.  
    24.         void surf (Input IN, inout SurfaceOutput o)
    25.         {
    26.             o.Albedo = IN.vertColor.rgb;
    27.         }
    28.         ENDCG
    29.     }
    30.  
    31.     FallBack "Diffuse"
    32.  
    33. }
    34.  
    Thanks for any insight you might have!
     
    SamuelEbert likes this.
  2. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Light?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    This is because vertex colors aren't gamma corrected when you're using linear color space.

    o.Albedo = GammaToLinearSpace(IN.vertColor.rgb);
     
  4. artofcode

    artofcode

    Joined:
    Mar 18, 2013
    Posts:
    8
    Thank you so much!
     
    SamuelEbert likes this.
  5. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    man it's like everytime i'm having a problem bgolus has already answered the question.
     
    SamuelEbert likes this.
  6. Gian1983

    Gian1983

    Joined:
    Jan 2, 2019
    Posts:
    32
    Dear all,

    I haven't found any 'standard' shader in Unity which supports vertex colors thus I tried this one (thanks a lot !), without the correction suggested by bgolus. But as you can see on the picture below, light colors (here it's supposed to be a very light yellow and I have the same issue with very light grey) are shining and blur !



    And it's the same with bgolus' correction (even if colors are obviously different).

    Do you know what I can do to avoid this effect ??
     
  7. SamuelEbert

    SamuelEbert

    Joined:
    Jul 30, 2018
    Posts:
    2
    Thank you so much. =) I’ve been trying for hours to figure out what’s causing the vertex colors to be so bright...

    In case anyone needs to do this in Shader Graph, here’s how I do it:
    upload_2021-4-23_21-19-9.png

    I know this may be irrelevant, but I needed this for a low-poly terrain. By the off-chance anyone’s looking for a low-poly shader, here's a screenshot of mine (be sure to set “Fragment Normal Space” to “World” in the Graph Settings):
    upload_2021-4-23_21-26-20.png upload_2021-4-23_21-50-32.png
     
    Last edited: Apr 23, 2021
    Stormer2020 and aducceschi like this.
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    aducceschi, Ferb and SamuelEbert like this.
  9. SamuelEbert

    SamuelEbert

    Joined:
    Jul 30, 2018
    Posts:
    2
    That's just what I needed. I'm inexperienced with Shader Graph and was not aware of that node. Thank you! =D
     
  10. nicocarrillo

    nicocarrillo

    Joined:
    Mar 1, 2023
    Posts:
    1
    Thank you, I solved the same problem with your answer.