Hello I'm writing some post process effects/shaders, and I need read acess to the shadow map texture. Has unity some built-in access to the shadow map texture, or I have to write my own procedure (i.e, render scene from light point of view)? I've seen that UnityCG.cginc has a sampler2D called _ShadowMapTexture, but is seems to be empty in my initial tests. Thanks in advance
No, there's no built-in way to access that. Part of the reason is that there can be more than one shadow map in the scene, so after scene is done, which one would you get? So yeah, rendering it manually from light's POV is the way to go.
@Aras: Is it in general possible to access the shadow map (the depth value) inside the fragment shader of a regular (forward rendering) shader?
I'm just posting this here , just in case someone stumbles across the same question! So I was also searching for a way to sample the screen space shadow texture with a commandbuffer and noticed, that they use this technique in the 3D GameKit from Unity! And it just seems to work straight out of the box this way! I saw many questions about this and nobody ever posted his working code, so here ya go! Code that should be attached to the main directional light: Code (CSharp): using UnityEditor; using UnityEngine; using UnityEngine.Rendering; namespace Gamekit3D { [ExecuteInEditMode] public class CopyShadowMap : MonoBehaviour { CommandBuffer cb = null; void OnEnable() { var light = GetComponent<Light>(); if (light) { cb = new CommandBuffer(); cb.name = "CopyShadowMap"; cb.SetGlobalTexture("_DirectionalShadowMask", new RenderTargetIdentifier(BuiltinRenderTextureType.CurrentActive)); light.AddCommandBuffer(UnityEngine.Rendering.LightEvent.AfterScreenspaceMask, cb); } } void OnDisable() { var light = GetComponent<Light>(); if (light) { light.RemoveCommandBuffer(UnityEngine.Rendering.LightEvent.AfterScreenspaceMask, cb); } } } } And in the shader, access the map likes this: Code (CSharp): sampler2D _DirectionalShadowMask; and in the surf/fragment shader: Code (CSharp): float shadowmask = tex2D(_DirectionalShadowMask,screenUV).b; Theres also this Macro written there, but I'm not sure for what it's used: Code (CSharp): UNITY_DECLARE_SHADOWMAP(_DirectionalShadowMap); (I just didnt used it and it still worked) UPDATE: Also thanks to @bgolus for clearing this up: That's for defining a shadow map texture object ... which technically the screen space shadow texture is not so can be ignored. You've already defined it as a sampler2D, which is what it is. Anyways, I hope this helps someone!