Hello, I'm new to shaders and would like to create a shader that does what is visible on the right side of my attached image. Sadly I only am able to achieve what is visible on the left side. I want to have the original cast shadows of my model, without the cast shadows being affected by the clipping. My current code is this: Code (CSharp): Shader "Custom/SeeThroughShader" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Standard fullforwardshadows addshadow #pragma target 3.0 sampler2D _MainTex; float4 _PlayerPos; struct Input { float2 uv_MainTex; float3 worldPos; }; half _Glossiness; half _Metallic; fixed4 _Color; void surf (Input IN, inout SurfaceOutputStandard o) { fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; float dist = distance(IN.worldPos, _PlayerPos); if(dist<5) { clip(c- 0.99); } o.Albedo = c.rgb; o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
The easiest solution is to duplicate your game object with the mesh. Set the current game object's mesh renderer to Cast Shadows > Off. Then make a copy of it, change the material to the default standard shader and Cast Shadows > Shadows Only. The shader only solution is to add some checks to see if A) The current render pass is the shadow caster pass and B) if the shadow caster pass is being used to render the depth texture or a shadow map. Knowing if it's the shadowcaster pass or not is trivial. Code (csharp): #if defined(UNITY_PASS_SHADOWCASTER) // this code will only exist in the shadow caster pass #endif Unfortunately, it's not as trivial to know if a shadow caster pass is being used for the camera depth texture (what Unity uses to receive directional shadows), or a shadow map. They both use the same pass. The best I've found so far is this: Code (csharp): #if defined(UNITY_PASS_SHADOWCASTER) // is a shadow caster or camera depth pass #if defined(SHADOWS_DEPTH) // is rendering the camera depth, directional shadow, or spot light shadow if (!any(unity_LightShadowBias)) { // is the camera depth texture, or a light with the shadow bias values set to 0 (don't do that!) } else { // is a shadow map } #endif #endif
Hey bgolus! Thanks so much for the swift reply. I tried you code and it worked! Basically I just added your code to the surf() function and then put the clip() function eveywhere, where its not the shadow map. Code (CSharp): #if defined(UNITY_PASS_SHADOWCASTER) // is a shadow caster or camera depth pass #if defined(SHADOWS_DEPTH) // is rendering the camera depth, directional shadow, or spot light shadow if (!any(unity_LightShadowBias)) { // is the camera depth texture, or a light with the shadow bias values set to 0 (don't do that!) if(dist<5) { clip(c- 0.99); } } else { // is a shadow map } #endif #else if(dist<5) { clip(c- 0.99); } #endif