如何检测特定的三维模型碰撞three.js

时间:2015-06-21 20:46:06

标签: javascript 3d three.js collision

我正在尝试使用Leap Motion制作可点击的3D对象。我有两个3D模型,IronMan头盔和Thor of Thor。如果选择了这两个对象中的一个,那么将改变颜色。因为钢铁侠将是绿色的,因为锤子将是白色的。但是当我选择锤子时,它会变为绿色而不是白色。我使用立方体作为指针来选择对象。你能帮我吗?

这是插入3d模型的代码:

var colladaMeshlist = []; 
model = new THREE.Object3D()
var object;
var skin;
var objectScale = 1.5;
rotX=0, rotY=0, rotZ=0;
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load( 'models/IronMan.dae', function (collada) {
    object = collada.scene;

    object.scale.x = object.scale.y = object.scale.z = objectScale;

    object.position.x= 20;
    object.position.y= -40;
    object.position.z= 0;

    object.rotation.x= rotX;
    object.rotation.y= rotY;
    object.rotation.z= rotZ;

    object.name="IronMan";      
    colladaMeshlist.push(object);
    model.add(object);

});
scene.add(model);

model2 = new THREE.Object3D()
var object2;
var skin2;
var objectScale2 = 1;
posX=0, posY=-40, posZ=0;
rotX=0, rotY=0, rotZ=0;
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load( 'models/Hammer.dae', function (collada) {
    object2 = collada.scene;

    object2.scale.x = object2.scale.y = object2.scale.z = objectScale2;

    object2.position.x= -20;
    object2.position.y= -20;
    object2.position.z= posZ;

    object2.rotation.x= rotX;
    object2.rotation.y= rotY;
    object2.rotation.z= rotZ;
    object2.name="Hammer";  
    colladaMeshlist.push(object2);
    model2.add(object2);
});
scene.add(model2);

这是选择对象的代码:

      var originPoint = cube.position.clone();
      for(var vertexIndex = 0; vertexIndex < cube.geometry.vertices.length; vertexIndex++)
       {
           var localVertex = cube.geometry.vertices[vertexIndex].clone();
           var globalVertex = localVertex.applyMatrix4( cube.matrix );
           var directionVector = globalVertex.sub( cube.position );
           var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
           var collisionResults = ray.intersectObjects(colladaMeshlist, true);

           if(collisionResults.length > 0 && collisionResults[0].distance < directionVector.length())  
          {
             collisionResults[0].object.material.color.setHex(0x4CAF50);
          } 
          else if (collisionResults.length > 0 && collisionResults[1].distance < directionVector.length())
          {
             collisionResults[1].object.material.color.setHex(0xffffff);
          }
  }

0 个答案:

没有答案