确定在three.js中单击的对象

时间:2015-11-02 05:39:03

标签: three.js

我希望检测对象何时被点击。我正在从three.js编辑器生成一个页面。这是一个很棒的环境。

根据这篇文章(https://mandemeskel.wordpress.com/2013/08/19/mouse-events-raycasting-with-three-js/)的建议,我最终得到了以下代码。我在有问题的地方发表评论。

function mousedown( event ) {
    var scene = this.parent;
    var projector = new THREE.Projector();
    var mouse_vector = new THREE.Vector3(),
    mouse = { x: 0, y: 0, z: 1 },
    ray = new THREE.Raycaster( new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0) ),
    intersects = []; 
    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
    mouse_vector.set( mouse.x, mouse.y, mouse.z );

    // How do I get the camera?
    projector.unprojectVector( mouse_vector, camera );

    var direction = mouse_vector.sub( camera.position ).normalize();

    ray.set( camera.position, direction );

    var object =  scene.getObjectByProperty( 'uuid', this.uuid, true );

    intersects = ray.intersectObject( object );

    if( intersects.length ) {
            alert( "hit" );
            event.preventDefault();  
    }
 }

我通过修改已发布网页中的app.js暂时解决了问题。通过将摄像机变量移动到全局,我绕过它,但它显然是一个软糖。有没有办法在不修改app.js的情况下访问编辑器生成的摄像机?

var camera;
var APP = {
Player: function () {

...

1 个答案:

答案 0 :(得分:0)

令人敬畏的mrdoob已修改编辑器以包含getCamera()调用 https://github.com/mrdoob/three.js/issues/7510#issuecomment-153059247 现在可以通过

访问默认摄像机
var camera = player.getCamera();