-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: native webvr #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import {version as VERSION} from '../package.json'; | ||
import window from 'global/window'; | ||
/* THIS CONFIGURES webvr-polyfill don't change the order */ | ||
import './webvr-config.js'; | ||
import 'webvr-polyfill/src/main'; | ||
import WebVRPolyfill from 'webvr-polyfill'; | ||
import videojs from 'video.js'; | ||
import * as THREE from 'three'; | ||
import VRControls from 'three/examples/js/controls/VRControls.js'; | ||
import VREffect from 'three/examples/js/effects/VREffect.js'; | ||
import WebVRManager from 'webvr-boilerplate/build/webvr-manager'; | ||
import OrbitControls from 'three/examples/js/controls/OrbitControls.js'; | ||
import rgbFragmentShader from './rgb-fragment-shader'; | ||
import rgbaFragmentShader from './rgba-fragment-shader'; | ||
import vertexShader from './vertex-shader'; | ||
|
||
// import controls so they get regisetered with videojs | ||
import './cardboard-button'; | ||
import './big-vr-play-button'; | ||
|
||
window.WebVRManager = WebVRManager; | ||
|
||
const validProjections = [ | ||
'360', | ||
'360_LR', | ||
|
@@ -35,11 +34,6 @@ const defaults = { | |
}; | ||
|
||
const errors = { | ||
'web-vr-no-devices-found': { | ||
headline: 'No 360 devices found', | ||
type: '360_NO_DEVICES_FOUND', | ||
message: 'Your browser supports 360, but no 360 displays found.' | ||
}, | ||
'web-vr-out-of-date': { | ||
headline: '360 is out of date', | ||
type: '360_OUT_OF_DATE', | ||
|
@@ -106,6 +100,10 @@ class VR extends Plugin { | |
|
||
super(player, settings); | ||
|
||
this.polyfill_ = new WebVRPolyfill({ | ||
TOUCH_PANNER_DISABLED: false | ||
}); | ||
|
||
this.options_ = settings; | ||
this.player_ = player; | ||
this.bigPlayButtonIndex_ = player.children().indexOf(player.getChild('BigPlayButton')) || 0; | ||
|
@@ -269,17 +267,27 @@ class VR extends Plugin { | |
} | ||
|
||
handleVrDisplayActivate_() { | ||
this.manager.enterVRMode_(); | ||
this.manager.setMode_(3); | ||
if (!this.vrDisplay) { | ||
return; | ||
} | ||
this.vrDisplay.requestPresent([{source: this.renderedCanvas}]); | ||
} | ||
|
||
handleVrDisplayDeactivate_() { | ||
if (!this.vrDisplay.isPresenting) { | ||
if (!this.vrDisplay || !this.vrDisplay.isPresenting) { | ||
return; | ||
} | ||
this.vrDisplay.exitPresent(); | ||
} | ||
|
||
requestAnimationFrame(fn) { | ||
if (this.vrDisplay) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have a vr display we want to request animation from on that instead of the plugin. |
||
return this.vrDisplay.requestAnimationFrame(fn); | ||
} | ||
|
||
return super.requestAnimationFrame(fn); | ||
} | ||
|
||
togglePlay_() { | ||
if (this.player_.paused()) { | ||
this.player_.play(); | ||
|
@@ -296,31 +304,22 @@ class VR extends Plugin { | |
} | ||
|
||
this.controls3d.update(); | ||
this.manager.render(this.scene, this.camera); | ||
this.effect.render(this.scene, this.camera); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. manager did this internally as well |
||
|
||
if (!this.vrDisplay) { | ||
this.animationFrameId_ = this.requestAnimationFrame(this.animate_); | ||
this.camera.getWorldDirection(this.cameraVector); | ||
return; | ||
} | ||
|
||
this.animationFrameId_ = this.vrDisplay.requestAnimationFrame(this.animate_); | ||
this.animationFrameId_ = this.requestAnimationFrame(this.animate_); | ||
|
||
if (!window.navigator.getGamepads) { | ||
return; | ||
} | ||
// Grab all gamepads | ||
const gamepads = window.navigator.getGamepads(); | ||
if (window.navigator.getGamepads) { | ||
// Grab all gamepads | ||
const gamepads = window.navigator.getGamepads(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was just a refactor, as it didn't really make sense the way it used to work |
||
|
||
for (let i = 0; i < gamepads.length; ++i) { | ||
const gamepad = gamepads[i]; | ||
for (let i = 0; i < gamepads.length; ++i) { | ||
const gamepad = gamepads[i]; | ||
|
||
// Make sure gamepad is defined | ||
if (!gamepad) { | ||
continue; | ||
} | ||
// Only take input if state has changed since we checked last | ||
if (gamepad.timestamp && !(gamepad.timestamp === this.prevTimestamps_[i])) { | ||
// Make sure gamepad is defined | ||
// Only take input if state has changed since we checked last | ||
if (!gamepad || !gamepad.timestamp || gamepad.timestamp === this.prevTimestamps_[i]) { | ||
continue; | ||
} | ||
for (let j = 0; j < gamepad.buttons.length; ++j) { | ||
if (gamepad.buttons[j].pressed) { | ||
this.togglePlay_(); | ||
|
@@ -366,7 +365,6 @@ class VR extends Plugin { | |
} | ||
|
||
this.scene = new THREE.Scene(); | ||
this.controls3d = new VRControls(this.camera); | ||
|
||
this.videoTexture = new THREE.VideoTexture(this.getVideoEl_()); | ||
|
||
|
@@ -392,45 +390,17 @@ class VR extends Plugin { | |
this.videoTexture.format = THREE.RGBFormat; | ||
} | ||
|
||
if (this.videoTexture.format === THREE.RGBAFormat && this.videoTexture.flipY === false) { | ||
this.movieMaterial = new THREE.ShaderMaterial({ | ||
uniforms: { | ||
texture: { value: this.videoTexture } | ||
}, | ||
vertexShader: [ | ||
'varying vec2 vUV;', | ||
'void main() {', | ||
' vUV = vec2( uv.x, 1.0 - uv.y );', | ||
' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', | ||
'}' | ||
].join('\n'), | ||
fragmentShader: [ | ||
'uniform sampler2D texture;', | ||
'varying vec2 vUV;', | ||
'void main() {', | ||
' gl_FragColor = texture2D( texture, vUV ).bgra;', | ||
'}' | ||
].join('\n') | ||
}); | ||
} else if (this.videoTexture.format === THREE.RGBFormat && this.videoTexture.flipY === false) { | ||
if ((this.videoTexture.format === THREE.RGBAFormat || this.videoTexture.format === THREE.RGBFormat) && this.videoTexture.flipY === false) { | ||
let fragmentShader = rgbFragmentShader; | ||
|
||
if (this.videoTexture.format === THREE.RGBAFormat) { | ||
fragmentShader = rgbaFragmentShader; | ||
} | ||
|
||
this.movieMaterial = new THREE.ShaderMaterial({ | ||
uniforms: { | ||
texture: { value: this.videoTexture } | ||
}, | ||
vertexShader: [ | ||
'varying vec2 vUV;', | ||
'void main() {', | ||
' vUV = vec2( uv.x, 1.0 - uv.y );', | ||
' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', | ||
'}' | ||
].join('\n'), | ||
fragmentShader: [ | ||
'uniform sampler2D texture;', | ||
'varying vec2 vUV;', | ||
'void main() {', | ||
' gl_FragColor = texture2D( texture, vUV );', | ||
'}' | ||
].join('\n') | ||
uniforms: {texture: {value: this.videoTexture}}, | ||
vertexShader, | ||
fragmentShader | ||
}); | ||
} else { | ||
this.movieMaterial = new THREE.MeshBasicMaterial({ map: this.videoTexture, overdraw: true, side: THREE.DoubleSide }); | ||
|
@@ -477,8 +447,6 @@ class VR extends Plugin { | |
// Previous timestamps for gamepad updates | ||
this.prevTimestamps_ = []; | ||
|
||
this.manager = new WebVRManager(this.renderer, this.effect, {hideButton: true}); | ||
|
||
this.renderedCanvas = this.renderer.domElement; | ||
|
||
const debounce = function(fn, wait) { | ||
|
@@ -514,24 +482,26 @@ class VR extends Plugin { | |
this.getVideoEl_().style.display = 'none'; | ||
|
||
if (window.navigator.getVRDisplays) { | ||
this.log('VR is supported, getting vr displays'); | ||
window.navigator.getVRDisplays().then((displays) => { | ||
if (displays.length > 0) { | ||
this.log('WebVR supported, VRDisplays found.'); | ||
this.log('VR Displays found', displays); | ||
this.vrDisplay = displays[0]; | ||
this.log(this.vrDisplay); | ||
this.log('Going to use VRControls on the first one', this.vrDisplay); | ||
|
||
// Native WebVR Head Mounted Displays (HMDs) like the HTC Vive | ||
// also need the cardboard button to enter fully immersive mode | ||
// so, we want to add the button if we're not polyfilled. | ||
if (!this.vrDisplay.isPolyfilled) { | ||
this.addCardboardButton_(); | ||
} | ||
// FIREFOX doesn't report the polyfill display as a | ||
// VRDisplay so this would error even though the video would | ||
// work | ||
} else if (!videojs.browser.IS_FIREFOX) { | ||
this.triggerError_({code: 'web-vr-no-devices-found', dismiss: false}); | ||
this.controls3d = new VRControls(this.camera); | ||
} else { | ||
this.log('no vr displays found going to use OrbitControls'); | ||
this.controls3d = new OrbitControls(this.camera, this.renderedCanvas); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When no VRDisplay is found webvr-polyfill no longer inserts one for native vr implementations. So we have to do our own controls. |
||
this.controls3d.target.set(0, 0, -1); | ||
} | ||
this.requestAnimationFrame(this.animate_); | ||
}); | ||
} else if (window.navigator.getVRDevices) { | ||
this.triggerError_({code: 'web-vr-out-of-date', dismiss: false}); | ||
|
@@ -545,7 +515,6 @@ class VR extends Plugin { | |
window.addEventListener('vrdisplayactivate', this.handleVrDisplayActivate_, true); | ||
window.addEventListener('vrdisplaydeactivate', this.handleVrDisplayDeactivate_, true); | ||
|
||
this.animate_(); | ||
this.initialized_ = true; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = `uniform sampler2D texture; | ||
varying vec2 vUV; | ||
void main() { | ||
gl_FragColor = texture2D(texture, vUV); | ||
}`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = `uniform sampler2D texture; | ||
varying vec2 vUV; | ||
void main() { | ||
gl_FragColor = texture2D(texture, vUV).bgra; | ||
}`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = `varying vec2 vUV; | ||
void main() { | ||
vUV = vec2( uv.x, 1.0 - uv.y ); | ||
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); | ||
}`; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically all the manager does with the two functions we used to call