获取相机位置javascript

bea*_*ear 1 javascript webvr aframe

我想在我的Aframe应用程序中获取相机的位置,但它始终为0,尽管我移动相机.

我尝试了事件监听器和定期检索位置,但没有获得更新的值.

这是场景:

<a-scene>
  <a-entity id="cameraWrapper" position="0 2 10" rotation="0 0 0">
    <a-camera near="0.1" user-height="0" id="camera"></a-camera>
  </a-entity>
  <a-plane position="0 4 4" rotation="-90 0 -90" width="4" height="4" color="#7BC8A4"></a-plane>
  <a-sky color="#ECECEC"></a-sky>
</a-scene>
Run Code Online (Sandbox Code Playgroud)

这是用于监控摄像机位置的javascript代码:

  <script>
    window.onload = function () {
      var pos = 
    document.querySelector('#camera').getAttribute('position');
    document.querySelector('[camera]').addEventListener('componentchanged', function (evt) {
        console.log('CAMERA x/y:', pos.x, pos.y);
        if (evt.name === 'position') { 
          console.log('Entity has moved from POSITION', evt.oldData, 'to', evt.newData, '!');
          return; 
        }
          return; 
      });
    };
  </script>
Run Code Online (Sandbox Code Playgroud)

但是条件if(evt.name ==='position')永远不会成立,并且相机位置保持为0,即使使用键盘输入在相机位置移动时也是如此.如何连续拍摄相机位置?

ngo*_*vin 5

我想是的evt.detail.name === 'position'.

我还建议使用A-Frame组件而不是松散的JavaScript:

AFRAME.registerComponent('listener', {
  tick: function () {
    console.log(this.el.getAttribute('position'));
  }
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<a-camera listener></a-camera>
Run Code Online (Sandbox Code Playgroud)