Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 38 additions & 35 deletions editor/src/editor/layout/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { FaCamera, FaImage, FaLightbulb, FaBone } from "react-icons/fa";
import { SiAdobeindesign, SiBabylondotjs } from "react-icons/si";

import { AdvancedDynamicTexture } from "babylonjs-gui";
import { BaseTexture, Node, Scene, Sound, Tools, IParticleSystem, Sprite, Skeleton } from "babylonjs";
import { BaseTexture, Node, Scene, Sound, Tools, IParticleSystem, Sprite, Skeleton, TransformNode } from "babylonjs";

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

Expand Down Expand Up @@ -43,6 +43,7 @@ import { isAdvancedDynamicTexture } from "../../tools/guards/texture";
import { updateIblShadowsRenderPipeline } from "../../tools/light/ibl";
import { UniqueNumber, waitNextAnimationFrame } from "../../tools/tools";
import { getSpriteManagerNodeFromSprite } from "../../tools/sprite/tools";
import { applyTransformNodeParentingConfiguration } from "../../tools/node/parenting";
import { isSprite, isSpriteManagerNode, isSpriteMapNode } from "../../tools/guards/sprites";
import { isAnyParticleSystem, isGPUParticleSystem, isParticleSystem } from "../../tools/guards/particles";
import {
Expand Down Expand Up @@ -405,7 +406,7 @@ export class EditorGraph extends Component<IEditorGraphProps, IEditorGraphState>
/**
* Pastes the previously copied nodes.
*/
public pasteSelectedNodes(parent?: Node): void {
public pasteSelectedNodes(parent?: Node, shift?: boolean): void {
if (!this._objectsToCopy.length) {
return;
}
Expand Down Expand Up @@ -439,10 +440,12 @@ export class EditorGraph extends Component<IEditorGraphProps, IEditorGraphState>
newNodes.splice(0, newNodes.length);
},
redo: () => {
nodesToCopy.forEach((object) => {
let node: Node | IParticleSystem | Sprite | null = null;
const tempTransfromNode = new TransformNode("tempParent", this.props.editor.layout.preview.scene);

try {
nodesToCopy.forEach((object) => {
let node: Node | IParticleSystem | Sprite | null = null;

defer: {
if (isAbstractMesh(object)) {
const suffix = "(Instanced Mesh)";
const name = isInstancedMesh(object) ? object.name : `${object.name.replace(` ${suffix}`, "")} ${suffix}`;
Expand All @@ -456,45 +459,45 @@ export class EditorGraph extends Component<IEditorGraphProps, IEditorGraphState>

const collisionMesh = getCollisionMeshFor(instance.sourceMesh);
collisionMesh?.updateInstances(instance.sourceMesh);

break defer;
}

if (isParticleSystem(object) && isAbstractMesh(parent)) {
} else if (isParticleSystem(object) && isAbstractMesh(parent)) {
const suffix = "(Clone)";
const name = `${object.name.replace(` ${suffix}`, "")} ${suffix}`;

node = object.clone(name, parent, false);

break defer;
}

if (isNode(object) || isSprite(object)) {
} else if (isNode(object) || isSprite(object)) {
node = cloneNode(this.props.editor, object);
break defer;
}
}

if (node) {
if (!isSprite(node)) {
node.id = Tools.RandomId();
}

node.uniqueId = UniqueNumber.Get();

if (parent && isNode(node)) {
node.parent = parent;
}

if (isAbstractMesh(node)) {
this.props.editor.layout.preview.scene.lights
.map((light) => light.getShadowGenerator())
.forEach((generator) => generator?.getShadowMap()?.renderList?.push(node));
if (node) {
if (!isSprite(node)) {
node.id = Tools.RandomId();
}

node.uniqueId = UniqueNumber.Get();

if (parent && isNode(node)) {
if (shift && isNode(object)) {
node.parent = object.parent;
applyTransformNodeParentingConfiguration(node, parent, tempTransfromNode);
} else {
node.parent = parent;
}
}

if (isAbstractMesh(node)) {
this.props.editor.layout.preview.scene.lights
.map((light) => light.getShadowGenerator())
.forEach((generator) => generator?.getShadowMap()?.renderList?.push(node));
}

newNodes.push(node);
}
});
} catch (e) {
console.error(e);
}

newNodes.push(node);
}
});
tempTransfromNode.dispose(false, true);
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion editor/src/editor/layout/graph/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class EditorGraphContextMenu extends Component<IEditorGraphContextMenuPro
{isNode(this.props.object) && (
<ContextMenuItem
disabled={this.props.editor.layout.graph._objectsToCopy.length === 0}
onClick={() => this.props.editor.layout.graph.pasteSelectedNodes(this.props.object)}
onClick={(ev) => this.props.editor.layout.graph.pasteSelectedNodes(this.props.object, ev.shiftKey)}
>
Paste <ContextMenuShortcut>{platform() === "darwin" ? "⌘+V" : "CTRL+V"}</ContextMenuShortcut>
</ContextMenuItem>
Expand Down
71 changes: 45 additions & 26 deletions editor/src/editor/layout/graph/label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DragEvent, useEffect, useRef, useState } from "react";
import { FaLock } from "react-icons/fa";
import { useEventListener } from "usehooks-ts";

import { Node, TransformNode, AbstractMesh, Vector3 } from "babylonjs";
import { TransformNode, AbstractMesh, Vector3 } from "babylonjs";

import { Input } from "../../../ui/shadcn/ui/input";

Expand All @@ -16,6 +16,7 @@ import { registerUndoRedo } from "../../../tools/undoredo";
import { isAnyParticleSystem } from "../../../tools/guards/particles";
import { isNodeSerializable, isNodeLocked } from "../../../tools/node/metadata";
import { isAbstractMesh, isInstancedMesh, isMesh, isNode, isTransformNode } from "../../../tools/guards/nodes";
import { applyNodeParentingConfiguration, applyTransformNodeParentingConfiguration, IOldNodeHierarchyConfiguration } from "../../../tools/node/parenting";

import { applySoundAsset } from "../preview/import/sound";
import { applyTextureAssetToObject } from "../preview/import/texture";
Expand Down Expand Up @@ -115,7 +116,7 @@ export function EditorGraphLabel(props: IEditorGraphLabelProps) {

const node = ev.dataTransfer.getData("graph/node");
if (node) {
return dropNodeFromGraph();
return dropNodeFromGraph(ev.shiftKey);
}

const asset = ev.dataTransfer.getData("assets");
Expand All @@ -124,7 +125,7 @@ export function EditorGraphLabel(props: IEditorGraphLabelProps) {
}
}

function dropNodeFromGraph() {
function dropNodeFromGraph(shift: boolean) {
const nodesToMove = props.editor.layout.graph.getSelectedNodes();

const newParent = props.object;
Expand All @@ -138,7 +139,13 @@ export function EditorGraphLabel(props: IEditorGraphLabelProps) {
return;
}

return oldHierarchyMap.set(n.nodeData, n.nodeData.parent);
return oldHierarchyMap.set(n.nodeData, {
parent: n.nodeData.parent,
position: n.nodeData["position"]?.clone(),
rotation: n.nodeData["rotation"]?.clone(),
scaling: n.nodeData["scaling"]?.clone(),
rotationQuaternion: n.nodeData["rotationQuaternion"]?.clone(),
} as IOldNodeHierarchyConfiguration);
}

if (isSound(n.nodeData)) {
Expand All @@ -161,7 +168,7 @@ export function EditorGraphLabel(props: IEditorGraphLabelProps) {
nodesToMove.forEach((n) => {
if (n.nodeData && oldHierarchyMap.has(n.nodeData)) {
if (isNode(n.nodeData)) {
return (n.nodeData.parent = oldHierarchyMap.get(n.nodeData) as Node);
return applyNodeParentingConfiguration(n.nodeData, oldHierarchyMap.get(n.nodeData) as IOldNodeHierarchyConfiguration);
}

if (isSound(n.nodeData)) {
Expand All @@ -184,36 +191,48 @@ export function EditorGraphLabel(props: IEditorGraphLabelProps) {
});
},
redo: () => {
nodesToMove.forEach((n) => {
if (n.nodeData === props.object) {
return;
}
const tempTransfromNode = new TransformNode("tempParent", props.editor.layout.preview.scene);

if (n.nodeData && oldHierarchyMap.has(n.nodeData)) {
if (isNode(n.nodeData)) {
return (n.nodeData.parent = isScene(props.object) ? null : newParent);
try {
nodesToMove.forEach((n) => {
if (n.nodeData === props.object) {
return;
}

if (isSound(n.nodeData)) {
if (isTransformNode(newParent) || isMesh(newParent) || isInstancedMesh(newParent)) {
return n.nodeData.attachToMesh(newParent);
if (n.nodeData && oldHierarchyMap.has(n.nodeData)) {
if (isNode(n.nodeData)) {
if (shift) {
return applyTransformNodeParentingConfiguration(n.nodeData, newParent, tempTransfromNode);
}

return (n.nodeData.parent = isScene(props.object) ? null : newParent);
}

if (isScene(newParent)) {
n.nodeData.detachFromMesh();
n.nodeData.spatialSound = false;
n.nodeData.setPosition(Vector3.Zero());
return (n.nodeData["_connectedTransformNode"] = null);
if (isSound(n.nodeData)) {
if (isTransformNode(newParent) || isMesh(newParent) || isInstancedMesh(newParent)) {
return n.nodeData.attachToMesh(newParent);
}

if (isScene(newParent)) {
n.nodeData.detachFromMesh();
n.nodeData.spatialSound = false;
n.nodeData.setPosition(Vector3.Zero());
return (n.nodeData["_connectedTransformNode"] = null);
}
}
}

if (isAnyParticleSystem(n.nodeData)) {
if (isAbstractMesh(newParent)) {
return (n.nodeData.emitter = newParent);
if (isAnyParticleSystem(n.nodeData)) {
if (isAbstractMesh(newParent)) {
return (n.nodeData.emitter = newParent);
}
}
}
}
});
});
} catch (e) {
console.error(e);
}

tempTransfromNode.dispose(false, true);
},
});

Expand Down
77 changes: 77 additions & 0 deletions editor/src/tools/node/parenting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Node, Vector3, Quaternion, TransformNode } from "babylonjs";

import { isScene } from "../guards/scene";
import { isAbstractMesh, isAnyTransformNode, isDirectionalLight, isPointLight, isSpotLight } from "../guards/nodes";

export interface IOldNodeHierarchyConfiguration {
parent: Node | null;
position?: Vector3;
rotation?: Vector3;
scaling?: Vector3;
rotationQuaternion?: Quaternion;
}

export function getNodeParentingConfiguration(node: Node) {
return {
parent: node.parent,
position: node["position"]?.clone(),
rotation: node["rotation"]?.clone(),
scaling: node["scaling"]?.clone(),
rotationQuaternion: node["rotationQuaternion"]?.clone(),
} as IOldNodeHierarchyConfiguration;
}

export function applyNodeParentingConfiguration(node: Node, config: IOldNodeHierarchyConfiguration) {
node.parent = config.parent;
if (config.position) {
node["position"]?.copyFrom(config.position);
}
if (config.rotation) {
node["rotation"]?.copyFrom(config.rotation);
}
if (config.scaling) {
node["scaling"]?.copyFrom(config.scaling);
}
if (config.rotationQuaternion) {
node["rotationQuaternion"]?.copyFrom(config.rotationQuaternion);
}
}

export function applyTransformNodeParentingConfiguration(node: Node, newParent: Node | null, tempTransfromNode: TransformNode) {
tempTransfromNode.parent = node.parent;
tempTransfromNode.position.setAll(0);
tempTransfromNode.rotation.setAll(0);
tempTransfromNode.scaling.setAll(1);
tempTransfromNode.rotationQuaternion = null;

if (isAbstractMesh(node) || isAnyTransformNode(node)) {
tempTransfromNode.position.copyFrom(node.position);
tempTransfromNode.rotation.copyFrom(node.rotation);
tempTransfromNode.scaling.copyFrom(node.scaling);
tempTransfromNode.rotationQuaternion = node.rotationQuaternion?.clone() || null;
}

if (isPointLight(node) || isDirectionalLight(node) || isSpotLight(node)) {
tempTransfromNode.position.copyFrom(node.position);
}

tempTransfromNode.computeWorldMatrix(true);

const effectiveParent = isScene(newParent) ? null : newParent;
tempTransfromNode.setParent(effectiveParent);

node.parent = effectiveParent;

if (isAbstractMesh(node) || isAnyTransformNode(node)) {
node.position.copyFrom(tempTransfromNode.position);
node.rotation.copyFrom(tempTransfromNode.rotation);
node.scaling.copyFrom(tempTransfromNode.scaling);
if (node.rotationQuaternion) {
node.rotationQuaternion.copyFrom(tempTransfromNode.rotationQuaternion!);
}
}

if (isPointLight(node) || isDirectionalLight(node) || isSpotLight(node)) {
node.position.copyFrom(tempTransfromNode.position);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"build-website": "yarn workspace babylonjs-editor-website build",
"build": "yarn build-tools && yarn build-editor && yarn build-plugins",
"build-all": "yarn build && yarn build-templates && yarn build-website",
"build-concurrently": "yarn build-tools && yarn build-editor && concurrently \"yarn workspace babylonjs-editor-plugins build-concurrently\" \"yarn workspace babylonjs-editor-templates build-concurrently\" \"yarn workspace babylonjs-editor-website build\" -c bgYellow.bold,bgBlue.bold,bgGreen.bold --names plugins,templates,website",
"build-all-concurrently": "yarn build-tools && yarn build-editor && concurrently \"yarn workspace babylonjs-editor-plugins build-concurrently\" \"yarn workspace babylonjs-editor-templates build-concurrently\" \"yarn workspace babylonjs-editor-website build\" -c bgYellow.bold,bgBlue.bold,bgGreen.bold --names plugins,templates,website",
"start": "yarn workspace babylonjs-editor start",
"package": "yarn clean && yarn clean:node_modules && yarn && yarn lint && yarn build-all && yarn test && node build.mjs",
"package": "yarn clean && yarn clean:node_modules && yarn && yarn lint && yarn build-all-concurrently && yarn test && node build.mjs",
"clean": "rimraf editor/build editor/declaration editor/electron-packages editor/coverage tools/build tools/declaration tools/coverage website/.next && yarn workspace babylonjs-editor-plugins clean && yarn workspace babylonjs-editor-templates clean",
"clean:node_modules": "rm -rf editor/node_modules tools/node_modules plugins/**/node_modules templates/**/node_modules website/node_modules node_modules",
"test": "yarn workspace babylonjs-editor-tools test && yarn workspace babylonjs-editor test",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3997,7 +3997,7 @@ babylonjs-editor@latest:
axios "1.12.0"
babylonjs "8.41.0"
babylonjs-addons "8.41.0"
babylonjs-editor-tools "link:../../AppData/Local/Yarn/Cache/v6/npm-babylonjs-editor-5.2.4-3cce3a704dc0c4572a85041a993264060376230a-integrity/node_modules/tools"
babylonjs-editor-tools "link:../../../Library/Caches/Yarn/v6/npm-babylonjs-editor-5.2.4-3cce3a704dc0c4572a85041a993264060376230a-integrity/node_modules/tools"
babylonjs-gui "8.41.0"
babylonjs-gui-editor "8.41.0"
babylonjs-loaders "8.41.0"
Expand Down