@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.
108 lines • 4.22 kB
JavaScript
import { RegisterClass } from "../../../../Misc/typeStore.js";
import { NodeParticleBlockConnectionPointTypes } from "../../Enums/nodeParticleBlockConnectionPointTypes.js";
import { NodeParticleBlock } from "../../nodeParticleBlock.js";
import { Vector3 } from "../../../../Maths/math.vector.js";
import { RandomRange } from "../../../../Maths/math.scalar.functions.js";
/**
* Block used to provide a flow of particles emitted from a sphere shape.
*/
export class SphereShapeBlock extends NodeParticleBlock {
/**
* Create a new SphereShapeBlock
* @param name defines the block name
*/
constructor(name) {
super(name);
this.registerInput("input", NodeParticleBlockConnectionPointTypes.Particle);
this.registerInput("radius", NodeParticleBlockConnectionPointTypes.Float, true, 1);
this.registerInput("radiusRange", NodeParticleBlockConnectionPointTypes.Float, true, 1, 0, 1);
this.registerInput("directionRandomizer", NodeParticleBlockConnectionPointTypes.Float, true, 0, 0, 1);
this.registerOutput("output", NodeParticleBlockConnectionPointTypes.Particle);
}
/**
* Gets the current class name
* @returns the class name
*/
getClassName() {
return "SphereShapeBlock";
}
/**
* Gets the input component
*/
get input() {
return this._inputs[0];
}
/**
* Gets the radius input component
*/
get radius() {
return this._inputs[1];
}
/**
* Gets the radiusRange input component
*/
get radiusRange() {
return this._inputs[2];
}
/**
* Gets the directionRandomizer input component
*/
get directionRandomizer() {
return this._inputs[3];
}
/**
* Gets the output component
*/
get output() {
return this._outputs[0];
}
/**
* Builds the block
* @param state defines the build state
*/
_build(state) {
const system = this.input.getConnectedValue(state);
system._directionCreation.process = (particle) => {
state.particleContext = particle;
state.systemContext = system;
const directionRandomizer = this.directionRandomizer.getConnectedValue(state);
const direction = particle.position.subtract(state.emitterPosition).normalize();
const randX = RandomRange(0, directionRandomizer);
const randY = RandomRange(0, directionRandomizer);
const randZ = RandomRange(0, directionRandomizer);
direction.x += randX;
direction.y += randY;
direction.z += randZ;
direction.normalize();
if (state.isEmitterTransformNode) {
Vector3.TransformNormalFromFloatsToRef(randX, randY, randZ, state.emitterWorldMatrix, particle.direction);
}
else {
particle.direction.copyFromFloats(randX, randY, randZ);
}
};
system._positionCreation.process = (particle) => {
state.particleContext = particle;
state.systemContext = system;
const radius = this.radius.getConnectedValue(state);
const radiusRange = this.radiusRange.getConnectedValue(state);
const randRadius = radius - RandomRange(0, radius * radiusRange);
const v = RandomRange(0, 1.0);
const phi = RandomRange(0, 2 * Math.PI);
const theta = Math.acos(2 * v - 1);
const randX = randRadius * Math.cos(phi) * Math.sin(theta);
const randY = randRadius * Math.cos(theta);
const randZ = randRadius * Math.sin(phi) * Math.sin(theta);
if (state.isEmitterTransformNode) {
Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, state.emitterWorldMatrix, particle.position);
}
else {
particle.position.copyFromFloats(randX, randY, randZ);
particle.position.addInPlace(state.emitterPosition);
}
};
this.output._storedValue = system;
}
}
RegisterClass("BABYLON.SphereShapeBlock", SphereShapeBlock);
//# sourceMappingURL=sphereShapeBlock.js.map