This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
DeviceOrientationControls.js
188 lines (113 loc) · 5.48 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
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
import * as THREE from 'three';
/**
* @classdesc Device Orientation Control
* @constructor
* @external DeviceOrientationControls
* @param {THREE.Camera} camera
* @param {HTMLElement} domElement
*/
function DeviceOrientationControls ( camera, domElement ) {
var scope = this;
var changeEvent = { type: 'change' };
var rotY = 0;
var rotX = 0;
var tempX = 0;
var tempY = 0;
this.camera = camera;
this.camera.rotation.reorder( 'YXZ' );
this.domElement = ( domElement !== undefined ) ? domElement : document;
this.enabled = true;
this.deviceOrientation = {};
this.screenOrientation = 0;
this.alpha = 0;
this.alphaOffsetAngle = 0;
var onDeviceOrientationChangeEvent = function( event ) {
scope.deviceOrientation = event;
};
var onScreenOrientationChangeEvent = function() {
scope.screenOrientation = window.orientation || 0;
};
var onTouchStartEvent = function (event) {
event.preventDefault();
event.stopPropagation();
tempX = event.touches[ 0 ].pageX;
tempY = event.touches[ 0 ].pageY;
};
var onTouchMoveEvent = function (event) {
event.preventDefault();
event.stopPropagation();
rotY += THREE.Math.degToRad( ( event.touches[ 0 ].pageX - tempX ) / 4 );
rotX += THREE.Math.degToRad( ( tempY - event.touches[ 0 ].pageY ) / 4 );
scope.updateAlphaOffsetAngle( rotY );
tempX = event.touches[ 0 ].pageX;
tempY = event.touches[ 0 ].pageY;
};
// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
var setCameraQuaternion = function( quaternion, alpha, beta, gamma, orient ) {
var zee = new THREE.Vector3( 0, 0, 1 );
var euler = new THREE.Euler();
var q0 = new THREE.Quaternion();
var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
var vectorFingerY;
var fingerQY = new THREE.Quaternion();
var fingerQX = new THREE.Quaternion();
if ( scope.screenOrientation == 0 ) {
vectorFingerY = new THREE.Vector3( 1, 0, 0 );
fingerQY.setFromAxisAngle( vectorFingerY, -rotX );
} else if ( scope.screenOrientation == 180 ) {
vectorFingerY = new THREE.Vector3( 1, 0, 0 );
fingerQY.setFromAxisAngle( vectorFingerY, rotX );
} else if ( scope.screenOrientation == 90 ) {
vectorFingerY = new THREE.Vector3( 0, 1, 0 );
fingerQY.setFromAxisAngle( vectorFingerY, rotX );
} else if ( scope.screenOrientation == - 90) {
vectorFingerY = new THREE.Vector3( 0, 1, 0 );
fingerQY.setFromAxisAngle( vectorFingerY, -rotX );
}
q1.multiply( fingerQY );
q1.multiply( fingerQX );
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
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, { passive: true } );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, { passive: true } );
window.addEventListener( 'deviceorientation', this.update.bind( this ), { passive: true } );
scope.domElement.addEventListener( 'touchstart', onTouchStartEvent, { passive: false } );
scope.domElement.addEventListener( 'touchmove', onTouchMoveEvent, { passive: false } );
scope.enabled = true;
};
this.disconnect = function() {
window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
window.removeEventListener( 'deviceorientation', this.update.bind( this ), false );
scope.domElement.removeEventListener( 'touchstart', onTouchStartEvent, false );
scope.domElement.removeEventListener( 'touchmove', onTouchMoveEvent, false );
scope.enabled = false;
};
this.update = function( ignoreUpdate ) {
if ( scope.enabled === false ) return;
var alpha = scope.deviceOrientation.alpha ? THREE.Math.degToRad( scope.deviceOrientation.alpha ) + scope.alphaOffsetAngle : 0; // Z
var beta = scope.deviceOrientation.beta ? THREE.Math.degToRad( scope.deviceOrientation.beta ) : 0; // X'
var gamma = scope.deviceOrientation.gamma ? THREE.Math.degToRad( scope.deviceOrientation.gamma ) : 0; // Y''
var orient = scope.screenOrientation ? THREE.Math.degToRad( scope.screenOrientation ) : 0; // O
setCameraQuaternion( scope.camera.quaternion, alpha, beta, gamma, orient );
scope.alpha = alpha;
if ( ignoreUpdate !== true ) { scope.dispatchEvent( changeEvent ); }
};
this.updateAlphaOffsetAngle = function( angle ) {
this.alphaOffsetAngle = angle;
this.update();
};
this.dispose = function() {
this.disconnect();
};
this.connect();
};
DeviceOrientationControls.prototype = Object.assign( Object.create( THREE.EventDispatcher.prototype), {
constructor: DeviceOrientationControls
} );
export { DeviceOrientationControls };