|
114 | 114 | function onAnimate () {
|
115 | 115 | if (!vrDisplay || !vrDisplay.isPresenting) {
|
116 | 116 | windowRaf(onAnimate);
|
117 |
| - return; |
118 | 117 | }
|
119 | 118 |
|
120 | 119 | if (vrDisplay) {
|
|
127 | 126 | return vrDisplay.requestAnimationFrame(onAnimate);
|
128 | 127 | }
|
129 | 128 |
|
130 |
| - vrDisplay.getFrameData(frameData); |
131 |
| - |
132 |
| - // convert view and projection matrices for use in Unity. |
133 |
| - mat4.copy(leftProjectionMatrix, frameData.leftProjectionMatrix); |
134 |
| - mat4.transpose(leftProjectionMatrix, leftProjectionMatrix); |
135 |
| - |
136 |
| - mat4.copy(rightProjectionMatrix, frameData.rightProjectionMatrix); |
137 |
| - mat4.transpose(rightProjectionMatrix, rightProjectionMatrix); |
138 |
| - |
139 |
| - mat4.copy(leftViewMatrix, frameData.leftViewMatrix); |
140 |
| - mat4.transpose(leftViewMatrix, leftViewMatrix); |
141 |
| - leftViewMatrix[2] *= -1; |
142 |
| - leftViewMatrix[6] *= -1; |
143 |
| - leftViewMatrix[10] *= -1; |
144 |
| - leftViewMatrix[14] *= -1; |
145 |
| - |
146 |
| - mat4.copy(rightViewMatrix, frameData.rightViewMatrix); |
147 |
| - mat4.transpose(rightViewMatrix, rightViewMatrix); |
148 |
| - rightViewMatrix[2] *= -1; |
149 |
| - rightViewMatrix[6] *= -1; |
150 |
| - rightViewMatrix[10] *= -1; |
151 |
| - rightViewMatrix[14] *= -1; |
152 |
| - |
153 |
| - // Sit Stand transform |
154 |
| - if (vrDisplay.stageParameters) { |
155 |
| - mat4.copy(sitStand, vrDisplay.stageParameters.sittingToStandingTransform); |
156 |
| - } else { |
157 |
| - mat4.identity(sitStand); |
158 |
| - mat4.translate(sitStand, sitStand, [0, defaultHeight, 0]); |
159 |
| - } |
160 |
| - mat4.transpose(sitStand, sitStand); |
161 |
| - |
162 |
| - // gamepads |
163 |
| - gamepads = navigator.getGamepads(); |
164 |
| - vrGamepads = []; |
165 |
| - for (var i = 0; i < gamepads.length; ++i) { |
166 |
| - var gamepad = gamepads[i]; |
167 |
| - if (gamepad && (gamepad.pose || gamepad.displayId)) { |
168 |
| - if (gamepad.pose.position && gamepad.pose.orientation) { |
169 |
| - // flips gamepad axis to work with Unity. |
170 |
| - var position = gamepad.pose.position; |
171 |
| - position[2] *= -1; |
172 |
| - var orientation = gamepad.pose.orientation; |
173 |
| - orientation[0] *= -1; |
174 |
| - orientation[1] *= -1; |
175 |
| - |
176 |
| - vrGamepads.push({ |
177 |
| - index: gamepad.index, |
178 |
| - hand: gamepad.hand, |
179 |
| - orientation: Array.from(orientation), |
180 |
| - position: Array.from(position) |
181 |
| - }); |
| 129 | + // Check for polyfill so that we can utilize its mouse-look controls. |
| 130 | + if (vrDisplay.isPresenting || isPolyfilled(vrDisplay)) { |
| 131 | + vrDisplay.getFrameData(frameData); |
| 132 | + |
| 133 | + // convert view and projection matrices for use in Unity. |
| 134 | + mat4.copy(leftProjectionMatrix, frameData.leftProjectionMatrix); |
| 135 | + mat4.transpose(leftProjectionMatrix, leftProjectionMatrix); |
| 136 | + |
| 137 | + mat4.copy(rightProjectionMatrix, frameData.rightProjectionMatrix); |
| 138 | + mat4.transpose(rightProjectionMatrix, rightProjectionMatrix); |
| 139 | + |
| 140 | + mat4.copy(leftViewMatrix, frameData.leftViewMatrix); |
| 141 | + mat4.transpose(leftViewMatrix, leftViewMatrix); |
| 142 | + leftViewMatrix[2] *= -1; |
| 143 | + leftViewMatrix[6] *= -1; |
| 144 | + leftViewMatrix[10] *= -1; |
| 145 | + leftViewMatrix[14] *= -1; |
| 146 | + |
| 147 | + mat4.copy(rightViewMatrix, frameData.rightViewMatrix); |
| 148 | + mat4.transpose(rightViewMatrix, rightViewMatrix); |
| 149 | + rightViewMatrix[2] *= -1; |
| 150 | + rightViewMatrix[6] *= -1; |
| 151 | + rightViewMatrix[10] *= -1; |
| 152 | + rightViewMatrix[14] *= -1; |
| 153 | + |
| 154 | + // Sit Stand transform |
| 155 | + if (vrDisplay.stageParameters) { |
| 156 | + mat4.copy(sitStand, vrDisplay.stageParameters.sittingToStandingTransform); |
| 157 | + } else { |
| 158 | + mat4.identity(sitStand); |
| 159 | + mat4.translate(sitStand, sitStand, [0, defaultHeight, 0]); |
| 160 | + } |
| 161 | + mat4.transpose(sitStand, sitStand); |
| 162 | + |
| 163 | + // gamepads |
| 164 | + gamepads = navigator.getGamepads(); |
| 165 | + vrGamepads = []; |
| 166 | + for (var i = 0; i < gamepads.length; ++i) { |
| 167 | + var gamepad = gamepads[i]; |
| 168 | + if (gamepad && (gamepad.pose || gamepad.displayId)) { |
| 169 | + if (gamepad.pose.position && gamepad.pose.orientation) { |
| 170 | + // flips gamepad axis to work with Unity. |
| 171 | + var position = gamepad.pose.position; |
| 172 | + position[2] *= -1; |
| 173 | + var orientation = gamepad.pose.orientation; |
| 174 | + orientation[0] *= -1; |
| 175 | + orientation[1] *= -1; |
| 176 | + |
| 177 | + vrGamepads.push({ |
| 178 | + index: gamepad.index, |
| 179 | + hand: gamepad.hand, |
| 180 | + orientation: Array.from(orientation), |
| 181 | + position: Array.from(position) |
| 182 | + }); |
| 183 | + } |
182 | 184 | }
|
183 | 185 | }
|
184 |
| - } |
185 | 186 |
|
186 |
| - var vrData = { |
187 |
| - leftProjectionMatrix: Array.from(leftProjectionMatrix), |
188 |
| - rightProjectionMatrix: Array.from(rightProjectionMatrix), |
189 |
| - leftViewMatrix: Array.from(leftViewMatrix), |
190 |
| - rightViewMatrix: Array.from(rightViewMatrix), |
191 |
| - sitStand: Array.from(sitStand), |
192 |
| - controllers: vrGamepads |
193 |
| - }; |
| 187 | + var vrData = { |
| 188 | + leftProjectionMatrix: Array.from(leftProjectionMatrix), |
| 189 | + rightProjectionMatrix: Array.from(rightProjectionMatrix), |
| 190 | + leftViewMatrix: Array.from(leftViewMatrix), |
| 191 | + rightViewMatrix: Array.from(rightViewMatrix), |
| 192 | + sitStand: Array.from(sitStand), |
| 193 | + controllers: vrGamepads |
| 194 | + }; |
194 | 195 |
|
195 |
| - gameInstance.SendMessage('WebVRCameraSet', 'WebVRData', JSON.stringify(vrData)); |
| 196 | + gameInstance.SendMessage('WebVRCameraSet', 'WebVRData', JSON.stringify(vrData)); |
| 197 | + } |
196 | 198 |
|
197 |
| - if (!vrDisplay.isPresenting) { |
| 199 | + if (!vrDisplay.isPresenting || isPolyfilled(vrDisplay)) { |
198 | 200 | submitNextFrame = false;
|
199 | 201 | }
|
200 | 202 | if (submitNextFrame) {
|
|
293 | 295 | return null;
|
294 | 296 | }
|
295 | 297 |
|
296 |
| - // Check to see if we are polyfilled. |
297 |
| - var isPolyfilled = (vrDisplay.deviceId || '').indexOf('polyfill') > 0 || |
298 |
| - (vrDisplay.displayName || '').indexOf('polyfill') > 0 || |
299 |
| - (vrDisplay.deviceName || '').indexOf('polyfill') > 0 || |
300 |
| - vrDisplay.hardwareUnitId; |
301 |
| - |
302 |
| - if (isPolyfilled) { |
| 298 | + if (isPolyfilled(vrDisplay)) { |
303 | 299 | showInstruction(document.querySelector('#novr'));
|
304 | 300 | } else {
|
305 | 301 | status.dataset.enabled = 'true';
|
306 | 302 | }
|
307 | 303 |
|
308 |
| - if (!vrDisplay.capabilities || !vrDisplay.capabilities.canPresent) { |
309 |
| - throw new Error('VR display is not capable of presenting'); |
| 304 | + if (vrDisplay.capabilities && vrDisplay.capabilities.canPresent) { |
| 305 | + // Enable button to toggle entering/exiting VR. |
| 306 | + entervrButton.dataset.enabled = 'true'; |
310 | 307 | }
|
311 | 308 |
|
312 |
| - // Enable button to toggle entering/exiting VR. |
313 |
| - entervrButton.dataset.enabled = 'true'; |
314 |
| - |
315 | 309 | return vrDisplay;
|
316 | 310 | }).catch(function (err) {
|
317 | 311 | console.error('Error occurred getting VR display:', err);
|
318 | 312 | });
|
319 | 313 | }
|
320 | 314 |
|
| 315 | + // Check to see if we are using polyfill. |
| 316 | + function isPolyfilled(display) { |
| 317 | + return (display.deviceId || '').indexOf('polyfill') > 0 || |
| 318 | + (display.displayName || '').indexOf('polyfill') > 0 || |
| 319 | + (display.deviceName || '').indexOf('polyfill') > 0 || |
| 320 | + display.hardwareUnitId; |
| 321 | + } |
| 322 | + |
321 | 323 | function onKeyUp(evt) {
|
322 | 324 | if (toggleVRKeyName && toggleVRKeyName === evt.key) {
|
323 | 325 | onToggleVR();
|
|
0 commit comments