Search Unity

Problems with directional light...

Discussion in 'Shaders' started by nyxoroso, Mar 20, 2019.

  1. nyxoroso

    nyxoroso

    Joined:
    Aug 10, 2018
    Posts:
    8
    I've got a problem with light. Again.
    The problem is that UNITY_LIGHT_ATTENUATION always returns a white color.
    Code (CSharp):
    1. Shader "Unlit/Test"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.     SubShader
    7.     {
    8.         Tags {
    9.         "Queue" = "Geometry"
    10.         "RenderType" = "Opaque"
    11.     }
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             Tags {
    17.                 "LightMode" = "ForwardBase"
    18.             }
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma target 3.0
    23.             #include "UnityCG.cginc"
    24.             #include "AutoLight.cginc"
    25.             #pragma multi_compile_fwdbase
    26.  
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 float4 pos : SV_POSITION;
    37.                 float4 vert : TEXCOORD3;
    38.                 LIGHTING_COORDS(1, 2)
    39.             };
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.pos = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = v.uv;
    46.                 o.vert = mul(unity_ObjectToWorld, v.vertex);
    47.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    48.                 return o;
    49.             }
    50.  
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 UNITY_LIGHT_ATTENUATION(atten, i, i.vert.xyz);
    54.  
    55.                 return atten;
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }
    61.  
    Any help will be appretiated.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    You need a shadowcaster pass, or a fallback with a shadowcaster pass.

    Add this before the last close brace (}).

    FallBack "Legacy Shaders/VertexLit"
     
  3. nyxoroso

    nyxoroso

    Joined:
    Aug 10, 2018
    Posts:
    8
    @bgolus, but I don't need shadow casting. Or is it strictly required for UNITY_LIGHT_ATTENUATION to run?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Unity's main directional light shadow receiving also uses the shadow caster pass. Unity renders out a depth texture, then casts the main directional light's shadow on the world position extrapolated from the depth texture. The depth texture is generated by rendering opaque objects' shadow caster pass.