Three.JS- Detecting intersection in Collada (Ray C

2019-08-07 05:11发布

问题:

There is a function in my prototype system which detects intersections when you click on a Collada file. The intersect function is below:

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

But when I use the:

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

The intersects matrix is empty and cannot detect intersections!!!

But when I use:

 controls = new THREE.TrackballControls( camera );

I can get the intersects matrix and it works very well, but another problem is revealed (look at here: Three.JS -- conflict Camera controls with a textbox in a scene)

Is there any code in the above function? For your information, I have used a simple camera:

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

Thanks.

回答1:

This is not a Collada problem.

Do this instead:

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

In other words, use container rather then renderer.domElement as the second argument to trackball controls.

http://jsfiddle.net/QXUwa/