【问题标题】:aframe - Set camera position at runtimeaframe - 在运行时设置相机位置
【发布时间】:2016-08-09 05:28:50
【问题描述】:

在 a-scene 中,我尝试在运行时更改我的相机位置。 DOM 属性发生变化,但相机没有移动。

我可能错过了什么?

我的 js 代码: document.querySelector('#myCameraPosition').setAttribute('position', '0 0 0');

我的场景:

    <a-entity id="myCameraPosition" position="0 0 50">
        <a-entity id="myCamera" camera look-controls keyboard-controls>
        </a-entity>
    </a-entity>

【问题讨论】:

    标签: webvr aframe


    【解决方案1】:

    在相机周围创建一个包装实体:

    <a-entity id='cameraWrapper' position="0 0 0">
      <a-camera></a-camera>
    </a-entity>
    

    然后改变包装器的位置:

    document.querySelector("#cameraWrapper").object3D.position.set(1, 1, 1);
    

    【讨论】:

    • 四年后...效果很好 - 谢谢!当启用 VR 模式时,我想弄清楚如何以编程方式移动相机,我快疯了。
    【解决方案2】:

    必须将“look-controls”设置为 false 才能进行 javascript 旋转控制,否则将无法正常工作:

    function resetView(el){
      el.setAttribute('look-controls','false');
      el.setAttribute('rotation',{x:0,y:0,z:0});
      el.setAttribute('look-controls','true');
    }
    

    【讨论】:

    • 这应该是setAttribute('look-controls', 'enabled', false),反之亦然true
    • @ngokevin 这不起作用..不管你添加了什么。
    【解决方案3】:

    在下面的示例中,我可以更改相机实体父级的位置,它可以按预期工作:

    https://aframe.io/examples/showcase/helloworld/

    是否有任何 openearthview 或加速组件写入相机实体父级的位置?他们可能会覆盖它的位置。

    【讨论】:

      【解决方案4】:
      document.getElementById('myCameraPosition').setAttribute('position', {x:0, y:0, z:0});
      

      document.querySelector('#myCameraPosition').setAttribute('position', {x:0, y:0, z:0});
      

      【讨论】:

        【解决方案5】:

        当涉及到位置时,您可以直接更改相机位置(与需要包装实体的旋转不同)。 所以

        document.getElementById('myCameraPosition').setAttribute('position', {x:0, y:0, z:0});
        

        应该工作

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-28
          • 2014-03-20
          相关资源
          最近更新 更多