Hello!
I have a compute shader that computes a volumetric fog volume (following this idea).
I pass in the inverse of the MVP matrix from the script, then get the clip space position of each cell and multiply it by that, then divide by w. It’s not coming out right and I’m not sure where I’m going wrong. Can anyone help?
Here’s the relevant bit of the script:
Matrix4x4 invProj = (Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix).inverse;
shader.SetVector("m0", invProj.GetRow(0));
shader.SetVector("m1", invProj.GetRow(1));
shader.SetVector("m2", invProj.GetRow(2));
shader.SetVector("m3", invProj.GetRow(3));
shader.Dispatch(kernelIndex, 128 / 4, 128 / 4, 128 / 4);
Here’s the relevant bit of the compute shader:
float4 m0;
float4 m1;
float4 m2;
float4 m3;
[numthreads(4,4,4)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float texZSize = 128;
float texXSize = 128;
float texYSize = 128;
float4x4 _ViewProjectInverse;
_ViewProjectInverse[0] = m0;
_ViewProjectInverse[1] = m1;
_ViewProjectInverse[2] = m2;
_ViewProjectInverse[3] = m3;
// get the cell position in normalised device coordinates
float3 cellPosition = float3(
(((id.x + 0.5) / texXSize) - 0.5) * 2,
(((id.y + 0.5) / texYSize) - 0.5) * 2,
(id.z + 0.5) / texZSize
);
// compute world space position of cell centre from clip pos
float4 wp = mul(_ViewProjectInverse, cellPosition);
wp /= wp.w;