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

后端 未结 5 2097
终归单人心
终归单人心 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

    The accepted answer has a problem and gives up to -10fps on slow computers compared to not limiting the fps, so for example without limiting 36pfs, with the accepted solution 26fps (for a 60fps, 1000/60 setTimeout).

    Instead you can do this:

    var dt=1000/60;
    var timeTarget=0;
    function render(){
      if(Date.now()>=timeTarget){
    
        gameLogic();
        renderer.render();
    
        timeTarget+=dt;
        if(Date.now()>=timeTarget){
          timeTarget=Date.now();
        }
      }
      requestAnimationFrame(render);
    }
    

    That way is not going to wait if its already behind.

提交回复
热议问题