forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceOrientationCameraController.js
134 lines (111 loc) · 4.14 KB
/
DeviceOrientationCameraController.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
/*global define*/
define([
'../Core/defined',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/Math',
'../Core/Matrix3',
'../Core/Quaternion'
], function(
defined,
destroyObject,
DeveloperError,
CesiumMath,
Matrix3,
Quaternion) {
'use strict';
/**
* @private
*/
function DeviceOrientationCameraController(scene) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError('scene is required.');
}
//>>includeEnd('debug');
this._scene = scene;
this._lastAlpha = undefined;
this._lastBeta = undefined;
this._lastGamma = undefined;
this._alpha = undefined;
this._beta = undefined;
this._gamma = undefined;
var that = this;
function callback(e) {
var alpha = e.alpha;
if (!defined(alpha)) {
that._alpha = undefined;
that._beta = undefined;
that._gamma = undefined;
return;
}
that._alpha = CesiumMath.toRadians(alpha);
that._beta = CesiumMath.toRadians(e.beta);
that._gamma = CesiumMath.toRadians(e.gamma);
}
window.addEventListener('deviceorientation', callback, false);
this._removeListener = function() {
window.removeEventListener('deviceorientation', callback, false);
};
}
var scratchQuaternion1 = new Quaternion();
var scratchQuaternion2 = new Quaternion();
var scratchMatrix3 = new Matrix3();
function rotate(camera, alpha, beta, gamma) {
var direction = camera.direction;
var right = camera.right;
var up = camera.up;
var bQuat = Quaternion.fromAxisAngle(direction, beta, scratchQuaternion2);
var gQuat = Quaternion.fromAxisAngle(right, gamma, scratchQuaternion1);
var rotQuat = Quaternion.multiply(gQuat, bQuat, gQuat);
var aQuat = Quaternion.fromAxisAngle(up, alpha, scratchQuaternion2);
Quaternion.multiply(aQuat, rotQuat, rotQuat);
var matrix = Matrix3.fromQuaternion(rotQuat, scratchMatrix3);
Matrix3.multiplyByVector(matrix, right, right);
Matrix3.multiplyByVector(matrix, up, up);
Matrix3.multiplyByVector(matrix, direction, direction);
}
DeviceOrientationCameraController.prototype.update = function() {
if (!defined(this._alpha)) {
return;
}
if (!defined(this._lastAlpha)) {
this._lastAlpha = this._alpha;
this._lastBeta = this._beta;
this._lastGamma = this._gamma;
}
var a = this._lastAlpha - this._alpha;
var b = this._lastBeta - this._beta;
var g = this._lastGamma - this._gamma;
rotate(this._scene.camera, -a, b, g);
this._lastAlpha = this._alpha;
this._lastBeta = this._beta;
this._lastGamma = this._gamma;
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
*
* @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
*/
DeviceOrientationCameraController.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the resources held by this object. Destroying an object allows for deterministic
* release of resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @returns {undefined}
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*/
DeviceOrientationCameraController.prototype.destroy = function() {
this._removeListener();
return destroyObject(this);
};
return DeviceOrientationCameraController;
});