Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
// VR | ||
export {default as VRAnimationLoop} from './webvr/vr-animation-loop'; | ||
|
||
// Misc | ||
export {addEvents} from './events/add-events'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* global document */ | ||
|
||
export function createEnterVRButton({canvas, title}) { | ||
const {top, left, width, height} = canvas.getBoundingClientRect(); | ||
|
||
const container = document.createElement('div'); | ||
container.style.position = 'absolute'; | ||
container.style.top = `${top}px`; | ||
container.style.left = `${left}px`; | ||
container.style.width = `${width}px`; | ||
container.style.height = `${height}px`; | ||
container.style.pointerEvents = 'none'; | ||
container.style.zIndex = '999'; | ||
document.body.appendChild(container); | ||
|
||
const button = document.createElement('button'); | ||
button.style.padding = '16px'; | ||
button.style.border = '1px solid #fff'; | ||
button.style.borderRadius = '8px'; | ||
button.style.background = 'rgba(0,0,0,0.5)'; | ||
button.style.color = '#fff'; | ||
button.style.font = 'normal 20px sans-serif'; | ||
button.style.cursor = 'pointer'; | ||
button.style.margin = '20px auto'; | ||
button.style.display = 'block'; | ||
button.style.pointerEvents = 'all'; | ||
button.textContent = title; | ||
container.appendChild(button); | ||
|
||
return button; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* global window, navigator */ | ||
import {AnimationLoop, withParameters, log} from '@luma.gl/core'; | ||
import {createEnterVRButton} from './utils'; | ||
|
||
export default class VRAnimationLoop extends AnimationLoop { | ||
onInitialize(...args) { | ||
this._enableWebVR(); | ||
return super.onInitialize(...args); | ||
} | ||
|
||
onRender(options) { | ||
const {width, height} = options; | ||
|
||
// Need both vrPresenting and vrFrame | ||
// to avoid race conditions when we exit VR | ||
// after we schedule an animation frame | ||
if (this.vrPresenting && this.vrFrame) { | ||
this.vrDisplay.getFrameData(this.vrFrameData); | ||
|
||
const { | ||
leftProjectionMatrix, | ||
leftViewMatrix, | ||
rightProjectionMatrix, | ||
rightViewMatrix | ||
} = this.vrFrameData; | ||
|
||
const leftEyeParams = Object.assign({}, options, { | ||
vrEye: 'left', | ||
vrProjectionMatrix: leftProjectionMatrix, | ||
vrViewMatrix: leftViewMatrix | ||
}); | ||
withParameters( | ||
this.gl, | ||
{ | ||
viewport: [0, 0, width * 0.5, height], | ||
scissor: [0, 0, width * 0.5, height], | ||
scissorTest: true | ||
}, | ||
() => super.onRender(leftEyeParams) | ||
); | ||
|
||
const rightEyeParams = Object.assign({}, options, { | ||
vrEye: 'right', | ||
vrProjectionMatrix: rightProjectionMatrix, | ||
vrViewMatrix: rightViewMatrix | ||
}); | ||
withParameters( | ||
this.gl, | ||
{ | ||
viewport: [width * 0.5, 0, width * 0.5, height], | ||
scissor: [width * 0.5, 0, width * 0.5, height], | ||
scissorTest: true | ||
}, | ||
() => super.onRender(rightEyeParams) | ||
); | ||
|
||
this.vrDisplay.submitFrame(); | ||
return true; | ||
} | ||
|
||
this.gl.viewport(0, 0, width, height); | ||
return super.onRender(options); | ||
} | ||
|
||
_vrDisplayPresentChange() { | ||
if (this.vrDisplay.isPresenting) { | ||
log.info(2, 'Entering VR')(); | ||
|
||
this.vrPresenting = true; | ||
this.vrButton.style.display = 'none'; | ||
} else { | ||
log.info(2, 'Exiting VR')(); | ||
|
||
this.vrPresenting = false; | ||
this.vrButton.style.display = 'block'; | ||
} | ||
} | ||
|
||
async _enableWebVR() { | ||
this.vrSupported = 'getVRDisplays' in navigator && window.VRFrameData; | ||
if (!this.vrSupported) return; | ||
|
||
this.vrFrameData = new window.VRFrameData(); | ||
this.vrPresenting = false; | ||
this.vrFrame = false; | ||
|
||
const displays = await navigator.getVRDisplays(); | ||
if (displays && displays.length) { | ||
log.info(2, 'Found VR Displays', displays)(); | ||
|
||
// TODO: Consider resizing canvas to match vrDisplay.getEyeParameters() | ||
// TODO: Maybe allow to select display? | ||
|
||
this.vrDisplay = displays[0]; | ||
this.vrButton = createEnterVRButton({ | ||
canvas: this.gl.canvas, | ||
title: `Enter VR (${this.vrDisplay.displayName})` | ||
}); | ||
this.vrButton.onclick = () => this.enterWebVR(); | ||
|
||
window.addEventListener('vrdisplaypresentchange', this._vrDisplayPresentChange.bind(this)); | ||
} | ||
} | ||
|
||
_requestAnimationFrame(renderFrame) { | ||
if (this.vrPresenting) { | ||
this.vrDisplay.requestAnimationFrame(() => { | ||
this.vrFrame = true; | ||
renderFrame(); | ||
this.vrFrame = false; | ||
}); | ||
} else { | ||
super._requestAnimationFrame(renderFrame); | ||
} | ||
} | ||
|
||
enterWebVR() { | ||
this.vrDisplay.requestPresent([ | ||
{ | ||
source: this.gl.canvas | ||
} | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters