0

我想创建多个立方体,在每个立方体上添加一个单击事件,并将它们与 GeometryUtils.merge 组合在一个“父”对象中。

    var cubes=[];
    var mainGeom =  new THREE.Geometry(); 
    var cubegeometry =  new THREE.CubeGeometry(50, 50, 50, 1, 1, 1 );

    cubes[1] = new THREE.Mesh( cubegeometry );
    cubes[1].position.y = -50; 
    THREE.GeometryUtils.merge( mainGeom, cubes[1] );

    cubes[2] = new THREE.Mesh( cubegeometry );
    cubes[2].position.y = 50; 
    THREE.GeometryUtils.merge( mainGeom, cubes[2] ); 

    cubes[3] = new THREE.Mesh( cubegeometry );
    cubes[3].position.z = -50; 
    THREE.GeometryUtils.merge( mainGeom, cubes[3] ); 

    cube = new THREE.Mesh( mainGeom,new THREE.MeshBasicMaterial( { color: 0xff0000 } )); 

结合工作正常。但是我怎样才能在每一个上添加一个点击事件呢?我正在使用这个:

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

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

    var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
    projector.unprojectVector( vector, camera ); 

    var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
    var intersects = raycaster.intersectObjects( cube );

    if ( intersects.length > 0 ) {
      console.log(intersects);
    } 
}

但 intersects.length 始终为 0!我究竟做错了什么?

非常感谢!

问候 - 科沙

4

1 回答 1

0

GeorgeProfenza 给了我一个很好的暗示。解决方案是将这些子对象添加到父对象,然后 - 这就是解决方案!谢谢

于 2013-08-08T09:11:25.190 回答