Debugging three-dimensional coordinates of a raycaster mouse

I made a raycaster to cross some object inside the canvas. It works well if the canvas is one in the browser window, but it does not get intersection if I put the canvas inside some other GUI, and therefore the position of the canvas is different. I think this is a problem with the coordinates of the mouse. How can I debug it? If I know the coordinates of the mouse, how can I understand what is the coordinate of the canvas?

function getPosition(event) {
// TODO: add the border of the canvas
var x = new Number();
var y = new Number();

if (event.x != undefined && event.y != undefined) {
      x = event.x;
      y = event.y;
 } else {
      // Firefox method to get the position
      x = event.clientX + document.body.scrollLeft +
          document.documentElement.scrollLeft;
      y = event.clientY + document.body.scrollTop +
          document.documentElement.scrollTop;
 }

 x -= canvas.offsetLeft;
 y -= canvas.offsetTop;

 return {x: x, y: y};    
}
+4
source share
1 answer

what you need:

// getBoundingClientRect() returns the element position relative to the browser visible viewport.
// clientX/Y returns the mouse position relative to the browser visible viewport.
// we then just have to find the difference between the two to get the mouse position in "canvas-space"

var canvasPosition = renderer.domElement.getBoundingClientRect();

var mouseX = event.clientX - canvasPosition.left;
var mouseY = event.clientY - canvasPosition.top;

var mouseVector = new THREE.Vector2 (
        2 * (mouseX / window.innerWidth) - 1,
    1 - 2 * (mouseY / window.innerHeight));

and you use mouseVectorto cross.

+4
source

Source: https://habr.com/ru/post/1584130/


All Articles