Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { Nullable } from "core/types";
import type { ThinParticleSystem } from "core/Particles/thinParticleSystem";
import { RegisterClass } from "../../../../Misc/typeStore";
import { NodeParticleBlockConnectionPointTypes } from "../../Enums/nodeParticleBlockConnectionPointTypes";
import { NodeParticleBlock } from "../../nodeParticleBlock";
import type { NodeParticleConnectionPoint } from "../../nodeParticleBlockConnectionPoint";
import type { NodeParticleBuildState } from "../../nodeParticleBuildState";
import type { Particle } from "core/Particles/particle";
import type { INodeParticleTextureData, ParticleTextureSourceBlock } from "../particleSourceTextureBlock";

import { RegisterClass } from "../../../../Misc/typeStore";
import { NodeParticleBlockConnectionPointTypes } from "../../Enums/nodeParticleBlockConnectionPointTypes";
import { NodeParticleBlock } from "../../nodeParticleBlock";
import { _ConnectAtTheEnd } from "core/Particles/Queue/executionQueue";
import { FlowMap } from "core/Particles/flowMap";
import { editableInPropertyPage, PropertyTypeForEdition } from "core/Decorators/nodeDecorator";
import type { ParticleTextureSourceBlock } from "../particleSourceTextureBlock";

/**
* Block used to update particle position based on a flow map
Expand Down Expand Up @@ -72,7 +74,7 @@ export class UpdateFlowMapBlock extends NodeParticleBlock {
let flowMap: FlowMap;

// eslint-disable-next-line github/no-then
void flowMapTexture.extractTextureContentAsync().then((textureContent) => {
void flowMapTexture.extractTextureContentAsync().then((textureContent: Nullable<INodeParticleTextureData>) => {
if (!textureContent) {
return;
}
Expand Down Expand Up @@ -104,6 +106,10 @@ export class UpdateFlowMapBlock extends NodeParticleBlock {
this.output._storedValue = system;
}

/**
* Serializes the block into a json object
* @returns The serialized object
*/
public override serialize(): any {
const serializationObject = super.serialize();

Expand All @@ -112,6 +118,10 @@ export class UpdateFlowMapBlock extends NodeParticleBlock {
return serializationObject;
}

/**
* Deserializes the block from a json object
* @param serializationObject The object to deserialize from
*/
public override _deserialize(serializationObject: any) {
super._deserialize(serializationObject);

Expand Down
137 changes: 137 additions & 0 deletions packages/dev/core/src/Particles/Node/Blocks/Update/updateNoiseBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { Nullable } from "core/types";
import type { Particle } from "core/Particles/particle";
import type { ThinParticleSystem } from "core/Particles/thinParticleSystem";
import type { NodeParticleConnectionPoint } from "core/Particles/Node/nodeParticleBlockConnectionPoint";
import type { NodeParticleBuildState } from "core/Particles/Node/nodeParticleBuildState";
import type { INodeParticleTextureData, ParticleTextureSourceBlock } from "core/Particles/Node/Blocks/particleSourceTextureBlock";

import { TmpVectors, Vector3 } from "core/Maths/math.vector";
import { RegisterClass } from "core/Misc/typeStore";
import { NodeParticleBlock } from "core/Particles/Node/nodeParticleBlock";
import { NodeParticleBlockConnectionPointTypes } from "core/Particles/Node/Enums/nodeParticleBlockConnectionPointTypes";
import { _ConnectAtTheEnd } from "core/Particles/Queue/executionQueue";

/**
* Block used to update particle position based on a noise texture
*/
export class UpdateNoiseBlock extends NodeParticleBlock {
/**
* Create a new UpdateNoiseBlock
* @param name defines the block name
*/
public constructor(name: string) {
super(name);

this.registerInput("particle", NodeParticleBlockConnectionPointTypes.Particle);
this.registerInput("noiseTexture", NodeParticleBlockConnectionPointTypes.Texture);
this.registerInput("strength", NodeParticleBlockConnectionPointTypes.Vector3, true, new Vector3(100, 100, 100));
this.registerOutput("output", NodeParticleBlockConnectionPointTypes.Particle);
}

/**
* Gets the particle component
*/
public get particle(): NodeParticleConnectionPoint {
return this._inputs[0];
}

/**
* Gets the noiseTexture input component
*/
public get noiseTexture(): NodeParticleConnectionPoint {
return this._inputs[1];
}

/**
* Gets the strength input component
*/
public get strength(): NodeParticleConnectionPoint {
return this._inputs[2];
}

/**
* Gets the output component
*/
public get output(): NodeParticleConnectionPoint {
return this._outputs[0];
}

/**
* Gets the current class name
* @returns the class name
*/
public override getClassName() {
return "UpdateNoiseBlock";
}

/**
* Builds the block
* @param state defines the current build state
*/
public override _build(state: NodeParticleBuildState) {
const system = this.particle.getConnectedValue(state) as ThinParticleSystem;

const strength = this.strength.getConnectedValue(state) as Vector3;
if (!strength) {
return;
}

const noiseTexture = this.noiseTexture.connectedPoint?.ownerBlock as ParticleTextureSourceBlock;
if (!noiseTexture) {
return;
}

let noiseData: INodeParticleTextureData;

// eslint-disable-next-line github/no-then
void noiseTexture.extractTextureContentAsync().then((textureContent: Nullable<INodeParticleTextureData>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be call on every frame right? Unless I'm wrong (which is totally possible). This will be called just once and never change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this needs to go inside the process noise function

if (!textureContent) {
return;
}
noiseData = textureContent;
});

const processNoise = (particle: Particle) => {
if (!noiseData) {
// If the noise data is not ready, we skip processing
return;
}

if (!particle._randomNoiseCoordinates1) {
particle._randomNoiseCoordinates1 = new Vector3(Math.random(), Math.random(), Math.random());
}

if (!particle._randomNoiseCoordinates2) {
particle._randomNoiseCoordinates2 = new Vector3(Math.random(), Math.random(), Math.random());
}

const fetchedColorR = system._fetchR(particle._randomNoiseCoordinates1.x, particle._randomNoiseCoordinates1.y, noiseData.width, noiseData.height, noiseData.data);
const fetchedColorG = system._fetchR(particle._randomNoiseCoordinates1.z, particle._randomNoiseCoordinates2.x, noiseData.width, noiseData.height, noiseData.data);
const fetchedColorB = system._fetchR(particle._randomNoiseCoordinates2.y, particle._randomNoiseCoordinates2.z, noiseData.width, noiseData.height, noiseData.data);

const force = TmpVectors.Vector3[0];
const scaledForce = TmpVectors.Vector3[1];

force.copyFromFloats((2 * fetchedColorR - 1) * strength.x, (2 * fetchedColorG - 1) * strength.y, (2 * fetchedColorB - 1) * strength.z);

force.scaleToRef(system._tempScaledUpdateSpeed, scaledForce);
particle.direction.addInPlace(scaledForce);
};

const noiseProcessing = {
process: processNoise,
previousItem: null,
nextItem: null,
};

if (system._updateQueueStart) {
_ConnectAtTheEnd(noiseProcessing, system._updateQueueStart);
} else {
system._updateQueueStart = noiseProcessing;
}

this.output._storedValue = system;
}
}

RegisterClass("BABYLON.UpdateNoiseBlock", UpdateNoiseBlock);
1 change: 1 addition & 0 deletions packages/dev/core/src/Particles/Node/Blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from "./Update/basicSpriteUpdateBlock";
export * from "./Update/basicColorUpdateBlock";
export * from "./Update/updateSpriteCellIndexBlock";
export * from "./Update/updateFlowMapBlock";
export * from "./Update/updateNoiseBlock";
export * from "./Update/updateAttractorBlock";
export * from "./Update/alignAngleBlock";
export * from "./Emitters/index";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import type { NodeParticleBuildState } from "../nodeParticleBuildState";
import type { Nullable } from "core/types";
import { TextureTools } from "core/Misc/textureTools";
import type { BaseTexture } from "../../../Materials/Textures/baseTexture";
import type { ProceduralTexture } from "../../../Materials";

/**
* Interface used to define texture data
*/
export interface INodeParticleTextureData {
width: number;
height: number;
data: Uint8ClampedArray;
}

/**
* Block used to provide a texture for particles in a particle system
Expand All @@ -15,11 +25,7 @@ export class ParticleTextureSourceBlock extends NodeParticleBlock {
private _url: string = "";
private _textureDataUrl: string = "";
private _sourceTexture: Nullable<BaseTexture> = null;
private _cachedData: Nullable<{
width: number;
height: number;
data: Uint8ClampedArray;
}> = null;
private _cachedData: Nullable<INodeParticleTextureData> = null;

/**
* Indicates if the texture data should be serialized as a base64 string.
Expand Down Expand Up @@ -136,19 +142,37 @@ export class ParticleTextureSourceBlock extends NodeParticleBlock {
return;
}
const size = texture.getSize();
TextureTools.GetTextureDataAsync(texture, size.width, size.height)
// eslint-disable-next-line github/no-then
.then((data) => {
this._cachedData = {
width: size.width,
height: size.height,
data: new Uint8ClampedArray(data),
};
texture.dispose();
resolve(this._cachedData);
})
// eslint-disable-next-line github/no-then
.catch(reject);
if (texture.getContent) {
const proceduralTexture = texture as ProceduralTexture;
proceduralTexture
.getContent()
// eslint-disable-next-line github/no-then
?.then((data) => {
this._cachedData = {
width: size.width,
height: size.height,
data: data as Uint8ClampedArray,
};
texture.dispose();
resolve(this._cachedData);
})
// eslint-disable-next-line github/no-then
.catch(reject);
} else {
TextureTools.GetTextureDataAsync(texture, size.width, size.height)
// eslint-disable-next-line github/no-then
.then((data) => {
this._cachedData = {
width: size.width,
height: size.height,
data: new Uint8ClampedArray(data),
};
texture.dispose();
resolve(this._cachedData);
})
// eslint-disable-next-line github/no-then
.catch(reject);
}
});
}

Expand Down
Loading