Skip to content

Commit 638dec6

Browse files
committed
WIP: RH camera fixes
1 parent 5e04d30 commit 638dec6

File tree

2 files changed

+31
-52
lines changed

2 files changed

+31
-52
lines changed

packages/dev/core/src/Cameras/targetCamera.ts

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Node.AddNodeConstructor("TargetCamera", (name, scene) => {
1212
return () => new TargetCamera(name, Vector3.Zero(), scene);
1313
});
1414

15+
// Temporary cache variables to avoid allocations.
16+
const TmpMatrix = Matrix.Zero();
17+
const TmpQuaternion = Quaternion.Identity();
18+
1519
/**
1620
* A target camera takes a mesh or position as a target and continues to look at it while it moves.
1721
* This is the base of the follow, arc rotate cameras and Free camera
@@ -36,7 +40,6 @@ export class TargetCamera extends Camera {
3640
*/
3741
@serialize()
3842
public updateUpVectorFromRotation = false;
39-
private _tmpQuaternion = new Quaternion();
4043

4144
/**
4245
* Define the current rotation of the camera
@@ -79,35 +82,27 @@ export class TargetCamera extends Camera {
7982
@serializeAsMeshReference("lockedTargetId")
8083
public lockedTarget: any = null;
8184

82-
/** @internal */
83-
public _currentTarget = Vector3.Zero();
84-
/** @internal */
85-
public _initialFocalDistance = 1;
86-
/** @internal */
87-
public _viewMatrix = Matrix.Zero();
88-
/** @internal */
89-
public _camMatrix = Matrix.Zero();
90-
/** @internal */
91-
public _cameraTransformMatrix = Matrix.Zero();
92-
/** @internal */
93-
public _cameraRotationMatrix = Matrix.Zero();
85+
protected readonly _currentTarget = Vector3.Zero();
86+
protected _initialFocalDistance = 1;
87+
protected readonly _viewMatrix = Matrix.Zero();
9488

9589
/** @internal */
96-
public _referencePoint = new Vector3(0, 0, 1);
90+
public readonly _cameraTransformMatrix = Matrix.Zero();
9791
/** @internal */
98-
public _transformedReferencePoint = Vector3.Zero();
92+
public readonly _cameraRotationMatrix = Matrix.Zero();
93+
94+
protected readonly _referencePoint: Vector3;
95+
protected readonly _transformedReferencePoint = Vector3.Zero();
9996

100-
protected _deferredPositionUpdate = new Vector3();
101-
protected _deferredRotationQuaternionUpdate = new Quaternion();
102-
protected _deferredRotationUpdate = new Vector3();
97+
protected readonly _deferredPositionUpdate = new Vector3();
98+
protected readonly _deferredRotationQuaternionUpdate = new Quaternion();
99+
protected readonly _deferredRotationUpdate = new Vector3();
103100
protected _deferredUpdated = false;
104101
protected _deferOnly: boolean = false;
105102

106103
/** @internal */
107104
public _reset: () => void;
108105

109-
private _defaultUp = Vector3.Up();
110-
111106
/**
112107
* Instantiates a target camera that takes a mesh or position as a target and continues to look at it while it moves.
113108
* This is the base of the follow, arc rotate cameras and Free camera
@@ -119,6 +114,8 @@ export class TargetCamera extends Camera {
119114
*/
120115
constructor(name: string, position: Vector3, scene?: Scene, setActiveOnSceneIfNoneActive = true) {
121116
super(name, position, scene, setActiveOnSceneIfNoneActive);
117+
118+
this._referencePoint = Vector3.Forward(this.getScene().useRightHandedSystem);
122119
}
123120

