如何在A-Frame中收听相机旋转的变化?

ngo*_*vin 4 aframe

有没有办法在当前视角上添加一个监听器?

换句话说,每当用户看到他的背后,我都想触发一个函数.

最快的方式似乎是有一个监听器来检查当前的头部旋转,并且如果在一定的度数范围内,则会触发该函数

ngo*_*vin 10

编辑

componentchange事件受到限制.并且不经历事件系统以进行频繁更新是更高效的.相机旋转始终在VR中每帧都会改变,因此无需考虑相机是否已更改.所以我们用组件刻度读取每一帧的旋转.

AFRAME.registerComponent('rotation-reader', {
    tick: function () {
        var rotation = this.el.getAttribute('rotation');
        if (rotation.y < 180) {
            // ...
        }
    }
  });

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

原版的

https://aframe.io/docs/0.2.0/core/entity.html#listening-for-component-changes

您可以使用该componentchanged事件来监听轮换中的更改:

document.querySelector('[camera]').addEventListener('componentchanged', function (evt) {
  if (evt.name !== 'rotation') { return; }
  if (evt.newData.y < 180) { // ... }
});
Run Code Online (Sandbox Code Playgroud)

或者作为一个组件更好(这将在旋转一定量时触发事件):

AFRAME.registerComponent('trigger-on-look-behind', {
  schema: {type: 'string'},

  init: function () {
    var eventName = this.data;
    this.el.addEventListener('componentchanged', function (evt) {
      if (evt.name !== 'rotation') { return; }
      if (evt.newData.y < 180) {
        this.emit(eventName);
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

然后:

<a-camera trigger-on-look-behind="looked-behind"></a-camera>
Run Code Online (Sandbox Code Playgroud)