Mapping camera frustum to 3D texture


I’m trying to map the camera frustum into a 3D texture so i can access the world-space position of each thread in the compute shader.
I thought this should work when looking at the 3D texture as if it is in clip-space, then transforming it to world-space with the inversed projection- and worldTransform-matrices.

It works with the current near an far clip values, however when adjusting these the thing breaks.

Could anyone explain what is going wrong?

Thanks in advance,


frustum_to_3Dtexture.tox (55.1 KB)

Hey @Bart_Kipping, you were quite close! Few things that was causing weird issues:

  1. you were writing out the maths for the matrix mults as color *= matrix but this will produce incorrect results because that expands to color = color * matrix, and the thing with matrix maths is the matrix ALWAYS goes to the left of the vector. This explains why going from NDC to projection space was producing weird results.

  2. next was the viewspace multiply, first was the same issue as above, then the other thing was the 4th component of color (color.a) had some other values left over from the perspective matrix multiply.
    when a vec4 represents a position, like it does in your file the 4th member must always be equal to 1. (for normals, it’s always 0) - so this was an easy fix, I just set color.a = 1 before doing the second matrix multiply.

frustum_to_3Dtexture_fixed.tox (54.6 KB)

1 Like

Aha! Typical. I was aware of point two, forgot to implement it in this version. The thing about left - right is very helpful information and makes total sense when thinking of the stuff i read about matrix multiplication.

Thanks a lot for helping me out @lucasm ! Crunched my brain on it.

1 Like

No prob! happy to help.