简体   繁体   English

三.js 动画中的时间

[英]Time in three.js animation

I am fairly new to Javascript and don't understand how the time argument works in the animate function below.我对 Javascript 还很陌生,不明白时间参数在下面的 animate 函数中是如何工作的。 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?我可以看到 animate 函数需要一个名为 time 的参数,但是当该函数作为 setAnimationLoop 中的回调被调用时,没有传入任何参数。有人可以解释一下这是如何工作的吗?

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但是当该函数作为setAnimationLoop中的回调被调用时,没有传入任何参数

The animation loop defined by WebGLRenderer.setAnimationLoop() is internally used with requestAnimationFrame() . WebGLRenderer.setAnimationLoop()定义的动画循环在内部与requestAnimationFrame() If you pass in a callback to requestAnimationFrame() , the function has automatically access to a DOMHighResTimeStamp .如果您将回调传递给requestAnimationFrame() ,则该函数会自动访问DOMHighResTimeStamp

Please read the respective documentation for more details:请阅读相应的文档以获取更多详细信息:

https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM