How to make screenshadow at ForwardBase (like deferredShading)?

Hi,

I want to render a shadow postprocess, that can make _ShadowMapTexture to screen shadow map, like deferredshading.
I write it in C# script, it’s seted to MainCamera:
void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
{…
Graphics.Blit (null, destTexture, m_ShadowMaterial, 0);

}
the destTexture is the out screen shadow map.

In shader, I write it like this:
fixed4 Frag_GenScreenSM_byViewDir( PX_T_VD_outvs i ) : COLOR
{
float3 kViewPos;
PX_CalcViewPos(_CameraDepthTexture, i.viewDir, i.uv, kViewPos); // I can get viewpos at there.
float3 kObjectPos = mul(UNITY_MATRIX_IT_MV, float4(kViewPos,1));
float3 kWorldPos = mul(_Object2World, float4(kObjectPos,1));
float4 kLightPos = mul(unity_World2Shadow[0], float4(kWorldPos,1));
float fShadowValue = (kLightPos.z / kLightPos.w) < tex2Dproj( _ShadowMapTexture, kLightPos ) ? 1: 0;

}

But the result is wrong. why and how to do it? Are there some examples? Thank you.

Is the UNITY_MATRIX_IT_MV can’t be used in camera’s postprocess?
So I write the code in c# script:
Camera curCamera = GetComponent ();
Matrix4x4 matCam2World = curCamera.cameraToWorldMatrix;
m_ShadowMaterial.SetMatrix (“_CameraToWorld_PX”, matCam2World);
And in shader:
float3 kWorldPos = mul(_CameraToWorld_PX, float4(kViewPos, 1));
float4 kLightPos = mul(unity_World2Shadow[0], float4(kWorldPos,1));

But the result is wrong too. So, I think the unity_World2Shadow[0] can’t be used as this.
How to set unity_World2Shadow[0] in c# script?

Is my thought correct? Thank you.

Oh, I was wrong. I using Pix to perf the render steps. I found: the _ShadowMapTexture is what I want screen shadow map.

But the result isn’t perfect. because:

  • The shadow isn’t very smooth.
  • I’m not want PSSM.
  • If there aren’t any reveive shadow object, the _ShadowMapTexture will be white. I need always generate screen shadow map.

So, I want to know:

  • How to write myself shader at CollectShadow step?
  • How to refuse PSSM?
  • How to always generat screen shadow map when has any object to cast shadow? Not by receive shadow object to control it.

Thank you very much.