Hi,
I would like to sample the screen space shadow map generated by Unity when I render a full quad in OnPostRender.
I have a custom shader/material that I set its parameters each frame with material.SetTexture, material.SetFloat, etc and I would like first to pass Unity’s internal _ShadowMapTexture to my shader and then sampling from it in the correct way.
I have read some comments about how to do it but I think the info is not well documented in general or has been deprecated.
What I have right now, is a very simple shader with the tag: { “RenderType”=“Opaque” “LightMode” = “ForwardBase” } (I don’t know why i need to set the lightmode tag)
Then after specifying the pragmas for the vertex and fragment shader and including UnityCG.cginc
i have the following:
#include "Lighting.cginc"
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
#pragma multi_compile _ SHADOWS_SCREEN
#include "AutoLight.cginc"
UNITY_DECLARE_SHADOWMAP(_ShadowMapTexture); // I included this because _ShadowMapTexture was still undefined
I don’t know if the order matters here or if I’m overriding things.
then in the v2f struct I added the macro
SHADOW_COORDS(4)
in the vertex shader i also added
TRANSFER_SHADOW(o)
and in the fragment shader i try to access it with
tex2D(_ShadowMapTexture, i.uv);
the result when I check the frame debugger, the texture is not passed to the shader, the tex2D sampling function says there is no matching 2 parameter intrinsic function and with tex2Dproj and passing a float4, i got the same error.
The only thing that I care is about reading that texture in my own full screen space shader. I’m not trying to write a shader for a shadow caster or to use the shader as a material for a gameObject, just read the value and do some processing with it.
Thanks in advance
