De-Panther / unity-webxr-export

Develop and export WebXR experiences using Unity WebGL
https://de-panther.github.io/unity-webxr-export/
Apache License 2.0
1.06k stars 111 forks source link

DeviceOrientationEvent.requestPermission() #168

Open psusset opened 3 years ago

psusset commented 3 years ago

Hello,

In order to make this plugin work on iOS, you need to ask user permission for device orientation capture. To do that, we need to insert the following code in the requestSession action.

navigator.xr.requestSession('immersive-vr', { requiredFeatures: thisXRMananger.gameModule.WebXR.Settings.VRRequiredReferenceSpace, optionalFeatures: thisXRMananger.gameModule.WebXR.Settings.VROptionalFeatures }).then(async (session) => { if (typeof(DeviceOrientationEvent) !== "undefined" && typeof(DeviceOrientationEvent).requestPermission === 'function') { DeviceOrientationEvent.requestPermission() .then(response => { if (response === 'granted') { session.isImmersive = true; session.isInSession = true; session.isAR = false; thisXRMananger.xrSession = session; thisXRMananger.onSessionStarted(session); } }).catch(function (error) { thisXRMananger.BrowserObject.resumeAsyncCallbacks(); thisXRMananger.BrowserObject.mainLoop.resume(); }); } else { session.isImmersive = true; session.isInSession = true; session.isAR = false; thisXRMananger.xrSession = session; thisXRMananger.onSessionStarted(session); } });

De-Panther commented 3 years ago

Thanks for the feedback. I don't have an iOS device, so I don't have a way to test it on those.

iOS in general doesn't support WebXR. I think that it does support WebVR, or some version of it with cardboard, but it's all using the WebXR Polyfill. So if the DeviceOrientationEvent is required, better report about it at the WebXR Polyfill repo, as I see you did https://github.com/immersive-web/webxr-polyfill/issues/161

psusset commented 3 years ago

In deed, it works with polyfill on iOS. I test this code on your plugin and it works well on iOS (at least for my purpose - cardboard with reticle). For those who want to support WebXR on iOS with this plugin, you will also need to change the html template so the unity can use all the screen in landscape mode.

GittyUpperNinja commented 3 years ago

OMG THANK YOU SO MUCH

jamwitk commented 2 years ago

Hi @psusset , it works for VR button in web site page but not in scene button. How do you use it for in scene button?

psusset commented 2 years ago

HI @jamwitk , I use an HTML button in the page and not a button on the unity side. I think it's not possible to use a button inside of Unity because the deviceorientationrequest must be handled by the webpage. But you can display your html button on top of the Unity canvas to get the same result. Thanks to javascript call, Unity can control the display of the button.

jamwitk commented 2 years ago

Thank you for your feedback, I have tried write into .jslib and call on start but it gives error such that it need user gesture something like this. It seems as you say it is not possible.

mergeInto(LibraryManager.library, {
    CheckForPermission : function (){
        if (typeof(DeviceOrientationEvent) !== "undefined" && typeof(DeviceOrientationEvent).requestPermission === 'function')
        {
            DeviceOrientationEvent.requestPermission().then(
                function(response) {
                    if (response === 'granted') {
                       window.alert("granted 1");
                    }
                }).catch(function (error) {
                window.alert(error);
            });
        }
        else
        {
            window.alert("granted 2");
        };
    },
})
De-Panther commented 2 years ago

I think that in Unity 2022.1 beta they added gyro support for WebGL. So there should be a way to allow gyro from Unity and not just from the page

De-Panther commented 2 years ago

Also, if you have relevant device, you should be active in the WebXR Polyfill issue I mentioned and the other relevant issues mentioned there.