Skip to content

Commit

Permalink
add: update to toggle vr
Browse files Browse the repository at this point in the history
  • Loading branch information
Valery-a committed Oct 14, 2024
1 parent c9770f7 commit 3358090
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/vr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,41 @@ export async function initVR () {
try {
isSupported = !!(await navigator.xr?.isSessionSupported('immersive-vr')) && !!XRSession.prototype.updateRenderState // e.g. android webview doesn't support updateRenderState
} catch (err) {
console.error('Error checking if VR is supported')
console.error(err)
console.error('Error checking if VR is supported', err)
}
if (!isSupported) return
renderer.xr.enabled = true

const createVrButton = () => {
const vrButton = VRButton.createButton(renderer)
document.body.appendChild(vrButton)
return vrButton
}

// VR Button
const vrButton = VRButton.createButton(renderer)
document.body.appendChild(vrButton)
renderer.xr.enabled = true
let vrButton = createVrButton()

const unsubWatchSetting = subscribeKey(options, 'vrSupport', () => {
const o = options
if (o.vrSupport) {
const handleVrToggle = () => {
if (options.vrSupport) {
if (!vrButton.parentNode) {
vrButton = createVrButton()
}
vrButton.hidden = false
} else {
disableVr()
}
})
}

const unsubWatchSetting = subscribeKey(options, 'vrSupport', handleVrToggle)

const disableVr = () => {
if (vrButton.parentNode) {
vrButton.remove()
}
renderer.xr.enabled = false
viewer.cameraObjectOverride = undefined
viewer.scene.remove(user)
}

// hack for vr camera
const user = new THREE.Group()
Expand All @@ -42,14 +59,6 @@ export async function initVR () {
const controller1 = renderer.xr.getControllerGrip(0)
const controller2 = renderer.xr.getControllerGrip(1)

const disableVr = () => {
vrButton.remove()
renderer.xr.enabled = false
viewer.cameraObjectOverride = undefined
viewer.scene.remove(user)
unsubWatchSetting()
}

// todo the logic written here can be hard to understand as it was designed to work in gamepad api emulation mode, will be refactored once there is a contro-max rewrite is done
const virtualGamepadIndex = 4
let connectedVirtualGamepad
Expand Down

0 comments on commit 3358090

Please sign in to comment.