Skip to content

Commit

Permalink
Fix transform parent dirty error when added as root entity (#2311)
Browse files Browse the repository at this point in the history
* fix: transform parent dirty error when added as root entity
  • Loading branch information
cptbtptpbcptdtptp authored Aug 7, 2024
1 parent f01c996 commit 17ccfb4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
23 changes: 13 additions & 10 deletions packages/core/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,19 @@ export class Entity extends EngineObject {
this._setActiveComponents(false, activeChangeFlag);
}

/**
* @internal
*/
_setTransformDirty() {
if (this.transform) {
this.transform._parentChange();
} else {
for (let i = 0, len = this._children.length; i < len; i++) {
this._children[i]._setTransformDirty();
}
}
}

private _addToChildrenList(index: number, child: Entity): void {
const children = this._children;
const childCount = children.length;
Expand Down Expand Up @@ -707,16 +720,6 @@ export class Entity extends EngineObject {
}
}

private _setTransformDirty() {
if (this.transform) {
this.transform._parentChange();
} else {
for (let i = 0, len = this._children.length; i < len; i++) {
this._children[i]._setTransformDirty();
}
}
}

private _setSiblingIndex(sibling: Entity[], target: number): void {
target = Math.min(target, sibling.length - 1);
if (target < 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export class Scene extends EngineObject {
if (!isRoot) {
entity._isRoot = true;
entity._removeFromParent();
entity._setTransformDirty();
}

// Add or remove from scene's rootEntities
Expand Down
30 changes: 27 additions & 3 deletions tests/src/core/Transform.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MathUtil, Matrix, Ray, Vector2, Vector3, Vector4 } from "@galacean/engine-math";
import { Entity, Scene } from "@galacean/engine-core";
import { Vector3 } from "@galacean/engine-math";
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { Camera, Entity } from "@galacean/engine-core";
import { expect } from "chai";

const canvasDOM = document.createElement("canvas");
Expand All @@ -9,9 +9,11 @@ canvasDOM.height = 1024;

describe("Transform test", function () {
let entity: Entity;
let scene: Scene;
before(async function () {
const engine = await WebGLEngine.create({ canvas: canvasDOM });
entity = engine.sceneManager.activeScene.createRootEntity();
scene = engine.sceneManager.scenes[0];
entity = scene.createRootEntity();
});

it("World direction", () => {
Expand All @@ -23,4 +25,26 @@ describe("Transform test", function () {
expect(transform.worldRight).to.deep.equal(new Vector3(0.7071067811865476, 0, -0.7071067811865476));
expect(transform.worldUp).to.deep.equal(new Vector3(0, 1, 0));
});

it("Parent Dirty", () => {
const root1 = scene.createRootEntity();
const root2 = scene.createRootEntity();
root1.transform.setPosition(1, 1, 1);
root2.transform.setPosition(0, 0, 0);

let worldPosition = root2.transform.worldPosition;
expect(worldPosition.x).to.equal(0);
expect(worldPosition.y).to.equal(0);
expect(worldPosition.z).to.equal(0);
root1.addChild(root2);
worldPosition = root2.transform.worldPosition;
expect(worldPosition.x).to.equal(1);
expect(worldPosition.y).to.equal(1);
expect(worldPosition.z).to.equal(1);
scene.addRootEntity(root2);
worldPosition = root2.transform.worldPosition;
expect(worldPosition.x).to.equal(0);
expect(worldPosition.y).to.equal(0);
expect(worldPosition.z).to.equal(0);
});
});

0 comments on commit 17ccfb4

Please sign in to comment.