Using Unity 2022.3.40f1 and URP.
I'm trying to implement occlusion culling with the depth buffer in a compute shader.
I don't know what's wrong here but my code only works fine with the scene camera but not the game camera.
I have already debugged the depth texture and it works fine.
Any ideas what's missing here?
My shader setup looks like this:
void Render (uint3 id : SV_DispatchThreadID)
{
Chunk chunk = chunkBuffer[id.x];
float4 clipPos = UnityWorldToClipPos(chunk.position);
float2 screenPos = ComputeScreenPos(clipPos);
float4 projectedPos = mul(UNITY_MATRIX_VP, float4(chunk.position, 1.0f));
float2 projectedUV = (projectedPos.xy / projectedPos.w + 1.0f) * 0.5f;
float sceneDepth = _DepthTexture.SampleLevel(sampler_DepthTexture, projectedUV, 0).r;
float chunkDepth = (projectedPos.z / projectedPos.w);
if (chunkDepth < sceneDepth) {
return; // is occluded
}
}
The issue was UNITY_MATRIX_VP
. I had to calculate my own view projection matrix like this: GL.GetGPUProjectionMatrix(_cam.projectionMatrix, false) * _cam.worldToCameraMatrix;