-
Notifications
You must be signed in to change notification settings - Fork 126
Introducing FreeFlight fallback #213
Changes from 1 commit
228fcc8
72e79dc
cb7d0b3
735f97e
f1c2e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ | |
'use strict'; | ||
|
||
var defaultHeight = 1.5; | ||
var entervrButton = document.querySelector('#entervr'); | ||
var container = document.querySelector('#game'); | ||
var status = document.querySelector('#status'); | ||
var icons = document.querySelector('#icons'); | ||
var controller = document.querySelector('#motion-controller'); | ||
|
||
var enterVRButton = document.getElementById('entervr'); | ||
var gameContainer = document.getElementById('game'); | ||
var vrHardwareStatus = document.getElementById('status'); | ||
var statusIcons = document.getElementById('icons'); | ||
var controllerIconTemplate = document.getElementById('motion-controller'); | ||
var noVRInstructions = document.getElementById('novr'); | ||
|
||
var windowRaf = window.requestAnimationFrame; | ||
var vrDisplay = null; | ||
var canvas = null; | ||
|
@@ -46,25 +49,34 @@ | |
} | ||
|
||
function onUnity (msg) { | ||
// Measure Round-Trip Time from Unity. | ||
if (msg.detail === 'Timer') { | ||
var delta = window.performance.now() - testTimeStart; | ||
console.log('return time (ms): ',delta); | ||
testTimeStart = null; | ||
return; | ||
} | ||
// This way of passing messages is deprecated. Use rich objects instead. | ||
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 is a |
||
if (typeof msg.detail === 'string') { | ||
// Measure Round-Trip Time from Unity. | ||
if (msg.detail === 'Timer') { | ||
var delta = window.performance.now() - testTimeStart; | ||
console.log('return time (ms): ',delta); | ||
testTimeStart = null; | ||
return; | ||
} | ||
|
||
// Wait for Unity to render the frame; then submit the frame to the VR display. | ||
if (msg.detail === 'PostRender') { | ||
submitNextFrame = vrDisplay && vrDisplay.isPresenting; | ||
if (submitNextFrame) { | ||
vrDisplay.requestAnimationFrame(onAnimate); | ||
// Wait for Unity to render the frame; then submit the frame to the VR display. | ||
if (msg.detail === 'PostRender') { | ||
submitNextFrame = vrDisplay && vrDisplay.isPresenting; | ||
if (submitNextFrame) { | ||
vrDisplay.requestAnimationFrame(onAnimate); | ||
} | ||
} | ||
|
||
// Handle quick VR/normal toggling. | ||
if (msg.detail.indexOf('ConfigureToggleVRKeyName') === 0) { | ||
toggleVRKeyName = msg.detail.split(':')[1]; | ||
} | ||
} | ||
|
||
// Handle quick VR/normal toggling. | ||
if (msg.detail.indexOf('ConfigureToggleVRKeyName') === 0) { | ||
toggleVRKeyName = msg.detail.split(':')[1]; | ||
// Handle an UI command | ||
if (msg.detail.type === 'ShowPanel') { | ||
var panelId = document.getElementById(msg.detail.panelId); | ||
showInstruction(panelId); | ||
} | ||
} | ||
|
||
|
@@ -88,7 +100,7 @@ | |
return vrDisplay.requestPresent([{source: canvas}]).then(function () { | ||
// Start stereo rendering in Unity. | ||
console.log('Entered VR mode'); | ||
gameInstance.SendMessage('WebVRCameraSet', 'Begin'); | ||
gameInstance.SendMessage('WebVRCameraSet', 'OnStartVR'); | ||
}).catch(function (err) { | ||
console.error('Unable to enter VR mode:', err); | ||
}); | ||
|
@@ -101,7 +113,7 @@ | |
} | ||
function done () { | ||
// End stereo rendering in Unity. | ||
gameInstance.SendMessage('WebVRCameraSet', 'End'); | ||
gameInstance.SendMessage('WebVRCameraSet', 'OnEndVR'); | ||
onResize(); | ||
} | ||
return vrDisplay.exitPresent().then(function () { | ||
|
@@ -223,11 +235,11 @@ | |
// scale game container so we get a proper sized mirror of VR content to desktop. | ||
var scaleX = window.innerWidth / renderWidth; | ||
var scaleY = window.innerHeight / renderHeight; | ||
container.setAttribute('style', `transform: scale(${scaleX}, ${scaleY}); transform-origin: top left;`); | ||
gameContainer.setAttribute('style', `transform: scale(${scaleX}, ${scaleY}); transform-origin: top left;`); | ||
} else { | ||
canvas.width = window.innerWidth; | ||
canvas.height = window.innerHeight; | ||
container.style.transform = ''; | ||
gameContainer.style.transform = ''; | ||
} | ||
} | ||
|
||
|
@@ -242,6 +254,7 @@ | |
} | ||
|
||
function showInstruction (el) { | ||
if (el.dataset.enabled) { return; } | ||
var confirmButton = el.querySelector('button'); | ||
el.dataset.enabled = true; | ||
confirmButton.addEventListener('click', onConfirm); | ||
|
@@ -252,19 +265,19 @@ | |
} | ||
|
||
function updateStatus () { | ||
if (parseInt(status.dataset.gamepads) !== vrGamepads.length) { | ||
if (parseInt(vrHardwareStatus.dataset.gamepads) !== vrGamepads.length) { | ||
var controllerClassName = 'controller-icon'; | ||
var controlIcons = icons.getElementsByClassName(controllerClassName); | ||
var controlIcons = statusIcons.getElementsByClassName(controllerClassName); | ||
while (controlIcons.length > 0) { | ||
controlIcons[0].parentNode.removeChild(controlIcons[0]); | ||
} | ||
|
||
vrGamepads.forEach(function (gamepad) { | ||
var controllerIcon = document.importNode(controller.content, true); | ||
var controllerIcon = document.importNode(controllerIconTemplate.content, true); | ||
controllerIcon.querySelector('img').className = controllerClassName; | ||
icons.appendChild(controllerIcon); | ||
statusIcons.appendChild(controllerIcon); | ||
}); | ||
status.dataset.gamepads = vrGamepads.length; | ||
vrHardwareStatus.dataset.gamepads = vrGamepads.length; | ||
} | ||
} | ||
|
||
|
@@ -287,26 +300,30 @@ | |
frameData = new VRFrameData(); | ||
|
||
return navigator.getVRDisplays().then(function(displays) { | ||
if (!displays.length) { | ||
return null; | ||
var canPresent = false; | ||
var hasPosition = false; | ||
var hasOrientation = false; | ||
var hasExternalDisplay = false; | ||
|
||
if (displays.length) { | ||
vrDisplay = displays[displays.length - 1]; | ||
canPresent = vrDisplay.capabilities.canPresent; | ||
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. perhaps might be easier to just manage 1 variable called |
||
hasPosition = vrDisplay.capabilities.hasPosition; | ||
hasOrientation = vrDisplay.capabilities.hasOrientation; | ||
hasExternalDisplay = vrDisplay.capabilities.hasExternalDisplay; | ||
} | ||
|
||
vrDisplay = displays[displays.length - 1]; | ||
|
||
if (!vrDisplay) { | ||
return null; | ||
} | ||
enterVRButton.dataset.enabled = canPresent; | ||
|
||
if (isPolyfilled(vrDisplay)) { | ||
showInstruction(document.querySelector('#novr')); | ||
} else { | ||
status.dataset.enabled = 'true'; | ||
} | ||
|
||
if (vrDisplay.capabilities && vrDisplay.capabilities.canPresent) { | ||
// Enable button to toggle entering/exiting VR. | ||
entervrButton.dataset.enabled = 'true'; | ||
} | ||
gameInstance.SendMessage( | ||
'WebVRCameraSet', 'OnVRCapabilities', | ||
JSON.stringify({ | ||
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. can we not just 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. can handle in #104 |
||
canPresent: canPresent, | ||
hasPosition: hasPosition, | ||
hasOrientation: hasOrientation, | ||
hasExternalDisplay: hasExternalDisplay | ||
}) | ||
); | ||
|
||
return vrDisplay; | ||
}).catch(function (err) { | ||
|
@@ -335,7 +352,7 @@ | |
window.addEventListener('keyup', onKeyUp, false); | ||
document.addEventListener('UnityLoaded', onUnityLoaded, false); | ||
document.addEventListener('Unity', onUnity); | ||
entervrButton.addEventListener('click', onToggleVR, false); | ||
enterVRButton.addEventListener('click', onToggleVR, false); | ||
|
||
onResize(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,9 @@ mergeInto(LibraryManager.library, { | |
|
||
ConfigureToggleVRKeyName: function (keyName) { | ||
document.dispatchEvent(new CustomEvent('Unity', {detail: 'ConfigureToggleVRKeyName:' + Pointer_stringify(keyName)})); | ||
}, | ||
|
||
ShowPanel: function (panelId) { | ||
document.dispatchEvent(new CustomEvent('Unity', {detail: {type:'ShowPanel', panelId: Pointer_stringify(panelId)}})); | ||
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. nit: space after |
||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I noticed this - thanks for fixing this 👍 😄