Kosha Misa
Kosha Misa

Reputation: 351

three.js - intersectObjects with GeometryUtils.merge

I'm would like to create multiple cubes, add a click-event on each one of them and combine them in a "parent" object with 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 } )); 

Combining is working fine. But how can i add a click event on each one of them? I am using this:

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

But the intersects.length is always 0! What am I doing wrong?

Thanks a lot!

Regards - Kosha

Upvotes: 0

Views: 1543

Answers (1)

Kosha Misa
Kosha Misa

Reputation: 351

GeorgeProfenza has given me a good hint. The solution was to add these Subobject to a parent object, then - this was the solution! Thanks

Upvotes: 0

Related Questions