Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

javascript - Continuously moving Aframe camera into direction of view

I want to continuously move the view of my Aframe scene in the current viewing direction.

There seem to be two components to do this, (1) getting current viewing direction in correct format, (2) defining and updating a continuous animation with the current viewing direction of the camera.

I managed to get the camera's viewing direction using the following code:

        <script>
AFRAME.registerComponent('listener', {
  tick: function () {
    var position = this.el.getAttribute('position');
    var rotation = this.el.getAttribute('rotation');
  }
});
        </script>

How can I create an animation that moves in the camera's viewing direction, here for example with the current tilt?

I tried this for motion:

AFRAME.registerComponent('listener', {
  tick: function () {
    var camp = document.querySelector("#cameraWrapper");
    camp.setAttribute('position', camp.getAttribute('position').add(0.1)););
  }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the direction of view should be in THREE.Vector3() type, you can get the direction like this:

var camera = document.querySelector("a-camera");
var direction = new THREE.Vector3().copy(camera.getWorldDirection());

Cause tick function would be executed in every render loop, if you use the component tick function to implement the movement, you may don't need an animation, just let the camera take a little step in the view direction. The code is like:

tick : fucntion()
{
    var camera = document.querySelector("a-camera");
    var stepFactor = 0.1;
    camera.parent.position.add(this.direction.multiplyScalar(stepFactor));
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/aframe/0.5.0/aframe.min.js"></script>
<script src="//cdn.rawgit.com/donmccurdy/aframe-extras/v3.7.0/dist/aframe-extras.min.js"></script>
<!DOCTYPE html>  
<html>  
<head>
<title>Camera Movement</title>
</head>
<body>
<script type="text/javascript">
AFRAME.registerComponent("listener", {
schema : 
{
stepFactor : {
type : "number",
default : 0.05
}
},
tick : function()
{this.el.components.camera.camera.parent.position.add(this.el.components.camera.camera.getWorldDirection().multiplyScalar(this.data.stepFactor));
}
});
</script>
<a-scene>
  <a-camera  listener="stepFactor:0.01" position="0 0 30">
    <a-entity id="cursor" cursor="fuse: true;fuseTimeout:500"
    material="color:black"
    geometry="primitive:ring"
    position="0 0 -1"
    scale="0.01 0.01 0.01"
    ></a-entity>
  </a-camera>
  <a-grid></a-grid>
<a-box position="0 1 -2" color="blue" move eve></a-box>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...