aframe - Set camera position at runtime

三世轮回 提交于 2019-11-30 07:39:33

问题


In a a-scene, I try to change my camera position at runtime. The DOM property changes but the camera does not move.

What could have I missed ?

my js code: document.querySelector('#myCameraPosition').setAttribute('position', '0 0 0');

My a-scene:

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

回答1:


Create a wrapper entity around the camera:

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

Then change position of the wrapper:

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



回答2:


Had to set "look-controls" to false for javascript rotation control, otherwise wouldn't work :

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



回答3:


On the example below I can change the position of the camera entity's parent and it works as expected:

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

Is any of the openearthview or acceleration components writing the position of the camera entitiy's parent? They might be overwritting its position.




回答4:


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

or

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



回答5:


When it comes to position you can directly change the camera position(unlike rotation where you need a wrapper entity). so

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

should work



来源:https://stackoverflow.com/questions/36677671/aframe-set-camera-position-at-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!