【发布时间】:2017-01-16 01:50:12
【问题描述】:
每当我点击场景时,我都会调用它。
function onMouseDown(event) {
controls.update();
var position = new THREE.Vector3();
position.setFromMatrixPosition( scene.group.children[0].matrixWorld );
position.normalize();
console.log(position);
var raycaster = new THREE.Raycaster(); // create once
var mouse = new THREE.Vector2(); // create once
mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;
//console.log(mouse);
raycaster.setFromCamera( mouse, camera );
console.log(raycaster.ray.direction);
var intersects = raycaster.intersectObjects( scene.group.children , false );
if ( intersects.length > 0 ) {
intersects[ 0 ].object.material.color.set( 0xff0000 );
console.log(intersects);
}
}
最初,当我没有应用旋转时,点击可以工作,但是一旦我调用
scope.target.x = offset * Math.sin( (scope.phi + iphi) * Math.PI / 180 ) * Math.cos( (scope.theta + itheta) * Math.PI / 180 );
scope.target.y = offset * Math.cos( (scope.phi + iphi) * Math.PI / 180 );
scope.target.z = offset * Math.sin( (scope.phi + iphi) * Math.PI / 180 ) * Math.sin( (scope.theta + itheta) * Math.PI / 180 );
scope.camera.lookAt( scope.target );
RayCaster 不返回任何对象。
我还注意到,即使在旋转之后,我的光线投射器也会在屏幕的同一位置给出相同的方向矢量。
然后我尝试手动在 raycaster 上应用旋转
var e = new THREE.Euler(((controls.phi-90)/180)*Math.PI, 0, (controls.theta/180)*Math.PI, 'XYZ');
raycaster.setFromCamera( mouse, camera );
raycaster.ray.direction.applyEuler( e );
但现在我在附近的位置得到交叉点,而不是在对象的确切位置。
【问题讨论】:
标签: 3d three.js raycasting