Search Unity

Lighting missing on back face

Discussion in 'Shaders' started by behw, May 18, 2022.

  1. behw

    behw

    Joined:
    Sep 16, 2021
    Posts:
    7
    I am quite new to making custom shaders, really appreciate any help. I have a vertex shader that does not render lighting correctly on the back face -- the front face and back face shows different lighting. I would like the back face to have the same lighting effect as the front. I understand that a possible method making use of VFACE or passing Cull Back and Cull Front instead of Cull Off.

    For VFACE, I am getting errors when I try to put "o.facing = v.facing;" into my vert function. My shader seems to be slightly different from other examples and I just can't seem to get it to work. Also, using two passes does not produce the desired effect as well. Is there something that I am doing wrongly?

    Code (CSharp):
    1.  
    2. Shader "Custom/Vertex Transparent Specular"
    3. {
    4.     Properties
    5.     {
    6.         _Color("Main Color", Color) = (1,1,1,1)
    7.         _Shininess("Shininess", Range(0, 1)) = 0.078125
    8.         _Detail("Detail (RGB) Gloss (A)", 2D) = "gray" {}
    9.     }
    10.  
    11.         SubShader
    12.     {
    13.         Tags { "RenderType" = "Transparent" }
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         LOD 200
    16.         Pass{
    17.              Cull Back
    18.         }
    19.       CGPROGRAM
    20.       #pragma surface surf BlinnPhong vertex:vert alpha:fade
    21.  
    22.         sampler2D _Detail;
    23.         float4 _Color;
    24.         float _Shininess;
    25.  
    26.         struct Input
    27.         {
    28.             float2 uv_Detail;
    29.             float4 vertColors;
    30.         };
    31.  
    32.         void vert(inout appdata_full v, out Input o)
    33.         {
    34.             o.vertColors = v.color;
    35.             o.uv_Detail = v.texcoord;  
    36.  
    37.         }
    38.  
    39.  
    40.         void surf(Input IN, inout SurfaceOutput o)
    41.         {
    42.             half4 c = IN.vertColors.rgba * _Color.rgba;              
    43.             c.rgb *= tex2D(_Detail,IN.uv_Detail).rgb * 2;
    44.             o.Albedo = c.rgb;
    45.             o.Gloss = tex2D(_Detail,IN.uv_Detail).a;
    46.             o.Alpha = c.a * _Color.a;
    47.             o.Specular = _Shininess;
    48.         }
    49.  
    50.         ENDCG
    51.     }
    52.  
    53.         Fallback "Specular"
    54. }
    55.  
    56.