forked from arkin0x/ONOSENDAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstPersonControls.js
489 lines (325 loc) · 10.3 KB
/
FirstPersonControls.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// I stole this from Three.js and overrode it to make it work nicer.
import {
MathUtils,
Spherical,
Vector3,
Vector2
} from 'three';
const _lookDirection = new Vector3();
const _spherical = new Spherical();
const _target = new Vector3();
const NON_DRAG_DISTANCE = 5
class FirstPersonControls {
constructor(object, domElement) {
this.object = object;
this.domElement = domElement;
// API
this.enabled = true;
this.mobile = false;
// base acceleration
this.accel = 1
// increases while a button is held down
this.accelMultiplier = 0
this.decel = this.mobile ? 0.999 : 0.90
this.minx = 0.1
this.dx = 0
this.dy = 0
this.dz = 0
this.lookSpeed = 0.005;
this.lookVertical = true;
this.heightSpeed = false;
this.heightCoef = 1.0;
this.heightMin = 0.0;
this.heightMax = 1.0;
this.constrainVertical = false;
this.verticalMin = 0;
this.verticalMax = Math.PI;
// internals
this.autoSpeedFactor = 0.0;
// touches
let touchPoints = 0 // incremented or decremented on touchstart/touchend
// this.mouse is used to provide mouse coords outside this object
this.mouse = new Vector2(0, 0)
this.mouseDownThisFrame = false
this.mouseDown = false
this.dragging = false;
this.mouseUpThisFrame = false
this.mouseXinitial = 0
this.mouseYinitial = 0
this.mouseXdelta = 0
this.mouseYdelta = 0
this.deltaScalar = 1
this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
this.moveRight = false;
this.viewHalfX = 0;
this.viewHalfY = 0;
this.cycle = 0
this.resetCycle = false
// private variables
let lat = 0;
let lon = 0;
//
function logToPanel(data) {
let ccs = document.getElementById('aug-ccs')
ccs.textContent = data
}
this.handleResize = function () {
if (this.domElement === document) {
this.viewHalfX = window.innerWidth / 2;
this.viewHalfY = window.innerHeight / 2;
} else {
this.viewHalfX = this.domElement.offsetWidth / 2;
this.viewHalfY = this.domElement.offsetHeight / 2;
}
};
this.relativeCenter = function (x, y) {
let xy = {
x: x - this.viewHalfX,
y: y - this.viewHalfY,
}
return xy
}
this.onMouseDown = function (event) {
if (this.domElement !== document) {
this.domElement.focus();
}
this.mouseDownThisFrame = true
this.mouseDown = true
let { x, y } = this.relativeCenter(event.pageX, event.pageY)
this.startDrag(x, y)
};
this.startDrag = function (x, y) {
this.mouseXinitial = x
this.mouseYinitial = y
this.mouseXdelta = this.mouseYdelta = 0
}
this.onMouseUp = function (event) {
this.mouseUpThisFrame = true
this.mouseDown = false
this.endDrag()
};
this.endDrag = function () {
this.mouseXinitial = 0
this.mouseYinitial = 0
this.mouseXdelta = this.mouseYdelta = 0
this.dragging = false
}
this.onMouseMove = function (event) {
// touches override so we don't conflict on devices that treat touches as mouse
if (touchPoints) return
// report mouse for external use
this.mouse.x = event.pageX
this.mouse.y = event.pageY
if (this.mouseDown) {
let { x, y } = this.relativeCenter(event.pageX, event.pageY)
this.drag(x, y)
}
};
this.drag = function (x, y) {
if (Math.abs(this.mouseXinitial - x) < NON_DRAG_DISTANCE && Math.abs(this.mouseYinitial - y) < NON_DRAG_DISTANCE) {
return
}
this.dragging = true
this.mouseXdelta = x - this.mouseXinitial
this.mouseYdelta = y - this.mouseYinitial
}
this.onFingerDown = function (event) {
// if we weren't mobile already, we are now
this.mobile = true
touchPoints++
if (touchPoints === 1) {
let tx = event.touches[0].pageX
let ty = event.touches[0].pageY
// report mouse for external use
this.mouse.x = tx
this.mouse.y = ty
let { x, y } = this.relativeCenter(tx, ty)
this.startDrag(x, y)
}
};
this.onFingerUp = function (event) {
touchPoints--
if (touchPoints === 0) this.endDrag()
};
this.onFingerMove = function (event) {
let touches = event.touches
let tx = touches[0].pageX
let ty = touches[0].pageY
this.mouse.x = tx
this.mouse.y = ty
let { x, y } = this.relativeCenter(tx, ty)
this.drag(x, y)
};
this.onKeyDown = function (event) {
switch (event.code) {
case 'ArrowUp':
case 'KeyW': this.moveForward = true; break;
case 'ArrowLeft':
case 'KeyA': this.moveLeft = true; break;
case 'ArrowDown':
case 'KeyS': this.moveBackward = true; break;
case 'ArrowRight':
case 'KeyD': this.moveRight = true; break;
case 'KeyR': this.moveUp = true; break;
case 'KeyF': this.moveDown = true; break;
case 'KeyE': this.cycle = 1; break;
case 'KeyQ': this.cycle = -1; break;
case 'Space': this.space = true; break;
}
};
this.onKeyUp = function (event) {
switch (event.code) {
case 'ArrowUp':
case 'KeyW': this.moveForward = false; break;
case 'ArrowLeft':
case 'KeyA': this.moveLeft = false; break;
case 'ArrowDown':
case 'KeyS': this.moveBackward = false; break;
case 'ArrowRight':
case 'KeyD': this.moveRight = false; break;
case 'KeyR': this.moveUp = false; break;
case 'KeyF': this.moveDown = false; break;
case 'Space': this.space = false; break;
}
};
this.lookAt = function (x, y, z) {
if (x.isVector3) {
_target.copy(x);
} else {
_target.set(x, y, z);
}
this.object.lookAt(_target);
setOrientation(this);
return this;
};
this.postUpdate = function () {
if (this.resetCycle) {
this.cycle = 0
this.resetCycle = false
}
this.mouseDownThisFrame = false
this.mouseUpThisFrame = false
if (this.cycle !== 0) {
this.resetCycle = true
}
}
this.update = function () {
const targetPosition = new Vector3();
return function update(delta) {
if (this.enabled === false) return;
if (this.heightSpeed) {
const y = MathUtils.clamp(this.object.position.y, this.heightMin, this.heightMax);
const heightDelta = y - this.heightMin;
this.autoSpeedFactor = delta * (heightDelta * this.heightCoef);
} else {
this.autoSpeedFactor = 0.0;
}
// logToPanel(touchPoints)
// handle touch controls
if (this.mobile) {
if (touchPoints === 2) {
this.moveForward = true
this.moveBackward = false
} else if (touchPoints === 3) {
this.moveBackward = true
this.moveForward = false
} else if (touchPoints === 0) {
this.moveBackward = false
this.moveForward = false
}
}
// increase accel if key continues to be held down.
if (this.moveForward || this.moveBackward || this.moveLeft || this.moveRight || this.moveUp || this.moveDown) {
this.accelMultiplier++
} else {
this.accelMultiplier--
if (this.accelMultiplier < 0) this.accelMultiplier = 0
}
let acc = this.accel// + this.accelMultiplier / 100
if (this.moveRight) {
this.dx += acc
} else if (this.moveLeft) {
this.dx -= acc
}
else this.dx *= this.decel
if (Math.abs(this.dx) < this.minx) this.dx = 0
this.object.translateX(this.dx)
if (this.moveUp) {
this.dy += acc
} else if (this.moveDown) {
this.dy -= acc
}
else this.dy *= this.decel
if (Math.abs(this.dy) < this.minx) this.dy = 0
this.object.translateY(this.dy)
if (this.moveForward) {
this.dz -= acc
} else if (this.moveBackward) {
this.dz += acc
}
else this.dz *= this.decel
if (Math.abs(this.dz) < this.minx) this.dz = 0
this.object.translateZ(this.dz)
let actualLookSpeed = delta * this.lookSpeed;
let verticalLookRatio = 1;
if (this.constrainVertical) {
verticalLookRatio = Math.PI / (this.verticalMax - this.verticalMin);
}
lon -= this.mouseXdelta * this.deltaScalar * actualLookSpeed;
if (this.lookVertical) lat -= this.mouseYdelta * this.deltaScalar * actualLookSpeed * verticalLookRatio;
lat = Math.max(- 85, Math.min(85, lat));
let phi = MathUtils.degToRad(90 - lat);
const theta = MathUtils.degToRad(lon);
if (this.constrainVertical) {
phi = MathUtils.mapLinear(phi, 0, Math.PI, this.verticalMin, this.verticalMax);
}
const position = this.object.position;
targetPosition.setFromSphericalCoords(1, phi, theta).add(position);
this.object.lookAt(targetPosition);
};
}();
this.dispose = function () {
this.domElement.removeEventListener('contextmenu', contextmenu);
this.domElement.removeEventListener('mousedown', _onMouseDown);
this.domElement.removeEventListener('mousemove', _onMouseMove);
this.domElement.removeEventListener('mouseup', _onMouseUp);
this.domElement.removeEventListener('touchstart', _onFingerDown);
this.domElement.removeEventListener('touchmove', _onFingerMove);
this.domElement.removeEventListener('touchend', _onFingerUp);
window.removeEventListener('keydown', _onKeyDown);
window.removeEventListener('keyup', _onKeyUp);
};
const _onMouseMove = this.onMouseMove.bind(this);
const _onMouseDown = this.onMouseDown.bind(this);
const _onMouseUp = this.onMouseUp.bind(this);
const _onFingerMove = this.onFingerMove.bind(this);
const _onFingerDown = this.onFingerDown.bind(this);
const _onFingerUp = this.onFingerUp.bind(this);
const _onKeyDown = this.onKeyDown.bind(this);
const _onKeyUp = this.onKeyUp.bind(this);
this.domElement.addEventListener('contextmenu', contextmenu);
this.domElement.addEventListener('mousedown', _onMouseDown);
this.domElement.addEventListener('mousemove', _onMouseMove);
this.domElement.addEventListener('mouseup', _onMouseUp);
this.domElement.addEventListener('touchstart', _onFingerDown);
this.domElement.addEventListener('touchmove', _onFingerMove);
this.domElement.addEventListener('touchend', _onFingerUp);
window.addEventListener('keydown', _onKeyDown);
window.addEventListener('keyup', _onKeyUp);
function setOrientation(controls) {
const quaternion = controls.object.quaternion;
_lookDirection.set(0, 0, - 1).applyQuaternion(quaternion);
_spherical.setFromVector3(_lookDirection);
lat = 90 - MathUtils.radToDeg(_spherical.phi);
lon = MathUtils.radToDeg(_spherical.theta);
}
this.handleResize();
setOrientation(this);
}
}
function contextmenu(event) {
event.preventDefault();
}
export { FirstPersonControls };