Found a big bug!
As shown in the image, a scalar of 1 should be appended to WorldPosition, Otherwise the w component of clipspace would always be 0, everything is broken after that.
By Appending this 1 component, I can finally see some bounaies and uvs!
The uv looks great!
Sampling some random texture with the uv works well!
HOWEVER, sampling the RenderTarget ( filled with depth texture from sceneCapture) with this uv always results in black… … WTF? It’s supposed to be quite bright!
The same RenderTarget works as expected in another material:
The updated hlsl code:
float4x4 vpmatrix = float4x4(Row0.r, Row1.r, Row2.r, Row3.r,
Row0.g, Row1.g, Row2.g, Row3.g,
Row0.b, Row1.b, Row2.b, Row3.b,
Row0.a, Row1.a, Row2.a, Row3.a);
float4 clipspace = mul( vpmatrix,WorldPosition);
float3 ndc = (clipspace / clipspace.w).xyz;
//convert uv from [-1,1] to [0,1]
float2 uv = (ndc.xy + 1)/2;
// Comment out the next line to see the uv
//return float3(uv * bounds,0);
// flip uv along y axis
float2 flippeduv = float2(uv.x,-uv.y);
// This is the bounds of SceneCapture's frustum
float bounds = (ndc.x>-1 && ndc.x < 1 &&
ndc.y > -1 && ndc.y < 1 &&
ndc.z > 0 && ndc.z < 1) ? 1 : 0;
float sample = saturate(Texture2DSample(Tex,TexSampler,flippeduv));
return sample * bounds;
// Always result in black



