Search Unity

Invisible Shadow-only Shader

Discussion in 'Shaders' started by Cygon4, Nov 22, 2014.

  1. Cygon4

    Cygon4

    Joined:
    Sep 17, 2012
    Posts:
    382
    Hi!

    I am trying to build a shader that will only cast a shadow but not render the object itself. There are various attempts in Unity Answers, the forums and various spots on the web, but nobody actually seems to have achieved this effect.

    The closest I got so far is this:

    Code (CSharp):
    1. Shader "Custom/Shadow Only" {
    2.   SubShader {
    3.  
    4.     Pass {
    5.       Tags { "LightMode" = "ShadowCaster" }
    6.  
    7.       CGPROGRAM
    8.  
    9.       #pragma vertex vert
    10.       #pragma fragment frag
    11.  
    12.       float4 vert(float4 v:POSITION) : SV_POSITION {
    13.         return mul (UNITY_MATRIX_MVP, v);
    14.         //return float4(0.0,0.0,0.0,1.0);
    15.       }
    16.  
    17.       fixed4 frag() : COLOR {
    18.         //discard;
    19.         return fixed4(0.0,0.0,0.0,1.0);
    20.       }
    21.  
    22.       ENDCG
    23.     }
    24.  
    25.   }
    26. }
    As you will notice, it obscures the shadow of any object behind it and itself.
    For some reason, there's also a strange moiré pattern on some of its faces:

    shadowcaster.png
     
    Last edited: Nov 22, 2014
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    It's work properly only with deferred rendering, at least in U5b. As far i remember in 4.6 it's work fine.
    Code (csharp):
    1.  
    2. Shader "Custom/ShadowOnly"
    3. {
    4.     Properties {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Pass {
    10.         Name "ShadowCaster"
    11.         Tags { "LightMode" = "ShadowCaster" }
    12.      
    13.         Fog {Mode Off}
    14.         ZWrite On ZTest LEqual Cull Off
    15.         Offset 1, 1
    16.  
    17.         CGPROGRAM
    18.         #pragma vertex vert
    19.         #pragma fragment frag
    20.         #pragma multi_compile_shadowcaster
    21.         #include "UnityCG.cginc"
    22.  
    23.         struct v2f {
    24.             V2F_SHADOW_CASTER;
    25.         };
    26.  
    27.         v2f vert( appdata_base v )
    28.         {
    29.             v2f o;
    30.             TRANSFER_SHADOW_CASTER(o)
    31.             return o;
    32.         }
    33.  
    34.         float4 frag( v2f i ) : SV_Target
    35.         {
    36.             SHADOW_CASTER_FRAGMENT(i)
    37.         }
    38.         ENDCG
    39.  
    40.         }
    41.     }
    42. }
    43.  
    44.  
    add
    Well is some with what inside answers link.
     
    Last edited: Nov 22, 2014
  3. Cygon4

    Cygon4

    Joined:
    Sep 17, 2012
    Posts:
    382
    Thank you.

    In deferred mode, my attempt seems to work, too. I'll go with mine for now but keep this bookmarked in case I run into trouble. If anyone has a suggestion for a method that works in both forward and deferred rendering, I'm still interested :)
     
  4. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    mouurusai's solution appears to work just fine for me in forward rendering mode too.
     
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Flailer, what version of Unity3D you are use?
     
  6. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    Tested it on the free version.
     
  7. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Version number :D
    It's doesn't work in 5.0.0b14
     
  8. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    4.5.4f1 lol they have changed the way shadows work under the hood in unity 5.0 though, Ill have a play with this at work when we try the beta of 5.0 in a week or so :p
     
  9. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Not sure where I found it but just disable receive shadows on the object and try this:
    Code (CSharp):
    1. Shader "Transparent/InvisibleShadowCaster"
    2. {
    3.      Subshader
    4.      {
    5.          UsePass "VertexLit/SHADOWCOLLECTOR"  
    6.          UsePass "VertexLit/SHADOWCASTER"
    7.      }
    8.      Fallback off
    9. }