Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Supporting WebXR's new requestAnimationFrame

Discussion in 'Web' started by wimachtendink, Oct 2, 2019.

  1. wimachtendink

    wimachtendink

    Joined:
    Jan 21, 2018
    Posts:
    8
    Hi all,

    I've recently been working on trying to update the Mozilla WebVR For Unity plugin ( https://github.com/mozilla/unity-webvr-export ) for use with the WebXR API, but it appears that unity's current window.requestAnimationFrame is fundamentally incompatible with WebXR's session.requestAnimationFrame(t, XRFrame).

    It seems any hackey-workaround would end up limiting the framerate of the XR device to the page framerate, which would defeat the point of the upgrade.

    it looks to me like the requestAnimationFrame for unity is set in %Build_Name_or_Whatever%.wasm.framework.unityweb then _emscripten_set_main_loop_timing, I'm pretty sure this is generated code which means I probably shouldn't touch it, which also means this is a bit of a dead end to my quest.

    But, it also seems like this could hook into unity nicely and there's already VR code in there: /*if (typeof Browser.vrDevice === "undefined") Browser.vrDevice = null; */

    I'm definitely out of my depth here, but WebXR allows for higher frame rates which really makes a huge difference. Many evangelize for ThreeJS/Aframe as the WebXR platform of choice, but so far using WebVR and Unity has been a really great experience and really don't like javaScript, so if there's anything that I can do, or anything that y'all can do, I would like to hear about it.

    Thank you
     
    Last edited: Oct 2, 2019
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    If you'd like to experiment with modifying emscripten_set_main_loop_timing() for WebXR purposes, you can certainly do it - check out the function inside library_browser.js in Emscripten installation tree inside Unity folders. There are two functions, emscripten_set_main_loop() and emscripten_set_main_loop_timing() that control the main loop registration for Unity - you can try hijacking that to get to your own WebXR updates to roll.

    The Browser.vrDevice bits are actually really old code that Mozilla authored in days way back when WebVR was in pre-draft stage. That code does not relate to the mozilla/webvr-export repository (that one was authored by a different bunch of people at Mozilla after WebVR got into a semi-stable draft phase)
     
  3. wimachtendink

    wimachtendink

    Joined:
    Jan 21, 2018
    Posts:
    8
    Thank you for your reply,

    It seems that in order to work on this I'm going to need to learn a lot more about emscripten.

    I was hoping I would be able to implement whatever my final solution is in args ie: UnityLoader.instantiate(x,y,ARGS), but it seems like most of the Library_* stuff is all hidden away and I can only access the elements declared in UnityLoader.Module? or perhaps $Browser__postset: , but I'm not having much luck with either.

    Is there any chance I've missed something obvious which will make my desired solution work? Or should I start digging in on emscripten?
     
  4. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    There is unfortunately no way to pass arguments to UnityLoader.instantiate() in order to modify how the compiled JavaScript functions behave.

    The Library_* files are located e.g. on Windows in a PATH like c:\program files\unity\Hub\Editor\2019.2.7f2\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\

    In order to modify how emscripten_set_main_loop() works, edit c:\program files\unity\Hub\Editor\2019.2.7f2\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\library_browser.js and find "emscripten_set_main_loop: function(..) { ..." there (around line 1123 on 2019.2.7f2)

    Browser__postset: is an old Emscripten legacy mechanism to export functions on Module - that can be used to export custom functions from Browser.foo to be callable via Module['foo'](), although in this case, if you are looking to modify an already existing function emscripten_set_main_loop() to target WebXR rAF() instead of regular rAF(), then you probably don't need to export any new functions.
     
  5. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589