@@ -174,7 +174,7 @@ class CameraControls extends Script {
174
174
* @type {HTMLElement }
175
175
* @private
176
176
*/
177
- _element ;
177
+ _element = this . app . graphicsDevice . canvas ;
178
178
179
179
/**
180
180
* @type {Mat4 }
@@ -302,54 +302,7 @@ class CameraControls extends Script {
302
302
*/
303
303
zoomScaleMin = 0 ;
304
304
305
- /**
306
- * @param {object } args - The script arguments.
307
- */
308
- constructor ( args ) {
309
- super ( args ) ;
310
- const {
311
- element,
312
- focusPoint,
313
- pitchRange,
314
- sceneSize,
315
- enableOrbit,
316
- enablePan,
317
- enableFly,
318
- rotateSpeed,
319
- rotateDamping,
320
- moveSpeed,
321
- moveFastSpeed,
322
- moveSlowSpeed,
323
- moveDamping,
324
- zoomSpeed,
325
- zoomPinchSens,
326
- zoomDamping,
327
- zoomMin,
328
- zoomMax,
329
- zoomScaleMin
330
- } = args . attributes ;
331
-
332
- this . _element = element ?? this . app . graphicsDevice . canvas ;
333
-
334
- this . enableOrbit = enableOrbit ?? this . enableOrbit ;
335
- this . enablePan = enablePan ?? this . enablePan ;
336
- this . enableFly = enableFly ?? this . enableFly ;
337
-
338
- this . sceneSize = sceneSize ?? this . sceneSize ;
339
-
340
- this . rotateSpeed = rotateSpeed ?? this . rotateSpeed ;
341
- this . rotateDamping = rotateDamping ?? this . rotateDamping ;
342
-
343
- this . moveSpeed = moveSpeed ?? this . moveSpeed ;
344
- this . moveFastSpeed = moveFastSpeed ?? this . moveFastSpeed ;
345
- this . moveSlowSpeed = moveSlowSpeed ?? this . moveSlowSpeed ;
346
- this . moveDamping = moveDamping ?? this . moveDamping ;
347
-
348
- this . zoomSpeed = zoomSpeed ?? this . zoomSpeed ;
349
- this . zoomPinchSens = zoomPinchSens ?? this . zoomPinchSens ;
350
- this . zoomDamping = zoomDamping ?? this . zoomDamping ;
351
- this . zoomScaleMin = zoomScaleMin ?? this . zoomScaleMin ;
352
-
305
+ initialize ( ) {
353
306
this . _onWheel = this . _onWheel . bind ( this ) ;
354
307
this . _onKeyDown = this . _onKeyDown . bind ( this ) ;
355
308
this . _onKeyUp = this . _onKeyUp . bind ( this ) ;
@@ -363,10 +316,10 @@ class CameraControls extends Script {
363
316
}
364
317
this . attach ( this . entity . camera ) ;
365
318
366
- this . focusPoint = focusPoint ?? this . focusPoint ;
367
- this . pitchRange = pitchRange ?? this . pitchRange ;
368
- this . zoomMin = zoomMin ?? this . zoomMin ;
369
- this . zoomMax = zoomMax ?? this . zoomMax ;
319
+ this . focusPoint = this . _origin ?? this . focusPoint ;
320
+ this . pitchRange = this . _pitchRange ?? this . pitchRange ;
321
+ this . zoomMin = this . _zoomMin ?? this . zoomMin ;
322
+ this . zoomMax = this . _zoomMax ?? this . zoomMax ;
370
323
}
371
324
372
325
/**
@@ -395,9 +348,12 @@ class CameraControls extends Script {
395
348
*/
396
349
set focusPoint ( point ) {
397
350
if ( ! this . _camera ) {
351
+ if ( point instanceof Vec3 ) {
352
+ this . _origin . copy ( point ) ;
353
+ }
398
354
return ;
399
355
}
400
- this . focus ( point , this . _camera . entity . getPosition ( ) , false ) ;
356
+ this . focus ( point , this . entity . getPosition ( ) , false ) ;
401
357
}
402
358
403
359
get focusPoint ( ) {
@@ -413,6 +369,9 @@ class CameraControls extends Script {
413
369
* @default [-360, 360]
414
370
*/
415
371
set pitchRange ( value ) {
372
+ if ( ! ( value instanceof Vec2 ) ) {
373
+ return ;
374
+ }
416
375
this . _pitchRange . copy ( value ) ;
417
376
this . _clampAngles ( this . _dir ) ;
418
377
this . _smoothTransform ( - 1 ) ;
@@ -430,7 +389,7 @@ class CameraControls extends Script {
430
389
* @default 0
431
390
*/
432
391
set zoomMin ( value ) {
433
- this . _zoomMin = value ;
392
+ this . _zoomMin = value ?? this . _zoomMin ;
434
393
this . _zoomDist = this . _clampZoom ( this . _zoomDist ) ;
435
394
this . _smoothZoom ( - 1 ) ;
436
395
}
@@ -448,7 +407,7 @@ class CameraControls extends Script {
448
407
* @default 0
449
408
*/
450
409
set zoomMax ( value ) {
451
- this . _zoomMax = value ;
410
+ this . _zoomMax = value ?? this . _zoomMax ;
452
411
this . _zoomDist = this . _clampZoom ( this . _zoomDist ) ;
453
412
this . _smoothZoom ( - 1 ) ;
454
413
@@ -464,7 +423,7 @@ class CameraControls extends Script {
464
423
* @returns {Vec3 } - The focus vector.
465
424
*/
466
425
_focusDir ( out ) {
467
- return out . copy ( this . _camera . entity . forward ) . mulScalar ( this . _zoomDist ) ;
426
+ return out . copy ( this . entity . forward ) . mulScalar ( this . _zoomDist ) ;
468
427
}
469
428
470
429
/**
@@ -600,7 +559,7 @@ class CameraControls extends Script {
600
559
if ( startFly ) {
601
560
// start fly
602
561
this . _zoomDist = this . _cameraDist ;
603
- this . _origin . copy ( this . _camera . entity . getPosition ( ) ) ;
562
+ this . _origin . copy ( this . entity . getPosition ( ) ) ;
604
563
this . _position . copy ( this . _origin ) ;
605
564
this . _cameraTransform . setTranslate ( 0 , 0 , 0 ) ;
606
565
this . _flying = true ;
@@ -782,22 +741,22 @@ class CameraControls extends Script {
782
741
783
742
tmpV1 . set ( 0 , 0 , 0 ) ;
784
743
if ( this . _key . forward ) {
785
- tmpV1 . add ( this . _camera . entity . forward ) ;
744
+ tmpV1 . add ( this . entity . forward ) ;
786
745
}
787
746
if ( this . _key . backward ) {
788
- tmpV1 . sub ( this . _camera . entity . forward ) ;
747
+ tmpV1 . sub ( this . entity . forward ) ;
789
748
}
790
749
if ( this . _key . left ) {
791
- tmpV1 . sub ( this . _camera . entity . right ) ;
750
+ tmpV1 . sub ( this . entity . right ) ;
792
751
}
793
752
if ( this . _key . right ) {
794
- tmpV1 . add ( this . _camera . entity . right ) ;
753
+ tmpV1 . add ( this . entity . right ) ;
795
754
}
796
755
if ( this . _key . up ) {
797
- tmpV1 . add ( this . _camera . entity . up ) ;
756
+ tmpV1 . add ( this . entity . up ) ;
798
757
}
799
758
if ( this . _key . down ) {
800
- tmpV1 . sub ( this . _camera . entity . up ) ;
759
+ tmpV1 . sub ( this . entity . up ) ;
801
760
}
802
761
tmpV1 . normalize ( ) ;
803
762
this . _moving = tmpV1 . length ( ) > 0 ;
@@ -840,8 +799,11 @@ class CameraControls extends Script {
840
799
* @param {Vec3 } point - The output point.
841
800
*/
842
801
_screenToWorldPan ( pos , point ) {
802
+ if ( ! this . _camera ) {
803
+ return ;
804
+ }
843
805
const mouseW = this . _camera . screenToWorld ( pos . x , pos . y , 1 ) ;
844
- const cameraPos = this . _camera . entity . getPosition ( ) ;
806
+ const cameraPos = this . entity . getPosition ( ) ;
845
807
846
808
const focusDir = this . _focusDir ( tmpV1 ) ;
847
809
const focalPos = tmpV2 . add2 ( cameraPos , focusDir ) ;
@@ -923,8 +885,8 @@ class CameraControls extends Script {
923
885
*/
924
886
_updateTransform ( ) {
925
887
tmpM1 . copy ( this . _baseTransform ) . mul ( this . _cameraTransform ) ;
926
- this . _camera . entity . setPosition ( tmpM1 . getTranslation ( ) ) ;
927
- this . _camera . entity . setEulerAngles ( tmpM1 . getEulerAngles ( ) ) ;
888
+ this . entity . setPosition ( tmpM1 . getTranslation ( ) ) ;
889
+ this . entity . setEulerAngles ( tmpM1 . getEulerAngles ( ) ) ;
928
890
}
929
891
930
892
/**
@@ -958,8 +920,8 @@ class CameraControls extends Script {
958
920
959
921
this . _cameraTransform . setTranslate ( 0 , 0 , 0 ) ;
960
922
961
- const pos = this . _camera . entity . getPosition ( ) ;
962
- const rot = this . _camera . entity . getRotation ( ) ;
923
+ const pos = this . entity . getPosition ( ) ;
924
+ const rot = this . entity . getRotation ( ) ;
963
925
this . _baseTransform . setTRS ( pos , rot , Vec3 . ONE ) ;
964
926
965
927
this . _zoomDist = this . _clampZoom ( tmpV1 . length ( ) ) ;
0 commit comments