-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControls.js
136 lines (83 loc) · 3.25 KB
/
Controls.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
import {
Euler,
EventDispatcher
} from 'three';
const _euler = new Euler( 0, 0, 0, 'YXZ' );
const _PI_2 = Math.PI / 2;
class Controls extends EventDispatcher {
constructor( camera, domElement ) {
super();
this.camera = camera;
this.domElement = domElement;
this.isActive = false;
this.minPolarAngle = 0;
this.maxPolarAngle = Math.PI;
this.pointerSpeed = Math.PI;
this._onMouseMove = onMouseMove.bind( this );
this._onTouchMove = onTouchMove.bind( this );
this._onMouseDown = onDown.bind( this );
this._onTouchStart = onDown.bind( this );
this._onMouseUp = onUp.bind( this );
this._onTouchEnd = onUp.bind( this );
this.connect();
}
connect() {
this.domElement.ownerDocument.addEventListener( 'mousemove', this._onMouseMove );
this.domElement.ownerDocument.addEventListener( 'touchmove', this._onTouchMove );
this.domElement.addEventListener('mousedown', this._onMouseDown );
this.domElement.addEventListener('touchstart', this._onTouchStart );
this.domElement.ownerDocument.addEventListener('mouseup', this._onMouseUp );
this.domElement.ownerDocument.addEventListener('touchend', this._onTouchEnd );
}
disconnect() {
this.domElement.ownerDocument.removeEventListener( 'mousemove', this._onMouseMove );
this.domElement.ownerDocument.removeEventListener( 'touchmove', this._onTouchMove );
this.domElement.removeEventListener('mousedown', this._onMouseDown );
this.domElement.removeEventListener('touchstart', this._onTouchStart );
this.domElement.ownerDocument.removeEventListener('mouseup', this._onMouseUp );
this.domElement.ownerDocument.removeEventListener('touchend', this._onTouchEnd );
}
dispose() {
this.disconnect();
}
getObject() {
return this.camera;
}
getDirection( v ) {
return v.set( 0, 0, - 1 ).applyQuaternion( this.camera.quaternion );
}
}
function onDown() {
this.touchStartX = event.touches ? event.touches[ 0 ].clientX : event.clientX;
this.touchStartY = event.touches ? event.touches[ 0 ].clientY : event.clientY;
this.isActive = true;
}
function onUp() {
this.isActive = false;
}
function onMouseMove( event ) {
if ( this.isActive === false ) return;
const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
const camera = this.camera;
_euler.setFromQuaternion( camera.quaternion );
_euler.y -= movementX * 0.002 * this.pointerSpeed;
_euler.x -= movementY * 0.002 * this.pointerSpeed;
_euler.x = Math.max( _PI_2 - this.maxPolarAngle, Math.min( _PI_2 - this.minPolarAngle, _euler.x ) );
camera.quaternion.setFromEuler( _euler );
}
function onTouchMove( event ) {
if ( this.isActive === false ) return;
const touch = event.touches[ 0 ];
const deltaX = touch.clientX - this.touchStartX;
const deltaY = touch.clientY - this.touchStartY;
this.touchStartX = touch.clientX;
this.touchStartY = touch.clientY;
const camera = this.camera;
_euler.setFromQuaternion( camera.quaternion );
_euler.y -= deltaX * 0.002 * this.pointerSpeed;
_euler.x -= deltaY * 0.002 * this.pointerSpeed;
_euler.x = Math.max( _PI_2 - this.maxPolarAngle, Math.min( _PI_2 - this.minPolarAngle, _euler.x ) );
camera.quaternion.setFromEuler( _euler );
}
export { Controls };