Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
50fd505
feat: update code
cptbtptpbcptdtptp Jul 8, 2025
1c8446c
feat: widget
cptbtptpbcptdtptp Jul 9, 2025
76c3200
feat: update code
cptbtptpbcptdtptp Jul 9, 2025
a219a9c
feat: update code
cptbtptpbcptdtptp Jul 9, 2025
730673b
feat: update code
cptbtptpbcptdtptp Jul 9, 2025
829dce7
feat: update code
cptbtptpbcptdtptp Jul 9, 2025
08de1e3
fix: unit test
cptbtptpbcptdtptp Jul 10, 2025
0820fd8
feat: udpate code
cptbtptpbcptdtptp Jul 20, 2025
a743375
fix: update code
cptbtptpbcptdtptp Jul 21, 2025
000f7ca
feat: udpate code
cptbtptpbcptdtptp Jul 21, 2025
8aa37fd
feat: update code
cptbtptpbcptdtptp Jul 21, 2025
0c6df5d
feat: udpate code
cptbtptpbcptdtptp Jul 21, 2025
2481ac0
feat: udpate code
cptbtptpbcptdtptp Jul 21, 2025
a5a7151
feat: update code
cptbtptpbcptdtptp Jul 23, 2025
9a95c0f
feat: udpate code
cptbtptpbcptdtptp Jul 23, 2025
1f9538e
feat: update code
cptbtptpbcptdtptp Jul 23, 2025
78ba4c6
feat: update code
cptbtptpbcptdtptp Jul 23, 2025
7c655f9
feat: updata code
cptbtptpbcptdtptp Jul 23, 2025
964ed0e
feat: lint code
cptbtptpbcptdtptp Jul 23, 2025
ecc917b
feat: update code
cptbtptpbcptdtptp Jul 31, 2025
165226f
feat: add unit test
cptbtptpbcptdtptp Aug 10, 2025
0fcd8a1
feat: update code
cptbtptpbcptdtptp Aug 11, 2025
730e529
feat: update code
cptbtptpbcptdtptp Aug 11, 2025
7be740c
fix: update code
cptbtptpbcptdtptp Aug 22, 2025
eae3432
feat: udpate code
cptbtptpbcptdtptp Aug 26, 2025
c5424cf
feat: udpate code
cptbtptpbcptdtptp Aug 26, 2025
c03c17c
feat: udpate code
cptbtptpbcptdtptp Aug 26, 2025
1c95b08
feat: udpate code
cptbtptpbcptdtptp Aug 26, 2025
e5de15c
feat: update code
cptbtptpbcptdtptp Aug 26, 2025
9aa0ea4
Merge branch 'dev/1.6' into feat/Widget
cptbtptpbcptdtptp Aug 26, 2025
ce2877c
feat: update code
cptbtptpbcptdtptp Aug 28, 2025
df970ec
fix: udpate code
cptbtptpbcptdtptp Sep 3, 2025
5a0a654
fix: update code
cptbtptpbcptdtptp Sep 3, 2025
6e3c6b9
feat: update code
cptbtptpbcptdtptp Sep 3, 2025
2ea2f4e
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
416042b
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
9a78804
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
087aa67
feat: udpate code
cptbtptpbcptdtptp Sep 4, 2025
5cf9d11
feat: udpate code
cptbtptpbcptdtptp Sep 4, 2025
74e37b3
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
73bd148
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
0072d66
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
d39c1eb
feat: update code
cptbtptpbcptdtptp Sep 4, 2025
d323db9
feat: udpate code
cptbtptpbcptdtptp Sep 5, 2025
a41a1bc
feat: udpate code
cptbtptpbcptdtptp Sep 5, 2025
a8cffc4
refactor: opt code
GuoLei1990 Sep 5, 2025
6bd7dc5
refactor: opt code
GuoLei1990 Sep 5, 2025
8988b34
refactor: opt code
GuoLei1990 Sep 5, 2025
24225be
refactor: opt code
GuoLei1990 Sep 5, 2025
baab21a
refactor: opt code
GuoLei1990 Sep 5, 2025
d15e41e
refactor: opt comments
GuoLei1990 Sep 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 74 additions & 83 deletions packages/core/src/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Transform extends Component {
private _worldUp: Vector3 = null;

@ignoreClone
private _isParentDirty: boolean = true;
protected _isParentDirty: boolean = true;
@ignoreClone
private _parentTransformCache: Transform = null;
private _dirtyFlag: number = TransformModifyFlags.WmWpWeWqWs;
Expand Down Expand Up @@ -261,16 +261,18 @@ export class Transform extends Component {
const { _position: position, _rotationQuaternion: rotationQuaternion, _scale: scale } = this;
// @ts-ignore
position._onValueChanged = rotationQuaternion._onValueChanged = scale._onValueChanged = null;
this._localMatrix.decompose(position, rotationQuaternion, scale);

value.decompose(position, rotationQuaternion, scale);
this._onLocalMatrixChanging?.();
this._setDirtyFlagTrue(TransformModifyFlags.LocalEuler);
this._setDirtyFlagFalse(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);

Comment on lines +265 to +269
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle Matrix.decompose() failure to avoid corrupt local state

Matrix.decompose(...) returns boolean; ignoring a false result can leave position/rotation/scale inconsistent (NaNs/garbage). Add a guard and a safe fallback.

Apply:

-    value.decompose(position, rotationQuaternion, scale);
-    this._onLocalMatrixChanging?.();
-    this._setDirtyFlagTrue(TransformModifyFlags.LocalEuler);
-    this._setDirtyFlagFalse(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);
+    const ok = value.decompose(position, rotationQuaternion, scale);
+    this._onLocalMatrixChanging?.();
+    if (!ok) {
+      // Fallback: extract translation; reset rotation to identity and scale to 1s.
+      value.getTranslation(position);
+      rotationQuaternion.set(0, 0, 0, 1);
+      scale.set(1, 1, 1);
+    }
+    this._setDirtyFlagTrue(TransformModifyFlags.LocalEuler);
+    this._setDirtyFlagFalse(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
value.decompose(position, rotationQuaternion, scale);
this._onLocalMatrixChanging?.();
this._setDirtyFlagTrue(TransformModifyFlags.LocalEuler);
this._setDirtyFlagFalse(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);
const ok = value.decompose(position, rotationQuaternion, scale);
this._onLocalMatrixChanging?.();
if (!ok) {
// Fallback: extract translation; reset rotation to identity and scale to defaults.
value.getTranslation(position);
rotationQuaternion.set(0, 0, 0, 1);
scale.set(1, 1, 1);
}
this._setDirtyFlagTrue(TransformModifyFlags.LocalEuler);
this._setDirtyFlagFalse(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);
🤖 Prompt for AI Agents
In packages/core/src/Transform.ts around lines 265-269, Matrix.decompose(...)
return value is ignored which can leave position/rotation/scale with NaNs on
failure; change the code to check the boolean result and only apply the
dirty-flag logic when decompose succeeds. On failure, restore previous
position/rotationQuaternion/scale (capture them before calling decompose) or set
safe defaults (position = zero, rotationQuaternion = identity, scale =
Vector3.One), and avoid marking LocalEuler as dirty while clearing
LocalMatrix|LocalQuat as appropriate; optionally log or warn about the decompose
failure for debugging.

// @ts-ignore
position._onValueChanged = this._onPositionChanged;
// @ts-ignore
rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged;
// @ts-ignore
scale._onValueChanged = this._onScaleChanged;

this._setDirtyFlagTrue(TransformModifyFlags.LocalEuler);
this._setDirtyFlagFalse(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);
const localUniformScaling = scale.x === scale.y && scale.y === scale.z;
if (this._localUniformScaling !== localUniformScaling) {
this._localUniformScaling = localUniformScaling;
Expand Down Expand Up @@ -309,7 +311,7 @@ export class Transform extends Component {
this._localMatrix.copyFrom(value);
}
this.localMatrix = this._localMatrix;
this._setDirtyFlagFalse(TransformModifyFlags.WorldMatrix);
this._onWorldMatrixChange();
}

/**
Expand Down Expand Up @@ -584,22 +586,71 @@ export class Transform extends Component {
target._scale.copyFrom(this.scale);
}

protected override _onDestroy(): void {
super._onDestroy();
//@ts-ignore
this._worldPosition._onValueChanged = null;
//@ts-ignore
this._rotation._onValueChanged = null;
//@ts-ignore
this._worldRotation._onValueChanged = null;
//@ts-ignore
this._rotationQuaternion._onValueChanged = null;
//@ts-ignore
this._worldRotationQuaternion._onValueChanged = null;
//@ts-ignore
this._position._onValueChanged = null;
//@ts-ignore
this._scale._onValueChanged = null;
protected _onLocalMatrixChanging?(): void;

protected _onWorldMatrixChange() {
this._setDirtyFlagFalse(TransformModifyFlags.WorldMatrix);
}

protected _isContainDirtyFlags(targetDirtyFlags: number): boolean {
return (this._dirtyFlag & targetDirtyFlags) === targetDirtyFlags;
}

protected _isContainDirtyFlag(type: number): boolean {
return (this._dirtyFlag & type) != 0;
}

protected _setDirtyFlagTrue(type: number) {
this._dirtyFlag |= type;
}

protected _setDirtyFlagFalse(type: number) {
this._dirtyFlag &= ~type;
}

protected _worldAssociatedChange(type: number): void {
const dispatchFlags = type & ~this._dirtyFlag;
this._dirtyFlag |= type;
this._entity._updateFlagManager.dispatch(dispatchFlags);
}

protected _getParentTransform(): Transform | null {
if (!this._isParentDirty) {
return this._parentTransformCache;
}
let parentCache: Transform = null;
let parent = this._entity.parent;
while (parent) {
const transform = parent.transform;
if (transform) {
parentCache = transform;
break;
} else {
parent = parent.parent;
}
}
this._parentTransformCache = parentCache;
this._isParentDirty = false;
return parentCache;
}

@ignoreClone
protected _onPositionChanged(): void {
this._setDirtyFlagTrue(TransformModifyFlags.LocalMatrix);
this._updateWorldPositionFlag();
}

@ignoreClone
protected _onWorldPositionChanged(): void {
const worldPosition = this._worldPosition;
const parent = this._getParentTransform();
if (parent) {
Matrix.invert(parent.worldMatrix, Transform._tempMat41);
Vector3.transformCoordinate(worldPosition, Transform._tempMat41, this._position);
} else {
this._position.copyFrom(worldPosition);
}
this._setDirtyFlagFalse(TransformModifyFlags.WorldPosition);
}

/**
Expand Down Expand Up @@ -708,26 +759,6 @@ export class Transform extends Component {
}
}

private _getParentTransform(): Transform | null {
if (!this._isParentDirty) {
return this._parentTransformCache;
}
let parentCache: Transform = null;
let parent = this._entity.parent;
while (parent) {
const transform = parent.transform;
if (transform) {
parentCache = transform;
break;
} else {
parent = parent.parent;
}
}
this._parentTransformCache = parentCache;
this._isParentDirty = false;
return parentCache;
}

private _getScaleMatrix(): Matrix3x3 {
const invRotation = Transform._tempQuat0;
const invRotationMat = Transform._tempMat30;
Expand All @@ -740,27 +771,6 @@ export class Transform extends Component {
return scaMat;
}

private _isContainDirtyFlags(targetDirtyFlags: number): boolean {
return (this._dirtyFlag & targetDirtyFlags) === targetDirtyFlags;
}

private _isContainDirtyFlag(type: number): boolean {
return (this._dirtyFlag & type) != 0;
}

private _setDirtyFlagTrue(type: number) {
this._dirtyFlag |= type;
}

private _setDirtyFlagFalse(type: number) {
this._dirtyFlag &= ~type;
}

private _worldAssociatedChange(type: number): void {
this._dirtyFlag |= type;
this._entity._updateFlagManager.dispatch(type);
}

private _rotateByQuat(rotateQuat: Quaternion, relativeToLocal: boolean): void {
if (relativeToLocal) {
Quaternion.multiply(this.rotationQuaternion, rotateQuat, this._rotationQuaternion);
Expand All @@ -786,25 +796,6 @@ export class Transform extends Component {
this._rotateByQuat(rotQuat, relativeToLocal);
}

@ignoreClone
private _onPositionChanged(): void {
this._setDirtyFlagTrue(TransformModifyFlags.LocalMatrix);
this._updateWorldPositionFlag();
}

@ignoreClone
private _onWorldPositionChanged(): void {
const worldPosition = this._worldPosition;
const parent = this._getParentTransform();
if (parent) {
Matrix.invert(parent.worldMatrix, Transform._tempMat41);
Vector3.transformCoordinate(worldPosition, Transform._tempMat41, this._position);
} else {
this._position.copyFrom(worldPosition);
}
this._setDirtyFlagFalse(TransformModifyFlags.WorldPosition);
}

@ignoreClone
private _onRotationChanged(): void {
this._setDirtyFlagTrue(TransformModifyFlags.LocalMatrix | TransformModifyFlags.LocalQuat);
Expand Down Expand Up @@ -884,7 +875,7 @@ export class Transform extends Component {
}

/**
* @internal
* Dirty flag of transform.
*/
export enum TransformModifyFlags {
LocalEuler = 0x1,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export { Script } from "./Script";
export { Renderer, RendererUpdateFlags } from "./Renderer";
export { dependentComponents, DependentMode } from "./ComponentsDependencies";
export { Camera } from "./Camera";
export { Transform } from "./Transform";
export { Transform, TransformModifyFlags } from "./Transform";
export { BoolUpdateFlag } from "./BoolUpdateFlag";
export type { EngineSettings } from "./EngineSettings";
export type { EngineConfiguration } from "./Engine";
Expand Down
Loading
Loading