检测THREE.js中的点击对象

时间:2011-10-31 16:16:08

标签: javascript three.js

我有一个THREE.js场景,其中出现了很多元素,我需要检测用户点击的对象。

到目前为止,我所做的是以下内容。相机不会移动太多 - 它只会将垂直位置改变一个有限的量,始终朝向同一点。我的近似方法如下:

  • 如果点击相对于画布
  • ,我会获取坐标
  • 我通过简单的重新缩放将它们转换为webGL场景中的水平和垂直坐标,并添加一个足够远的Z坐标。
  • 我从上面的点开始拍摄水平光线,由THREE.Ray()
  • 构建
  • 我使用ray.intersectObjects()来查找光线的第一个元素。

此方法近似有效,但有时距离实际点几个像素。

  

是否有更可靠的技术来找出用户点击的对象?

4 个答案:

答案 0 :(得分:9)

取决于您使用的是哪种相机。

1) PerspectiveCamera Mr.doob 提供的确定链接。
2) OrthographicCamera :完全不同:

var init = function() {
  camera = new THREE.OrthographicCamera( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, NEAR, FAR);
  document.addEventListener( 'mousedown', onDocumentMouseDown, false );
}

function onDocumentMouseDown( e ) {
  e.preventDefault();
  var mouseVector = new THREE.Vector3();
  mouseVector.x = 2 * (e.clientX / SCREEN_WIDTH) - 1;
  mouseVector.y = 1 - 2 * ( e.clientY / SCREEN_HEIGHT );
  var raycaster = projector.pickingRay( mouseVector.clone(), camera );
  var intersects = raycaster.intersectObject( TARGET );
  for( var i = 0; i < intersects.length; i++ ) {
    var intersection = intersects[ i ],
    obj = intersection.object;
    console.log("Intersected object", obj);
  }
}

答案 1 :(得分:4)

看看这个:

var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 5000);
var object; //your object

document.addEventListener('mousedown', onMouseDown, false);

function onMouseDown(e) {
    var vectorMouse = new THREE.Vector3( //vector from camera to mouse
        -(window.innerWidth/2-e.clientX)*2/window.innerWidth,
        (window.innerHeight/2-e.clientY)*2/window.innerHeight,
        -1/Math.tan(22.5*Math.PI/180)); //22.5 is half of camera frustum angle 45 degree
    vectorMouse.applyQuaternion(camera.quaternion);
    vectorMouse.normalize();        

    var vectorObject = new THREE.Vector3(); //vector from camera to object
    vectorObject.set(object.x - camera.position.x,
                     object.y - camera.position.y,
                     object.z - camera.position.z);
    vectorObject.normalize();
    if (vectorMouse.angleTo(vectorObject)*180/Math.PI < 1) {
        //mouse's position is near object's position

    }
}

答案 2 :(得分:3)

检查鼠标与3d空间中的任何立方体的交叉点并改变它的颜色。也许this会帮助你。

答案 3 :(得分:0)

我试图为画布实现这个问题时遇到了问题,画布没有占据屏幕的整个宽度和高度。这是我发现的解决方案非常好的解决方案。

初始化现有画布上的所有内容:

var init = function() {
  var canvas_model = document.getElementById('model')
  var viewSize = 50 // Depending on object size, canvas size etc.
  var camera = new THREE.OrthographicCamera(-canvas_model.clientWidth/viewSize, canvas_model.clientWidth/viewSize, canvas_model.clientHeight/viewSize, -canvas_model.clientHeight/viewSize, 0.01, 2000),
}

向画布添加事件侦听器:

canvas_model.addEventListener('click', function(event){
  var bounds = canvas_model.getBoundingClientRect()
  mouse.x = ( (event.clientX - bounds.left) / canvas_model.clientWidth ) * 2 - 1;
  mouse.y = - ( (event.clientY - bounds.top) / canvas_model.clientHeight ) * 2 + 1;
  raycaster.setFromCamera( mouse, camera );
  var intersects = raycaster.intersectObjects(scene.children, true);
  if (intersects.length > 0) {
     // Do stuff
  }
}, false)

或者对于'touchstart'事件,将计算mouse.x和mouse.y的行更改为:

mouse.x = ( (event.touches[0].clientX - bounds.left) / canvas_model.clientWidth ) * 2 - 1;
mouse.y = - ( (event.touches[0].clientY - bounds.top) / canvas_model.clientHeight ) * 2 + 1;