Skip to content

Commit 9281914

Browse files
committed
fix: Preserve world transform on graph parenting
1 parent 7f55483 commit 9281914

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

editor/src/editor/layout/graph.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { IoCheckmark, IoSparklesSharp } from "react-icons/io5";
1414
import { SiAdobeindesign, SiBabylondotjs } from "react-icons/si";
1515

1616
import { AdvancedDynamicTexture } from "babylonjs-gui";
17-
import { BaseTexture, Node, Scene, Sound, Tools, IParticleSystem, ParticleSystem } from "babylonjs";
17+
import { BaseTexture, Node, Scene, Sound, Tools, TransformNode, IParticleSystem, ParticleSystem } from "babylonjs";
1818

1919
import { Editor } from "../main";
2020

@@ -399,7 +399,7 @@ export class EditorGraph extends Component<IEditorGraphProps, IEditorGraphState>
399399
instance.rotation.copyFrom(object.rotation);
400400
instance.scaling.copyFrom(object.scaling);
401401
instance.rotationQuaternion = object.rotationQuaternion?.clone() ?? null;
402-
instance.parent = object.parent;
402+
this._setParentPreservingWorldTransform(instance, object.parent);
403403

404404
const collisionMesh = getCollisionMeshFor(instance.sourceMesh);
405405
collisionMesh?.updateInstances(instance.sourceMesh);
@@ -427,7 +427,7 @@ export class EditorGraph extends Component<IEditorGraphProps, IEditorGraphState>
427427
node.uniqueId = UniqueNumber.Get();
428428

429429
if (parent && isNode(node)) {
430-
node.parent = parent;
430+
this._setParentPreservingWorldTransform(node, parent);
431431
}
432432

433433
if (isAbstractMesh(node)) {
@@ -899,4 +899,32 @@ export class EditorGraph extends Component<IEditorGraphProps, IEditorGraphState>
899899

900900
this.refresh();
901901
}
902+
903+
private _setParentPreservingWorldTransform(node: Node, newParent: Node | null): void {
904+
if (!(node instanceof TransformNode)) {
905+
// For non-transform nodes, just set the parent directly
906+
node.parent = newParent;
907+
return;
908+
}
909+
910+
// Store the current world transform
911+
const worldPosition = node.getAbsolutePosition();
912+
const worldRotation = node.rotationQuaternion || node.rotation.toQuaternion();
913+
const worldScaling = node.absoluteScaling;
914+
915+
// Set the new parent
916+
node.parent = newParent;
917+
918+
// Restore the world transform
919+
node.setAbsolutePosition(worldPosition);
920+
921+
if (node.rotationQuaternion) {
922+
node.absoluteRotationQuaternion.copyFrom(worldRotation);
923+
} else {
924+
const rotationVector = worldRotation.toEulerAngles();
925+
node.rotation.copyFrom(rotationVector);
926+
}
927+
928+
node.scaling.copyFrom(worldScaling);
929+
}
902930
}

0 commit comments

Comments
 (0)