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

Commit

Permalink
Merge pull request #157 from caseyyee/2d
Browse files Browse the repository at this point in the history
fixes 2d fallback
  • Loading branch information
caseyyee authored Mar 1, 2018
2 parents 6ec6542 + f40686a commit 2aef756
Showing 1 changed file with 78 additions and 76 deletions.
154 changes: 78 additions & 76 deletions Assets/WebGLTemplates/WebVR/webvr.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
function onAnimate () {
if (!vrDisplay || !vrDisplay.isPresenting) {
windowRaf(onAnimate);
return;
}

if (vrDisplay) {
Expand All @@ -127,74 +126,77 @@
return vrDisplay.requestAnimationFrame(onAnimate);
}

vrDisplay.getFrameData(frameData);

// convert view and projection matrices for use in Unity.
mat4.copy(leftProjectionMatrix, frameData.leftProjectionMatrix);
mat4.transpose(leftProjectionMatrix, leftProjectionMatrix);

mat4.copy(rightProjectionMatrix, frameData.rightProjectionMatrix);
mat4.transpose(rightProjectionMatrix, rightProjectionMatrix);

mat4.copy(leftViewMatrix, frameData.leftViewMatrix);
mat4.transpose(leftViewMatrix, leftViewMatrix);
leftViewMatrix[2] *= -1;
leftViewMatrix[6] *= -1;
leftViewMatrix[10] *= -1;
leftViewMatrix[14] *= -1;

mat4.copy(rightViewMatrix, frameData.rightViewMatrix);
mat4.transpose(rightViewMatrix, rightViewMatrix);
rightViewMatrix[2] *= -1;
rightViewMatrix[6] *= -1;
rightViewMatrix[10] *= -1;
rightViewMatrix[14] *= -1;

// Sit Stand transform
if (vrDisplay.stageParameters) {
mat4.copy(sitStand, vrDisplay.stageParameters.sittingToStandingTransform);
} else {
mat4.identity(sitStand);
mat4.translate(sitStand, sitStand, [0, defaultHeight, 0]);
}
mat4.transpose(sitStand, sitStand);

// gamepads
gamepads = navigator.getGamepads();
vrGamepads = [];
for (var i = 0; i < gamepads.length; ++i) {
var gamepad = gamepads[i];
if (gamepad && (gamepad.pose || gamepad.displayId)) {
if (gamepad.pose.position && gamepad.pose.orientation) {
// flips gamepad axis to work with Unity.
var position = gamepad.pose.position;
position[2] *= -1;
var orientation = gamepad.pose.orientation;
orientation[0] *= -1;
orientation[1] *= -1;

vrGamepads.push({
index: gamepad.index,
hand: gamepad.hand,
orientation: Array.from(orientation),
position: Array.from(position)
});
// Check for polyfill so that we can utilize its mouse-look controls.
if (vrDisplay.isPresenting || isPolyfilled(vrDisplay)) {
vrDisplay.getFrameData(frameData);

// convert view and projection matrices for use in Unity.
mat4.copy(leftProjectionMatrix, frameData.leftProjectionMatrix);
mat4.transpose(leftProjectionMatrix, leftProjectionMatrix);

mat4.copy(rightProjectionMatrix, frameData.rightProjectionMatrix);
mat4.transpose(rightProjectionMatrix, rightProjectionMatrix);

mat4.copy(leftViewMatrix, frameData.leftViewMatrix);
mat4.transpose(leftViewMatrix, leftViewMatrix);
leftViewMatrix[2] *= -1;
leftViewMatrix[6] *= -1;
leftViewMatrix[10] *= -1;
leftViewMatrix[14] *= -1;

mat4.copy(rightViewMatrix, frameData.rightViewMatrix);
mat4.transpose(rightViewMatrix, rightViewMatrix);
rightViewMatrix[2] *= -1;
rightViewMatrix[6] *= -1;
rightViewMatrix[10] *= -1;
rightViewMatrix[14] *= -1;

// Sit Stand transform
if (vrDisplay.stageParameters) {
mat4.copy(sitStand, vrDisplay.stageParameters.sittingToStandingTransform);
} else {
mat4.identity(sitStand);
mat4.translate(sitStand, sitStand, [0, defaultHeight, 0]);
}
mat4.transpose(sitStand, sitStand);

// gamepads
gamepads = navigator.getGamepads();
vrGamepads = [];
for (var i = 0; i < gamepads.length; ++i) {
var gamepad = gamepads[i];
if (gamepad && (gamepad.pose || gamepad.displayId)) {
if (gamepad.pose.position && gamepad.pose.orientation) {
// flips gamepad axis to work with Unity.
var position = gamepad.pose.position;
position[2] *= -1;
var orientation = gamepad.pose.orientation;
orientation[0] *= -1;
orientation[1] *= -1;

vrGamepads.push({
index: gamepad.index,
hand: gamepad.hand,
orientation: Array.from(orientation),
position: Array.from(position)
});
}
}
}
}

var vrData = {
leftProjectionMatrix: Array.from(leftProjectionMatrix),
rightProjectionMatrix: Array.from(rightProjectionMatrix),
leftViewMatrix: Array.from(leftViewMatrix),
rightViewMatrix: Array.from(rightViewMatrix),
sitStand: Array.from(sitStand),
controllers: vrGamepads
};
var vrData = {
leftProjectionMatrix: Array.from(leftProjectionMatrix),
rightProjectionMatrix: Array.from(rightProjectionMatrix),
leftViewMatrix: Array.from(leftViewMatrix),
rightViewMatrix: Array.from(rightViewMatrix),
sitStand: Array.from(sitStand),
controllers: vrGamepads
};

gameInstance.SendMessage('WebVRCameraSet', 'WebVRData', JSON.stringify(vrData));
gameInstance.SendMessage('WebVRCameraSet', 'WebVRData', JSON.stringify(vrData));
}

if (!vrDisplay.isPresenting) {
if (!vrDisplay.isPresenting || isPolyfilled(vrDisplay)) {
submitNextFrame = false;
}
if (submitNextFrame) {
Expand Down Expand Up @@ -293,31 +295,31 @@
return null;
}

// Check to see if we are polyfilled.
var isPolyfilled = (vrDisplay.deviceId || '').indexOf('polyfill') > 0 ||
(vrDisplay.displayName || '').indexOf('polyfill') > 0 ||
(vrDisplay.deviceName || '').indexOf('polyfill') > 0 ||
vrDisplay.hardwareUnitId;

if (isPolyfilled) {
if (isPolyfilled(vrDisplay)) {
showInstruction(document.querySelector('#novr'));
} else {
status.dataset.enabled = 'true';
}

if (!vrDisplay.capabilities || !vrDisplay.capabilities.canPresent) {
throw new Error('VR display is not capable of presenting');
if (vrDisplay.capabilities && vrDisplay.capabilities.canPresent) {
// Enable button to toggle entering/exiting VR.
entervrButton.dataset.enabled = 'true';
}

// Enable button to toggle entering/exiting VR.
entervrButton.dataset.enabled = 'true';

return vrDisplay;
}).catch(function (err) {
console.error('Error occurred getting VR display:', err);
});
}

// Check to see if we are using polyfill.
function isPolyfilled(display) {
return (display.deviceId || '').indexOf('polyfill') > 0 ||
(display.displayName || '').indexOf('polyfill') > 0 ||
(display.deviceName || '').indexOf('polyfill') > 0 ||
display.hardwareUnitId;
}

function onKeyUp(evt) {
if (toggleVRKeyName && toggleVRKeyName === evt.key) {
onToggleVR();
Expand Down

0 comments on commit 2aef756

Please sign in to comment.