Hello, 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: http://www.roadtovr.com/wp-content/uploads/2015/01/hololens-vid-0.jpg 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, b
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): Shader "Unlit/VertColorWithShadow" { Properties { _MainColor("Main Color", COLOR) = (0,0,0,1) _ShadowColor("Shadow Color", COLOR) = (1,0,0,1) } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Tags {"LightMode" = "ForwardBase"} CGPROGRAM #pragma multi_compile_fwdbase #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "AutoLight.cginc" float4 _MainColor; float4 _ShadowColor; struct appdata { float4 vertex : POSITION; }; struct v2f { float4 pos : SV_POSITION; float4 mainColor : COLOR; LIGHTING_COORDS(1,2) }; v2f vert (appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.mainColor = _MainColor; TRANSFER_VERTEX_TO_FRAGMENT(o); return o; } fixed4 frag (v2f i) : SV_Target { fixed atten = 1.0 - LIGHT_ATTENUATION(i); fixed3 shadowColor = atten * _ShadowColor.rgb; fixed4 finalColor = i.mainColor + fixed4(shadowColor, 1.0); return finalColor; } ENDCG } // Pass to render object as a shadow caster Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" } ZWrite On ZTest LEqual Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc" struct v2f { V2F_SHADOW_CASTER; }; v2f vert( appdata_base v ) { v2f o; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) return o; } float4 frag( v2f i ) : SV_Target { return float4(1.0, 1.0, 1.0, 1.0); } ENDCG } } } Depending on your settings, you will get something like this
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. Note: 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: https://developer.microsoft.com/en-us/windows/holographic/designing_for_mixed_reality I'm sharing this post / shader with the Unity community in general. because if it works...it's a indispensable utility to have. Thanks for creating it. Cheers, b
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!
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)
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!