How to restore 3d position from DepthRenderer Z?

Hi!

I created an example where I am attempting to restore a 3D point in the world using the Z depth stored in the scene.enableDepthRenderer:

However, it works incorrectly; when I move the mouse upward on that example, the sphere goes inside the plane.

Have no idea what I am doing wrong, tried many ways to store depth and restore, asked all possible AI, all the results just does not work as expected.

I want to restore the 3D point on the CPU side under the mouse cursor.

Please help =)

If you can’t solve a problem, then you need more information that you can analyse.

Maybe this is the solution, even though the ball is placed incorrectly.

Made with CoPilot:

Vector3 Explanatory PG | Babylon.js Playground

I spent quite a lot on that, playing with AI and my self, it looks like or there are problem in Babylonjs or there are something AI and I can not find.

@CodingCrusader

I fixed it myself. AI was completely wrong.

There was only one small problem - depthTexture was flipped on the Y axis, all else was right, here is a working example if someone needs it:

@Deltakosh I am not sure who is responsible for documentation, but I think it is a good idea to add this example to it, in the section:

Now it does not explain that the screen space Vector3 it expects must contain non-linear Z in [0-1] range.

But also, it is a good idea first to check this bug related to this unproject:

3 Likes

I think this is because HTML canvas (and therefore also mouse pos) has its origin at top-left whereas WebGL at bottom-left.

Probably yes, or because Babylon.js uses flipped y coordinate.

But more interesting is why it works.

Non-linear Z in [0-1] range goes together with space X,Y in pixel range [0-screenSize], not [0-1] and then there are a few linear (matrix) transforms - and we got correct space 3D point… I have no idea.

This can help with a few of the logic: Babylon.js docs

1 Like