-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPointerLockControls.js
70 lines (41 loc) · 1.47 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
module.exports = (THREE) => {
function PointerLockControls(camera) {
var scope = this;
camera.rotation.set(0, 0, 0);
var pitchObject = new THREE.Object3D();
pitchObject.add(camera);
var yawObject = new THREE.Object3D();
yawObject.position.y = 10;
yawObject.add(pitchObject);
var PI_2 = Math.PI / 2;
var onMouseMove = function(event) {
if (scope.enabled === false) return;
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
yawObject.rotation.y -= movementX * 0.002;
pitchObject.rotation.x -= movementY * 0.002;
pitchObject.rotation.x = Math.max(-PI_2, Math.min(PI_2, pitchObject.rotation.x));
};
this.dispose = function() {
document.removeEventListener('mousemove', onMouseMove, false);
};
document.addEventListener('mousemove', onMouseMove, false);
this.enabled = false;
this.getObject = function() {
return yawObject;
};
this.getDirection = function() {
// assumes the camera itself is not rotated
var direction = new THREE.Vector3(0, 0, -1);
var rotation = new THREE.Euler(0, 0, 0, "YXZ");
return function(v) {
rotation.set(pitchObject.rotation.x, yawObject.rotation.y, 0);
v.copy(direction).applyEuler(rotation);
return v;
};
}();
}
return {
PointerLockControls
};
};