Search Unity

Receiving shadows with a Vertex/Fragment shader

Discussion in 'Shaders' started by Dreamteck, Aug 20, 2018.

  1. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336
    I'm attempting to write a very simple vertex-lit vertex-fragment shader. I'm unable to make it receive shadows. I know that I have to use the AutoLight library to get the attenuation for the fragment function but attenuation always seems to return 1 no matter what I do.


    Code (CSharp):
    1. Shader "Simple/VertexLit"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1, 1, 1, 1)
    7.         _Emission ("Emission", Color)= (0, 0, 0, 0)
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque"}
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             Tags { "LightMode"="ForwardBase" }
    17.             Cull Back
    18.             Lighting On
    19.  
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma multi_compile_fwdbase
    24.            
    25.             #include "UnityCG.cginc"
    26.             #include "AutoLight.cginc"
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float4 normal : NORMAL;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float2 uv : TEXCOORD0;
    38.                 float4 pos : SV_POSITION;
    39.                 fixed4 color : COLOR;
    40.                 float3 normal : NORMAL;
    41.                 LIGHTING_COORDS(1,2)
    42.             };
    43.  
    44.             sampler2D _MainTex;
    45.             float4 _MainTex_ST;
    46.             fixed4 _Color;
    47.             fixed4 _Emission;
    48.            
    49.             v2f vert (appdata v)
    50.             {
    51.                 v2f o;
    52.                 o.pos = UnityObjectToClipPos(v.vertex);
    53.                 o.normal = UnityObjectToWorldNormal(v.normal);
    54.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    55.                 o.color = fixed4(ShadeSH9(half4(v.normal.xyz, 1.0)) + ShadeVertexLights(v.vertex, v.normal), 1);
    56.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    57.                 return o;
    58.             }
    59.            
    60.             fixed4 frag (v2f i) : SV_Target
    61.             {
    62.                 fixed4 col = tex2D(_MainTex, i.uv);
    63.                 return  col * i.color * _Color * LIGHT_ATTENUATION(i);
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    69.  
    What am I missing?