Skip to content

Commit

Permalink
Merge branch 'master' of github.com:borismus/webvr-polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
borismus committed Nov 10, 2015
2 parents 52fa5e3 + 81d7549 commit 760a796
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ are supported:
//K_FILTER: 0.98, // Default: 0.98.
// How far into the future to predict during fast motion.
//PREDICTION_TIME_S: 0.050, // Default: 0.050s.
// Flag to disable touch panner. In case you have your own touch controls
//TOUCH_PANNER_DISABLED: true, // Default: false.
}
19 changes: 16 additions & 3 deletions build/webvr-polyfill.js

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "webvr-polyfill",
"version": "0.2.0",
"homepage": "https://github.com/borismus/webvr-polyfill",
"authors": [
"Boris Smus <[email protected]>"
],
"description": "Use WebVR today, on mobile or desktop, without requiring a special browser build.",
"main": "build/webvr-polyfill.js",
"keywords": [
"vr",
"webvr"
],
"license": "Apache2"
}
28 changes: 21 additions & 7 deletions src/fusion-position-sensor-vr-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function FusionPositionSensorVRDevice() {

this.filter = new ComplementaryFilter(WebVRConfig.K_FILTER || 0.98);
this.posePredictor = new PosePredictor(WebVRConfig.PREDICTION_TIME_S || 0.050);
this.touchPanner = new TouchPanner();
if (!WebVRConfig.TOUCH_PANNER_DISABLED) {
this.touchPanner = new TouchPanner();
}

this.filterToWorldQ = new THREE.Quaternion();

Expand All @@ -51,6 +53,9 @@ function FusionPositionSensorVRDevice() {

// Keep track of a reset transform for resetSensor.
this.resetQ = new THREE.Quaternion();

this.isFirefoxAndroid = Util.isFirefoxAndroid();
this.isIOS = Util.isIOS();
}
FusionPositionSensorVRDevice.prototype = new PositionSensorVRDevice();

Expand Down Expand Up @@ -79,7 +84,9 @@ FusionPositionSensorVRDevice.prototype.getOrientation = function() {
var out = new THREE.Quaternion();
out.copy(this.filterToWorldQ);
out.multiply(this.resetQ);
out.multiply(this.touchPanner.getOrientation());
if (this.touchPanner) {
out.multiply(this.touchPanner.getOrientation());
}
out.multiply(this.predictedQ);
out.multiply(this.worldToScreenQ);
return out;
Expand All @@ -91,14 +98,21 @@ FusionPositionSensorVRDevice.prototype.resetSensor = function() {
var yaw = euler.y;
console.log('resetSensor with yaw: %f', yaw);
this.resetQ.setFromAxisAngle(new THREE.Vector3(0, 0, 1), -yaw);
this.touchPanner.resetSensor();
if (this.touchPanner) {
this.touchPanner.resetSensor();
}
};

FusionPositionSensorVRDevice.prototype.onDeviceMotionChange_ = function(deviceMotion) {
var accGravity = deviceMotion.accelerationIncludingGravity;
var rotRate = deviceMotion.rotationRate;
var timestampS = deviceMotion.timeStamp / 1000;

// Firefox Android timeStamp returns one thousandth of a millisecond.
if (this.isFirefoxAndroid) {
timestampS /= 1000;
}

var deltaS = timestampS - this.previousTimestampS;
if (deltaS <= Util.MIN_TIMESTEP || deltaS > Util.MAX_TIMESTEP) {
console.warn('Invalid timestamps detected. Time step between successive ' +
Expand All @@ -109,9 +123,9 @@ FusionPositionSensorVRDevice.prototype.onDeviceMotionChange_ = function(deviceMo
this.accelerometer.set(-accGravity.x, -accGravity.y, -accGravity.z);
this.gyroscope.set(rotRate.alpha, rotRate.beta, rotRate.gamma);

// In iOS, rotationRate is reported in degrees, so we first convert to
// radians.
if (Util.isIOS()) {
// With iOS and Firefox Android, rotationRate is reported in degrees,
// so we first convert to radians.
if (this.isIOS || this.isFirefoxAndroid) {
this.gyroscope.multiplyScalar(Math.PI / 180);
}

Expand All @@ -134,7 +148,7 @@ FusionPositionSensorVRDevice.prototype.setScreenTransform_ = function() {
case 90:
this.worldToScreenQ.setFromAxisAngle(new THREE.Vector3(0, 0, 1), -Math.PI/2);
break;
case -90:
case -90:
this.worldToScreenQ.setFromAxisAngle(new THREE.Vector3(0, 0, 1), Math.PI/2);
break;
case 180:
Expand Down
4 changes: 4 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Util.isIOS = function() {
return /iPad|iPhone|iPod/.test(navigator.platform);
};

Util.isFirefoxAndroid = function() {
return navigator.userAgent.indexOf('Firefox') !== -1 && navigator.userAgent.indexOf('Android') !== -1;
}

// Helper method to validate the time steps of sensor timestamps.
Util.isTimestampDeltaValid = function(timestampDeltaS) {
if (isNaN(timestampDeltaS)) {
Expand Down

0 comments on commit 760a796

Please sign in to comment.