Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Shadow catcher or Matte shadow shader

Discussion in 'VR' started by behram, Jul 1, 2016.

  1. behram


    Jun 21, 2016
    Is anyone using a Shadow Catcher shader in any of their Hololens projects ?
    Since Mixed Reality is being supported by Unity I have a feeling more people will ask for such functionality.Especially those of us coming from VFX.

    The image below illustrates what I'm trying to describe:

    The image might be an artistic impression and a Post Processed render. Those shadows really ground / Anchor the hologram onto the table.

    A video in blender that illustrates the process:

    Can we have a shader that only renders shadows being cast on it (ideally a plane) and renders everything else transparent ?

    Thank you,

    Attached Files:

    Last edited: Dec 22, 2016
  2. BrandonFogerty


    Jan 29, 2016
    Hi behram,

    Unity has built in support for shadows. There are 2 parts involved in getting shadows to render. You need a shadow caster like the holographic rocket in your picture. You also need a shadow receiver. You probably want the spatial mesh to be a shadow receiver in your case.

    All holograms must have a MeshRenderer component in order to be rendered. If you want a hologram to be a shadow caster, you must make sure it's "Cast Shadows" property is set to "On" like so,

    Next, you need to cast the shadow onto something. So on your spatial mesh, you need to make sure the "receive shadows" property is set to true like so,

    Once you have done this, you will get something like the following if you are using the standard shader on your ground mesh.

    Since the HoloLens uses an additive display, the color black is basically transparent. So you may need to play with the actual color of your shadow otherwise you may not see it. Here is a shader, that I put together, that allows you to adjust the shadow color of your shadow receiver. Just create a material and set it to use the following shader. Then apply the material to your ground object.

    Code (CSharp):
    1. Shader "Unlit/VertColorWithShadow"
    2. {
    3.     Properties
    4.     {
    5.         _MainColor("Main Color", COLOR) = (0,0,0,1)
    6.         _ShadowColor("Shadow Color", COLOR) = (1,0,0,1)
    7.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 100
    14.         Pass
    15.         {
    16.             Tags {"LightMode" = "ForwardBase"}
    18.             CGPROGRAM
    19.             #pragma multi_compile_fwdbase
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    23.             #include "UnityCG.cginc"
    24.             #include "AutoLight.cginc"
    26.             float4 _MainColor;
    27.             float4 _ShadowColor;
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.             };
    34.             struct v2f
    35.             {
    36.                 float4 pos : SV_POSITION;
    37.                 float4 mainColor : COLOR;
    38.                 LIGHTING_COORDS(1,2)
    39.             };
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    45.                 o.mainColor = _MainColor;
    47.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    49.                 return o;
    50.             }
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 fixed atten = 1.0 - LIGHT_ATTENUATION(i);
    55.                 fixed3 shadowColor = atten * _ShadowColor.rgb;
    56.                 fixed4 finalColor = i.mainColor + fixed4(shadowColor, 1.0);
    58.                 return finalColor;
    59.             }
    60.             ENDCG
    61.         }
    63.         // Pass to render object as a shadow caster
    64.         Pass
    65.         {
    66.             Name "ShadowCaster"
    67.             Tags { "LightMode" = "ShadowCaster" }
    69.             ZWrite On ZTest LEqual Cull Off
    71.             CGPROGRAM
    72.             #pragma vertex vert
    73.             #pragma fragment frag
    74.             #pragma multi_compile_shadowcaster
    75.             #include "UnityCG.cginc"
    77.             struct v2f {
    78.                 V2F_SHADOW_CASTER;
    79.             };
    81.             v2f vert( appdata_base v )
    82.             {
    83.                 v2f o;
    85.                 return o;
    86.             }
    88.             float4 frag( v2f i ) : SV_Target
    89.             {
    90.                 return float4(1.0, 1.0, 1.0, 1.0);
    91.             }
    92.             ENDCG
    93.         }
    94.     }
    95. }
    Depending on your settings, you will get something like this

    Nick_Counter and behram like this.
  3. behram


    Jun 21, 2016
    Thanks Brandon,
    The shader works in the viewport (pic attached) but not on the device.
    I am using the AstroMan asset in the Model Explorer example scene and a simple 3D plane object right below the model.

    There is a default script on the Astroman ( SetRendererSettings ) which iterates through child objects and switches off MeshRenderer cast & receive shadow settings. So I have commented out those lines just in case.

    Ok I will have to learn how to attach a shader to the Spatial Mesh that is generated at runtime. Let me look around the forums and docs and get back if I still cant hack this.

    fwiw thebanjomatic from the MS forums also pointed me to this Doc:

    I'm sharing this post / shader with the Unity community in general. because if it's a indispensable utility to have.
    Thanks for creating it.

    Attached Files:

  4. ivodopiviz


    Nov 8, 2014
    Sorry for bringing up an old project, but I'm in trying to get working a similar scheme but without using Hololens. What would I need to change to get this working in a normal scene?

    The idea is to get the shadows casted over a 2D object but using a 3D invisible plane as reference for the perspective.

    Thanks in advance!
  5. smoluck


    Mar 8, 2016
    Hi, i'm using Hololens since a few days, and i can say that the Shader gaved by
    BrandonFogerty works well under Hololens. Except that my shadows should be close to white.... sounds weird. With that additive display tech, it's not totally impossible to represent shadows or reable text, because all the black values inside the display will be completely wipe out like an Alpha.

    that's really making me crying, because the first advertising (commercial) video presentation just show off things and graphics that you can't get in reality (mixed Reality by the fact)
  6. cvasquez-coiron


    Nov 10, 2014
    Hi Brandon,
    Thank you very much for sharing this shader.
    It works perfectly fine in the Hololens device look-through AR display.
    BUT when I record a video using the "Take Video" option on Microsoft HoloLens tool, the Plain object is shown below the shadow... What would you recommend to do to be able to record a video using this shadow caster?

    Thank you very much!