Aidan Young
Aidan Young

Reputation: 614

A-frame exit vr mode on function

I am wondering using A-frame (https://aframe.io) how I can get a user to exit vr mode if they're in vr mode when a function called myFunction() occurs. Just for clarification. When a function called myFunction() occurs, if the user isn't in vr mode, there won't we an effect but if the user is in vr mode, they will be exited from vr mode. How can this be done?

Upvotes: 1

Views: 918

Answers (3)

Synn
Synn

Reputation: 1178

In A-Frame you can detect if the user is in vr or not with :

sceneEl.is('vr-mode')

https://aframe.io/docs/1.3.0/core/scene.html#states

Then just exit vr with the exitVR() method :

sceneEl.exitVR()

https://aframe.io/docs/1.3.0/core/scene.html#methods

You can get the sceneEl with:

const sceneEl = document.querySelector('a-scene');

In conclusion, just do:

const sceneEl = document.querySelector('a-scene');
if (sceneEl.is('vr-mode')) sceneEl.exitVR();

Upvotes: 2

Thushara Buddhika
Thushara Buddhika

Reputation: 1820

This code worked for me,

import {useThree} from "@react-three/fiber";

const ABC = ({ x, y, x}) => {

    const { gl } = useThree();

    const exitXR = async (gl) => {
        const session = gl.xr.getSession();
        if ( session !== null ) {
            await session.end();
        }
    }
    
return (
        <group>
            <Html>
                <div onClick={()=>exitXR(gl)}> EXIT </div>
            </Html>
        </group>
    )
}

Upvotes: 2

Mugen87
Mugen87

Reputation: 31026

Assuming you have a reference to the renderer, you should be able to do the following:

async function exitXR( renderer ) {

  const session = renderer.xr.getSession();

  if ( session !== null ) {
    
    await session.end();

    // execute optional code after WebXR shutdown
  
  }

}

Upvotes: 2

Related Questions