Search Unity

Objects cast shadows even when they have Cast Shadows = Off

Discussion in 'Shaders' started by swanickj, Jun 20, 2019.

  1. swanickj

    swanickj

    Joined:
    May 15, 2019
    Posts:
    28
    So i'm working on reducing draw calls for an Oculus Quest app, and have found that all of my objects are completely ignoring the Mesh Renderer Cast Shadows and Receive Shadows options.

    All of my static geometry is ligthmap static, and my realtime light is meant to only cast shadows on a few dynamic objects, with the rest of my light being baked.

    Of all the objects in this Light DepthPass Job, only Cell_LOD0 is supposed to be casting shadows. All the rest have their MeshRenderers' Cast Shadows = Off, and Receive Shadows = Off.
    upload_2019-6-20_11-53-14.png

    These shadows were composed with amplify shader editor, with Shadowcasting=Off, but with Fallback = "Diffuse". I think the shadowcaster pass comes from the diffuse fallback. Indeed, no shadowcasting happens if i remove the fallback, or if i add ForceNoShadowCasting=True and noshadow. however the diffuse fallback should obey the mesh renderer's instruction to not shadowcast, right? I want to use the same material for multiple LOD levels, but only use shadows for LOD0.
    upload_2019-6-20_11-56-21.png

    here's the shader for the Cell LOD's:

    Code (CSharp):
    1. // Made with Amplify Shader Editor
    2. // Available at the Unity Asset Store - http://u3d.as/y3X
    3. Shader "TPM/Abbvie/Cell Body QUEST"
    4. {
    5.     Properties
    6.     {
    7.         _TextureSample0("Texture Sample 0", 2D) = "bump" {}
    8.         _ColorSlider("Color Slider", Float) = 0
    9.         _Color1("Color 1", Color) = (0,0,0,0)
    10.         _Color2("Color2", Color) = (0,0,0,0)
    11.         _Color0("Color 0", Color) = (0.4386792,0.7881798,1,1)
    12.         _Color3("Color 3", Color) = (0,0,0,0)
    13.         _EmissionStrength("Emission Strength", Range( 0 , 1)) = 0.66
    14.         _Smoothness("Smoothness", Range( 0 , 1)) = 1
    15.         _Float0("Float 0", Range( 0 , 10)) = 0.8
    16.         _Float1("Float 1", Range( 0 , 10)) = 0.8
    17.         _Metallic("Metallic", Range( 0 , 1)) = 1
    18.         [HideInInspector] _texcoord( "", 2D ) = "white" {}
    19.         [HideInInspector] __dirty( "", Int ) = 1
    20.     }
    21.  
    22.     SubShader
    23.     {
    24.         Tags{ "RenderType" = "Opaque"  "Queue" = "Geometry+0" "IsEmissive" = "true"  }
    25.         Cull Back
    26.         Blend One Zero , SrcAlpha OneMinusSrcAlpha
    27.        
    28.         CGPROGRAM
    29.         #include "UnityShaderVariables.cginc"
    30.         #pragma target 3.0
    31.         #pragma surface surf Standard keepalpha vertex:vertexDataFunc
    32.         struct Input
    33.         {
    34.             float3 worldPos;
    35.             float2 uv_texcoord;
    36.             float3 worldNormal;
    37.             INTERNAL_DATA
    38.         };
    39.  
    40.         uniform sampler2D _TextureSample0;
    41.         uniform float4 _TextureSample0_ST;
    42.         uniform float _ColorSlider;
    43.         uniform float4 _Color2;
    44.         uniform float4 _Color1;
    45.         uniform float4 _Color0;
    46.         uniform float _Float0;
    47.         uniform float4 _Color3;
    48.         uniform float _Float1;
    49.         uniform float _EmissionStrength;
    50.         uniform float _Metallic;
    51.         uniform float _Smoothness;
    52.  
    53.         void vertexDataFunc( inout appdata_full v, out Input o )
    54.         {
    55.             UNITY_INITIALIZE_OUTPUT( Input, o );
    56.             float3 ase_worldPos = mul( unity_ObjectToWorld, v.vertex );
    57.             float mulTime51 = _Time.y * 3.0;
    58.             v.vertex.xyz += ( sin( ( ase_worldPos.z + mulTime51 ) ) * 0.02 * float3(0,1,1) );
    59.         }
    60.  
    61.         void surf( Input i , inout SurfaceOutputStandard o )
    62.         {
    63.             float2 uv_TextureSample0 = i.uv_texcoord * _TextureSample0_ST.xy + _TextureSample0_ST.zw;
    64.             o.Normal = UnpackNormal( tex2D( _TextureSample0, uv_TextureSample0 ) );
    65.             float layeredBlendVar13 = _ColorSlider;
    66.             float4 layeredBlend13 = ( lerp( _Color2,_Color1 , layeredBlendVar13 ) );
    67.             o.Albedo = layeredBlend13.rgb;
    68.             float3 ase_worldPos = i.worldPos;
    69.             float3 ase_worldViewDir = normalize( UnityWorldSpaceViewDir( ase_worldPos ) );
    70.             float3 ase_worldNormal = WorldNormalVector( i, float3( 0, 0, 1 ) );
    71.             float fresnelNdotV11 = dot( ase_worldNormal, ase_worldViewDir );
    72.             float fresnelNode11 = ( 0.0 + 1.0 * pow( 1.0 - fresnelNdotV11, _Float0 ) );
    73.             float smoothstepResult43 = smoothstep( 0.2 , 0.5 , fresnelNode11);
    74.             float fresnelNdotV39 = dot( ase_worldNormal, ase_worldViewDir );
    75.             float fresnelNode39 = ( 0.0 + 1.0 * pow( 1.0 - fresnelNdotV39, _Float1 ) );
    76.             float smoothstepResult42 = smoothstep( 0.3 , 0.8 , fresnelNode39);
    77.             o.Emission = ( ( _Color0 + ( smoothstepResult43 * _Color3 * smoothstepResult42 ) ) * _EmissionStrength ).rgb;
    78.             o.Metallic = _Metallic;
    79.             o.Smoothness = _Smoothness;
    80.             o.Alpha = 1;
    81.         }
    82.  
    83.         ENDCG
    84.     }
    85.     Fallback "Legacy Shaders/Bumped Diffuse"
    86.     CustomEditor "ASEMaterialInspector"
    87. }
    88. /*ASEBEGIN
    89. Version=16200
    90. 7;2;1906;996;2066.546;378.2836;1.6;True;True
    91. Node;AmplifyShaderEditor.RangedFloatNode;32;-1492.538,243.6727;Float;False;Property;_Float0;Float 0;8;0;Create;True;0;0;False;0;0.8;0.2;0;10;0;1;FLOAT;0
    92. Node;AmplifyShaderEditor.RangedFloatNode;40;-1691.267,378.1498;Float;False;Property;_Float1;Float 1;9;0;Create;True;0;0;False;0;0.8;0.31;0;10;0;1;FLOAT;0
    93. Node;AmplifyShaderEditor.FresnelNode;39;-1412.98,323.2838;Float;False;Standard;WorldNormal;ViewDir;False;5;0;FLOAT3;0,0,1;False;4;FLOAT3;0,0,0;False;1;FLOAT;0;False;2;FLOAT;1;False;3;FLOAT;5;False;1;FLOAT;0
    94. Node;AmplifyShaderEditor.FresnelNode;11;-1099.051,272.0065;Float;False;Standard;WorldNormal;ViewDir;False;5;0;FLOAT3;0,0,1;False;4;FLOAT3;0,0,0;False;1;FLOAT;0;False;2;FLOAT;1;False;3;FLOAT;5;False;1;FLOAT;0
    95. Node;AmplifyShaderEditor.SimpleTimeNode;51;-630.1241,1125.117;Float;False;1;0;FLOAT;3;False;1;FLOAT;0
    96. Node;AmplifyShaderEditor.SmoothstepOpNode;42;-804.5237,350.7168;Float;False;3;0;FLOAT;0;False;1;FLOAT;0.3;False;2;FLOAT;0.8;False;1;FLOAT;0
    97. Node;AmplifyShaderEditor.ColorNode;33;-824.0675,525.2963;Float;False;Property;_Color3;Color 3;5;0;Create;True;0;0;False;0;0,0,0,0;1,0.04171816,0,0;True;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    98. Node;AmplifyShaderEditor.WorldPosInputsNode;53;-671.7242,1192.317;Float;False;0;4;FLOAT3;0;FLOAT;1;FLOAT;2;FLOAT;3
    99. Node;AmplifyShaderEditor.SmoothstepOpNode;43;-708.5237,246.7168;Float;False;3;0;FLOAT;0;False;1;FLOAT;0.2;False;2;FLOAT;0.5;False;1;FLOAT;0
    100. Node;AmplifyShaderEditor.SimpleMultiplyOpNode;34;-483.2966,224.5576;Float;False;3;3;0;FLOAT;0;False;1;COLOR;0,0,0,0;False;2;FLOAT;0;False;1;COLOR;0
    101. Node;AmplifyShaderEditor.SimpleAddOpNode;52;-135.724,1141.116;Float;False;2;2;0;FLOAT;0;False;1;FLOAT;0;False;1;FLOAT;0
    102. Node;AmplifyShaderEditor.ColorNode;31;-1735.757,179.8189;Float;False;Property;_Color0;Color 0;4;0;Create;True;0;0;False;0;0.4386792,0.7881798,1,1;0,0.334156,0.462264,1;True;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    103. Node;AmplifyShaderEditor.ColorNode;8;-1049.315,-96.41956;Float;False;Property;_Color2;Color2;3;0;Create;True;0;0;False;0;0,0,0,0;0.5283019,0.5283019,0.5283019,1;False;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    104. Node;AmplifyShaderEditor.RangedFloatNode;45;-338.9242,1022.717;Float;False;Constant;_Float2;Float 2;16;0;Create;True;0;0;False;0;0.02;0;0;0;0;1;FLOAT;0
    105. Node;AmplifyShaderEditor.Vector3Node;48;-63.72401,891.5172;Float;False;Constant;_Vector0;Vector 0;16;0;Create;True;0;0;False;0;0,1,1;0,0,0;0;4;FLOAT3;0;FLOAT;1;FLOAT;2;FLOAT;3
    106. Node;AmplifyShaderEditor.ColorNode;9;-1053.516,-280.0175;Float;False;Property;_Color1;Color 1;2;0;Create;True;0;0;False;0;0,0,0,0;0.9716981,0,0,1;False;0;5;COLOR;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    107. Node;AmplifyShaderEditor.RangedFloatNode;35;-286.8441,316.2189;Float;False;Property;_EmissionStrength;Emission Strength;6;0;Create;True;0;0;False;0;0.66;0.39;0;1;0;1;FLOAT;0
    108. Node;AmplifyShaderEditor.RangedFloatNode;10;-767.6179,-269.8175;Float;False;Property;_ColorSlider;Color Slider;1;0;Create;True;0;0;False;0;0;0.1;0;0;0;1;FLOAT;0
    109. Node;AmplifyShaderEditor.SinOpNode;49;29.07603,1102.716;Float;False;1;0;FLOAT;0;False;1;FLOAT;0
    110. Node;AmplifyShaderEditor.SimpleAddOpNode;14;-324.4944,171.946;Float;False;2;2;0;COLOR;0,0,0,0;False;1;COLOR;0,0,0,0;False;1;COLOR;0
    111. Node;AmplifyShaderEditor.LayeredBlendNode;13;-737.3191,-99.81947;Float;False;6;0;FLOAT;0.2;False;1;COLOR;0,0,0,0;False;2;COLOR;0,0,0,0;False;3;FLOAT;0;False;4;FLOAT;0;False;5;FLOAT;0;False;1;COLOR;0
    112. Node;AmplifyShaderEditor.RangedFloatNode;38;-459.3242,763.5171;Float;False;Property;_Metallic;Metallic;10;0;Create;True;0;0;False;0;1;1;0;1;0;1;FLOAT;0
    113. Node;AmplifyShaderEditor.SamplerNode;15;-1391.79,13.02596;Float;True;Property;_TextureSample0;Texture Sample 0;0;0;Create;True;0;0;False;0;None;fcf454157409ecd4c8b6a46d5fe24b13;True;0;True;bump;Auto;True;Object;-1;Auto;Texture2D;6;0;SAMPLER2D;;False;1;FLOAT2;0,0;False;2;FLOAT;0;False;3;FLOAT2;0,0;False;4;FLOAT2;0,0;False;5;FLOAT;1;False;5;FLOAT3;0;FLOAT;1;FLOAT;2;FLOAT;3;FLOAT;4
    114. Node;AmplifyShaderEditor.SimpleMultiplyOpNode;36;-97.06509,188.4704;Float;False;2;2;0;COLOR;0,0,0,0;False;1;FLOAT;0;False;1;COLOR;0
    115. Node;AmplifyShaderEditor.RangedFloatNode;37;-374.5242,881.917;Float;False;Property;_Smoothness;Smoothness;7;0;Create;True;0;0;False;0;1;0.491;0;1;0;1;FLOAT;0
    116. Node;AmplifyShaderEditor.RangedFloatNode;54;-878.1244,1232.318;Float;False;Constant;_Float3;Float 3;16;0;Create;True;0;0;False;0;5;0;0;0;0;1;FLOAT;0
    117. Node;AmplifyShaderEditor.SimpleMultiplyOpNode;55;-410.9241,1195.517;Float;False;2;2;0;FLOAT3;0,0,0;False;1;FLOAT;0;False;1;FLOAT3;0
    118. Node;AmplifyShaderEditor.SimpleMultiplyOpNode;46;253.0759,974.7169;Float;False;3;3;0;FLOAT;0;False;1;FLOAT;0;False;2;FLOAT3;0,0,0;False;1;FLOAT3;0
    119. Node;AmplifyShaderEditor.SimpleDivideOpNode;41;-444.5241,357.1169;Float;False;2;0;FLOAT;0;False;1;FLOAT;0;False;1;FLOAT;0
    120. Node;AmplifyShaderEditor.StandardSurfaceOutputNode;0;267.2,78.39998;Float;False;True;2;Float;ASEMaterialInspector;0;0;Standard;TPM/Abbvie/Cell Body QUEST;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;False;Back;0;False;-1;0;False;-1;False;0;False;-1;0;False;-1;False;0;Opaque;0.5;True;False;0;False;Opaque;;Geometry;All;True;True;True;True;True;True;True;True;True;True;True;True;True;True;True;True;True;0;False;-1;False;0;False;-1;255;False;-1;255;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;0;False;-1;False;2;15;10;25;False;0.5;True;0;5;False;-1;10;False;-1;2;5;False;-1;10;False;-1;0;False;-1;0;False;-1;0;False;0;0,0,0,0;VertexOffset;True;False;Cylindrical;False;Relative;0;Legacy Shaders/Bumped Diffuse;-1;-1;-1;-1;0;False;0;0;False;-1;-1;0;False;-1;0;0;0;16;0;FLOAT3;0,0,0;False;1;FLOAT3;0,0,0;False;2;FLOAT3;0,0,0;False;3;FLOAT;0;False;4;FLOAT;0;False;5;FLOAT;0;False;6;FLOAT3;0,0,0;False;7;FLOAT3;0,0,0;False;8;FLOAT;0;False;9;FLOAT;0;False;10;FLOAT;0;False;13;FLOAT3;0,0,0;False;11;FLOAT3;0,0,0;False;12;FLOAT3;0,0,0;False;14;FLOAT4;0,0,0,0;False;15;FLOAT3;0,0,0;False;0
    121. WireConnection;39;3;40;0
    122. WireConnection;11;3;32;0
    123. WireConnection;42;0;39;0
    124. WireConnection;43;0;11;0
    125. WireConnection;34;0;43;0
    126. WireConnection;34;1;33;0
    127. WireConnection;34;2;42;0
    128. WireConnection;52;0;53;3
    129. WireConnection;52;1;51;0
    130. WireConnection;49;0;52;0
    131. WireConnection;14;0;31;0
    132. WireConnection;14;1;34;0
    133. WireConnection;13;0;10;0
    134. WireConnection;13;1;8;0
    135. WireConnection;13;2;9;0
    136. WireConnection;36;0;14;0
    137. WireConnection;36;1;35;0
    138. WireConnection;55;0;53;0
    139. WireConnection;55;1;54;0
    140. WireConnection;46;0;49;0
    141. WireConnection;46;1;45;0
    142. WireConnection;46;2;48;0
    143. WireConnection;0;0;13;0
    144. WireConnection;0;1;15;0
    145. WireConnection;0;2;36;0
    146. WireConnection;0;3;38;0
    147. WireConnection;0;4;37;0
    148. WireConnection;0;11;46;0
    149. ASEEND*/
    150. //CHKSM=17D449F6F993D43F5759D3762248CB00D44E8826
    Is there any way that I can retain the ability to shadowcast but also be able disable shadowcasting through the Mesh Renderer?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    That DepthPass is only tangentially related to shadows. While it is using the ShadowCaster pass, it's rendering the Camera Depth Texture. For desktop & console, the depth texture is used for the main directional light's shadow receiving, but on mobile it's only for things like soft particles and any post processing. I would suggest disabling soft particles in the quality settings, and checking to see if you camera has its Depth Texture enabled when running the game (it'll be getting enabled via a script someplace, possibly a post process which you may want to disable for the Quest anyway).
     
    Danimate likes this.
  3. swanickj

    swanickj

    Joined:
    May 15, 2019
    Posts:
    28
    That was indeed the issue! A script was telling the camera to render a depth texture. Fortunately, that bit of code is no longer important, and i get to remove it for free! Thanks again bgolus!
     
  4. swanickj

    swanickj

    Joined:
    May 15, 2019
    Posts:
    28
    Oh and by the way, what's the reason for the difference in directional light shadow receiving between desktop and mobile? i understand the basics of tile based renderers and i'd imagine there's a need to avoid the cost of switching render targets for rendering a depth texture, but I wonder what the alternative method for mobile is - if you don't mind me asking?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Rendering the shadows onto the depth texture as a full screen pass separate from the main scene rendering allows the complexity of the shadow sampling to be removed from the forward shader. While it means re-rendering the scene twice, the depth only pass to generate the depth texture is quite cheap for anything not using alpha testing. The result is the the main scene rendering only has to sample a screen space texture to get the shadows. The reduced complexity of the shaders is usually a greater savings in rendering time than the additional cost of rendering the scene twice, especially when using soft shadows which greatly increases the complexity of the shadow shader.

    It also has benefits like being able to sample the shadows at a different screen resolution than the final scene. Super Mario Odyssey does this all over the place, and Unity sort of does this by not having any support for MSAA on the main directional shadows.

    On mobile both the additional draw calls, and the cost of decompressing the depth buffer to a texture, are far more costly. Plus, just in general, soft shadows were too expensive to do on most mobile hardware. So instead the forward shaders sample the shadow maps directly. Technically this is still how it works on PC for any shadow casting spot lights or additional shadow casting directional lights beyond the main one.
     
    Danimate and swanickj like this.
  6. swanickj

    swanickj

    Joined:
    May 15, 2019
    Posts:
    28
    That makes a lot of sense! Thanks for the knowledge!