diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index cfe9f9de4f..be0f29ceb9 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -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; @@ -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) { diff --git a/packages/core/src/Scene.ts b/packages/core/src/Scene.ts index 0dc8ff9936..ecbcf66f28 100644 --- a/packages/core/src/Scene.ts +++ b/packages/core/src/Scene.ts @@ -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 diff --git a/tests/src/core/Transform.test.ts b/tests/src/core/Transform.test.ts index 0174b64f26..5a0eb9a734 100644 --- a/tests/src/core/Transform.test.ts +++ b/tests/src/core/Transform.test.ts @@ -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"); @@ -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", () => { @@ -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); + }); });