最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Three.js Inaccurate Raycaster - Stack Overflow

matteradmin9PV0评论

I am trying to detect clicks on my Plane mesh. I set up a raycaster using the examples as a guide.

Here is the code: /

When you click below the marker line, no click will be detected even though the click was inside the plane (marker line has no effect).

Also try resizing the screen. Then, even clicks above the marker line may not work.

Maybe this has to do with use of an orthographic camera? Or not updating some required matrix?

function onMouseDown(event) {
  event.preventDefault();

  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  //console.log("x: " + mouse.x + ", y: " + mouse.y);

  raycaster.setFromCamera(mouse, camera)

  var intersects = raycaster.intersectObjects(objects);

  if (intersects.length > 0) {
    console.log("touched:" + intersects[0]);
  } else {
    console.log("not touched");
  }
}

I am trying to detect clicks on my Plane mesh. I set up a raycaster using the examples as a guide.

Here is the code: http://jsfiddle/BAR24/o24eexo4/2/

When you click below the marker line, no click will be detected even though the click was inside the plane (marker line has no effect).

Also try resizing the screen. Then, even clicks above the marker line may not work.

Maybe this has to do with use of an orthographic camera? Or not updating some required matrix?

function onMouseDown(event) {
  event.preventDefault();

  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  //console.log("x: " + mouse.x + ", y: " + mouse.y);

  raycaster.setFromCamera(mouse, camera)

  var intersects = raycaster.intersectObjects(objects);

  if (intersects.length > 0) {
    console.log("touched:" + intersects[0]);
  } else {
    console.log("not touched");
  }
}
Share Improve this question edited Aug 29, 2016 at 2:17 BAR asked Aug 29, 2016 at 2:12 BARBAR 17.3k27 gold badges106 silver badges207 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Your CSS will affect your raycasting calculations. One thing you can do is set

body {
    margin: 0px;
}

For more information, see THREE.js Ray Intersect fails by adding div.

You also have to handle window resizing correctly. The typical pattern looks like this:

function onWindowResize() {

    var aspect = window.innerWidth / window.innerHeight;

    camera.left   = - frustumSize * aspect / 2;
    camera.right  =   frustumSize * aspect / 2;
    camera.top    =   frustumSize / 2;
    camera.bottom = - frustumSize / 2;

    camera.updateProjectionMatrix();

    renderer.setSize( window.innerWidth, window.innerHeight );

}

Study the three.js examples. In particular, see http://threejs/examples/webgl_interactive_cubes_ortho.html.

Also, read this answer, which describes how to properly instantiate an orthographic camera.

three.js r.80

Try this... It will work anywhere even you have margin and scroll!

function onMouseDown(event) {
      event.preventDefault();

      var position = $(WGL.ctx.domElement).offset(); // i'm using jquery to get position 
      var scrollUp = $(document).scrollTop();
      if (event.clientX != undefined) {
           mouse.x = ((event.clientX - position.left) / WGL.ctx.domElement.clientWidth) * 2 - 1;
           mouse.y = - ((event.clientY - position.top + scrollUp) / WGL.ctx.domElement.clientHeight) * 2 + 1;
      } else {
           mouse.x = ((event.originalEvent.touches[0].pageX - position.left) / WGL.ctx.domElement.clientWidth) * 2 - 1;
           mouse.y = - ((event.originalEvent.touches[0].pageY + position.top - scrollUp) / WGL.ctx.domElement.clientHeight) * 2 + 1;
      }

      raycaster.setFromCamera(mouse, camera)
      var intersects = raycaster.intersectObjects(objects);  
    }
Post a comment

comment list (0)

  1. No comments so far