@@ -12,6 +12,10 @@ Node.AddNodeConstructor("TargetCamera", (name, scene) => {
12
12
return ( ) => new TargetCamera ( name , Vector3 . Zero ( ) , scene ) ;
13
13
} ) ;
14
14
15
+ // Temporary cache variables to avoid allocations.
16
+ const TmpMatrix = Matrix . Zero ( ) ;
17
+ const TmpQuaternion = Quaternion . Identity ( ) ;
18
+
15
19
/**
16
20
* A target camera takes a mesh or position as a target and continues to look at it while it moves.
17
21
* This is the base of the follow, arc rotate cameras and Free camera
@@ -36,13 +40,12 @@ export class TargetCamera extends Camera {
36
40
*/
37
41
@serialize ( )
38
42
public updateUpVectorFromRotation = false ;
39
- private _tmpQuaternion = new Quaternion ( ) ;
40
43
41
44
/**
42
45
* Define the current rotation of the camera
43
46
*/
44
47
@serializeAsVector3 ( )
45
- public rotation = new Vector3 ( 0 , 0 , 0 ) ;
48
+ public rotation : Vector3 ;
46
49
47
50
/**
48
51
* Define the current rotation of the camera as a quaternion to prevent Gimbal lock
@@ -79,35 +82,27 @@ export class TargetCamera extends Camera {
79
82
@serializeAsMeshReference ( "lockedTargetId" )
80
83
public lockedTarget : any = null ;
81
84
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 ( ) ;
94
88
95
89
/** @internal */
96
- public _referencePoint = new Vector3 ( 0 , 0 , 1 ) ;
90
+ public readonly _cameraTransformMatrix = Matrix . Zero ( ) ;
97
91
/** @internal */
98
- public _transformedReferencePoint = Vector3 . Zero ( ) ;
92
+ public readonly _cameraRotationMatrix = Matrix . Zero ( ) ;
93
+
94
+ protected readonly _referencePoint : Vector3 ;
95
+ protected readonly _transformedReferencePoint = Vector3 . Zero ( ) ;
99
96
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 ( ) ;
103
100
protected _deferredUpdated = false ;
104
101
protected _deferOnly : boolean = false ;
105
102
106
103
/** @internal */
107
104
public _reset : ( ) => void ;
108
105
109
- private _defaultUp = Vector3 . Up ( ) ;
110
-
111
106
/**
112
107
* Instantiates a target camera that takes a mesh or position as a target and continues to look at it while it moves.
113
108
* This is the base of the follow, arc rotate cameras and Free camera
@@ -119,6 +114,11 @@ export class TargetCamera extends Camera {
119
114
*/
120
115
constructor ( name : string , position : Vector3 , scene ?: Scene , setActiveOnSceneIfNoneActive = true ) {
121
116
super ( name , position , scene , setActiveOnSceneIfNoneActive ) ;
117
+
118
+ this . _referencePoint = Vector3 . Forward ( this . getScene ( ) . useRightHandedSystem ) ;
119
+
120
+ // Set the y component of the rotation to Math.PI in right-handed system for backwards compatibility.
121
+ this . rotation = new Vector3 ( 0 , this . getScene ( ) . useRightHandedSystem ? Math . PI : 0 , 0 ) ;
122
122
}
123
123
124
124
/**
@@ -265,36 +265,17 @@ export class TargetCamera extends Camera {
265
265
266
266
this . _referencePoint . normalize ( ) . scaleInPlace ( this . _initialFocalDistance ) ;
267
267
268
- Matrix . LookAtLHToRef ( this . position , target , this . _defaultUp , this . _camMatrix ) ;
269
- this . _camMatrix . invert ( ) ;
270
-
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 ;
268
+ if ( this . getScene ( ) . useRightHandedSystem ) {
269
+ Matrix . LookAtRHToRef ( this . position , target , Vector3 . UpReadOnly , TmpMatrix ) ;
277
270
} else {
278
- this . rotation . y = - Math . atan ( vDir . z / vDir . x ) - Math . PI / 2.0 ;
271
+ Matrix . LookAtLHToRef ( this . position , target , Vector3 . UpReadOnly , TmpMatrix ) ;
279
272
}
273
+ TmpMatrix . invert ( ) ;
280
274
281
- this . rotation . z = 0 ;
275
+ const rotationQuaternion = this . rotationQuaternion || TmpQuaternion ;
276
+ Quaternion . FromRotationMatrixToRef ( TmpMatrix , rotationQuaternion ) ;
282
277
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
- }
278
+ rotationQuaternion . toEulerAnglesToRef ( this . rotation ) ;
298
279
}
299
280
300
281
/**
@@ -450,7 +431,7 @@ export class TargetCamera extends Camera {
450
431
* @returns the current camera
451
432
*/
452
433
private _rotateUpVectorWithCameraRotationMatrix ( ) : TargetCamera {
453
- Vector3 . TransformNormalToRef ( this . _defaultUp , this . _cameraRotationMatrix , this . upVector ) ;
434
+ Vector3 . TransformNormalToRef ( Vector3 . UpReadOnly , this . _cameraRotationMatrix , this . upVector ) ;
454
435
return this ;
455
436
}
456
437
@@ -482,8 +463,8 @@ export class TargetCamera extends Camera {
482
463
if ( this . rotationQuaternion ) {
483
464
Axis . Y . rotateByQuaternionToRef ( this . rotationQuaternion , this . upVector ) ;
484
465
} else {
485
- Quaternion . FromEulerVectorToRef ( this . rotation , this . _tmpQuaternion ) ;
486
- Axis . Y . rotateByQuaternionToRef ( this . _tmpQuaternion , this . upVector ) ;
466
+ Quaternion . FromEulerVectorToRef ( this . rotation , TmpQuaternion ) ;
467
+ Axis . Y . rotateByQuaternionToRef ( TmpQuaternion , this . upVector ) ;
487
468
}
488
469
}
489
470
this . _computeViewMatrix ( this . position , this . _currentTarget , this . upVector ) ;
0 commit comments