@@ -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,7 +40,6 @@ 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
@@ -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,8 @@ 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 ) ;
122
119
}
123
120
124
121
/**
@@ -255,6 +252,7 @@ export class TargetCamera extends Camera {
255
252
* @param target Defines the new target as a Vector
256
253
*/
257
254
public setTarget ( target : Vector3 ) : void {
255
+ // REVIEW: why is this here?
258
256
this . upVector . normalize ( ) ;
259
257
260
258
this . _initialFocalDistance = target . subtract ( this . position ) . length ( ) ;
@@ -263,38 +261,19 @@ export class TargetCamera extends Camera {
263
261
this . position . z += Epsilon ;
264
262
}
265
263
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 ) ;
270
265
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 ) ;
277
268
} else {
278
- this . rotation . y = - Math . atan ( vDir . z / vDir . x ) - Math . PI / 2.0 ;
269
+ Matrix . LookAtLHToRef ( this . position , target , Vector3 . UpReadOnly , TmpMatrix ) ;
279
270
}
271
+ TmpMatrix . invert ( ) ;
280
272
281
- this . rotation . z = 0 ;
273
+ const rotationQuaternion = this . rotationQuaternion || TmpQuaternion ;
274
+ Quaternion . FromRotationMatrixToRef ( TmpMatrix , rotationQuaternion ) ;
282
275
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 ) ;
298
277
}
299
278
300
279
/**
@@ -450,7 +429,7 @@ export class TargetCamera extends Camera {
450
429
* @returns the current camera
451
430
*/
452
431
private _rotateUpVectorWithCameraRotationMatrix ( ) : TargetCamera {
453
- Vector3 . TransformNormalToRef ( this . _defaultUp , this . _cameraRotationMatrix , this . upVector ) ;
432
+ Vector3 . TransformNormalToRef ( Vector3 . UpReadOnly , this . _cameraRotationMatrix , this . upVector ) ;
454
433
return this ;
455
434
}
456
435
@@ -482,8 +461,8 @@ export class TargetCamera extends Camera {
482
461
if ( this . rotationQuaternion ) {
483
462
Axis . Y . rotateByQuaternionToRef ( this . rotationQuaternion , this . upVector ) ;
484
463
} 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 ) ;
487
466
}
488
467
}
489
468
this . _computeViewMatrix ( this . position , this . _currentTarget , this . upVector ) ;
0 commit comments