@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
102 lines • 4.17 kB
JavaScript
import { TmpVectors, Vector3 } from "../../../../Maths/math.vector.js";
import { RegisterClass } from "../../../../Misc/typeStore.js";
import { NodeParticleBlock } from "../../nodeParticleBlock.js";
import { NodeParticleBlockConnectionPointTypes } from "../../Enums/nodeParticleBlockConnectionPointTypes.js";
import { _ConnectAtTheEnd } from "../../../Queue/executionQueue.js";
/**
* 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
*/
constructor(name) {
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
*/
get particle() {
return this._inputs[0];
}
/**
* Gets the noiseTexture input component
*/
get noiseTexture() {
return this._inputs[1];
}
/**
* Gets the strength input component
*/
get strength() {
return this._inputs[2];
}
/**
* Gets the output component
*/
get output() {
return this._outputs[0];
}
/**
* Gets the current class name
* @returns the class name
*/
getClassName() {
return "UpdateNoiseBlock";
}
/**
* Builds the block
* @param state defines the current build state
*/
_build(state) {
const system = this.particle.getConnectedValue(state);
const strength = this.strength.getConnectedValue(state);
if (!strength) {
return;
}
const noiseTexture = this.noiseTexture.connectedPoint?.ownerBlock;
if (!noiseTexture) {
return;
}
const processNoiseAsync = async (particle) => {
// eslint-disable-next-line github/no-then
const textureContent = await noiseTexture.extractTextureContentAsync();
if (!textureContent) {
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, textureContent.width, textureContent.height, textureContent.data);
const fetchedColorG = system._fetchR(particle._randomNoiseCoordinates1.z, particle._randomNoiseCoordinates2.x, textureContent.width, textureContent.height, textureContent.data);
const fetchedColorB = system._fetchR(particle._randomNoiseCoordinates2.y, particle._randomNoiseCoordinates2.z, textureContent.width, textureContent.height, textureContent.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: processNoiseAsync,
previousItem: null,
nextItem: null,
};
if (system._updateQueueStart) {
_ConnectAtTheEnd(noiseProcessing, system._updateQueueStart);
}
else {
system._updateQueueStart = noiseProcessing;
}
this.output._storedValue = system;
}
}
RegisterClass("BABYLON.UpdateNoiseBlock", UpdateNoiseBlock);
//# sourceMappingURL=updateNoiseBlock.js.map