Limiting framerate in Three.js to increase performance, requestAnimationFrame?

后端 未结 5 2090
终归单人心
终归单人心 2020-12-24 03:26

I was thinking that for some projects I do 60fps is not totally needed. I figured I could have more objects and things that ran at 30fps if I could get it to run smoothly at

5条回答
  •  执念已碎
    2020-12-24 03:51

    I came across this article which gives two ways of solving the custom frame rate issue.

    http://codetheory.in/controlling-the-frame-rate-with-requestanimationframe/

    I think this way is more robust, as it will have a steady animation speed even on computers that do not render the canvas consistently at 60 fps. Below is an example

    var now,delta,then = Date.now();
    var interval = 1000/30;
    
    function animate() {
        requestAnimationFrame (animate);
        now = Date.now();
        delta = now - then;
        //update time dependent animations here at 30 fps
        if (delta > interval) {
            sphereMesh.quaternion.multiplyQuaternions(autoRotationQuaternion, sphereMesh.quaternion);
            then = now - (delta % interval);
        }
        render();
    }
    

提交回复
热议问题