From 75a6d51ac8c8fab3f2aa648bddea5a59146bb65a Mon Sep 17 00:00:00 2001 From: Jordan Santell Date: Mon, 5 Mar 2018 09:31:54 -0800 Subject: [PATCH] Handle Chrome m66+'s change of reporting a deviceorientation rotationRate in degrees, rather than (incorrect) radians. Fixes #18 --- src/sensor-fusion/fusion-pose-sensor.js | 10 +++++++--- src/util.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sensor-fusion/fusion-pose-sensor.js b/src/sensor-fusion/fusion-pose-sensor.js index 161f43e..3a970a3 100644 --- a/src/sensor-fusion/fusion-pose-sensor.js +++ b/src/sensor-fusion/fusion-pose-sensor.js @@ -62,6 +62,10 @@ function FusionPoseSensor(kFilter, predictionTime, yawOnly, isDebug) { this.isFirefoxAndroid = Util.isFirefoxAndroid(); this.isIOS = Util.isIOS(); + // Chrome as of m66 started reporting `rotationRate` in degrees rather + // than radians, to be consistent with other browsers. + // https://github.com/immersive-web/cardboard-vr-display/issues/18 + this.isChromeUsingDegrees = Util.getChromeVersion() >= 66; this.orientationOut_ = new Float32Array(4); } @@ -153,9 +157,9 @@ FusionPoseSensor.prototype.updateDeviceMotion_ = function(deviceMotion) { this.gyroscope.set(rotRate.alpha, rotRate.beta, rotRate.gamma); } - // With iOS and Firefox Android, rotationRate is reported in degrees, - // so we first convert to radians. - if (this.isIOS || this.isFirefoxAndroid) { + // Browsers on iOS, Firefox/Android, and Chrome m66/Android `rotationRate` + // is reported in degrees, so we first convert to radians. + if (this.isIOS || this.isFirefoxAndroid || this.isChromeUsingDegrees) { this.gyroscope.multiplyScalar(Math.PI / 180); } diff --git a/src/util.js b/src/util.js index 3464d6e..52fd2e2 100644 --- a/src/util.js +++ b/src/util.js @@ -59,6 +59,18 @@ export const isFirefoxAndroid = (function() { }; })(); +/** + * Returns a number value indiciating the version of Chrome being used, + * or otherwise `null` if not on Chrome. + */ +export const getChromeVersion = (function() { + const match = navigator.userAgent.match(/.*Chrome\/([0-9]+)/); + const value = match ? parseInt(match[1], 10) : null; + return function() { + return value; + }; +})(); + export const isR7 = (function() { var isR7 = navigator.userAgent.indexOf('R7 Build') !== -1; return function() {