Three.JS- Detecting intersection in Collada (Ray Casting)

大憨熊 提交于 2019-12-11 07:50:00

问题


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/



来源:https://stackoverflow.com/questions/12492702/three-js-detecting-intersection-in-collada-ray-casting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!