I am fairly new to Javascript and don't understand how the time argument works in the animate function below. I can see that the animate function requires an argument called time but when the function is called as the callback in setAnimationLoop, no argument is passed in. Can somebody explain how this is working?
import * as THREE from './js/three.module.js';
let camera, scene, renderer;
let geometry, material, mesh;
init();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;
scene = new THREE.Scene();
geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
material = new THREE.MeshNormalMaterial();
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );
}
function animation( time ) {
mesh.rotation.x = time / 2000;
mesh.rotation.y = time / 1000;
renderer.render( scene, camera );
}
but when the function is called as the callback in setAnimationLoop, no argument is passed in
The animation loop defined by WebGLRenderer.setAnimationLoop() is internally used with requestAnimationFrame(). If you pass in a callback to requestAnimationFrame(), the function has automatically access to a DOMHighResTimeStamp.
Please read the respective documentation for more details:
https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame