forux
June 29, 2025, 4:06pm
1
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
forux
June 29, 2025, 6:24pm
3
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.
forux
June 30, 2025, 10:24am
4
@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:
public static _UnprojectFromInvertedMatrixToRef<T extends Vector3>(source: DeepImmutable<Vector3>, matrix: DeepImmutable<Matrix>, result: T): T {
Vector3.TransformCoordinatesToRef(source, matrix, result);
const m = matrix.m;
const num = source._x * m[3] + source._y * m[7] + source._z * m[11] + m[15];
if (WithinEpsilon(num, 1.0)) {
result.scaleInPlace(1.0 / num);
}
return result;
}
This is from here:
This must be the opposite …
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.
forux
June 30, 2025, 10:50am
6
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