-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceOrientationControls.js
156 lines (89 loc) · 3.96 KB
/
DeviceOrientationControls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* @author ThreeJS Authors / https://github.com/mrdoob/three.js
* @author crazyh / https://github.com/crazyh2
*/
( function () {
const _zee = new THREE.Vector3( 0, 0, 1 );
const _euler = new THREE.Euler();
const _q0 = new THREE.Quaternion();
const _q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
const _changeEvent = {
type: 'change'
};
class DeviceOrientationControls extends THREE.EventDispatcher {
constructor( object ) {
super();
if ( window.isSecureContext === false ) {
console.error( 'THREE.DeviceOrientationControls: DeviceOrientationEvent is only available in secure contexts (https)' );
}
const scope = this;
const EPS = 0.000001;
const lastQuaternion = new THREE.Quaternion();
this.object = object;
this.object.rotation.reorder( 'YXZ' );
this.enabled = true;
this.deviceOrientation = {};
this.screenOrientation = 0;
this.alphaOffset = 0; // radians
const onDeviceOrientationChangeEvent = function ( event ) {
scope.deviceOrientation = event;
};
const 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''
const setObjectQuaternion = 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 () {
onScreenOrientationChangeEvent(); // run once on load
// iOS 13+
if ( window.DeviceOrientationEvent && typeof(window.DeviceOrientationEvent.requestPermission) === "function" ) {
const permissionPrompt = function() {
window.DeviceOrientationEvent.requestPermission().then( function ( response ) {
if ( response == 'granted' ) {
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
}
} ).catch( function ( error ) {
console.error( 'THREE.DeviceOrientationControls: Unable to use DeviceOrientation API:', error );
} );
document.body.removeEventListener("click", permissionPrompt);
};
document.body.addEventListener("click", permissionPrompt);
} else {
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
}
scope.enabled = true;
};
this.disconnect = function () {
window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent );
window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
scope.enabled = false;
};
this.update = function () {
if ( scope.enabled === false ) return;
const device = scope.deviceOrientation;
if ( device ) {
const alpha = device.alpha ? THREE.Math.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z
const beta = device.beta ? THREE.Math.degToRad( device.beta ) : 0; // X'
const gamma = device.gamma ? THREE.Math.degToRad( device.gamma ) : 0; // Y''
const orient = scope.screenOrientation ? THREE.Math.degToRad( scope.screenOrientation ) : 0; // O
setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );
if ( 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
lastQuaternion.copy( scope.object.quaternion );
scope.dispatchEvent( _changeEvent );
}
}
};
this.dispose = function () {
scope.disconnect();
};
this.connect();
}
}
THREE.DeviceOrientationControls = DeviceOrientationControls;
} )();