-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceOrientationControls.js
More file actions
193 lines (144 loc) · 5.61 KB
/
Copy pathDeviceOrientationControls.js
File metadata and controls
193 lines (144 loc) · 5.61 KB
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
/* globals THREE */
/**
* DeviceOrientationControls - applies device orientation on object rotation
*
* @param {Object} object - instance of THREE.Object3D
* @constructor
*
* @author richt / http://richt.me
* @author WestLangley / http://github.com/WestLangley
* @author jonobr1 / http://jonobr1.com
* @author arodic / http://aleksandarrodic.com
* @author doug / http://github.com/doug
*
* W3C Device Orientation control
* (http://w3c.github.io/deviceorientation/spec-source-orientation.html)
*/
var GdeviceOrientation; // global version for access in other code
(function() {
var deviceOrientation = { alpha:0.0, beta:90.0, gamma:90.0, absolute:false };
GdeviceOrientation = deviceOrientation;
var screenOrientation = window.orientation || 0;
function onDeviceOrientationChangeEvent(evt) {
if ((evt.alpha !== undefined) && (evt.alpha !== null)) {
deviceOrientation = evt;
GdeviceOrientation = deviceOrientation;
}
}
window.addEventListener('deviceorientation', onDeviceOrientationChangeEvent, false);
function getOrientation() {
switch (window.screen.orientation || window.screen.mozOrientation) {
case 'landscape-primary':
return 90;
case 'landscape-secondary':
return -90;
case 'portrait-secondary':
return 180;
case 'portrait-primary':
return 0;
}
// this returns 90 if width is greater then height
// and window orientation is undefined OR 0
// if (!window.orientation && window.innerWidth > window.innerHeight)
// return 90;
return window.orientation || 0;
}
function onScreenOrientationChangeEvent() {
screenOrientation = getOrientation();
}
window.addEventListener('orientationchange', onScreenOrientationChangeEvent, false);
THREE.DeviceOrientationControls = function(object) {
this.object = object;
this.object.rotation.reorder('YXZ');
this.freeze = true;
this.movementSpeed = 1.0;
this.rollSpeed = 0.005;
this.autoAlign = true;
this.autoForward = false;
this.alpha = 0.0;
this.alphaoffset = 0.0001; // added (non-zero to prevent mysterious jump when it gets added)
//this.alphalock = false; // added // didn't work. couldn't see why it was unstable
this.beta = 0;
this.gamma = 0;
this.orient = 0;
this.alignQuaternion = new THREE.Quaternion();
this.orientationQuaternion = new THREE.Quaternion();
var quaternion = new THREE.Quaternion();
var quaternionLerp = new THREE.Quaternion();
var tempVector3 = new THREE.Vector3();
var tempMatrix4 = new THREE.Matrix4();
var tempEuler = new THREE.Euler(0, 0, 0, 'YXZ');
var tempQuaternion = new THREE.Quaternion();
var zee = new THREE.Vector3(0, 0, 1);
var up = new THREE.Vector3(0, 1, 0);
var v0 = new THREE.Vector3(0, 0, 0);
var euler = new THREE.Euler();
var q0 = new THREE.Quaternion(); // - PI/2 around the x-axis
var q1 = new THREE.Quaternion(- Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));
this.update = (function(delta) {
return function(delta) {
if (this.freeze) return;
// should not need this
//var orientation = getOrientation();
//if (orientation !== this.screenOrientation) {
//this.screenOrientation = orientation;
//this.autoAlign = true;
//}
//if (this.alphalock && deviceOrientation.gamma)
// this.alphaoffset = THREE.Math.radToDeg(this.alpha) - deviceOrientation.alpha;
this.alpha = deviceOrientation.gamma ? THREE.Math.degToRad(deviceOrientation.alpha + this.alphaoffset) : 0; // Z
this.beta = deviceOrientation.beta ? THREE.Math.degToRad(deviceOrientation.beta) : 0; // X'
this.gamma = deviceOrientation.gamma ? THREE.Math.degToRad(deviceOrientation.gamma) : 0; // Y''
this.orient = screenOrientation ? THREE.Math.degToRad(screenOrientation) : 0; // O
// The angles alpha, beta and gamma
// form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
// 'ZXY' for the device, but 'YXZ' for us
euler.set(this.beta, this.alpha, - this.gamma, 'YXZ');
quaternion.setFromEuler(euler);
quaternionLerp.slerp(quaternion, 0.5); // interpolate
// orient the device
if (this.autoAlign) this.orientationQuaternion.copy(quaternion); // interpolation breaks the auto alignment
else this.orientationQuaternion.copy(quaternionLerp);
// camera looks out the back of the device, not the top
this.orientationQuaternion.multiply(q1);
// adjust for screen orientation
this.orientationQuaternion.multiply(q0.setFromAxisAngle(zee, - this.orient));
this.object.quaternion.copy(this.alignQuaternion);
this.object.quaternion.multiply(this.orientationQuaternion);
if (this.autoForward) {
tempVector3
.set(0, 0, -1)
.applyQuaternion(this.object.quaternion, 'ZXY')
.setLength(this.movementSpeed / 50); // TODO: why 50 :S
this.object.position.add(tempVector3);
}
if (this.autoAlign && this.alpha !== 0) {
this.autoAlign = false;
this.align();
}
};
})();
// //debug
// window.addEventListener('click', (function(){
// this.align();
// }).bind(this));
this.align = function() {
tempVector3
.set(0, 0, -1)
.applyQuaternion( tempQuaternion.copy(this.orientationQuaternion).inverse(), 'ZXY' );
tempEuler.setFromQuaternion(
tempQuaternion.setFromRotationMatrix(
tempMatrix4.lookAt(tempVector3, v0, up)
)
);
tempEuler.set(0, tempEuler.y, 0);
this.alignQuaternion.setFromEuler(tempEuler);
};
this.connect = function() {
this.freeze = false;
};
this.disconnect = function() {
this.freeze = true;
};
};
})();