|
| 1 | +var registerComponent = require('../../core/component').registerComponent; |
| 2 | +var THREE = require('../../../lib/three'); |
| 3 | +var utils = require('../../utils/'); |
| 4 | + |
| 5 | +var dummyDolly = new THREE.Object3D(); |
| 6 | +var controls = new THREE.VRControls(dummyDolly); |
| 7 | + |
| 8 | +var ENTER_VR_CLASS = 'a-enter-vr'; |
| 9 | +var ENTER_VR_NO_HEADSET = 'data-a-enter-vr-no-headset'; |
| 10 | +var ENTER_VR_NO_WEBVR = 'data-a-enter-vr-no-webvr'; |
| 11 | +var ENTER_VR_BTN_CLASS = 'a-enter-vr-button'; |
| 12 | +var ENTER_VR_MODAL_CLASS = 'a-enter-vr-modal'; |
| 13 | +var HIDDEN_CLASS = 'a-hidden'; |
| 14 | +var ORIENTATION_MODAL_CLASS = 'a-orientation-modal'; |
| 15 | + |
| 16 | +module.exports.Component = registerComponent('vr-mode-ui', { |
| 17 | + dependencies: ['vr-mode'], |
| 18 | + |
| 19 | + schema: { |
| 20 | + enabled: { default: true } |
| 21 | + }, |
| 22 | + |
| 23 | + init: function () { |
| 24 | + var isIOS = utils.isIOS(); |
| 25 | + var self = this; |
| 26 | + var scene = this.el; |
| 27 | + var vrMode = scene.components['vr-mode']; |
| 28 | + |
| 29 | + this.enterVRBound = vrMode.enterVR.bind(vrMode); |
| 30 | + this.exitVRBound = vrMode.exitVR.bind(vrMode); |
| 31 | + this.insideLoader = false; |
| 32 | + this.enterVREl = null; |
| 33 | + this.orientationModalEl = null; |
| 34 | + |
| 35 | + // Hide/show VR UI when entering/exiting VR mode. |
| 36 | + scene.addEventListener('vrmode-enter', this.hide.bind(this)); |
| 37 | + scene.addEventListener('vrmode-exit', this.show.bind(this)); |
| 38 | + |
| 39 | + window.addEventListener('message', function (event) { |
| 40 | + if (event.data.type === 'loaderReady') { |
| 41 | + self.insideLoader = true; |
| 42 | + self.remove(); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + // Orientational modal toggling on iOS. |
| 47 | + window.addEventListener('orientationchange', function () { |
| 48 | + if (!isIOS) { return; } |
| 49 | + if (!self.orientationModalEl) { return; } |
| 50 | + |
| 51 | + if (!utils.isLandscape() && scene.is('vrmode')) { |
| 52 | + // Show if in VR-mode on portrait. |
| 53 | + self.orientationModalEl.classList.remove(HIDDEN_CLASS); |
| 54 | + } else { |
| 55 | + self.orientationModalEl.classList.add(HIDDEN_CLASS); |
| 56 | + } |
| 57 | + }); |
| 58 | + }, |
| 59 | + |
| 60 | + update: function () { |
| 61 | + var scene = this.el; |
| 62 | + |
| 63 | + if (!this.data.enabled || this.insideLoader) { return this.remove(); } |
| 64 | + if (this.enterVREl || this.orientationModalEl) { return; } |
| 65 | + |
| 66 | + // Add UI if enabled and not already present. |
| 67 | + this.enterVREl = createEnterVR(this.enterVRBound, scene.isMobile); |
| 68 | + this.el.appendChild(this.enterVREl); |
| 69 | + |
| 70 | + this.orientationModalEl = createOrientationModal(this.exitVRBound); |
| 71 | + this.el.appendChild(this.orientationModalEl); |
| 72 | + }, |
| 73 | + |
| 74 | + remove: function () { |
| 75 | + [this.enterVREl, this.orientationModalEl].forEach(function (uiElement) { |
| 76 | + if (uiElement) { |
| 77 | + uiElement.parentNode.removeChild(uiElement); |
| 78 | + } |
| 79 | + }); |
| 80 | + }, |
| 81 | + |
| 82 | + hide: function () { |
| 83 | + this.enterVREl.classList.add(HIDDEN_CLASS); |
| 84 | + }, |
| 85 | + |
| 86 | + show: function () { |
| 87 | + this.enterVREl.classList.remove(HIDDEN_CLASS); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +/** |
| 92 | + * Creates Enter VR flow (button and compatibility modal). |
| 93 | + * |
| 94 | + * Creates a button that when clicked will enter into stereo-rendering mode for VR. |
| 95 | + * |
| 96 | + * For compatibility: |
| 97 | + * - Mobile always has compatibility via polyfill. |
| 98 | + * - If desktop browser does not have WebVR excluding polyfill, disable button, show modal. |
| 99 | + * - If desktop browser has WebVR excluding polyfill but not headset connected, |
| 100 | + * don't disable button, but show modal. |
| 101 | + * - If desktop browser has WebVR excluding polyfill and has headset connected, then |
| 102 | + * then no modal. |
| 103 | + * |
| 104 | + * Structure: <div><modal/><button></div> |
| 105 | + * |
| 106 | + * @returns {Element} Wrapper <div>. |
| 107 | + */ |
| 108 | +function createEnterVR (enterVRHandler, isMobile) { |
| 109 | + var compatModal; |
| 110 | + var compatModalLink; |
| 111 | + var compatModalText; |
| 112 | + // window.hasNativeVRSupport is set in src/aframe-core.js. |
| 113 | + var hasWebVR = isMobile || window.hasNonPolyfillWebVRSupport; |
| 114 | + var orientation; |
| 115 | + var vrButton; |
| 116 | + var wrapper; |
| 117 | + |
| 118 | + // Create elements. |
| 119 | + wrapper = document.createElement('div'); |
| 120 | + wrapper.classList.add(ENTER_VR_CLASS); |
| 121 | + compatModal = document.createElement('div'); |
| 122 | + compatModal.className = ENTER_VR_MODAL_CLASS; |
| 123 | + compatModalText = document.createElement('p'); |
| 124 | + compatModalLink = document.createElement('a'); |
| 125 | + compatModalLink.setAttribute('href', 'http://mozvr.com/#start'); |
| 126 | + compatModalLink.setAttribute('target', '_blank'); |
| 127 | + compatModalLink.innerHTML = 'Learn more.'; |
| 128 | + vrButton = document.createElement('button'); |
| 129 | + vrButton.className = ENTER_VR_BTN_CLASS; |
| 130 | + |
| 131 | + // Insert elements. |
| 132 | + if (compatModal) { |
| 133 | + compatModal.appendChild(compatModalText); |
| 134 | + compatModal.appendChild(compatModalLink); |
| 135 | + wrapper.appendChild(compatModal); |
| 136 | + } |
| 137 | + wrapper.appendChild(vrButton); |
| 138 | + |
| 139 | + if (!checkHeadsetConnected() && !isMobile) { |
| 140 | + compatModalText.innerHTML = 'Your browser supports WebVR. To enter VR, connect a headset, or use a mobile phone.'; |
| 141 | + wrapper.setAttribute(ENTER_VR_NO_HEADSET, ''); |
| 142 | + } |
| 143 | + |
| 144 | + // Handle enter VR flows. |
| 145 | + if (!hasWebVR) { |
| 146 | + compatModalText.innerHTML = 'Your browser does not support WebVR. To enter VR, use a VR-compatible browser or a mobile phone.'; |
| 147 | + wrapper.setAttribute(ENTER_VR_NO_WEBVR, ''); |
| 148 | + } else { |
| 149 | + vrButton.addEventListener('click', enterVRHandler); |
| 150 | + } |
| 151 | + return wrapper; |
| 152 | + |
| 153 | + /** |
| 154 | + * Check for headset connection by looking at orientation {0 0 0}. |
| 155 | + */ |
| 156 | + function checkHeadsetConnected () { |
| 157 | + controls.update(); |
| 158 | + orientation = dummyDolly.quaternion; |
| 159 | + if (orientation._x !== 0 || orientation._y !== 0 || orientation._z !== 0) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +function createOrientationModal (exitVRHandler) { |
| 166 | + var modal = document.createElement('div'); |
| 167 | + modal.className = ORIENTATION_MODAL_CLASS; |
| 168 | + modal.classList.add(HIDDEN_CLASS); |
| 169 | + |
| 170 | + var exit = document.createElement('button'); |
| 171 | + exit.innerHTML = 'Exit VR'; |
| 172 | + exit.addEventListener('click', exitVRHandler); |
| 173 | + modal.appendChild(exit); |
| 174 | + |
| 175 | + return modal; |
| 176 | +} |
0 commit comments