【问题标题】:Three.js Inaccurate RaycasterThree.js 不准确的 Raycaster
【发布时间】:2017-01-04 23:26:17
【问题描述】:

我正在尝试检测平面网格上的点击。我使用示例作为指南设置了一个光线投射器。

代码如下: http://jsfiddle.net/BAR24/o24eexo4/2/

当您在标记线下方单击时,即使单击在平面内,也不会检测到单击(标记线无效)。

还可以尝试调整屏幕大小。然后,即使在标记线上方点击也可能不起作用。

也许这与使用正交相机有关?还是不更新一些必需的矩阵?

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");
  }
}

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    您的 CSS 会影响您的光线投射计算。您可以做的一件事是设置

    body {
        margin: 0px;
    }
    

    有关详细信息,请参阅THREE.js Ray Intersect fails by adding div

    您还必须正确处理窗口大小调整。典型的模式如下所示:

    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 );
    
    }
    

    研究三个.js 示例。具体见http://threejs.org/examples/webgl_interactive_cubes_ortho.html

    另外,请阅读 this answer,其中描述了如何正确实例化正交相机。

    three.js r.80

    【讨论】:

    • 看起来它修复了小提琴示例。明天将测试我的程序。谢谢!
    【解决方案2】:

    试试这个......即使你有边距和滚动,它也可以在任何地方工作!

    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);  
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-05
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2016-04-25
      • 2018-02-17
      • 2013-04-14
      相关资源
      最近更新 更多