Search Unity

Vertex lit shader lighting is too dark

Discussion in 'Shaders' started by whydoidoit, Oct 17, 2012.

  1. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    Cross posted on Unity Answers (with pictures) here.

    Ok, let me preface this with: this is the first vertex/fragment shader I have written. I have a surface shader version of this that is working well - but I need it to work in the Vertex pipeline. The shader basically uses a reference map to recolour various parts of a combined mesh with different colours. That works fine. The problem is that it's too dark! The shader is written to handle one directional light, but I've also tried calling ShadeVertexLights and it's has the same effect.

    Code (csharp):
    1. Shader "Custom/ColorRemappingVertexLit" {
    2.     Properties {
    3.        _Color ("Main Color", Color) = (1,1,1,1)
    4.        _MainTex ("Base (RGB)", 2D) = "white" {}
    5.        _Color1Tex ("Color1 (RGB)", 2D) = "white" {}
    6.        _Color2Tex ("Color2 (RGB)", 2D) = "white" {}
    7.        _Color3Tex ("Color3 (RGB)", 2D) = "white" {}
    8.     }
    9.     SubShader {
    10.          Lighting On
    11.  
    12.        Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "LightMode" = "Vertex" }
    13.        Pass {
    14.  
    15.          CGPROGRAM
    16.          #pragma vertex vert
    17.          #pragma fragment frag
    18.          #define USING_DIRECTIONAL_LIGHT
    19.          #include "UnityCG.cginc"
    20.  
    21.  
    22.  
    23.          sampler2D _MainTex;
    24.          sampler2D _Color1Tex;
    25.          sampler2D _Color2Tex;
    26.          sampler2D _Color3Tex;
    27.          float4 _Color;
    28.          float4 _MainTex_ST;
    29.          float4 _Color1Tex_ST;
    30.  
    31.          struct a2v
    32.          {
    33.           float4 vertex : POSITION;
    34.           float3 normal : NORMAL;
    35.           float4 texcoord : TEXCOORD0;
    36.           float4 texcoord1 : TEXCOORD1;
    37.          };
    38.  
    39.          struct v2f
    40.          {
    41.           float4 pos : SV_POSITION;
    42.           float2 uv  : TEXCOORD0;
    43.           float2 uv2 : TEXCOORD1;
    44.           float4 color : COLOR;
    45.          };
    46.  
    47.          v2f vert (a2v v)
    48.          {
    49.           v2f o;
    50.           o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    51.           o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    52.           o.uv2 = TRANSFORM_TEX (v.texcoord1, _Color1Tex);
    53.           float3 n = mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1));
    54.           float LdotN = saturate (dot (normalize(unity_LightPosition[0]), n));
    55.  
    56.                 o.color = UNITY_LIGHTMODEL_AMBIENT * _Color;
    57.  
    58.                 o.color += LdotN * unity_LightColor[0];
    59.           return o;
    60.          }
    61.  
    62.          half4 frag(v2f i) : COLOR
    63.          {
    64.           half4 c1 = tex2D (_Color1Tex, i.uv2);
    65.  
    66.           half4 c = tex2D (_MainTex, i.uv);
    67.  
    68.           if(c1.r==1  c1.g==1  c1.b==1)
    69.           {
    70.               return c * i.color;
    71.           }
    72.           else
    73.           {
    74.               half4 c2 = tex2D (_Color2Tex, i.uv2);
    75.               half4 c3 = tex2D (_Color3Tex, i.uv2);
    76.               half3 f = saturate(((c1.rgb * c.r) + (c2.rgb * c.g) + (c3.rgb * c.b))) * i.color;
    77.               return half4(f, c.a);
    78.  
    79.           }
    80.  
    81.          }
    82.          ENDCG
    83.        }
    84.     }
    85.     FallBack "Diffuse"
    86. }
     
  2. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Code (csharp):
    1.           float3 n = mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1));
    I think this should be:

    Code (csharp):
    1.           float3 n = normalize(mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 0.0)));
    I.e. the fourth component should be 0.0 and you have to normalize the vector (because there might be some scaling in the modelview matrix).

    Furthermore, you might have to multiply the resulting diffuse illumination with 2.0 to get the same intensity as the built-in shaders (because many of the built-in shaders do this multiplication; I don't know why).
     
  3. whydoidoit

    whydoidoit

    Joined:
    Mar 12, 2012
    Posts:
    365
    Thanks for the response! It turned out that it looked too bright when I switched that float4 (v.normal, 0.0) - so I left it at 1.

    What fixed it was your pointer on doubling everything (WTH!)

    Code (csharp):
    1. Shader "Custom/ColorRemappingVertexLit" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _Color1Tex ("Color1 (RGB)", 2D) = "white" {}
    6.         _Color2Tex ("Color2 (RGB)", 2D) = "white" {}
    7.         _Color3Tex ("Color3 (RGB)", 2D) = "white" {}
    8.     }
    9.     SubShader {
    10.             Lighting On
    11.        
    12.         Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "LightMode" = "Vertex" }
    13.         Pass {
    14.        
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #define USING_DIRECTIONAL_LIGHT
    19.             #include "UnityCG.cginc"
    20.            
    21.    
    22.        
    23.             sampler2D _MainTex;
    24.             sampler2D _Color1Tex;
    25.             sampler2D _Color2Tex;
    26.             sampler2D _Color3Tex;
    27.             float4 _Color;
    28.             float4 _MainTex_ST;
    29.             float4 _Color1Tex_ST;
    30.            
    31.             struct a2v
    32.             {
    33.                 float4 vertex : POSITION;
    34.                 float3 normal : NORMAL;
    35.                 float4 texcoord : TEXCOORD0;
    36.                 float4 texcoord1 : TEXCOORD1;
    37.             };
    38.            
    39.             struct v2f
    40.             {
    41.                 float4 pos : SV_POSITION;
    42.                 float2 uv  : TEXCOORD0;
    43.                 float2 uv2 : TEXCOORD1;
    44.                 float4 color : COLOR;
    45.             };
    46.  
    47.             v2f vert (a2v v)
    48.             {
    49.                 v2f o;
    50.                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    51.                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    52.                 o.uv2 = TRANSFORM_TEX (v.texcoord1, _Color1Tex);
    53.                 float3 n = normalize(mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1)));
    54.                 float LdotN =  dot (unity_LightPosition[0], n);
    55.  
    56.                 o.color = UNITY_LIGHTMODEL_AMBIENT * _Color * 2 ;
    57.  
    58.                 o.color += (LdotN * unity_LightColor[0]) * _Color * 2;
    59.                 return o;
    60.             }
    61.            
    62.             half4 frag(v2f i) : COLOR
    63.             {
    64.                 half4 c1 = tex2D (_Color1Tex, i.uv2);
    65.            
    66.                 half4 c = tex2D (_MainTex, i.uv);
    67.                
    68.                 if(c1.r==1  c1.g==1  c1.b==1)
    69.                 {
    70.                     return c * i.color * 2;
    71.                 }
    72.                 else
    73.                 {
    74.                     half4 c2 = tex2D (_Color2Tex, i.uv2);
    75.                     half4 c3 = tex2D (_Color3Tex, i.uv2);
    76.                     half3 f = saturate(((c1.rgb * c.r) + (c2.rgb * c.g) + (c3.rgb * c.b))) * i.color;
    77.                     return half4(f, c.a) * 2;
    78.    
    79.                 }
    80.                
    81.             }
    82.             ENDCG
    83.         }
    84.     }
    85.     FallBack "Diffuse"
    86. }
    87.  
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I believe it's just a fudge to fit with engine parameters like lights go from 0..8, therefore you need to fudge to fit that, mostly because everything else is fudged to have a smoother lit scene with less banding when not using the newer kind of lighting. AFAIK :)
     
  5. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    I have tried once ,if it is a direction light,it seems that the z component needed to be modified by the w component when camera and shader pass both in vertexlit mode