I'm trying to click on VR simulation mode in the browser (Will also be used on mobile), and show a yellow ray from the camera (my eyes/center of window) to the mouse cursor, but I can't get the right direction/position.
public function onMouseDown(e:Event): Void
{
// The camera in VR was created automatically by the threejs engine.
var camera:WebXRArrayCamera = renderer.xr.getCamera();
// x and y will get a value from (-1 to +1)
// I have no clear with z must be in -0.5 and not another value.
var x:Float = (event.clientX / canvas.parentElement.offsetWidth ) * 2 - 1;
var y:Float = -(event.clientY / canvas.parentElement.offsetHeight) * 2 + 1;
var z:Float = -0.5;
var mouse:Vector3 = new Vector3(x, y, z);
var origin:Vector3 = new Vector3();
origin.copy(camera.position);
var direction:Vector3 = new Vector3();
direction.copy(mouse);
var arrow:ArrowHelper = new ArrowHelper(direction, origin, 5, 0xffff00);
scene.add(arrow);
}
If I click in the center of window, it's more or less correct, but if I move my cursor mouse, for example, to the right, the result is wrong as you can see in the screenshoot https://ibb.co/TBnNtnb
https://jsfiddle.net/0t2yczh7/
The yellow arrow shuld be exactly centered under my mouse cursor and it is not. If I go more to the right the offset is bigger.
What I am doing wrong?
By other hand, the unit of measure in my world is the meter, so the values for x and y (-1 to +1) I'm not sure if is wrong. How I can translate pixels (canvas.parentElement.offsetWidth) to meters?? For example, something like 1920x1080 to 7.55x4.25, depending of the Fov.
You can utilize
raycastersHere is how you would set it up:
In your
animationloop, you would need to add the following:Lastly, you need to add an event listener:
That should do it~