-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clock: Consider changing getElapsedTime behavior #30497
Comments
Would it be an option for you to migrate to THREE.Timer instead? Doc page: https://threejs.org/docs/index.html#examples/en/misc/Timer It has been added as a new alternative to I'm personally not in favor of changing the API of |
This could be an option. We'd have to extend it while deprecating the previous APIs to allow the usage of old methods like I guess my only issue is the mandatory _usePageVisibilityAPI. It should be possible to disable it with a parameter in the constructor to give the choice to use it or not in my opinion. |
Making the usage optional is okay, imo. I vote to let the usage of the API enabled by default but an additional ctor parameter sounds good to me. |
As an additional note: In context of three.js/src/nodes/utils/Timer.js Lines 4 to 18 in 2061c3e
There is also the elapsed time that is passed as a parameter to the animation loop. That often makes the usage of a separate timer obsolete. That said, it's still important to have something like |
Description
Over the years, I came across multiple projects that were calling
.getElapsedTime()
and messing up their deltas instead of reading the.elapsedTime
property.The core problem is that
.getElapsedTime()
does more than just "get" a value - it has the side effect of updating the internal state by calling .getDelta(). As much as the doc is relatively clear about it, users would typically expect a getter method to be pure and not modify state.Here is a potential solutions to address this issue.
Solution
Rename the methods to better reflect their behavior ( example below )
Alternatives
Maybe make the doc clearer, but I've seen this error made by advanced three.js developer so I'm not sure it would help.
Additional context
The issue / misconception being present in community project like Drei and React three fiber and Tres made me feel like it was time to talk about this,
pmndrs/react-three-fiber#3455
pmndrs/drei#2341
https://github.com/Tresjs/tres/pull/928/files
This is also misused in the official examples.
Here each call will call getDelta while a single getDelta followed by reading .elapsedTime would be more appropriate
three.js/examples/misc_raycaster_helper.html
Lines 97 to 98 in eea50d8
Same for these that while harmless because they are called immediately after, make unnecessary call to getDelta internally.
three.js/examples/webgl_buffergeometry_lines.html
Lines 120 to 121 in b8d79b8
three.js/examples/webgl_geometry_dynamic.html
Lines 122 to 123 in 6144493
three.js/examples/webgpu_tsl_vfx_linkedparticles.html
Lines 440 to 442 in 8a475a6
Happy to eventually make a PR once the way to go is decided.
The text was updated successfully, but these errors were encountered: