Custom Logic in the Render Loop
You can add custom logic to the render loop for animations, game mechanics, or other time-based updates:
// Track time for smooth animations
let previousTime = 0;
engine.runRenderLoop(() => {
const currentTime = performance.now();
const deltaTime = (currentTime - previousTime) / 1000; // Convert to seconds
previousTime = currentTime;
// Update game objects based on elapsed time
updateGameObjects(deltaTime);
// Render the scene
scene.render();
});
function updateGameObjects(deltaTime) {
// Time-based animation example
sphere.rotation.y += 1.0 * deltaTime; // Rotate 1 radian per second
// Game logic updates
updatePlayerPosition(deltaTime);
checkCollisions();
updateAI(deltaTime);
}
Using delta time (the time elapsed since the last frame) ensures animations run at consistent speeds regardless of frame rate.