I am using Three.js to create an animation, but my animate()
function is not working properly. Simplified code below:
let obj;
let camera = new THREE.PerspectiveCamera(fov, asp, near, far);
let scene = new THREE.Scene();
const loader = new THREE.GLTFLoader();
async function init() {
obj = await loader.loadAsync("./public/modelo/scene.gltf");
scene.add(obj.scene);
animate(obj.scene.children[0]);
renderer.render(scene, camera);
function animate(objeto){
console.log(objeto);
requestAnimationFrame(animate);
objeto.rotation.z += 0.005;
renderer.render(scene, camera);
}
}
init();
The function works for the first call, but after that I can see things going wrong via my console.log()
statement. On the first run of animate()
, the line prints out the object, as expected. But, on all ensuing calls, the statement just returns a floating point number that increases greatly with each successive call. I checked and made sure that the object was intact at the end of the first function call, so I'm not sure what's going on here. Any suggestions?