-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointerlockcontrolsMobile.js
202 lines (180 loc) · 5.82 KB
/
PointerlockcontrolsMobile.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
import {
Euler,
EventDispatcher,
Vector3
} from "https://cdn.skypack.dev/[email protected]/build/three.module";
var mobile
mobile = false;
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
var ww = document.body.clientWidth / 2;
var wh = document.body.clientHeight / 2;
var array_x = []
var array_y = []
var touches_x, touches_y;
var last_x, last_y;
var eulerX_angle = [].map(Number)
var eulerY_angle = [].map(Number)
var eulerY, eulerX;
var eulerY_final = 0;
var eulerX_final = 0;
var eulerY_total, eulerX_total;
var euler = new Euler(0, 0, 0, 'YXZ');
var PI_2 = Math.PI / 2;
var PI_2y = 2 * (Math.PI);
var vec = new Vector3();
var PointerLockControls = function (camera, domElement) {
if (isMobile) {
mobile = true;
} else {
mobile = false;
}
if (domElement === undefined) {
console.warn('THREE.PointerLockControls: The second parameter "domElement" is now mandatory.');
domElement = document.body;
}
this.domElement = domElement;
this.isLocked = false;
this.minPolarAngle = 0; // radians
this.maxPolarAngle = 2 * (Math.PI); // radians
var scope = this;
var changeEvent = {
type: 'change'
};
var lockEvent = {
type: 'lock'
};
var unlockEvent = {
type: 'unlock'
};
//Function for getting arrays fires on touchmove
function arrayTouches(e) {
// array for touches
touches_x = e.changedTouches[0].clientX - ww
touches_y = e.changedTouches[0].clientY - wh
array_x.push(touches_x)
array_y.push(touches_y)
last_x = array_x[array_x.length - 2];
last_y = array_y[array_y.length - 2];
// array for euler
eulerY = (touches_x - last_x) * 0.004;
eulerX = (touches_y - last_y) * 0.004;
if (eulerY) {
eulerY_angle.push(eulerY)
eulerX_angle.push(eulerX)
} else {
eulerY_angle.push(0);
eulerX_angle.push(0);
}
scope.dispatchEvent(changeEvent);
}
//Function for set rotation from set arrays fires on touchmove
function onTouchMove(e) {
euler.setFromQuaternion(camera.rotation);
eulerY_total = eulerY_angle.reduce(function (sum, element) {
return sum + element;
}, 0);
eulerX_total = eulerX_angle.reduce(function (sum, element) {
return sum + element;
}, 0);
euler.y = eulerY_final + eulerY_total
euler.x = eulerX_final + eulerX_total
camera.quaternion.setFromEuler(euler);
scope.dispatchEvent(changeEvent);
};
// the delta value of euler and touchmove should be offset for recalibration from where the last move stopped
function onTouchEnd(e) {
eulerY_final = euler.y;
eulerX_final = euler.x;
eulerY_angle = [];
eulerX_angle = [];
array_x = [];
array_y = [];
scope.dispatchEvent(changeEvent);
}
// mouseevent
function onMouseMove(event) {
if (scope.isLocked === false) return;
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
euler.setFromQuaternion(camera.quaternion);
euler.y -= movementX * 0.002;
euler.x -= movementY * 0.002;
euler.x = Math.max(PI_2 - scope.maxPolarAngle, Math.min(PI_2 - scope.minPolarAngle, euler.x));
euler.y = Math.max(-PI_2y, Math.min(PI_2y, euler.y));
camera.quaternion.setFromEuler(euler);
scope.dispatchEvent(changeEvent);
}
function onPointerlockChange() {
if (document.pointerLockElement === scope.domElement) {
scope.dispatchEvent(lockEvent);
scope.isLocked = true;
} else {
scope.dispatchEvent(unlockEvent);
scope.isLocked = false;
}
}
function onPointerlockError() {
console.error('THREE.PointerLockControls: Unable to use Pointer Lock API');
}
this.connect = function () {
document.addEventListener('mousemove', onMouseMove, false);
document.addEventListener('touchmove', onTouchMove, false);
document.addEventListener('touchmove', arrayTouches, false);
document.addEventListener('touchend', onTouchEnd, false);
document.addEventListener('pointerlockchange', onPointerlockChange, false);
document.addEventListener('pointerlockerror', onPointerlockError, false);
};
this.disconnect = function () {
document.removeEventListener('mousemove', onMouseMove, false);
document.removeEventListener('touchmove', arrayTouches, false);
document.removeEventListener('touchend', onTouchEnd, false);
document.removeEventListener('touchmove', onTouchMove, false);
document.removeEventListener('pointerlockchange', onPointerlockChange, false);
document.removeEventListener('pointerlockerror', onPointerlockError, false);
};
this.dispose = function () {
this.disconnect();
};
this.getObject = function () { // retaining this method for backward compatibility
return camera;
};
this.getDirection = function () {
var direction = new Vector3(0, 0, -1);
return function (v) {
return v.copy(direction).applyQuaternion(camera.quaternion);
};
}();
this.moveForward = function (distance) {
vec.setFromMatrixColumn(camera.matrix, 0);
vec.crossVectors(camera.up, vec);
camera.position.addScaledVector(vec, distance);
};
this.moveBackward = function (distance) {
vec.setFromMatrixColumn(camera.matrix, 0);
vec.crossVectors(camera.up, vec);
camera.position.addScaledVector(vec, distance);
};
this.moveRight = function (distance) {
vec.setFromMatrixColumn(camera.matrix, 0);
camera.position.addScaledVector(vec, distance);
};
if (mobile == true) {
this.lock = function () {
scope.isLocked = true;
this.connect();
};
} else {
this.lock = function () {
this.domElement.requestPointerLock();
};
}
this.unlock = function () {
document.exitPointerLock();
};
this.connect();
};
PointerLockControls.prototype = Object.create(EventDispatcher.prototype);
PointerLockControls.prototype.constructor = PointerLockControls;
export {
PointerLockControls
};