Depth to World Position

Hi,

In a post process material, what would be the best way to get the World Position of a pixel rendered in the CustomDepth GBuffer?

I tried CameraPosition - (CameraVector * CustomDepth.r), but the result is obviously wrong.

Thanks.

That doesn’t seem far off. You probably just need to divide the CustomDepth value by the dotproduct of cameravector and cameraforwardvector.

Remember that depth is actually from an orthographic perspective, whereas real distance is shaped like a circle around the camera (ie, the side of the frame actually is further from the camera for a given ‘depth’).

Thanks!
That was exactly it.

For others who might be interested in the answer, the code I’m using is:

WorldPosition = CameraPosition + CameraVector * CustomDepth.r / dot(CameraVector, CameraDirectionVector)

4 Likes

Excuse me for being a muppet, but what’s the difference between CameraVector and CameraDirectionVector?

Camera vector is a vector, that points from pixel to camera position. CameraDirectionVector, is a vector, that corresponds to direction, where camera is pointing in worldspace.

Exactly what i needed! Cheers!

When i use th code, the result is a white scene, normal?

Late, but for the record, if the output is always 1 / white…

Be sure to normalize the vectors before taking a dot product.

Then maybe double check the rendering settings for your mesh in the Details panel. “Render CustomDepth” needs to be True (it’s false by default). For non-custom depth, “Render in Depth Pass” is True by default.

This code will always return 1 if the object is not set to render in CustomDepth. If Render in Depth Pass is ticked, you may be able to replace “SceneTexture:CustomDepth.r” with the “SceneTexture:SceneDepth.r” or just use the “SceneDepth” node.

Also, keep in mind- this finds the World Position of some point being rendered, not the WP / pivot point of an entire object.

For the above formula, remember to normalize vectors before taking a dot product.

WorldPosition = CameraPosition + CameraVector * { CustomDepth.r / 
    dot [ normalize(CameraVector), normalize(CameraDirectionVector) ] } 

Since getting the order of operations right took some thought - here’s the same setup in BP using a SceneDepth node:

Much thanks to everyone here over the last 6 years - really helped my understanding.

5 Likes

In 5.3, the WorldPosition node gives the same result as the calculation mentioned above.