Vector3.Project incorrectly projects behind/inverse

As the title says, Vector3.Project will not give the right cordinates if the point is behind the camera. Here is a PG Repro:

Turn around to see the marker being displayed at the incorrect point.

Hi,

this is because of your css definitions. You min/max to the borders of the canvas. This will roughly explain it -

Babylon.js Playground (babylonjs.com)

You can see that the marker is disappearing now when rotating to the right. Since you are adding the marker to the body, css overflow won’t do any good. what you can do is to hide the marker if it is out of boundries, or change the html structure to fit the scenario

1 Like

@RaananW,

The marker is clamped to edges of the canvas (Math.min(Math.max(pos.x, 0), canvas.width - 16), see lines 42/43). I’ve confirmed that Vector3.Project will return incorrect values. Here is another PG that directly displays the output of Vector3.Project. When turning around, the projected x/y is incorrect.

Yeah, this is exactly what I am saying - this is incorrect, because you keep the marker on the canvas instead of removing it from it.

What is wrong with the project values? how does your playground show this?

1 Like

If you turn around, Vector3.Project will return coordinates that are not correct. The values displayed in the tp left are directly from Vector3.Project. When turning around, I get something like this:


Which is incorrect. The sphere is behind the camera in the screenshot, not in front, and yet Vector3.Project thinks it is in front of the camera.

vector project provides 2d coordinates of the object, excluding depth. It projects a vector to screen space. These are the coordinates on the screen, in which the object would be. You are practically showing this the way you use it. Why is that not correct?

What did you expect the project function to return?

1 Like

I would assume since the object is not on screen, It would return something like when the “inverse” is not on screen, which is the coordinates relative to the object (e.g. (4300, 200)).

Also, what does the z returned mean/do? I would like to try and detect if the object is off screen, so I can add additional logic for depth (I need it for my use case).

The Z appears to give a relative distance of the point to the center of the canvas (where the canvas is facing), being < 1 when by the point and > 1 when by the “inverse” point, getting closer to 1 as the camera’s facing gets closer to that of either point, so using 1 < z < 1.25 I can effectively check for the depth.

Thanks for all the help!

2 Likes

I have a question from Vortex’s playground:
My revised PG:

In this scene, the target sphere is on the right side.The indicator should be on the right side as well. But it’s on the top-left corner now.

The mechanism you explained is rational. For it is outside the viewport (& behind the camera in theory). But how to put the indicator on the right side as in real world?