@@ -64,15 +64,21 @@ export class Transform extends Component {
64
64
* World position.
65
65
*/
66
66
get worldPosition ( ) : Vector3 {
67
+ const worldPosition = this . _worldPosition ;
67
68
if ( this . _isContainDirtyFlag ( TransformFlag . WorldPosition ) ) {
69
+ //@ts -ignore
70
+ worldPosition . _onValueChanged = null ;
68
71
if ( this . _getParentTransform ( ) ) {
69
- this . worldMatrix . getTranslation ( this . _worldPosition ) ;
72
+ this . worldMatrix . getTranslation ( worldPosition ) ;
70
73
} else {
71
- this . _position . cloneTo ( this . _worldPosition ) ;
74
+ this . _position . cloneTo ( worldPosition ) ;
72
75
}
76
+ //@ts -ignore
77
+ worldPosition . _onValueChanged = this . _onWorldPositionChanged ;
73
78
this . _setDirtyFlagFalse ( TransformFlag . WorldPosition ) ;
74
79
}
75
- return this . _worldPosition ;
80
+
81
+ return worldPosition ;
76
82
}
77
83
78
84
set worldPosition ( value : Vector3 ) {
@@ -86,12 +92,18 @@ export class Transform extends Component {
86
92
* Rotations are performed around the Y axis, the X axis, and the Z axis, in that order.
87
93
*/
88
94
get rotation ( ) : Vector3 {
95
+ const rotation = this . _rotation ;
89
96
if ( this . _isContainDirtyFlag ( TransformFlag . LocalEuler ) ) {
90
- this . _rotationQuaternion . toEuler ( this . _rotation ) ;
91
- this . _rotation . scale ( MathUtil . radToDegreeFactor ) ; // radians to degrees
97
+ //@ts -ignore
98
+ rotation . _onValueChanged = null ;
99
+ this . _rotationQuaternion . toEuler ( rotation ) ;
100
+ //@ts -ignore
101
+ rotation . _onValueChanged = this . _onRotationChanged ;
102
+ rotation . scale ( MathUtil . radToDegreeFactor ) ; // radians to degrees
92
103
this . _setDirtyFlagFalse ( TransformFlag . LocalEuler ) ;
93
104
}
94
- return this . _rotation ;
105
+
106
+ return rotation ;
95
107
}
96
108
97
109
set rotation ( value : Vector3 ) {
@@ -105,12 +117,17 @@ export class Transform extends Component {
105
117
* Rotations are performed around the Y axis, the X axis, and the Z axis, in that order.
106
118
*/
107
119
get worldRotation ( ) : Vector3 {
120
+ const worldRotation = this . _worldRotation ;
108
121
if ( this . _isContainDirtyFlag ( TransformFlag . WorldEuler ) ) {
109
- this . worldRotationQuaternion . toEuler ( this . _worldRotation ) ;
110
- this . _worldRotation . scale ( MathUtil . radToDegreeFactor ) ; // Radian to angle
122
+ //@ts -ignore
123
+ worldRotation . _onValueChanged = null ;
124
+ this . worldRotationQuaternion . toEuler ( worldRotation ) ;
125
+ worldRotation . scale ( MathUtil . radToDegreeFactor ) ; // Radian to angle
126
+ //@ts -ignore
127
+ worldRotation . _onValueChanged = this . _onWorldRotationChanged ;
111
128
this . _setDirtyFlagFalse ( TransformFlag . WorldEuler ) ;
112
129
}
113
- return this . _worldRotation ;
130
+ return worldRotation ;
114
131
}
115
132
116
133
set worldRotation ( value : Vector3 ) {
@@ -123,16 +140,21 @@ export class Transform extends Component {
123
140
* Local rotation, defining the rotation by using a unit quaternion.
124
141
*/
125
142
get rotationQuaternion ( ) : Quaternion {
143
+ const rotationQuaternion = this . _rotationQuaternion ;
126
144
if ( this . _isContainDirtyFlag ( TransformFlag . LocalQuat ) ) {
145
+ //@ts -ignore
146
+ rotationQuaternion . _onValueChanged = null ;
127
147
Quaternion . rotationEuler (
128
148
MathUtil . degreeToRadian ( this . _rotation . x ) ,
129
149
MathUtil . degreeToRadian ( this . _rotation . y ) ,
130
150
MathUtil . degreeToRadian ( this . _rotation . z ) ,
131
- this . _rotationQuaternion
151
+ rotationQuaternion
132
152
) ;
153
+ //@ts -ignore
154
+ rotationQuaternion . _onValueChanged = this . _onRotationQuaternionChanged ;
133
155
this . _setDirtyFlagFalse ( TransformFlag . LocalQuat ) ;
134
156
}
135
- return this . _rotationQuaternion ;
157
+ return rotationQuaternion ;
136
158
}
137
159
138
160
set rotationQuaternion ( value : Quaternion ) {
@@ -145,16 +167,21 @@ export class Transform extends Component {
145
167
* World rotation, defining the rotation by using a unit quaternion.
146
168
*/
147
169
get worldRotationQuaternion ( ) : Quaternion {
170
+ const worldRotationQuaternion = this . _worldRotationQuaternion ;
148
171
if ( this . _isContainDirtyFlag ( TransformFlag . WorldQuat ) ) {
172
+ //@ts -ignore
173
+ worldRotationQuaternion . _onValueChanged = null ;
149
174
const parent = this . _getParentTransform ( ) ;
150
175
if ( parent != null ) {
151
- Quaternion . multiply ( parent . worldRotationQuaternion , this . rotationQuaternion , this . _worldRotationQuaternion ) ;
176
+ Quaternion . multiply ( parent . worldRotationQuaternion , this . rotationQuaternion , worldRotationQuaternion ) ;
152
177
} else {
153
- this . rotationQuaternion . cloneTo ( this . _worldRotationQuaternion ) ;
178
+ this . rotationQuaternion . cloneTo ( worldRotationQuaternion ) ;
154
179
}
180
+ //@ts -ignore
181
+ worldRotationQuaternion . _onValueChanged = this . _onWorldRotationQuaternionChanged ;
155
182
this . _setDirtyFlagFalse ( TransformFlag . WorldQuat ) ;
156
183
}
157
- return this . _worldRotationQuaternion ;
184
+ return worldRotationQuaternion ;
158
185
}
159
186
160
187
set worldRotationQuaternion ( value : Quaternion ) {
@@ -211,7 +238,9 @@ export class Transform extends Component {
211
238
if ( this . _localMatrix !== value ) {
212
239
value . cloneTo ( this . _localMatrix ) ;
213
240
}
241
+
214
242
this . _localMatrix . decompose ( this . _position , this . _rotationQuaternion , this . _scale ) ;
243
+
215
244
this . _setDirtyFlagTrue ( TransformFlag . LocalEuler ) ;
216
245
this . _setDirtyFlagFalse ( TransformFlag . LocalMatrix ) ;
217
246
this . _updateAllWorldFlag ( ) ;
@@ -252,69 +281,28 @@ export class Transform extends Component {
252
281
constructor ( entity : Entity ) {
253
282
super ( entity ) ;
254
283
255
- //@ts -ignore
256
- this . _position . _onValueChanged = ( ) => {
257
- this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix ) ;
258
- this . _updateWorldPositionFlag ( ) ;
259
- } ;
284
+ this . _onPositionChanged = this . _onPositionChanged . bind ( this ) ;
285
+ this . _onWorldPositionChanged = this . _onWorldPositionChanged . bind ( this ) ;
286
+ this . _onRotationChanged = this . _onRotationChanged . bind ( this ) ;
287
+ this . _onWorldRotationChanged = this . _onWorldRotationChanged . bind ( this ) ;
288
+ this . _onRotationQuaternionChanged = this . _onRotationQuaternionChanged . bind ( this ) ;
289
+ this . _onWorldRotationQuaternionChanged = this . _onWorldRotationQuaternionChanged . bind ( this ) ;
290
+ this . _onScaleChanged = this . _onScaleChanged . bind ( this ) ;
260
291
261
292
//@ts -ignore
262
- this . _worldPosition . _onValueChanged = ( ) => {
263
- const worldPosition = this . _worldPosition ;
264
- const parent = this . _getParentTransform ( ) ;
265
- if ( parent ) {
266
- Matrix . invert ( parent . worldMatrix , Transform . _tempMat41 ) ;
267
- Vector3 . transformCoordinate ( worldPosition , Transform . _tempMat41 , this . _position ) ;
268
- } else {
269
- worldPosition . cloneTo ( this . _position ) ;
270
- }
271
- this . _setDirtyFlagFalse ( TransformFlag . WorldPosition ) ;
272
- } ;
273
-
293
+ this . _position . _onValueChanged = this . _onPositionChanged ;
274
294
//@ts -ignore
275
- this . _rotation . _onValueChanged = ( ) => {
276
- this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix | TransformFlag . LocalQuat ) ;
277
- this . _setDirtyFlagFalse ( TransformFlag . LocalEuler ) ;
278
- this . _updateWorldRotationFlag ( ) ;
279
- } ;
280
-
295
+ this . _worldPosition . _onValueChanged = this . _onWorldPositionChanged ;
281
296
//@ts -ignore
282
- this . _worldRotation . _onValueChanged = ( ) => {
283
- const worldRotation = this . _worldRotation ;
284
- Quaternion . rotationEuler (
285
- MathUtil . degreeToRadian ( worldRotation . x ) ,
286
- MathUtil . degreeToRadian ( worldRotation . y ) ,
287
- MathUtil . degreeToRadian ( worldRotation . z ) ,
288
- this . _worldRotationQuaternion
289
- ) ;
290
- this . _setDirtyFlagFalse ( TransformFlag . WorldEuler ) ;
291
- } ;
292
-
297
+ this . _rotation . _onValueChanged = this . _onRotationChanged ;
293
298
//@ts -ignore
294
- this . _rotationQuaternion . _onValueChanged = ( ) => {
295
- this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix | TransformFlag . LocalEuler ) ;
296
- this . _setDirtyFlagFalse ( TransformFlag . LocalQuat ) ;
297
- this . _updateWorldRotationFlag ( ) ;
298
- } ;
299
-
299
+ this . _worldRotation . _onValueChanged = this . _onWorldRotationChanged ;
300
300
//@ts -ignore
301
- this . _worldRotationQuaternion . _onValueChanged = ( ) => {
302
- const worldRotationQuaternion = this . _worldRotationQuaternion ;
303
- const parent = this . _getParentTransform ( ) ;
304
- if ( parent ) {
305
- Quaternion . invert ( parent . worldRotationQuaternion , Transform . _tempQuat0 ) ;
306
- Quaternion . multiply ( worldRotationQuaternion , Transform . _tempQuat0 , this . _rotationQuaternion ) ;
307
- } else {
308
- worldRotationQuaternion . cloneTo ( this . _rotationQuaternion ) ;
309
- }
310
- this . _setDirtyFlagFalse ( TransformFlag . WorldQuat ) ;
311
- } ;
312
-
301
+ this . _rotationQuaternion . _onValueChanged = this . _onRotationQuaternionChanged ;
313
302
//@ts -ignore
314
- this . _scale . _onValueChanged = ( ) => {
315
- this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix ) ;
316
- this . _updateWorldScaleFlag ( ) ;
317
- } ;
303
+ this . _worldRotationQuaternion . _onValueChanged = this . _onWorldRotationQuaternionChanged ;
304
+ //@ts -ignore
305
+ this . _scale . _onValueChanged = this . _onScaleChanged ;
318
306
}
319
307
320
308
/**
@@ -511,11 +499,10 @@ export class Transform extends Component {
511
499
return ;
512
500
}
513
501
const rotMat = Transform . _tempMat43 ;
514
- const worldRotationQuaternion = this . _worldRotationQuaternion ;
515
502
516
503
worldUp = worldUp ?? Transform . _tempVec3 . setValue ( 0 , 1 , 0 ) ;
517
504
Matrix . lookAt ( position , worldPosition , worldUp , rotMat ) ;
518
- rotMat . getRotation ( worldRotationQuaternion ) . invert ( ) ;
505
+ Quaternion . invert ( rotMat . getRotation ( Transform . _tempQuat0 ) , this . _worldRotationQuaternion ) ;
519
506
}
520
507
521
508
/**
@@ -695,10 +682,8 @@ export class Transform extends Component {
695
682
private _rotateByQuat ( rotateQuat : Quaternion , relativeToLocal : boolean ) {
696
683
if ( relativeToLocal ) {
697
684
Quaternion . multiply ( this . rotationQuaternion , rotateQuat , this . _rotationQuaternion ) ;
698
- this . rotationQuaternion = this . _rotationQuaternion ;
699
685
} else {
700
686
Quaternion . multiply ( this . worldRotationQuaternion , rotateQuat , this . _worldRotationQuaternion ) ;
701
- this . worldRotationQuaternion = this . _worldRotationQuaternion ;
702
687
}
703
688
}
704
689
@@ -716,8 +701,65 @@ export class Transform extends Component {
716
701
Quaternion . rotationEuler ( x * radFactor , y * radFactor , z * radFactor , rotQuat ) ;
717
702
this . _rotateByQuat ( rotQuat , relativeToLocal ) ;
718
703
}
719
- }
720
704
705
+ private _onPositionChanged ( ) : void {
706
+ this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix ) ;
707
+ this . _updateWorldPositionFlag ( ) ;
708
+ }
709
+
710
+ private _onWorldPositionChanged ( ) : void {
711
+ const worldPosition = this . _worldPosition ;
712
+ const parent = this . _getParentTransform ( ) ;
713
+ if ( parent ) {
714
+ Matrix . invert ( parent . worldMatrix , Transform . _tempMat41 ) ;
715
+ Vector3 . transformCoordinate ( worldPosition , Transform . _tempMat41 , this . _position ) ;
716
+ } else {
717
+ worldPosition . cloneTo ( this . _position ) ;
718
+ }
719
+ this . _setDirtyFlagFalse ( TransformFlag . WorldPosition ) ;
720
+ }
721
+
722
+ private _onRotationChanged ( ) : void {
723
+ this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix | TransformFlag . LocalQuat ) ;
724
+ this . _setDirtyFlagFalse ( TransformFlag . LocalEuler ) ;
725
+ this . _updateWorldRotationFlag ( ) ;
726
+ }
727
+
728
+ private _onWorldRotationChanged ( ) : void {
729
+ const worldRotation = this . _worldRotation ;
730
+ Quaternion . rotationEuler (
731
+ MathUtil . degreeToRadian ( worldRotation . x ) ,
732
+ MathUtil . degreeToRadian ( worldRotation . y ) ,
733
+ MathUtil . degreeToRadian ( worldRotation . z ) ,
734
+ this . _worldRotationQuaternion
735
+ ) ;
736
+ this . _setDirtyFlagFalse ( TransformFlag . WorldEuler ) ;
737
+ }
738
+
739
+ private _onRotationQuaternionChanged ( ) : void {
740
+ this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix | TransformFlag . LocalEuler ) ;
741
+ this . _setDirtyFlagFalse ( TransformFlag . LocalQuat ) ;
742
+ this . _updateWorldRotationFlag ( ) ;
743
+ }
744
+
745
+ private _onWorldRotationQuaternionChanged ( ) : void {
746
+ const worldRotationQuaternion = this . _worldRotationQuaternion ;
747
+ const parent = this . _getParentTransform ( ) ;
748
+ if ( parent ) {
749
+ const invParentQuaternion = Transform . _tempQuat0 ;
750
+ Quaternion . invert ( parent . worldRotationQuaternion , invParentQuaternion ) ;
751
+ Quaternion . multiply ( worldRotationQuaternion , invParentQuaternion , this . _rotationQuaternion ) ;
752
+ } else {
753
+ worldRotationQuaternion . cloneTo ( this . _rotationQuaternion ) ;
754
+ }
755
+ this . _setDirtyFlagFalse ( TransformFlag . WorldQuat ) ;
756
+ }
757
+
758
+ private _onScaleChanged ( ) : void {
759
+ this . _setDirtyFlagTrue ( TransformFlag . LocalMatrix ) ;
760
+ this . _updateWorldScaleFlag ( ) ;
761
+ }
762
+ }
721
763
/**
722
764
* Dirty flag of transform.
723
765
*/
0 commit comments