【问题标题】:three.js Unproject camera within groupthree.js 在组内取消投影相机
【发布时间】:2016-03-06 18:31:56
【问题描述】:

我在 Group 对象中有一个子相机,我需要获取原点/方向:

var cameraRig = new THREE.Group();
cameraRig.add( cameraPerspective );
cameraRig.add( cameraOrtho );
scene.add( cameraRig );

function relativeMousePosition () {
    var canvasBoundingBox = renderer.domElement.getBoundingClientRect();
    var mouse3D = new THREE.Vector3(0, 0, 0.5);
    mouse3D.x = ((mouseX - 0) / canvasBoundingBox.width) * 2 - 1;
    mouse3D.y = -((mouseY - 0) / canvasBoundingBox.height) * 2 + 1;
    return mouse3D;
}

cameraRig.position.set(89,34,91);
cameraRig.lookAt(something.position);
cameraPerspective.position.set(123,345,123);

var dir = relativeMousePosition().unproject(camera).sub(cameraPerspective.position).normalize();
var origin = cameraPerspective.position;

上面的代码给出了带有cameraRig上下文的原点+方向。当我将相机排除在外,将场景作为直接父级时,它会为我提供我想要的世界原点/方向。那么如何结合 cameraRig 来获取世界原点/方向,以便我可以进行拾取或其他操作?

FIDDLEhttps://jsfiddle.net/647qzhab/1/

更新:

正如 Falk 在评论中提到的:

var dir = relativeMousePosition().unproject(camera).sub(cameraPerspective.getWorldPosition()).normalize();
var origin = cameraPerspective.getWorldPosition();

结果更好,但还不能完全令人满意,因为相机旋转似乎还没有应用。

【问题讨论】:

  • 您可以使用camera.getWorldPosition()camera.getWorldRotation()
  • camera.getWorldPosition() 似乎是非常正确的事情。它让原点是正确的。但我还不知道如何使用 camera.getWorldRotation(),它返回一个欧拉旋转,不知道如何在方向上应用它......提示赞赏:)
  • 是的,我没注意。还有camera.getWorldDirection(),这就是你想要的;-)
  • 嗯,我试过camera.getWorldDirection() 已经准备好了。它为我提供了相机视图中心的世界方向。但我需要基于相机世界投影/旋转的鼠标指针方向。该怎么做?

标签: camera three.js


【解决方案1】:

我需要为 cameraRig 更新 matrixWorld:

cameraRig.position.set(89,34,91);
cameraRig.lookAt(something.position);
cameraPerspective.position.set(123,345,123);

cameraRig.updateMatrixWorld(true);
cameraPerspective.updateMatrixWorld(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2019-04-10
    相关资源
    最近更新 更多