-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android magic window #4363
Merged
Merged
Android magic window #4363
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/* global DeviceOrientationEvent */ | ||
var registerComponent = require('../core/component').registerComponent; | ||
var THREE = require('../lib/three'); | ||
var utils = require('../utils/'); | ||
var bind = utils.bind; | ||
var PolyfillControls = require('../utils').device.PolyfillControls; | ||
|
||
// To avoid recalculation at every mouse movement tick | ||
var PI_2 = Math.PI / 2; | ||
|
@@ -30,8 +30,7 @@ module.exports.Component = registerComponent('look-controls', { | |
// To save / restore camera pose | ||
this.savedRotation = new THREE.Vector3(); | ||
this.savedPosition = new THREE.Vector3(); | ||
this.polyfillObject = new THREE.Object3D(); | ||
this.polyfillControls = new PolyfillControls(this.polyfillObject); | ||
this.magicWindowObject = new THREE.Object3D(); | ||
this.rotation = {}; | ||
this.deltaRotation = {}; | ||
this.savedPose = null; | ||
|
@@ -41,6 +40,8 @@ module.exports.Component = registerComponent('look-controls', { | |
this.el.object3D.matrixAutoUpdate = false; | ||
this.el.object3D.updateMatrix(); | ||
|
||
this.setupMagicWindowControls(); | ||
|
||
this.savedPose = { | ||
position: new THREE.Vector3(), | ||
rotation: new THREE.Euler() | ||
|
@@ -50,6 +51,24 @@ module.exports.Component = registerComponent('look-controls', { | |
if (this.el.sceneEl.is('vr-mode')) { this.onEnterVR(); } | ||
}, | ||
|
||
setupMagicWindowControls: function () { | ||
var magicWindowControls; | ||
// Only on mobile devices and only enabled if DeviceOrientation permission has been granted. | ||
if (utils.device.isMobile()) { | ||
magicWindowControls = this.magicWindowControls = new THREE.DeviceOrientationControls(this.magicWindowObject); | ||
if (typeof DeviceOrientationEvent === 'undefined' && DeviceOrientationEvent.requestPermission) { | ||
magicWindowControls.enabled = false; | ||
if (this.el.sceneEl.components['device-orientation-permission-ui'].premissionGranted) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is a typo here premissionGranted => permissionGranted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
magicWindowControls.enabled = true; | ||
} else { | ||
this.el.scenEl.addEventListener('deviceorientationpermissiongranted', function () { | ||
magicWindowControls.enabled = true; | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
|
||
update: function (oldData) { | ||
var data = this.data; | ||
|
||
|
@@ -208,9 +227,11 @@ module.exports.Component = registerComponent('look-controls', { | |
|
||
// In VR mode, THREE is in charge of updating the camera rotation. | ||
if (sceneEl.is('vr-mode') && sceneEl.checkHeadsetConnected()) { return; } | ||
// Calculate polyfilled HMD quaternion. | ||
this.polyfillControls.update(); | ||
hmdEuler.setFromQuaternion(this.polyfillObject.quaternion, 'YXZ'); | ||
// Calculate magic window HMD quaternion. | ||
if (this.magicWindowControls && this.magicWindowControls.enabled) { | ||
this.magicWindowControls.update(); | ||
hmdEuler.setFromQuaternion(this.magicWindowObject.quaternion, 'YXZ'); | ||
} | ||
|
||
// On mobile, do camera rotation with touch events and sensors. | ||
object3D.rotation.x = hmdEuler.x + pitchObject.rotation.x; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* @author richt / http://richt.me | ||
* @author WestLangley / http://github.com/WestLangley | ||
* | ||
* W3C Device Orientation control (http://w3c.github.io/deviceorientation/spec-source-orientation.html) | ||
*/ | ||
|
||
THREE.DeviceOrientationControls = function ( object ) { | ||
|
||
var scope = this; | ||
|
||
this.object = object; | ||
this.object.rotation.reorder( 'YXZ' ); | ||
|
||
this.enabled = true; | ||
|
||
this.deviceOrientation = {}; | ||
this.screenOrientation = 0; | ||
|
||
this.alphaOffset = 0; // radians | ||
|
||
var onDeviceOrientationChangeEvent = function ( event ) { | ||
|
||
scope.deviceOrientation = event; | ||
|
||
}; | ||
|
||
var onScreenOrientationChangeEvent = function () { | ||
|
||
scope.screenOrientation = window.orientation || 0; | ||
|
||
}; | ||
|
||
// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y'' | ||
|
||
var setObjectQuaternion = function () { | ||
|
||
var zee = new THREE.Vector3( 0, 0, 1 ); | ||
|
||
var euler = new THREE.Euler(); | ||
|
||
var q0 = new THREE.Quaternion(); | ||
|
||
var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis | ||
|
||
return function ( quaternion, alpha, beta, gamma, orient ) { | ||
|
||
euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us | ||
|
||
quaternion.setFromEuler( euler ); // orient the device | ||
|
||
quaternion.multiply( q1 ); // camera looks out the back of the device, not the top | ||
|
||
quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) ); // adjust for screen orientation | ||
|
||
}; | ||
|
||
}(); | ||
|
||
this.connect = function () { | ||
|
||
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false ); | ||
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false ); | ||
|
||
scope.enabled = true; | ||
|
||
}; | ||
|
||
this.disconnect = function () { | ||
|
||
window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false ); | ||
window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false ); | ||
|
||
scope.enabled = false; | ||
|
||
}; | ||
|
||
this.update = function () { | ||
|
||
if ( scope.enabled === false ) return; | ||
|
||
var device = scope.deviceOrientation; | ||
|
||
if ( device ) { | ||
|
||
var alpha = device.alpha ? THREE.Math.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z | ||
|
||
var beta = device.beta ? THREE.Math.degToRad( device.beta ) : 0; // X' | ||
|
||
var gamma = device.gamma ? THREE.Math.degToRad( device.gamma ) : 0; // Y'' | ||
|
||
var orient = scope.screenOrientation ? THREE.Math.degToRad( scope.screenOrientation ) : 0; // O | ||
|
||
setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient ); | ||
|
||
} | ||
|
||
|
||
}; | ||
|
||
this.dispose = function () { | ||
|
||
scope.disconnect(); | ||
|
||
}; | ||
|
||
this.connect(); | ||
|
||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the condition is always false. How can requestPermission exist if DeviceOrientationEvent is undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!