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

AR Multi Directional light shadows

Discussion in 'AR' started by nobu77, Mar 15, 2018.

  1. nobu77

    nobu77

    Joined:
    Apr 19, 2014
    Posts:
    2
    Hi ,

    I am working on an AR project these days and I was using this shader provided by Unity to cast shadows in my scene.

    Today I wanted to add a second directional light (which should also cast shadows) so I followed the comments written in the shader. I added a new pass with Light Mode set to ForwardAdd and changed the pragma to multi_compile_fwdadd. It didn't seem to do the trick and I couldn't find a solution anywhere to cast the shadow of my second light. Am I missing something obvious when following the comments?

    Here the shader :
    Code (CSharp):
    1. Shader "Custom/Matte Shadow" {
    2. Properties
    3. {
    4.     _Fade ("Wave scale", Range (0,1)) = 0.5 // sliders
    5.          _FadeMarkerDist ("Wave scale", Range (0,1)) = 0.5 // sliders
    6. }
    7.  
    8. SubShader {
    9.         Pass {
    10.        
    11.             // 1.) This will be the base forward rendering pass in which ambient, vertex, and
    12.             // main directional light will be applied. Additional lights will need additional passes
    13.             // using the "ForwardAdd" lightmode.
    14.             // see: http://docs.unity3d.com/Manual/SL-PassTags.html
    15.             Tags { "LightMode" = "ForwardBase" "RenderType"="Opaque" "Queue"="Geometry+1" "ForceNoShadowCasting"="True"  }
    16.                        
    17.                         LOD 150
    18.                         Blend Zero SrcColor
    19.                         ZWrite On
    20.        
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #include "UnityCG.cginc"
    25.             // 2.) This matches the "forward base" of the LightMode tag to ensure the shader compiles
    26.             // properly for the forward bass pass. As with the LightMode tag, for any additional lights
    27.             // this would be changed from _fwdbase to _fwdadd.
    28.             #pragma multi_compile_fwdbase
    29.             // 3.) Reference the Unity library that includes all the lighting shadow macros
    30.             #include "AutoLight.cginc"
    31.                        
    32.                         float _Fade;
    33.                         float _FadeMarkerDist;
    34.             struct v2f
    35.             {
    36.                 float4 pos : SV_POSITION;
    37.                                 float3 worldPos : TEXCOORD2;
    38.                 float3 objWorldPos : TEXCOORD3;
    39.  
    40.                 // 4.) The LIGHTING_COORDS macro (defined in AutoLight.cginc) defines the parameters needed to sample
    41.                 // the shadow map. The (0,1) specifies which unused TEXCOORD semantics to hold the sampled values -
    42.                 // As I'm not using any texcoords in this shader, I can use TEXCOORD0 and TEXCOORD1 for the shadow
    43.                 // sampling. If I was already using TEXCOORD for UV coordinates, say, I could specify
    44.                 // LIGHTING_COORDS(1,2) instead to use TEXCOORD1 and TEXCOORD2.
    45.                 LIGHTING_COORDS(0,1)
    46.             };
    47.             v2f vert(appdata_base v) {
    48.                 v2f o;
    49.                 o.pos = UnityObjectToClipPos (v.vertex);
    50.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    51.                                 o.objWorldPos = mul(unity_ObjectToWorld,float4(0,0,0,1));
    52.  
    53.                 // 5.) The TRANSFER_VERTEX_TO_FRAGMENT macro populates the chosen LIGHTING_COORDS in the v2f structure
    54.                 // with appropriate values to sample from the shadow/lighting map
    55.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    56.                
    57.                 return o;
    58.             }
    59.             fixed4 frag(v2f i) : COLOR {
    60.            
    61.                 // 6.) The LIGHT_ATTENUATION samples the shadowmap (using the coordinates calculated by TRANSFER_VERTEX_TO_FRAGMENT
    62.                 // and stored in the structure defined by LIGHTING_COORDS), and returns the value as a float.
    63.                 float attenuation = LIGHT_ATTENUATION(i);
    64.  
    65.  
    66.                                 float dist = distance(i.worldPos, i.objWorldPos);
    67.  
    68.                 return  min(1,fixed4(1.0,1.0,1.0,1.0) * attenuation + (dist * (_Fade/_FadeMarkerDist)));
    69.             }
    70.             ENDCG
    71.         }
    72.  
    73.                 Pass {
    74.        
    75.             Tags { "LightMode" = "ForwardAdd" "RenderType"="Opaque" "Queue"="Geometry+1" "ForceNoShadowCasting"="True"  }
    76.                         LOD 150
    77.                         Blend Zero One
    78.                         ZWrite On
    79.        
    80.             CGPROGRAM
    81.             #pragma vertex vert
    82.             #pragma fragment frag
    83.             #include "UnityCG.cginc"
    84.             #pragma multi_compile_fwdadd
    85.             #include "AutoLight.cginc"
    86.                        
    87.                         float _Fade;
    88.                         float _FadeMarkerDist;
    89.             struct v2f
    90.             {
    91.                 float4 pos : SV_POSITION;
    92.                                 float3 worldPos : TEXCOORD2;
    93.                 float3 objWorldPos : TEXCOORD3;
    94.  
    95.                 LIGHTING_COORDS(0,1)
    96.             };
    97.             v2f vert(appdata_base v) {
    98.                 v2f o;
    99.                 o.pos = UnityObjectToClipPos (v.vertex);
    100.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    101.                                 o.objWorldPos = mul(unity_ObjectToWorld,float4(0,0,0,1));
    102.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    103.                
    104.                 return o;
    105.             }
    106.             fixed4 frag(v2f i) : COLOR {
    107.                 float attenuation = LIGHT_ATTENUATION(i);
    108.                                 float dist = distance(i.worldPos, i.objWorldPos);
    109.                 return  min(1,fixed4(1.0,1.0,1.0,1.0) * attenuation + (dist * (_Fade/_FadeMarkerDist)));
    110.             }
    111.             ENDCG
    112.         }
    113.     }
    114.     Fallback "VertexLit"
    115. }
    Thanks :)
     
  2. nobu77

    nobu77

    Joined:
    Apr 19, 2014
    Posts:
    2
    Somehow replacing the LightMode by ForwardAdd in the first pass did the trick
     
  3. lauraaa

    lauraaa

    Joined:
    Dec 30, 2016
    Posts:
    16
    I have the same question too, but is trying to make it work with other lights, e.g. spotlight, point light.
    Do you figure out how to add additional pass properly?
     
    shivaji0548 likes this.