Skip to content

Commit 5efd7c7

Browse files
deltakoshDavid Catuhe
andauthored
Add support for more features in the particules to NPE converter (#17050)
Co-authored-by: David Catuhe <[email protected]>
1 parent a6add41 commit 5efd7c7

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

packages/dev/core/src/Particles/Node/Blocks/systemBlock.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { _TriggerSubEmitter } from "./Triggers/triggerTools";
1212
* Block used to get a system of particles
1313
*/
1414
export class SystemBlock extends NodeParticleBlock {
15+
private static _IdCounter = 0;
16+
1517
/**
1618
* Gets or sets the blend mode for the particle system
1719
*/
@@ -58,6 +60,9 @@ export class SystemBlock extends NodeParticleBlock {
5860
@editableInPropertyPage("Do no start", PropertyTypeForEdition.Boolean, "ADVANCED", { embedded: true, notifiers: { rebuild: true } })
5961
public doNoStart = false;
6062

63+
/** @internal */
64+
public _internalId = SystemBlock._IdCounter++;
65+
6166
/**
6267
* Create a new SystemBlock
6368
* @param name defines the block name

packages/dev/core/src/Particles/Node/nodeParticleSystemSet.helper.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ function _CreateAndConnectInput(connectionPoint: NodeParticleConnectionPoint, na
3232
async function _ExtractDatafromParticleSystemAsync(particleSystem: ParticleSystem, target: NodeParticleSystemSet) {
3333
// Main system
3434
const system = new SystemBlock(particleSystem.name);
35+
system.capacity = particleSystem.getCapacity();
36+
system.emitRate = particleSystem.emitRate;
3537

3638
// Create particle
3739
const createParticleBlock = new CreateParticleBlock("Create particle");
@@ -107,11 +109,37 @@ async function _ExtractDatafromParticleSystemAsync(particleSystem: ParticleSyste
107109
const color1Block = new ParticleInputBlock("Color1");
108110
color1Block.value = particleSystem.color2;
109111

110-
const randomBlock = new ParticleRandomBlock("Random");
111-
color0Block.output.connectTo(randomBlock.min);
112-
color1Block.output.connectTo(randomBlock.max);
112+
const randomColorBlock = new ParticleRandomBlock("Random Color");
113+
color0Block.output.connectTo(randomColorBlock.min);
114+
color1Block.output.connectTo(randomColorBlock.max);
113115

114-
randomBlock.output.connectTo(createParticleBlock.color);
116+
randomColorBlock.output.connectTo(createParticleBlock.color);
117+
118+
// Emit power
119+
const minEmitPowerBlock = new ParticleInputBlock("Min Emit Power");
120+
minEmitPowerBlock.value = particleSystem.minEmitPower;
121+
122+
const maxEmitPowerBlock = new ParticleInputBlock("Max Emit Power");
123+
maxEmitPowerBlock.value = particleSystem.maxEmitPower;
124+
125+
const randomEmitPowerBlock = new ParticleRandomBlock("Random Emit Power");
126+
minEmitPowerBlock.output.connectTo(randomEmitPowerBlock.min);
127+
maxEmitPowerBlock.output.connectTo(randomEmitPowerBlock.max);
128+
129+
randomEmitPowerBlock.output.connectTo(createParticleBlock.emitPower);
130+
131+
// Lifetime
132+
const minLifetimeBlock = new ParticleInputBlock("Min Lifetime");
133+
minLifetimeBlock.value = particleSystem.minLifeTime;
134+
135+
const maxLifetimeBlock = new ParticleInputBlock("Max Lifetime");
136+
maxLifetimeBlock.value = particleSystem.maxLifeTime;
137+
138+
const randomLifetimeBlock = new ParticleRandomBlock("Random Lifetime");
139+
minLifetimeBlock.output.connectTo(randomLifetimeBlock.min);
140+
maxLifetimeBlock.output.connectTo(randomLifetimeBlock.max);
141+
142+
randomLifetimeBlock.output.connectTo(createParticleBlock.lifeTime);
115143

116144
// Texture
117145
const textureBlock = new ParticleTextureSourceBlock("Texture");
@@ -141,7 +169,7 @@ async function _ExtractDatafromParticleSystemAsync(particleSystem: ParticleSyste
141169
* @param name The name of the node particle system set.
142170
* @param particleSystems The particle systems to convert.
143171
* @returns The converted node particle system set or null if conversion failed.
144-
* #0K3AQ2#3625
172+
* #0K3AQ2#3627
145173
*/
146174
export async function ConvertToNodeParticleSystemSetAsync(name: string, particleSystems: ParticleSystem[]): Promise<Nullable<NodeParticleSystemSet>> {
147175
if (!particleSystems || !particleSystems.length) {

packages/dev/core/src/Particles/Node/nodeParticleSystemSet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ export class NodeParticleSystemSet {
270270

271271
const system = block.createSystem(state);
272272
system._source = this;
273+
system._blockReference = block._internalId;
273274

274275
// Errors
275276
state.emitErrors();

packages/dev/core/src/Particles/particleSystem.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export class ParticleSystem extends ThinParticleSystem {
109109
/** @internal */
110110
public _source: Nullable<NodeParticleSystemSet> = null;
111111

112+
/** @internal */
113+
public _blockReference: number = 0;
114+
112115
/**
113116
* Gets the NodeParticleSystemSet that this particle system belongs to.
114117
*/

packages/tools/nodeParticleEditor/src/components/preview/previewManager.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import { SceneLoaderFlags } from "core/Loading/sceneLoaderFlags";
1010
import type { NodeParticleSystemSet } from "core/Particles/Node/nodeParticleSystemSet";
1111
import { LogEntry } from "../log/logComponent";
1212
import { GridMaterial } from "materials/grid/gridMaterial";
13-
import { MeshBuilder } from "core/Meshes";
13+
import { MeshBuilder } from "core/Meshes/meshBuilder";
14+
import type { AbstractMesh } from "core/Meshes/abstractMesh";
1415
import { SceneInstrumentation } from "core/Instrumentation/sceneInstrumentation";
1516
import type { ThinParticleSystem } from "core/Particles/thinParticleSystem";
1617
import type { ParticleSystemSet } from "core/Particles/particleSystemSet";
18+
import { EngineStore } from "core/Engines";
19+
import type { ParticleSystem } from "core/Particles";
1720

1821
export class PreviewManager {
1922
private _nodeParticleSystemSet: NodeParticleSystemSet;
@@ -128,6 +131,45 @@ export class PreviewManager {
128131
// Ignore the error
129132
this._globalState.onIsLoadingChanged.notifyObservers(false);
130133
}
134+
135+
// Synchronize with main
136+
for (const engine of EngineStore.Instances) {
137+
for (const scene of engine.scenes) {
138+
if (scene === this._scene) {
139+
continue;
140+
}
141+
142+
void this._reconnectEmittersAsync(scene);
143+
}
144+
}
145+
}
146+
147+
private async _reconnectEmittersAsync(scene: Scene) {
148+
const map = new Map<number, Nullable<AbstractMesh | Vector3>>();
149+
150+
for (const ps of scene.particleSystems) {
151+
const particleSystem = ps as ParticleSystem;
152+
const source = particleSystem.source;
153+
if (source === this._nodeParticleSystemSet) {
154+
// Keep track of particle system reference and emitter
155+
const reference = particleSystem._blockReference;
156+
const emitter = particleSystem.emitter;
157+
158+
particleSystem.dispose();
159+
160+
map.set(reference, emitter);
161+
}
162+
}
163+
164+
const newSet = await this._nodeParticleSystemSet.buildAsync(scene);
165+
for (const [reference, emitter] of map) {
166+
const particleSystem = (newSet.systems as ParticleSystem[]).find((ps) => ps._blockReference === reference);
167+
if (particleSystem) {
168+
particleSystem.emitter = emitter;
169+
}
170+
}
171+
172+
newSet.start();
131173
}
132174

133175
public dispose() {

0 commit comments

Comments
 (0)