Three.JS- Collada的检测交叉口(Ray Casting)

时间:2012-09-19 10:15:18

标签: three.js collada intersect raycasting

我的原型系统中有一个功能,可以在您单击Collada文件时检测交叉点。交叉函数如下:

  function Intersectfun ( event ) {
   mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
   mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
   var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
   var toIntersect = [];
   THREE.SceneUtils.traverseHierarchy(scene, function (child) {
   if (child instanceof THREE.Mesh) {
    toIntersect.push(child);
   }
  });
  // Unproject the vector
  projector.unprojectVector(vector, camera);
  var ray = new THREE.Ray( camera.position, vector.subSelf(camera.position).normalize());
  var intersects = ray.intersectObjects( toIntersect );
  if(intersects.length){
   target = intersects[0].object;
  }
 }

但是当我使用:

controls = new THREE.TrackballControls( camera, renderer.domElement );

相交矩阵为空,无法检测交叉点!!!

但是当我使用时:

 controls = new THREE.TrackballControls( camera );

我可以得到相交矩阵并且效果很好,但是另一个问题被揭示出来(请看这里:Three.JS -- conflict Camera controls with a textbox in a scene

上述功能中是否有任何代码?为了您的信息,我使用了一个简单的相机:

 camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );

感谢。

1 个答案:

答案 0 :(得分:1)

这不是Collada问题。

请改为:

// container
container = document.createElement('div');
document.body.appendChild(container);

// renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);

//controls
controls = new THREE.TrackballControls(camera, container);

换句话说,使用container而不是renderer.domElement作为轨迹球控件的第二个参数。

http://jsfiddle.net/QXUwa/