124121
/**
@@ -255,6 +252,7 @@ export class TargetCamera extends Camera {
255252
* @param target Defines the new target as a Vector
256253
*/
257254
public setTarget(target: Vector3): void {
255+
// REVIEW: why is this here?
258256
this.upVector.normalize();
259257

260258
this._initialFocalDistance = target.subtract(this.position).length();
@@ -263,38 +261,19 @@ export class TargetCamera extends Camera {
263261
this.position.z += Epsilon;
264262
}
265263

266-
this._referencePoint.normalize().scaleInPlace(this._initialFocalDistance);
267-
268-
Matrix.LookAtLHToRef(this.position, target, this._defaultUp, this._camMatrix);
269-
this._camMatrix.invert();
264+
this._referencePoint.scaleInPlace(this._initialFocalDistance);
270265

271-
this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
272-
273-
const vDir = target.subtract(this.position);
274-
275-
if (vDir.x >= 0.0) {
276-
this.rotation.y = -Math.atan(vDir.z / vDir.x) + Math.PI / 2.0;
266+
if (this.getScene().useRightHandedSystem) {
267+
Matrix.LookAtRHToRef(this.position, target, Vector3.UpReadOnly, TmpMatrix);
277268
} else {
278-
this.rotation.y = -Math.atan(vDir.z / vDir.x) - Math.PI / 2.0;
269+
Matrix.LookAtLHToRef(this.position, target, Vector3.UpReadOnly, TmpMatrix);
279270
}
271+
TmpMatrix.invert();
280272

281-
this.rotation.z = 0;
273+
const rotationQuaternion = this.rotationQuaternion || TmpQuaternion;
274+
Quaternion.FromRotationMatrixToRef(TmpMatrix, rotationQuaternion);
282275

283-
if (isNaN(this.rotation.x)) {
284-
this.rotation.x = 0;
285-
}
286-
287-
if (isNaN(this.rotation.y)) {
288-
this.rotation.y = 0;
289-
}
290-
291-
if (isNaN(this.rotation.z)) {
292-
this.rotation.z = 0;
293-
}
294-
295-
if (this.rotationQuaternion) {
296-
Quaternion.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this.rotationQuaternion);
297-
}
276+
rotationQuaternion.toEulerAnglesToRef(this.rotation);
298277
}
299278

300279
/**
@@ -450,7 +429,7 @@ export class TargetCamera extends Camera {
450429
* @returns the current camera
451430
*/
452431
private _rotateUpVectorWithCameraRotationMatrix(): TargetCamera {
453-
Vector3.TransformNormalToRef(this._defaultUp, this._cameraRotationMatrix, this.upVector);
432+
Vector3.TransformNormalToRef(Vector3.UpReadOnly, this._cameraRotationMatrix, this.upVector);
454433
return this;
455434
}
456435

@@ -482,8 +461,8 @@ export class TargetCamera extends Camera {
482461
if (this.rotationQuaternion) {
483462
Axis.Y.rotateByQuaternionToRef(this.rotationQuaternion, this.upVector);
484463
} else {
485-
Quaternion.FromEulerVectorToRef(this.rotation, this._tmpQuaternion);
486-
Axis.Y.rotateByQuaternionToRef(this._tmpQuaternion, this.upVector);
464+
Quaternion.FromEulerVectorToRef(this.rotation, TmpQuaternion);
465+
Axis.Y.rotateByQuaternionToRef(TmpQuaternion, this.upVector);
487466
}
488467
}
489468
this._computeViewMatrix(this.position, this._currentTarget, this.upVector);

packages/dev/loaders/src/glTF/2.0/glTFLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,8 +1569,8 @@ export class GLTFLoader implements IGLTFLoader {
15691569
this._babylonScene._blockEntityCollection = false;
15701570
camera._babylonCamera = babylonCamera;
15711571

1572-
// Rotation by 180 as glTF has a different convention than Babylon.
1573-
babylonCamera.rotation.set(0, Math.PI, 0);
1572+
// glTF cameras look towards the local -Z axis.
1573+
babylonCamera.setTarget(new Vector3(0, 0, -1));
15741574

15751575
switch (camera.type) {
15761576
case CameraType.PERSPECTIVE: {

0 commit comments

Comments
 (0)