Pixel position in world space from fragment postprocess shader issue

Hello everyone!
I’m trying to create fog of war feature in my project using postprocess shader. The first step is to calculate pixel position in world space using fragment and depth map and make this pixel black if it’s x and z coordinate in between 0 and 1 for example. It’s like a common topic as I searched the internet, but I had no luck to get working solution for it. Most commonly every thread stops on getting camera space coordinates of a fragment on camera near plane. I’ve spent a week trying to figure out how to implement my needs (to be honest I’m very new to computer graphics programming) and hasn’t done it so far.
Here is my playground with my best approach. The zone (volume) bounded by xyz coordinates in main if statement getting black, but if I rotate my camera it’s getting curved and moving.
The second issue I’ve got is when I create long zone along x coordinate it’s becoming curved dunno why. My best assumption that depth texture values is non-linearly distributed.

Thanks in advance, I will be grateful for any help.

Welcome aboard!

The depth renderer is rendering linear depth in the depth texture by default but you would need the non linear z value if you use it “as is” as the z NDC coordinate. You can instruct the depth renderer to render non linear z values by passing true as the second parameter to enableDepthRenderer:

Note I’m not quite sure what you want to achieve with the vectorToPixel computation, I’m just returning the world space coordinate.

If you want to use linear z values instead you can compute the world coordinate like this:


Ahh, you are my savior! The first playground is perfectly what I need. So many thanks!
I’ve only changed forced use of 32 bits for depth map, because without it a wierd effect comes up when camera too far from volume I want to darken. Will it couse any performance issues?
The result in playground below:

No, there’s no real performance issue by using a 32 bits texture instead of a 16 bits one. Only that the texture is taking two times more memory but that should not be a problem.

1 Like