-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathdevice-orientation-controls.js
289 lines (245 loc) · 8.83 KB
/
device-orientation-controls.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Modified version of THREE.DeviceOrientationControls from three.js
// will use the deviceorientationabsolute event if available
import { Euler, EventDispatcher, MathUtils, Quaternion, Vector3 } from "three";
const isIOS = navigator.userAgent.match(/iPhone|iPad|iPod/i);
const _zee = new Vector3(0, 0, 1);
const _euler = new Euler();
const _q0 = new Quaternion();
const _q1 = new Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5)); // - PI/2 around the x-axis
const _changeEvent = { type: "change" };
class DeviceOrientationControls extends 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 Quaternion();
this.object = object;
this.object.rotation.reorder("YXZ");
this.enabled = true;
this.deviceOrientation = null;
this.screenOrientation = 0;
this.alphaOffset = 0; // radians
this.initialOffset = null; // used in fix provided in issue #466, iOS related
this.TWO_PI = 2 * Math.PI;
this.HALF_PI = 0.5 * Math.PI;
this.orientationChangeEventName =
"ondeviceorientationabsolute" in window
? "deviceorientationabsolute"
: "deviceorientation";
this.smoothingFactor = 1;
const onDeviceOrientationChangeEvent = function ({
alpha,
beta,
gamma,
webkitCompassHeading,
}) {
if (isIOS) {
const ccwNorthHeading = 360 - webkitCompassHeading;
scope.alphaOffset = MathUtils.degToRad(ccwNorthHeading - alpha);
scope.deviceOrientation = { alpha, beta, gamma, webkitCompassHeading };
} else {
if (alpha < 0) alpha += 360;
scope.deviceOrientation = { alpha, beta, gamma };
}
window.dispatchEvent(
new CustomEvent("camera-rotation-change", {
detail: { cameraRotation: object.rotation },
}),
);
};
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 !== undefined &&
typeof window.DeviceOrientationEvent.requestPermission === "function"
) {
window.DeviceOrientationEvent.requestPermission()
.then((response) => {
if (response === "granted") {
window.addEventListener(
"orientationchange",
onScreenOrientationChangeEvent,
);
window.addEventListener(
scope.orientationChangeEventName,
onDeviceOrientationChangeEvent,
);
}
})
.catch(function (error) {
console.error(
"THREE.DeviceOrientationControls: Unable to use DeviceOrientation API:",
error,
);
});
} else {
window.addEventListener(
"orientationchange",
onScreenOrientationChangeEvent,
);
window.addEventListener(
scope.orientationChangeEventName,
onDeviceOrientationChangeEvent,
);
}
scope.enabled = true;
};
this.disconnect = function () {
window.removeEventListener(
"orientationchange",
onScreenOrientationChangeEvent,
);
window.removeEventListener(
scope.orientationChangeEventName,
onDeviceOrientationChangeEvent,
);
scope.enabled = false;
scope.initialOffset = false;
scope.deviceOrientation = null;
};
this.update = function ({ theta = 0 } = { theta: 0 }) {
if (scope.enabled === false) return;
const device = scope.deviceOrientation;
if (device) {
let alpha = device.alpha
? MathUtils.degToRad(device.alpha) + scope.alphaOffset
: 0; // Z
let beta = device.beta ? MathUtils.degToRad(device.beta) : 0; // X'
let gamma = device.gamma ? MathUtils.degToRad(device.gamma) : 0; // Y''
const orient = scope.screenOrientation
? MathUtils.degToRad(scope.screenOrientation)
: 0; // O
if (isIOS) {
const currentQuaternion = new Quaternion();
setObjectQuaternion(currentQuaternion, alpha, beta, gamma, orient);
// Extract the Euler angles from the quaternion and add the heading angle to the Y-axis rotation of the Euler angles
// (If we replace only the alpha value of the quaternion without using Euler angles, the camera will rotate unexpectedly. This is because a quaternion does not represent rotation values individually but rather through a combination of rotation axes and weights.)
const currentEuler = new Euler().setFromQuaternion(
currentQuaternion,
"YXZ",
);
console.log(currentEuler.x, currentEuler.y, currentEuler.z);
// Replace the current alpha value of the Euler angles and reset the quaternion
currentEuler.y = MathUtils.degToRad(
360 - device.webkitCompassHeading,
);
currentQuaternion.setFromEuler(currentEuler);
scope.object.quaternion.copy(currentQuaternion);
} else {
if (this.smoothingFactor < 1) {
if (this.lastOrientation) {
const k = this.smoothingFactor;
alpha = this._getSmoothedAngle(
alpha,
this.lastOrientation.alpha,
k,
);
beta = this._getSmoothedAngle(
beta + Math.PI,
this.lastOrientation.beta,
k,
);
gamma = this._getSmoothedAngle(
gamma + this.HALF_PI,
this.lastOrientation.gamma,
k,
Math.PI,
);
} else {
beta += Math.PI;
gamma += this.HALF_PI;
}
this.lastOrientation = {
alpha,
beta,
gamma,
};
}
setObjectQuaternion(
scope.object.quaternion,
alpha + theta,
this.smoothingFactor < 1 ? beta - Math.PI : beta,
this.smoothingFactor < 1 ? gamma - this.HALF_PI : gamma,
orient,
);
}
// NB - NOT present in IOS fixed version issue #466
// Is it needed?
if (8 * (1 - lastQuaternion.dot(scope.object.quaternion)) > EPS) {
lastQuaternion.copy(scope.object.quaternion);
scope.dispatchEvent(_changeEvent);
}
}
};
// NW Added
this._orderAngle = function (a, b, range = this.TWO_PI) {
if (
(b > a && Math.abs(b - a) < range / 2) ||
(a > b && Math.abs(b - a) > range / 2)
) {
return { left: a, right: b };
} else {
return { left: b, right: a };
}
};
// NW Added
this._getSmoothedAngle = function (a, b, k, range = this.TWO_PI) {
const angles = this._orderAngle(a, b, range);
const angleshift = angles.left;
const origAnglesRight = angles.right;
angles.left = 0;
angles.right -= angleshift;
if (angles.right < 0) angles.right += range;
let newangle =
origAnglesRight == b
? (1 - k) * angles.right + k * angles.left
: k * angles.right + (1 - k) * angles.left;
newangle += angleshift;
if (newangle >= range) newangle -= range;
return newangle;
};
// Provided in fix on issue #466 - iOS related
this.updateAlphaOffset = function () {
scope.initialOffset = false;
};
this.dispose = function () {
scope.disconnect();
};
// provided with fix on issue #466
this.getAlpha = function () {
const { deviceOrientation: device } = scope;
return device && device.alpha
? MathUtils.degToRad(device.alpha) + scope.alphaOffset
: 0;
};
// provided with fix on issue #466
this.getBeta = function () {
const { deviceOrientation: device } = scope;
return device && device.beta ? MathUtils.degToRad(device.beta) : 0;
};
this.connect();
}
}
export { DeviceOrientationControls };