Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 that framerate. I figured if I edited the requestAnimationFrame shim inside of three.js I could limit it to 30 that way. But I was wondering if there was a better way to do this using three.js itself as provided. Also, will this give me the kind of performance increase I am thinking. Will I be able to render twice as many objects at 30fps as I will at 60? I know the difference between running things at 30 and 60, but will I be able to get it to run at a smooth constant 30fps?

I generally use the WebGLRenderer, and fall back to Canvas if needed except for projects that are targeting one specifically, and typically those are webgl shader projects.

like image 234
Cory Gross Avatar asked Jul 01 '12 19:07

Cory Gross


People also ask

How do I slow down requestAnimationFrame?

If your animation requires a different frames per second (up to 60 fps) or simply doesn't require that high a level of refresh rate, you can slow it down by calling requestAnimationFrame inside setTimeout() .

How do you control speed in requestAnimationFrame?

Method 1: Update data via setInterval, and graphics via RAF. Keep those values in an object for each animated element. Assign the transform string to a variable in the object each setInterval 'frame'. Keep these objects in an array. Set your interval to your desired fps in ms: ms=(1000/fps).

How does a setTimeout differ from window requestAnimationFrame?

While setTimeout is executed in x milliseconds (technically, x + y milliseconds as I explained earlier in this post), requestAnimationFrame keeps staying in the list called animation frame request callback list. The more you create requestAnimationFrame, the more handlers will be in the list.


1 Answers

What about something like this:

function animate() {      setTimeout( function() {          requestAnimationFrame( animate );      }, 1000 / 30 );      renderer.render();  } 
like image 81
mrdoob Avatar answered Oct 09 '22 20:10

mrdoob