three.js - 使用GeometryUtils.merge的intersectObjects

时间:2013-08-07 21:20:29

标签: three.js

我想创建多个多维数据集,在每个多维数据集上添加一个click事件,并将它们组合在一个带有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! 我做错了什么?

非常感谢!

问候 - Kosha

1 个答案:

答案 0 :(得分:0)

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