Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 2aef756

Browse files
authored
Merge pull request #157 from caseyyee/2d
fixes 2d fallback
2 parents 6ec6542 + f40686a commit 2aef756

File tree

1 file changed

+78
-76
lines changed

1 file changed

+78
-76
lines changed

Assets/WebGLTemplates/WebVR/webvr.js

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
function onAnimate () {
115115
if (!vrDisplay || !vrDisplay.isPresenting) {
116116
windowRaf(onAnimate);
117-
return;
118117
}
119118

120119
if (vrDisplay) {
@@ -127,74 +126,77 @@
127126
return vrDisplay.requestAnimationFrame(onAnimate);
128127
}
129128

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+
}
182184
}
183185
}
184-
}
185186

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+
};
194195

195-
gameInstance.SendMessage('WebVRCameraSet', 'WebVRData', JSON.stringify(vrData));
196+
gameInstance.SendMessage('WebVRCameraSet', 'WebVRData', JSON.stringify(vrData));
197+
}
196198

197-
if (!vrDisplay.isPresenting) {
199+
if (!vrDisplay.isPresenting || isPolyfilled(vrDisplay)) {
198200
submitNextFrame = false;
199201
}
200202
if (submitNextFrame) {
@@ -293,31 +295,31 @@
293295
return null;
294296
}
295297

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)) {
303299
showInstruction(document.querySelector('#novr'));
304300
} else {
305301
status.dataset.enabled = 'true';
306302
}
307303

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';
310307
}
311308

312-
// Enable button to toggle entering/exiting VR.
313-
entervrButton.dataset.enabled = 'true';
314-
315309
return vrDisplay;
316310
}).catch(function (err) {
317311
console.error('Error occurred getting VR display:', err);
318312
});
319313
}
320314

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+
321323
function onKeyUp(evt) {
322324
if (toggleVRKeyName && toggleVRKeyName === evt.key) {
323325
onToggleVR();

0 commit comments

Comments
 (0)