My current project involves loading .obj files using THREE.OBJLoader() and then adding each object to the screen by pushing it into the myObjs[] array.
var myObjs = [];
var loader = new THREE.OBJLoader();
loader.addEventListener( 'load', function ( event ) {
var object = event.content;
object.position.x=xpos;
object.position.y = ypos;
scene.add( object );
teeth.push(object);
});
loader.load( 'obj/myobj1.obj' );
loader.load('obj/myobj2.obj');
loader.load('obj/myobj3.obj');
To determine whether an object is clicked or not, I use the following code:
function onDocumentMouseDown( event ) {
event.preventDefault();
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var intersects = ray.intersectObjects( teeth, true );
if ( intersects.length > 0 ) {
intersects[0].object.position.z=50;
}
Everything seems to be working fine, but I'm currently struggling with figuring out how to track which object in the myObjs[] array was clicked. Any ideas on how to map intersects[0].object to myObjs array would be greatly appreciated.
Best regards, ZB