You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building WebXR apps with TresJS and @tresjs/rapier, and everything driven by useLoop stops as soon as I enter an immersive session.
During an immersive session the browser no longer runs window.requestAnimationFrame callbacks. Frames only come through XRSession.requestAnimationFrame. useCreateRafLoop is built on useRafFn, so once the session starts, onBeforeRender and onRender stop firing, TresCanvas stops rendering (the headset shows the last frame), and <Physics> stops stepping because it steps the world from onBeforeRender.
I checked this on a Quest 3 (Meta Browser) and an XREAL Aura (Chrome on Android XR) with three r172. In both, window.requestAnimationFrame callbacks ran 0 times per second while the session was presenting.
#668 asked for XR loop support in 2024 and was closed in favour of Tresjs/XR#33, which is still open. @tresjs/xr isn't on npm, so as far as I can tell core has no way to keep the loop running in XR today.
I'd like to send a PR for this.
Suggested solution
Keep useRafFn for normal frames, and while a session is presenting, pass the same tick to renderer.xr.setAnimationLoop(). Switch on sessionstart and back on sessionend. React Three Fiber does the same with gl.xr.setAnimationLoop. The hooks would also get the XRFrame as frame in the loop context.
On where this belongs: I think it has to be in core. The loop that useLoop, cientos and @tresjs/rapier depend on is created in core's renderer manager, so a separate package can't make those hooks run in XR without replacing core's loop, which is what Tresjs/XR#33 does. The change itself stays inside useCreateRafLoop and the renderer manager.
I have this running as a patch on 5.9.3. Results on both headsets with three r172:
Quest 3
XREAL Aura
Hook calls per second in XR
89–91, each with an XRFrame
72–73, each with an XRFrame
Renders per frame
1
1
@tresjs/rapier collision events in XR
fire
fire
stop() / start() during a session
works
works
Back to the window loop after sessionend
yes
yes
WebGL errors or warnings
0
0
App with its own renderer.setAnimationLoop() (TresJS loop left running)
app loop keeps running, 90 fps
app loop keeps running, 72–73 fps
I repeated the runs on both headsets with three r186, where TresJS uses Timer instead of Clock, and got the same results: 90–91 hook calls per second in XR on the Quest 3 and 72–73 on the Aura, collisions firing, stop() / start() working, and no errors.
A few things I ran into while getting there:
My first attempt used renderer.setAnimationLoop(). That also restarts three's window loop, which three had just stopped on sessionstart. The tick then ran from window frames too, without an XRFrame, and rendered into the XR framebuffer outside a session frame. Quest logged 133 INVALID_FRAMEBUFFER_OPERATION warnings at session start. Switching to renderer.xr.setAnimationLoop() fixed it.
three binds the XR framebuffer before sessionstart while it waits for the reference space. Rendering from the window loop in that gap also produces those warnings, so the patch skips rendering until the session is presenting.
setSize has to be skipped while presenting. The patch applies the canvas size again after sessionend.
three keeps only one XR animation loop. An app that calls renderer.setAnimationLoop() itself without stopping TresJS's loop lost its loop at sessionstart and froze. The patch now detects that and leaves the XR loop to the app, so those apps behave as they do today.
Three things I'd like your opinion on before opening the PR:
In XR the headset needs a new frame every time, so the patch renders every XR frame even in on-demand and manual mode. Is that OK, or should manual stay manual in XR?
To detect an app's own loop, the patch wraps renderer.xr.setAnimationLoop. Would you prefer an explicit option, for example a way to opt out of the XR loop?
three's WebGPU XRManager has no xr.setAnimationLoop. It runs whatever loop renderer.setAnimationLoop() held when the session started. For WebGPURenderer I'd drive TresJS with renderer.setAnimationLoop() all the time. Does that fit @tresjs/core/webgpu, or would you rather leave WebGPU out of the first PR?
If the approach looks fine, I'll open the PR against main in TypeScript, with unit tests (session switch, app-loop case, cleanup) and useLoop docs: the frame field, fpsLimit not applying in XR, and renderer.xr.getCamera() for the headset camera.
Alternative
The workaround I use now is to call renderer.setAnimationLoop() from the app, render there and step Rapier by hand. It works, but useLoop and everything built on it (cientos, @tresjs/rapier) still do nothing in XR.
I also considered driving TresJS with renderer.setAnimationLoop() all the time on WebGL. It's a single code path, but fpsLimit would need reimplementing, and any app that calls renderer.setAnimationLoop() would replace TresJS's loop outside XR as well, which would break existing apps.
Or this could wait for @tresjs/xr (Tresjs/XR#33). That PR has been open since June 2024, though, and apps that only use useLoop still wouldn't get a loop in XR.
Additional context
Tested with @tresjs/core 5.9.3 (patched) and @tresjs/rapier 1.2.0, with three 0.172 and 0.186 on both headsets. I haven't tried WebGPURenderer yet. I'll do that as part of the PR.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Description
I'm building WebXR apps with TresJS and
@tresjs/rapier, and everything driven byuseLoopstops as soon as I enter an immersive session.During an immersive session the browser no longer runs
window.requestAnimationFramecallbacks. Frames only come throughXRSession.requestAnimationFrame.useCreateRafLoopis built onuseRafFn, so once the session starts,onBeforeRenderandonRenderstop firing,TresCanvasstops rendering (the headset shows the last frame), and<Physics>stops stepping because it steps the world fromonBeforeRender.I checked this on a Quest 3 (Meta Browser) and an XREAL Aura (Chrome on Android XR) with three r172. In both,
window.requestAnimationFramecallbacks ran 0 times per second while the session was presenting.#668 asked for XR loop support in 2024 and was closed in favour of Tresjs/XR#33, which is still open.
@tresjs/xrisn't on npm, so as far as I can tell core has no way to keep the loop running in XR today.I'd like to send a PR for this.
Suggested solution
Keep
useRafFnfor normal frames, and while a session is presenting, pass the same tick torenderer.xr.setAnimationLoop(). Switch onsessionstartand back onsessionend. React Three Fiber does the same withgl.xr.setAnimationLoop. The hooks would also get theXRFrameasframein the loop context.On where this belongs: I think it has to be in core. The loop that
useLoop, cientos and@tresjs/rapierdepend on is created in core's renderer manager, so a separate package can't make those hooks run in XR without replacing core's loop, which is what Tresjs/XR#33 does. The change itself stays insideuseCreateRafLoopand the renderer manager.I have this running as a patch on 5.9.3. Results on both headsets with three r172:
XRFrameXRFrame@tresjs/rapiercollision events in XRstop()/start()during a sessionsessionendrenderer.setAnimationLoop()(TresJS loop left running)I repeated the runs on both headsets with three r186, where TresJS uses
Timerinstead ofClock, and got the same results: 90–91 hook calls per second in XR on the Quest 3 and 72–73 on the Aura, collisions firing,stop()/start()working, and no errors.A few things I ran into while getting there:
renderer.setAnimationLoop(). That also restarts three's window loop, which three had just stopped onsessionstart. The tick then ran from window frames too, without anXRFrame, and rendered into the XR framebuffer outside a session frame. Quest logged 133INVALID_FRAMEBUFFER_OPERATIONwarnings at session start. Switching torenderer.xr.setAnimationLoop()fixed it.sessionstartwhile it waits for the reference space. Rendering from the window loop in that gap also produces those warnings, so the patch skips rendering until the session is presenting.setSizehas to be skipped while presenting. The patch applies the canvas size again aftersessionend.renderer.setAnimationLoop()itself without stopping TresJS's loop lost its loop atsessionstartand froze. The patch now detects that and leaves the XR loop to the app, so those apps behave as they do today.Three things I'd like your opinion on before opening the PR:
on-demandandmanualmode. Is that OK, or shouldmanualstay manual in XR?renderer.xr.setAnimationLoop. Would you prefer an explicit option, for example a way to opt out of the XR loop?XRManagerhas noxr.setAnimationLoop. It runs whatever looprenderer.setAnimationLoop()held when the session started. ForWebGPURendererI'd drive TresJS withrenderer.setAnimationLoop()all the time. Does that fit@tresjs/core/webgpu, or would you rather leave WebGPU out of the first PR?If the approach looks fine, I'll open the PR against
mainin TypeScript, with unit tests (session switch, app-loop case, cleanup) anduseLoopdocs: theframefield,fpsLimitnot applying in XR, andrenderer.xr.getCamera()for the headset camera.Alternative
The workaround I use now is to call
renderer.setAnimationLoop()from the app, render there and step Rapier by hand. It works, butuseLoopand everything built on it (cientos,@tresjs/rapier) still do nothing in XR.I also considered driving TresJS with
renderer.setAnimationLoop()all the time on WebGL. It's a single code path, butfpsLimitwould need reimplementing, and any app that callsrenderer.setAnimationLoop()would replace TresJS's loop outside XR as well, which would break existing apps.Or this could wait for
@tresjs/xr(Tresjs/XR#33). That PR has been open since June 2024, though, and apps that only useuseLoopstill wouldn't get a loop in XR.Additional context
Tested with @tresjs/core 5.9.3 (patched) and @tresjs/rapier 1.2.0, with three 0.172 and 0.186 on both headsets. I haven't tried
WebGPURendereryet. I'll do that as part of the PR.All reactions