R81中的ThreeJS光线投射与R71不同

时间:2016-10-12 18:46:04

标签: three.js raycasting

我有一个带网眼的飞机。当用户双击网格时,我的代码会绘制一个球。这在R71中运行得很好,但是一旦切换到R81,raycaster就不会返回相交。这是代码:

在init()中:

// Plane
plane = new THREE.Mesh(
    new THREE.PlaneBufferGeometry( 1000, 1000, 3, 3 ),
    new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: .5, transparent: true } )
);

plane.visible = false;
scene.add( plane ); 
planes.push(plane);

在doubleClickEvent()中:

event.preventDefault();

var mouse = new THREE.Vector2((event.clientX  / window.innerWidth ) * 2 - 1, -(((event.clientY / window.innerHeight ) * 2 - 1)));
var directionVector = new THREE.Vector3();
directionVector.set(mouse.x, mouse.y, 0.1);
directionVector.unproject(camera);
directionVector.sub(camera.position);           
directionVector.normalize();
raycaster.set(camera.position, directionVector);
intersects = raycaster.intersectObjects(planes);
if (intersects.length) {
    var sphereParent = new THREE.Object3D();
    var sphereGeometry = new THREE.SphereGeometry(.1, 16, 8);
    var sphereMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
    var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
    sphereParent.add(sphere);
    sphereParent.position.set(intersects[0].point.x, intersects[0].point.y, 0.0);
    scene.add(sphereParent);
    objects.push(sphereParent);
}

如果我改变

intersects = raycaster.intersectObjects(planes); 

intersects = raycaster.intersectObjects(scene.children);

球被拉出但是它被拉到了错误的位置。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我找到了答案。光线投射不起作用的原因是因为平面的可见性是错误的。解决方案是改变材料可见性而非平面的可见性。