WebXR get XRSession or onExit event

Hey, im trying out webXR with ThreeJs. Currently Im creating the VRButton and can enter the vr world and everything works well. But what I dont find on the web is how to get the navigator or the XRSession.
And further on how to get events from XRSession. I need to know when the user exits VR.

this is my onClick function which gets fired when clicking on VRButton.

private onClick() {

      Globals.vrEnabled = !Globals.vrEnabled


      this.sceneSetup.xrSession = this.sceneSetup.renderer.xr.getSession() // getSession() return null

      // Results in: VR SESSION null true
      console.log('VR SESSION', this.sceneSetup.xrSession, this.sceneSetup.renderer.xr.isPresenting)

      // Error because XRSession is null
      this.sceneSetup.xrSession.addEventListener('end', ()=> {
          console.log('END OF SESSION - NO VR ANYMORE')

Problem is that the XRSession returned from getSession() is always null. Why is that? How to get the XRSession properly

The session variable is only set after WebXRManager.setSesstion() was called. And this only happens if you start a XR session by activating the ENTER VR button.

Have you considered to register event listeners to the session start and end events like so?

renderer.xr.addEventListener( 'sessionstart', function ( event ) {


} );

renderer.xr.addEventListener( 'sessionend', function ( event ) {


} );

I have seen that before, but it gave me an Error:

Property ‘addEventListener’ does not exist on type ‘WebXRManager’.

Found a workaround for this:

let xr : any = this.renderer.xr

xr.addEventListener('sessionstart', this.onSessionStart.bind(this))

xr.addEventListener('sessionend', this.onSessionEnd.bind(this))

Probably happens because of missing typescript typings.

Thanks @Mugen87

Should be fixed with next release!

1 Like