8

カメラの現在位置を取得するにはどうすればよいですか? 空のエンティティを回転できるように。

私が持っていると仮定します:

<a-scene>
  <a-camera id="camera></a-camera>
  <a-sky id="mySky"></a-sky>
</a-scene>
4

2 に答える 2

19

カメラの位置を取得するには:

var pos = document.querySelector('#camera').getAttribute('position');

カメラのワールド位置を取得するために、カメラのローカル位置を変換できます。

var cameraEl = document.querySelector('#camera');
var worldPos = new THREE.Vector3();
worldPos.setFromMatrixPosition(cameraEl.object3D.matrixWorld);
console.log(worldPos.x);

変更をリッスンするには、次のcomponentchangedイベントを使用します。

cameraEl.addEventListener('componentchanged', function (evt) {
  if (evt.detail.name !== 'position') { return; }
  console.log(evt.detail.newData);
});

ポーリングすることで、よりパフォーマンスが向上する可能性があります。

AFRAME.registerComponent('camera-listener', {
  tick: function () {
    var cameraEl = this.el.sceneEl.camera.el;
    cameraEl.getAttribute('position');
    cameraEl.getAttribute('rotation');

    // Do something.
  }
});
于 2016-08-15T17:42:32.503 に答える