- Home /
Sampling the cascaded shadow maps in a shader
I'm writing a fog volume shader that simulates light scattering and crepuscular rays. I figured the best way to do this would be to sample the depth shadow maps that get generated in the Shadows.RenderShadowmapDir just like the CollectShadows step does, so I wrote a shader that uses a lot of the same code (i.e. from Internal-PrePassCollectShadows)
Simple enough, right? However I just found out that _ShadowMapTexture now refers to the screenspace shadow map instead of the depth cascaded map that the shadow collect step uses, even though they use the same property name. This map isn't useful to me, I need that light depth information too. Some internal Unity step must have bound another render texture. Is there some way I can access that texture?
Answer by AkashicMike · May 04, 2015 at 08:59 AM
I just figured out a hack that gets me access to that texture, but I really doubt that this method is safe, supported, or portable in the least.
I figured that since the shadow depth map is being unbound after the collect shadows step, Unity is probably releasing it to the RT pool. If that's the case, then we just might be able to get it back by asking for a "temporary RT" that just so happens to match the resolution and format of the cascaded shadow map. The following code demonstrates this by drawing the shadow depth texture to the screen:
int shadowMapID = Shader.PropertyToID ("_ShadowDepthMap");
CommandBuffer commands = new CommandBuffer ();
commands.name = "Hack up some render targets";
Camera cam = Camera.main;
cam.AddCommandBuffer (CameraEvent.AfterEverything, commands);
// Ask the RT pool to give me a temporary RT that matches the properties of the cascaded shadow map
// If all goes well, it should just give you the actual RT used for the shadow maps.
commands.GetTemporaryRT (shadowMapID, 2048, 2048, 16, FilterMode.Bilinear, RenderTextureFormat.Shadowmap);
commands.Blit (shadowMapID, BuiltinRenderTextureType.CameraTarget);
Answer by jpfranssen · Feb 05, 2016 at 02:45 AM
@AkashicMike not sure if you still need it but there may be a better more full-proof way.
https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetShadowSamplingMode.html