I’m trying to play a glb animation in A-FRAME with Three.js, now it works just for a second and then it stop, could someone help me please? this is my code:
JavaScript
x
26
26
1
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
2
<script>
3
AFRAME.registerComponent('move', {
4
init: function () {
5
setTimeout( () => {
6
let position = this.el.getAttribute("position")
7
console.log(this.el.components['gltf-model'].model )
8
// Create an AnimationMixer, and get the list of AnimationClip instances
9
const mixer = new THREE.AnimationMixer( this.el.components['gltf-model'].model);
10
const clips = this.el.components['gltf-model'].model.animations[0];
11
var clock = new THREE.Clock();
12
// Play all animations
13
14
mixer.clipAction( clips ).play();
15
//In the animation block of your scene:
16
var delta = 0.25 * clock.getDelta();
17
mixer.update( delta );
18
}, 2000)
19
}
20
})
21
</script>
22
23
<a-scene>
24
<a-entity gltf-model="https://rawcdn.githack.com/BabylonJS/MeshesLibrary/55f475726670be2e7e4017b5f88c5762a90508c2/shark.glb" move position=".5 0.5 -5" scale="0.5 0.5 0.5"></a-entity>
25
</a-scene>
26
Advertisement
Answer
Wait until the model is loaded with:
JavaScript121this.el.addEventListener("model-loaded", evt => /* stuff */)}
2
update the animation in the renderloop – on each
tick
. You could use an interval or something else, but aframe has already a built-in function for this purpose:
JavaScript
1
25
25
1
<script src="https://aframe.io/releases/1.3.0/aframe.min.js"></script>
2
<script>
3
AFRAME.registerComponent('move', {
4
init: function() {
5
// wait until the model is loaded
6
this.el.addEventListener("model-loaded", evt => {
7
const mixer = new THREE.AnimationMixer(this.el.components['gltf-model'].model);
8
const clips = this.el.components['gltf-model'].model.animations[0];
9
mixer.clipAction(clips).play();
10
// "expose" the animation mixer
11
this.mixer = mixer;
12
})
13
},
14
// on each render loop (actually before each render loop)
15
tick: function(t, dt) {
16
if (!this.mixer) return; // if the mixer exists
17
this.mixer.update(dt / 1000) // update it with the delta time
18
}
19
})
20
</script>
21
22
<a-scene>
23
<a-entity gltf-model="https://rawcdn.githack.com/BabylonJS/MeshesLibrary/55f475726670be2e7e4017b5f88c5762a90508c2/shark.glb"
24
move position=".5 0.5 -5" scale="0.5 0.5 0.5"></a-entity>
25
</a-scene>