webvr-polyfill
Advanced tools
Comparing version 0.9.21 to 0.9.22
{ | ||
"name": "webvr-polyfill", | ||
"version": "0.9.21", | ||
"homepage": "https://github.com/borismus/webvr-polyfill", | ||
"version": "0.9.22", | ||
"homepage": "https://github.com/googlevr/webvr-polyfill", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Boris Smus <boris@smus.com>", |
@@ -62,3 +62,3 @@ # WebVR Polyfill | ||
// Flag to disable touch panner. In case you have your own touch controls. | ||
TOUCH_PANNER_DISABLED: true, // Default: false. | ||
TOUCH_PANNER_DISABLED: false, // Default: true. | ||
@@ -65,0 +65,0 @@ // Enable yaw panning only, disabling roll and pitch. This can be useful |
@@ -27,2 +27,14 @@ /* | ||
/** | ||
* The base class for all VR frame data. | ||
*/ | ||
function VRFrameData() { | ||
this.leftProjectionMatrix = new Float32Array(16); | ||
this.leftViewMatrix = new Float32Array(16); | ||
this.rightProjectionMatrix = new Float32Array(16); | ||
this.rightViewMatrix = new Float32Array(16); | ||
this.pose = null; | ||
}; | ||
/** | ||
* The base class for all VR displays. | ||
@@ -35,2 +47,5 @@ */ | ||
this.depthNear = 0.01; | ||
this.depthFar = 10000.0; | ||
this.isConnected = true; | ||
@@ -62,2 +77,8 @@ this.isPresenting = false; | ||
VRDisplay.prototype.getFrameData = function(frameData) { | ||
// TODO: Technically this should retain it's value for the duration of a frame | ||
// but I doubt that's practical to do in javascript. | ||
return Util.frameDataFromPose(frameData, this.getPose(), this); | ||
}; | ||
VRDisplay.prototype.getPose = function() { | ||
@@ -196,7 +217,5 @@ // TODO: Technically this should retain it's value for the duration of a frame | ||
// Already presenting, just changing configuration | ||
var changed = false; | ||
var layer = self.layer_; | ||
if (layer.source !== incomingLayer.source) { | ||
layer.source = incomingLayer.source; | ||
changed = true; | ||
} | ||
@@ -207,13 +226,8 @@ | ||
layer.leftBounds[i] = leftBounds[i]; | ||
changed = true; | ||
} | ||
if (layer.rightBounds[i] !== rightBounds[i]) { | ||
layer.rightBounds[i] = rightBounds[i]; | ||
changed = true; | ||
} | ||
} | ||
if (changed) { | ||
self.fireVRDisplayPresentChange_(); | ||
} | ||
resolve(); | ||
@@ -326,3 +340,3 @@ return; | ||
VRDisplay.prototype.fireVRDisplayPresentChange_ = function() { | ||
var event = new CustomEvent('vrdisplaypresentchange', {detail: {vrdisplay: this}}); | ||
var event = new CustomEvent('vrdisplaypresentchange', {display: this}); | ||
window.dispatchEvent(event); | ||
@@ -435,2 +449,3 @@ }; | ||
module.exports.VRFrameData = VRFrameData; | ||
module.exports.VRDisplay = VRDisplay; | ||
@@ -437,0 +452,0 @@ module.exports.VRDevice = VRDevice; |
@@ -252,2 +252,3 @@ /* | ||
self.bufferWidth = value; | ||
self.realCanvasWidth.set.call(canvas, value); | ||
self.onResize(); | ||
@@ -265,2 +266,3 @@ } | ||
self.bufferHeight = value; | ||
self.realCanvasHeight.set.call(canvas, value); | ||
self.onResize(); | ||
@@ -267,0 +269,0 @@ } |
@@ -16,3 +16,3 @@ /* | ||
var Util = require('./util.js'); | ||
var WebVRPolyfill = require('./webvr-polyfill.js'); | ||
var WebVRPolyfill = require('./webvr-polyfill.js').WebVRPolyfill; | ||
@@ -30,4 +30,4 @@ // Initialize a WebVRConfig just in case. | ||
// Flag to disable touch panner. In case you have your own touch controls. | ||
TOUCH_PANNER_DISABLED: false, | ||
// Flag to enable touch panner. In case you have your own touch controls. | ||
TOUCH_PANNER_DISABLED: true, | ||
@@ -34,0 +34,0 @@ // Flag to disabled the UI in VR Mode. |
178
src/util.js
@@ -218,2 +218,180 @@ /* | ||
Util.frameDataFromPose = (function() { | ||
var piOver180 = Math.PI / 180.0; | ||
var rad45 = Math.PI * 0.25; | ||
// Borrowed from glMatrix. | ||
function mat4_perspectiveFromFieldOfView(out, fov, near, far) { | ||
var upTan = Math.tan(fov ? (fov.upDegrees * piOver180) : rad45), | ||
downTan = Math.tan(fov ? (fov.downDegrees * piOver180) : rad45), | ||
leftTan = Math.tan(fov ? (fov.leftDegrees * piOver180) : rad45), | ||
rightTan = Math.tan(fov ? (fov.rightDegrees * piOver180) : rad45), | ||
xScale = 2.0 / (leftTan + rightTan), | ||
yScale = 2.0 / (upTan + downTan); | ||
out[0] = xScale; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
out[3] = 0.0; | ||
out[4] = 0.0; | ||
out[5] = yScale; | ||
out[6] = 0.0; | ||
out[7] = 0.0; | ||
out[8] = -((leftTan - rightTan) * xScale * 0.5); | ||
out[9] = ((upTan - downTan) * yScale * 0.5); | ||
out[10] = far / (near - far); | ||
out[11] = -1.0; | ||
out[12] = 0.0; | ||
out[13] = 0.0; | ||
out[14] = (far * near) / (near - far); | ||
out[15] = 0.0; | ||
return out; | ||
} | ||
function mat4_fromRotationTranslation(out, q, v) { | ||
// Quaternion math | ||
var x = q[0], y = q[1], z = q[2], w = q[3], | ||
x2 = x + x, | ||
y2 = y + y, | ||
z2 = z + z, | ||
xx = x * x2, | ||
xy = x * y2, | ||
xz = x * z2, | ||
yy = y * y2, | ||
yz = y * z2, | ||
zz = z * z2, | ||
wx = w * x2, | ||
wy = w * y2, | ||
wz = w * z2; | ||
out[0] = 1 - (yy + zz); | ||
out[1] = xy + wz; | ||
out[2] = xz - wy; | ||
out[3] = 0; | ||
out[4] = xy - wz; | ||
out[5] = 1 - (xx + zz); | ||
out[6] = yz + wx; | ||
out[7] = 0; | ||
out[8] = xz + wy; | ||
out[9] = yz - wx; | ||
out[10] = 1 - (xx + yy); | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
}; | ||
function mat4_translate(out, a, v) { | ||
var x = v[0], y = v[1], z = v[2], | ||
a00, a01, a02, a03, | ||
a10, a11, a12, a13, | ||
a20, a21, a22, a23; | ||
if (a === out) { | ||
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; | ||
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; | ||
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; | ||
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; | ||
} else { | ||
a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; | ||
a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; | ||
a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; | ||
out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; | ||
out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; | ||
out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; | ||
out[12] = a00 * x + a10 * y + a20 * z + a[12]; | ||
out[13] = a01 * x + a11 * y + a21 * z + a[13]; | ||
out[14] = a02 * x + a12 * y + a22 * z + a[14]; | ||
out[15] = a03 * x + a13 * y + a23 * z + a[15]; | ||
} | ||
return out; | ||
}; | ||
mat4_invert = function(out, a) { | ||
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], | ||
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], | ||
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], | ||
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], | ||
b00 = a00 * a11 - a01 * a10, | ||
b01 = a00 * a12 - a02 * a10, | ||
b02 = a00 * a13 - a03 * a10, | ||
b03 = a01 * a12 - a02 * a11, | ||
b04 = a01 * a13 - a03 * a11, | ||
b05 = a02 * a13 - a03 * a12, | ||
b06 = a20 * a31 - a21 * a30, | ||
b07 = a20 * a32 - a22 * a30, | ||
b08 = a20 * a33 - a23 * a30, | ||
b09 = a21 * a32 - a22 * a31, | ||
b10 = a21 * a33 - a23 * a31, | ||
b11 = a22 * a33 - a23 * a32, | ||
// Calculate the determinant | ||
det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | ||
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | ||
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | ||
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; | ||
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | ||
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | ||
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | ||
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; | ||
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | ||
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | ||
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | ||
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; | ||
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; | ||
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; | ||
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; | ||
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; | ||
return out; | ||
}; | ||
var defaultOrientation = new Float32Array([0, 0, 0, 1]); | ||
var defaultPosition = new Float32Array([0, 0, 0]); | ||
function updateEyeMatrices(projection, view, pose, parameters, vrDisplay) { | ||
mat4_perspectiveFromFieldOfView(projection, parameters ? parameters.fieldOfView : null, vrDisplay.depthNear, vrDisplay.depthFar); | ||
var orientation = pose.orientation || defaultOrientation; | ||
var position = pose.position || defaultPosition; | ||
mat4_fromRotationTranslation(view, orientation, position); | ||
if (parameters) | ||
mat4_translate(view, view, parameters.offset); | ||
mat4_invert(view, view); | ||
} | ||
return function(frameData, pose, vrDisplay) { | ||
if (!frameData || !pose) | ||
return false; | ||
frameData.pose = pose; | ||
frameData.timestamp = pose.timestamp; | ||
updateEyeMatrices( | ||
frameData.leftProjectionMatrix, frameData.leftViewMatrix, | ||
pose, vrDisplay.getEyeParameters("left"), vrDisplay); | ||
updateEyeMatrices( | ||
frameData.rightProjectionMatrix, frameData.rightViewMatrix, | ||
pose, vrDisplay.getEyeParameters("right"), vrDisplay); | ||
return true; | ||
}; | ||
})(); | ||
module.exports = Util; |
@@ -16,2 +16,3 @@ /* | ||
var Util = require('./util.js'); | ||
var CardboardVRDisplay = require('./cardboard-vr-display.js'); | ||
@@ -22,2 +23,3 @@ var MouseKeyboardVRDisplay = require('./mouse-keyboard-vr-display.js'); | ||
var VRDisplay = require('./base.js').VRDisplay; | ||
var VRFrameData = require('./base.js').VRFrameData; | ||
var HMDVRDevice = require('./base.js').HMDVRDevice; | ||
@@ -43,2 +45,5 @@ var PositionSensorVRDevice = require('./base.js').PositionSensorVRDevice; | ||
} | ||
// Put a shim in place to update the API to 1.1 if needed. | ||
InstallWebVRSpecShim(); | ||
} | ||
@@ -113,2 +118,5 @@ | ||
}); | ||
// Provide the VRFrameData object. | ||
window.VRFrameData = VRFrameData; | ||
}; | ||
@@ -192,2 +200,24 @@ | ||
module.exports = WebVRPolyfill; | ||
// Installs a shim that updates a WebVR 1.0 spec implementation to WebVR 1.1 | ||
function InstallWebVRSpecShim() { | ||
if ('VRDisplay' in window && !('VRFrameData' in window)) { | ||
// Provide the VRFrameData object. | ||
window.VRFrameData = VRFrameData; | ||
// A lot of Chrome builds don't have depthNear and depthFar, even | ||
// though they're in the WebVR 1.0 spec. Patch them in if they're not present. | ||
if(!('depthNear' in window.VRDisplay.prototype)) { | ||
window.VRDisplay.prototype.depthNear = 0.01; | ||
} | ||
if(!('depthFar' in window.VRDisplay.prototype)) { | ||
window.VRDisplay.prototype.depthFar = 10000.0; | ||
} | ||
window.VRDisplay.prototype.getFrameData = function(frameData) { | ||
return Util.frameDataFromPose(frameData, this.getPose(), this); | ||
} | ||
} | ||
}; | ||
module.exports.WebVRPolyfill = WebVRPolyfill; |
Sorry, the diff of this file is too big to display
796107
13285