-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointerLockControls.js
182 lines (151 loc) · 5.1 KB
/
PointerLockControls.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
/**
* Fork of the default three.js PointerLockControls. This fork differs in that
* it allows using the mouse as an analog device (for example, to control
* things like thrust) without dictating it be used only as a camera mover.
*/
import { Euler, EventDispatcher, Quaternion, Vector3 } from 'three';
const lockModes = {
// Mouse does not cause the camera to move in this mode.
// TODO: currently shows visuals too, please visuals move elsewhere.
frozen: 2,
// Can look freely in all directions without restriction.
freeLook: 4,
// Can look 110 degrees from origin before mouse stops moving.
headLook: 8,
};
const PointerLockControls = function (camera, domElement, onMouseCb) {
if (domElement === undefined) {
console.warn(
'SpaceJunkie PointerLockControls: The second parameter "domElement" is now mandatory.'
);
domElement = document.body;
}
if (!onMouseCb) {
onMouseCb = () => {};
}
this.domElement = domElement;
// If true, the browser will hide the cursor.
this.isPointerLocked = false;
this.lockMode = lockModes.freeLook;
// Set to constrain the pitch of the camera
// Range is 0 to Math.PI radians
this.minPolarAngle = 0; // radians
this.maxPolarAngle = Math.PI; // radians
// Constrain pitch in headlook mode.
this.headXMax = 1565;
this.headYMax = 1110;
// 0 here actually means center of the screen.
this.mouseX = 0;
this.mouseY = 0;
//
// internals
//
const scope = this;
const changeEvent = {type: 'change'};
const lockEvent = {type: 'lock'};
const unlockEvent = {type: 'unlock'};
const euler = new Euler(0, 0, 0, 'YXZ');
const PI_2 = Math.PI / 2;
function onMouseMove(event) {
if (scope.isPointerLocked === false) {
return;
}
// Headlook mode. Used when the camera is locked to i.e. the bridge cam.
const mx = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
const my = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
if (scope.lockMode === lockModes.headLook) {
// Limit how far the player can turn their necks.
if (Math.abs(scope.mouseX + mx) < scope.headXMax) {
scope.mouseX += mx;
}
if (Math.abs(scope.mouseY + my) < scope.headYMax) {
scope.mouseY += my;
}
}
else {
// Allow fracturing of vertebrae.
scope.mouseX += mx;
scope.mouseY += my;
}
scope.dispatchEvent(changeEvent);
}
function onPointerlockChange() {
if (scope.domElement.ownerDocument.pointerLockElement === scope.domElement) {
scope.dispatchEvent(lockEvent);
scope.isPointerLocked = true;
} else {
scope.dispatchEvent(unlockEvent);
scope.isPointerLocked = false;
}
}
function onPointerlockError() {
console.error('THREE.PointerLockControls: Unable to use Pointer Lock API');
}
this.connect = function () {
scope.domElement.ownerDocument.addEventListener('mousemove', onMouseMove, false);
scope.domElement.ownerDocument.addEventListener('pointerlockchange', onPointerlockChange, false);
scope.domElement.ownerDocument.addEventListener('pointerlockerror', onPointerlockError, false);
};
this.disconnect = function () {
scope.domElement.ownerDocument.removeEventListener('mousemove', onMouseMove, false);
scope.domElement.ownerDocument.removeEventListener('pointerlockchange', onPointerlockChange, false);
scope.domElement.ownerDocument.removeEventListener('pointerlockerror', onPointerlockError, false);
};
this.dispose = function () {
this.disconnect();
};
this.getObject = function () { // retaining this method for backward compatibility
return camera;
};
// Locks the mouse pointer.
this.lock = function () {
this.domElement.requestPointerLock();
};
// Unlocks the mouse pointer.
this.unlock = function () {
scope.domElement.ownerDocument.exitPointerLock();
};
// Locks or unlocks the mouse pointer.
this.toggle = function () {
if (this.isPointerLocked) {
this.unlock();
} else {
this.lock();
}
};
this.getLockMode = function() {
return this.lockMode;
}
// Sets the lock mode and undoes any external quaternion references.
this.setLockMode = function(mode) {
this.lockMode = mode;
};
// Updates camera angle relative to parent.
this.updateOrientation = function () {
let x = scope.mouseX;
let y = scope.mouseY;
if (scope.lockMode === lockModes.frozen) {
// This is intentional - only want mouse to fire if ptr lock isn't using
// it (i.e. frozen).
onMouseCb(x, y);
x = y = 0;
}
euler.setFromQuaternion(camera.quaternion);
euler.y = x * -0.002;
euler.x = y * -0.002;
euler.x = Math.max(PI_2 - scope.maxPolarAngle, Math.min(PI_2 - scope.minPolarAngle, euler.x));
camera.quaternion.setFromEuler(euler);
}
// Sets mouse to center of screen.
this.resetMouse = function() {
this.mouseX = 0;
this.mouseY = 0;
}
this.connect();
};
PointerLockControls.prototype = Object.create( EventDispatcher.prototype );
PointerLockControls.prototype.constructor = PointerLockControls;
export {
PointerLockControls,
lockModes,
};