Skip to content

Commit 395ea7e

Browse files
kpal81xdmarklundin
authored and
Martin Valigursky
committed
Refactor Camera controls (#7291)
* Updated camera controls to use initialize * Added null checks --------- Co-authored-by: Mark Lundin <[email protected]>
1 parent 1edd5cf commit 395ea7e

File tree

5 files changed

+35
-73
lines changed

5 files changed

+35
-73
lines changed

examples/src/examples/camera/fly.example.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const createFlyCamera = (focus) => {
9292

9393
/** @type {CameraControls} */
9494
const script = camera.script.create(CameraControls, {
95-
attributes: {
95+
properties: {
9696
enableOrbit: false,
9797
enablePan: false,
9898
focusPoint: bbox.center,

examples/src/examples/camera/multi.example.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const createMultiCamera = (focus) => {
9595

9696
/** @type {CameraControls} */
9797
const script = camera.script.create(CameraControls, {
98-
attributes: {
98+
properties: {
9999
focusPoint: bbox.center,
100100
sceneSize: bbox.halfExtents.length()
101101
}

examples/src/examples/camera/orbit.example.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const createOrbitCamera = (focus) => {
9393

9494
/** @type {CameraControls} */
9595
const script = camera.script.create(CameraControls, {
96-
attributes: {
96+
properties: {
9797
enableFly: false,
9898
focusPoint: bbox.center,
9999
sceneSize: bbox.halfExtents.length()

examples/src/examples/misc/editor.example.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const cameraOffset = 4 * camera.camera?.aspectRatio;
127127
camera.setPosition(cameraOffset, cameraOffset, cameraOffset);
128128
app.root.addChild(camera);
129129
const cameraControls = /** @type {CameraControls} */ (camera.script.create(CameraControls, {
130-
attributes: {
130+
properties: {
131131
focusPoint: pc.Vec3.ZERO,
132132
sceneSize: 5
133133
}

scripts/esm/camera-controls.mjs

+31-69
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class CameraControls extends Script {
174174
* @type {HTMLElement}
175175
* @private
176176
*/
177-
_element;
177+
_element = this.app.graphicsDevice.canvas;
178178

179179
/**
180180
* @type {Mat4}
@@ -302,54 +302,7 @@ class CameraControls extends Script {
302302
*/
303303
zoomScaleMin = 0;
304304

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() {
353306
this._onWheel = this._onWheel.bind(this);
354307
this._onKeyDown = this._onKeyDown.bind(this);
355308
this._onKeyUp = this._onKeyUp.bind(this);
@@ -363,10 +316,10 @@ class CameraControls extends Script {
363316
}
364317
this.attach(this.entity.camera);
365318

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;
370323
}
371324

372325
/**
@@ -395,9 +348,12 @@ class CameraControls extends Script {
395348
*/
396349
set focusPoint(point) {
397350
if (!this._camera) {
351+
if (point instanceof Vec3) {
352+
this._origin.copy(point);
353+
}
398354
return;
399355
}
400-
this.focus(point, this._camera.entity.getPosition(), false);
356+
this.focus(point, this.entity.getPosition(), false);
401357
}
402358

403359
get focusPoint() {
@@ -413,6 +369,9 @@ class CameraControls extends Script {
413369
* @default [-360, 360]
414370
*/
415371
set pitchRange(value) {
372+
if (!(value instanceof Vec2)) {
373+
return;
374+
}
416375
this._pitchRange.copy(value);
417376
this._clampAngles(this._dir);
418377
this._smoothTransform(-1);
@@ -430,7 +389,7 @@ class CameraControls extends Script {
430389
* @default 0
431390
*/
432391
set zoomMin(value) {
433-
this._zoomMin = value;
392+
this._zoomMin = value ?? this._zoomMin;
434393
this._zoomDist = this._clampZoom(this._zoomDist);
435394
this._smoothZoom(-1);
436395
}
@@ -448,7 +407,7 @@ class CameraControls extends Script {
448407
* @default 0
449408
*/
450409
set zoomMax(value) {
451-
this._zoomMax = value;
410+
this._zoomMax = value ?? this._zoomMax;
452411
this._zoomDist = this._clampZoom(this._zoomDist);
453412
this._smoothZoom(-1);
454413

@@ -464,7 +423,7 @@ class CameraControls extends Script {
464423
* @returns {Vec3} - The focus vector.
465424
*/
466425
_focusDir(out) {
467-
return out.copy(this._camera.entity.forward).mulScalar(this._zoomDist);
426+
return out.copy(this.entity.forward).mulScalar(this._zoomDist);
468427
}
469428

470429
/**
@@ -600,7 +559,7 @@ class CameraControls extends Script {
600559
if (startFly) {
601560
// start fly
602561
this._zoomDist = this._cameraDist;
603-
this._origin.copy(this._camera.entity.getPosition());
562+
this._origin.copy(this.entity.getPosition());
604563
this._position.copy(this._origin);
605564
this._cameraTransform.setTranslate(0, 0, 0);
606565
this._flying = true;
@@ -782,22 +741,22 @@ class CameraControls extends Script {
782741

783742
tmpV1.set(0, 0, 0);
784743
if (this._key.forward) {
785-
tmpV1.add(this._camera.entity.forward);
744+
tmpV1.add(this.entity.forward);
786745
}
787746
if (this._key.backward) {
788-
tmpV1.sub(this._camera.entity.forward);
747+
tmpV1.sub(this.entity.forward);
789748
}
790749
if (this._key.left) {
791-
tmpV1.sub(this._camera.entity.right);
750+
tmpV1.sub(this.entity.right);
792751
}
793752
if (this._key.right) {
794-
tmpV1.add(this._camera.entity.right);
753+
tmpV1.add(this.entity.right);
795754
}
796755
if (this._key.up) {
797-
tmpV1.add(this._camera.entity.up);
756+
tmpV1.add(this.entity.up);
798757
}
799758
if (this._key.down) {
800-
tmpV1.sub(this._camera.entity.up);
759+
tmpV1.sub(this.entity.up);
801760
}
802761
tmpV1.normalize();
803762
this._moving = tmpV1.length() > 0;
@@ -840,8 +799,11 @@ class CameraControls extends Script {
840799
* @param {Vec3} point - The output point.
841800
*/
842801
_screenToWorldPan(pos, point) {
802+
if (!this._camera) {
803+
return;
804+
}
843805
const mouseW = this._camera.screenToWorld(pos.x, pos.y, 1);
844-
const cameraPos = this._camera.entity.getPosition();
806+
const cameraPos = this.entity.getPosition();
845807

846808
const focusDir = this._focusDir(tmpV1);
847809
const focalPos = tmpV2.add2(cameraPos, focusDir);
@@ -923,8 +885,8 @@ class CameraControls extends Script {
923885
*/
924886
_updateTransform() {
925887
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());
928890
}
929891

930892
/**
@@ -958,8 +920,8 @@ class CameraControls extends Script {
958920

959921
this._cameraTransform.setTranslate(0, 0, 0);
960922

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();
963925
this._baseTransform.setTRS(pos, rot, Vec3.ONE);
964926

965927
this._zoomDist = this._clampZoom(tmpV1.length());

0 commit comments

Comments
 (0)