我想瞄准具有相机视觉的物体(因为用户会看到物体,而不是用鼠标指向物体).
我像这样从相机投射光线
rotation.x = camera.rotation.x;
rotation.y = camera.rotation.y;
rotation.z = camera.rotation.z;
raycaster.ray.direction.copy( direction ).applyEuler(rotation);
raycaster.ray.origin.copy( camera.position );
var intersections = raycaster.intersectObjects( cubes.children );
Run Code Online (Sandbox Code Playgroud)
这让我得到了交叉点,但它似乎有时会徘徊.所以我想添加目标(十字准线).那将是光线末端或中间的物体(网格)上的某种东西.
我该如何添加它?当我创建一个常规线时,它位于相机前面,因此屏幕会变黑.
您可以将简单几何体构造的十字准线添加到相机中,如下所示:
var material = new THREE.LineBasicMaterial({ color: 0xAAFFAA });
// crosshair size
var x = 0.01, y = 0.01;
var geometry = new THREE.Geometry();
// crosshair
geometry.vertices.push(new THREE.Vector3(0, y, 0));
geometry.vertices.push(new THREE.Vector3(0, -y, 0));
geometry.vertices.push(new THREE.Vector3(0, 0, 0));
geometry.vertices.push(new THREE.Vector3(x, 0, 0));
geometry.vertices.push(new THREE.Vector3(-x, 0, 0));
var crosshair = new THREE.Line( geometry, material );
// place it in the center
var crosshairPercentX = 50;
var crosshairPercentY = 50;
var crosshairPositionX = (crosshairPercentX / 100) * 2 - 1;
var crosshairPositionY = (crosshairPercentY / 100) * 2 - 1;
crosshair.position.x = crosshairPositionX * camera.aspect;
crosshair.position.y = crosshairPositionY;
crosshair.position.z = -0.3;
camera.add( crosshair );
scene.add( camera );
Run Code Online (Sandbox Code Playgroud)
Three.js r.71
http://jsfiddle.net/L0rdzbej/9/
如果您没有特殊的用例,您需要像往常一样从相机中检索位置和旋转,我猜您的"徘徊"可以通过使用以下参数调用您的raycaster来修复:
raycaster.set( camera.getWorldPosition(), camera.getWorldDirection() );
var intersections = raycaster.intersectObjects( cubes.children );
Run Code Online (Sandbox Code Playgroud)
投射可见光线 然后,您可以通过使用箭头帮助器绘制箭头来显示3D空间中的光线投射.在光线投射后执行此操作:
scene.remove ( arrow );
arrow = new THREE.ArrowHelper( camera.getWorldDirection(), camera.getWorldPosition(), 100, Math.random() * 0xffffff );
scene.add( arrow );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5406 次 |
| 最近记录: |