|
| 1 | +// Modified version of THREE.DeviceOrientationControls from three.js |
| 2 | +// will use the deviceorientationabsolute event if available |
| 3 | + |
| 4 | +import { |
| 5 | + Euler, |
| 6 | + EventDispatcher, |
| 7 | + MathUtils, |
| 8 | + Quaternion, |
| 9 | + Vector3 |
| 10 | +} from 'three'; |
| 11 | + |
| 12 | +const _zee = new Vector3( 0, 0, 1 ); |
| 13 | +const _euler = new Euler(); |
| 14 | +const _q0 = new Quaternion(); |
| 15 | +const _q1 = new Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis |
| 16 | + |
| 17 | +const _changeEvent = { type: 'change' }; |
| 18 | + |
| 19 | +class DeviceOrientationControls extends EventDispatcher { |
| 20 | + |
| 21 | + constructor( object ) { |
| 22 | + |
| 23 | + super(); |
| 24 | + |
| 25 | + if ( window.isSecureContext === false ) { |
| 26 | + |
| 27 | + console.error( 'THREE.DeviceOrientationControls: DeviceOrientationEvent is only available in secure contexts (https)' ); |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + const scope = this; |
| 32 | + |
| 33 | + const EPS = 0.000001; |
| 34 | + const lastQuaternion = new Quaternion(); |
| 35 | + |
| 36 | + this.object = object; |
| 37 | + this.object.rotation.reorder( 'YXZ' ); |
| 38 | + |
| 39 | + this.enabled = true; |
| 40 | + |
| 41 | + this.deviceOrientation = {}; |
| 42 | + this.screenOrientation = 0; |
| 43 | + |
| 44 | + this.alphaOffset = 0; // radians |
| 45 | + |
| 46 | + this.orientationChangeEventName = 'ondeviceorientationabsolute' in window ? 'deviceorientationabsolute' : 'deviceorientation'; |
| 47 | + |
| 48 | + const onDeviceOrientationChangeEvent = function ( event ) { |
| 49 | + |
| 50 | + scope.deviceOrientation = event; |
| 51 | + |
| 52 | + }; |
| 53 | + |
| 54 | + const onScreenOrientationChangeEvent = function () { |
| 55 | + |
| 56 | + scope.screenOrientation = window.orientation || 0; |
| 57 | + |
| 58 | + }; |
| 59 | + |
| 60 | + // The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y'' |
| 61 | + |
| 62 | + const setObjectQuaternion = function ( quaternion, alpha, beta, gamma, orient ) { |
| 63 | + |
| 64 | + _euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us |
| 65 | + |
| 66 | + quaternion.setFromEuler( _euler ); // orient the device |
| 67 | + |
| 68 | + quaternion.multiply( _q1 ); // camera looks out the back of the device, not the top |
| 69 | + |
| 70 | + quaternion.multiply( _q0.setFromAxisAngle( _zee, - orient ) ); // adjust for screen orientation |
| 71 | + |
| 72 | + }; |
| 73 | + |
| 74 | + this.connect = function () { |
| 75 | + |
| 76 | + onScreenOrientationChangeEvent(); // run once on load |
| 77 | + |
| 78 | + // iOS 13+ |
| 79 | + |
| 80 | + if ( window.DeviceOrientationEvent !== undefined && typeof window.DeviceOrientationEvent.requestPermission === 'function' ) { |
| 81 | + |
| 82 | + window.DeviceOrientationEvent.requestPermission().then( function ( response ) { |
| 83 | + |
| 84 | + if ( response == 'granted' ) { |
| 85 | + |
| 86 | + window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent ); |
| 87 | + window.addEventListener( this.orientationChangeEventName, onDeviceOrientationChangeEvent ); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + } ).catch( function ( error ) { |
| 92 | + |
| 93 | + console.error( 'THREE.DeviceOrientationControls: Unable to use DeviceOrientation API:', error ); |
| 94 | + |
| 95 | + } ); |
| 96 | + |
| 97 | + } else { |
| 98 | + |
| 99 | + window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent ); |
| 100 | + window.addEventListener( this.orientationChangeEventName, onDeviceOrientationChangeEvent ); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + scope.enabled = true; |
| 105 | + |
| 106 | + }; |
| 107 | + |
| 108 | + this.disconnect = function () { |
| 109 | + |
| 110 | + window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent ); |
| 111 | + window.removeEventListener( this.orientationChangeEventName , onDeviceOrientationChangeEvent ); |
| 112 | + |
| 113 | + scope.enabled = false; |
| 114 | + |
| 115 | + }; |
| 116 | + |
| 117 | + this.update = function () { |
| 118 | + |
| 119 | + if ( scope.enabled === false ) return; |
| 120 | + |
| 121 | + const device = scope.deviceOrientation; |
| 122 | + |
| 123 | + if ( device ) { |
| 124 | + |
| 125 | + const alpha = device.alpha ? MathUtils.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z |
| 126 | + |
| 127 | + const beta = device.beta ? MathUtils.degToRad( device.beta ) : 0; // X' |
| 128 | + |
| 129 | + const gamma = device.gamma ? MathUtils.degToRad( device.gamma ) : 0; // Y'' |
| 130 | + |
| 131 | + const orient = scope.screenOrientation ? MathUtils.degToRad( scope.screenOrientation ) : 0; // O |
| 132 | + |
| 133 | + setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient ); |
| 134 | + |
| 135 | + if ( 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) { |
| 136 | + |
| 137 | + lastQuaternion.copy( scope.object.quaternion ); |
| 138 | + scope.dispatchEvent( _changeEvent ); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + }; |
| 145 | + |
| 146 | + this.dispose = function () { |
| 147 | + |
| 148 | + scope.disconnect(); |
| 149 | + |
| 150 | + }; |
| 151 | + |
| 152 | + this.connect(); |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | +export { DeviceOrientationControls }; |
0 commit comments