使用 4x4 矩阵动态移动、旋转透视相机

Move, rotate perspective camera with 4x4 matrix dynamically

亲爱的社区,

最近几天我一直在处理同样的问题,这是我相机的 View Matrix (4x4) 的更新。它应该用于通过 three.js.

中的 AR-Scene 设置位置

自定义矩阵包含来自 Direct Sparse Odometry 的值,这些值将通过 WebView 的 JavaScript-Interface 解析为字符串,并提供我当前姿势的值。根据我的初始点,DSO 开始跟踪房间和摄像机位置(让我们忽略它以保持简单),之后应该与透视摄像机同步。 将此字符串转换为 4x4 矩阵有效,但设置:

  1. matrixWorldInverse (ViewMatrix?) 没有引起任何结果(不更新,也没有使用任何 'common' 更新函数)
  2. projectionMatrix 导致场景的放大和缩小,但不提供任何移动、旋转。据我所知,它仅用于特征?

有没有其他方法可以在场景内移动、旋转相机?

如果我使用 Matrix.set() 方法或 fromArray() and/or threejs 提供的任何其他更新函数,则没有任何变化。

非常感谢!

   var renderer, scene, camera, box, geometry, material, poseMatrix;

   init();
   animate();
   render();

   function init() {
        // renderer
       renderer = new THREE.WebGLRenderer({
           canvas: document.getElementById("mCanvas"),
           alpha: true
       });
       renderer.setClearColor(0xffffff, 0);
       renderer.setSize(window.innerWidth, window.innerHeight);
       document.body.appendChild(renderer.domElement);
       // scene
       scene = new THREE.Scene();

       // camera
       camera = new THREE.PerspectiveCamera(
           75,
           window.innerWidth / window.innerHeight,
           1,
           1000
       );

       scene.add(camera);

       scene.add(new THREE.AxesHelper(100));

       placeBox();
       initPose();

   }

   function render() {
       renderer.render(scene, camera);
   }

   function placeBox()
   {
   // Using a test object which needs to have an absolute  position in world coordinates
       geometry = new THREE.BoxBufferGeometry(3, 5, 3 );
       material = new THREE.MeshLambertMaterial({color: 0xfece46, wireframe: true, wireframeLinewidth: 3.1});
       box = new THREE.Mesh(geometry, material);

       // depends on parent coordinates
       box.position.set(0, 0, -10);
       scene.add(box);
       }

   function animate() {
       setCurrentPose();
       renderer.render(scene, camera);
   }

   function initPose(){
       // Default value provided by DSO
       var mTmpPose = "1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1"; //initial point of DSO
       var mDefaultPose = mTmpPose.split(',');

       // DSO Standard Pose 4x4 Matrix
      camera.matrixAutoUpdate = false;
      camera.matrixWorldInverse.fromArray(mDefaultPose);

       camera.updateWorldMatrix();

     //camera.updateProjectionMatrix(); <- does not work
    //camera.updateMatrix(); <- same issue

   }
   function setCurrentPose(){

   //Provides a string with current pose values
       var tmp = Android.getCurrentPose(); // example: 0.8953773, 0.39648485, 0.20272951, 0.0,
                                           //         -0.3792577, 0.44037524, 0.81377715, 0.0,
                                           //           0.23337325,-0.8055243, 0.5446719, 0.0,
                                           // .          -2.5238492, 13.470952, 0.715593, 1.0
       var pose = tmp[0].split(',');

       camera.matrixWorldInverse.fromArray(pose);
   // camera.projectionMatrix.fromArray(pose);

       camera.updateWorldMatrix();
    //   camera.updateProjectionMatrix(); <- does not work
   // camera.updateMatrix(); <- same issue
   }



在增强现实中无法设置相机矩阵(就像在 VR 中一样) 因为在 AR 中相机位置由用户控制。这不是 Aframe/Three.js 独有的,并且在所有主要的增强现实框架中都是相同的。

一般情况下,场景的原点和相机的初始位置是相同的,这使得框架更容易相对于原点(相机的起始位置)放置内容。

一旦场景的原点被确定,相机就可以根据用户的判断在场景中自由移动,所以你不能强迫相机朝某个方向看,因为渲染引擎没有那个控制。

[更新]

如果您试图强制用户关注场景中的特定内容,那么您可以尝试移动场景内容,使其直接出现在用户的视野中。