本文介绍了三.JS-在Collada中检测交叉点(Ray Casting)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是当我使用:

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

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

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

但是当我使用:

 controls = new THREE.TrackballControls( camera );

我可以得到相交矩阵并且它工作得很好,但是发现了另一个问题(看这里:Three.JS -- 与场景中的文本框冲突的相机控件)

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

谢谢.

推荐答案

这不是 Collada 的问题.

This is not a Collada problem.

改为这样做:

// 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 作为轨迹球控件的第二个参数.

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

http://jsfiddle.net/QXUwa/

这篇关于三.JS-在Collada中检测交叉点(Ray Casting)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 05:19