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?