123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400 |
- module BABYLON {
- /**
- * This represents a particle system in Babylon.
- * Particles are often small sprites used to simulate hard-to-reproduce phenomena like fire, smoke, water, or abstract visual effects like magic glitter and faery dust.
- * Particles can take different shapes while emitted like box, sphere, cone or you can write your custom function.
- * @example https://doc.babylonjs.com/babylon101/particles
- */
- export class ParticleSystem extends BaseParticleSystem implements IDisposable, IAnimatable, IParticleSystem {
- /**
- * This function can be defined to provide custom update for active particles.
- * This function will be called instead of regular update (age, position, color, etc.).
- * Do not forget that this function will be called on every frame so try to keep it simple and fast :)
- */
- public updateFunction: (particles: Particle[]) => void;
- private _emitterWorldMatrix: Matrix;
- /**
- * This function can be defined to specify initial direction for every new particle.
- * It by default use the emitterType defined function
- */
- public startDirectionFunction: (worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle) => void;
- /**
- * This function can be defined to specify initial position for every new particle.
- * It by default use the emitterType defined function
- */
- public startPositionFunction: (worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle) => void;
- /**
- * @hidden
- */
- public _inheritedVelocityOffset = new BABYLON.Vector3();
- /**
- * An event triggered when the system is disposed
- */
- public onDisposeObservable = new Observable<ParticleSystem>();
- private _onDisposeObserver: Nullable<Observer<ParticleSystem>>;
- /**
- * Sets a callback that will be triggered when the system is disposed
- */
- public set onDispose(callback: () => void) {
- if (this._onDisposeObserver) {
- this.onDisposeObservable.remove(this._onDisposeObserver);
- }
- this._onDisposeObserver = this.onDisposeObservable.add(callback);
- }
- private _particles = new Array<Particle>();
- private _epsilon: number;
- private _capacity: number;
- private _stockParticles = new Array<Particle>();
- private _newPartsExcess = 0;
- private _vertexData: Float32Array;
- private _vertexBuffer: Nullable<Buffer>;
- private _vertexBuffers: { [key: string]: VertexBuffer } = {};
- private _spriteBuffer: Nullable<Buffer>;
- private _indexBuffer: Nullable<WebGLBuffer>;
- private _effect: Effect;
- private _customEffect: Nullable<Effect>;
- private _cachedDefines: string;
- private _scaledColorStep = new Color4(0, 0, 0, 0);
- private _colorDiff = new Color4(0, 0, 0, 0);
- private _scaledDirection = Vector3.Zero();
- private _scaledGravity = Vector3.Zero();
- private _currentRenderId = -1;
- private _alive: boolean;
- private _useInstancing = false;
- private _started = false;
- private _stopped = false;
- private _actualFrame = 0;
- private _scaledUpdateSpeed: number;
- private _vertexBufferSize: number;
- /** @hidden */
- public _currentEmitRateGradient: Nullable<FactorGradient>;
- /** @hidden */
- public _currentEmitRate1 = 0;
- /** @hidden */
- public _currentEmitRate2 = 0;
- /** @hidden */
- public _currentStartSizeGradient: Nullable<FactorGradient>;
- /** @hidden */
- public _currentStartSize1 = 0;
- /** @hidden */
- public _currentStartSize2 = 0;
- private readonly _rawTextureWidth = 256;
- private _rampGradientsTexture: Nullable<RawTexture>;
- private _useRampGradients = false;
- /** Gets or sets a boolean indicating that ramp gradients must be used
- * @see http://doc.babylonjs.com/babylon101/particles#ramp-gradients
- */
- public get useRampGradients(): boolean {
- return this._useRampGradients;
- }
- public set useRampGradients(value: boolean) {
- if (this._useRampGradients === value) {
- return;
- }
- this._useRampGradients = value;
- this._resetEffect();
- }
- // Sub-emitters
- /**
- * The Sub-emitters templates that will be used to generate the sub particle system to be associated with the system, this property is used by the root particle system only.
- * When a particle is spawned, an array will be chosen at random and all the emitters in that array will be attached to the particle. (Default: [])
- */
- public subEmitters: Array<ParticleSystem | SubEmitter | Array<SubEmitter>>;
- // the subEmitters field above converted to a constant type
- private _subEmitters: Array<Array<SubEmitter>>;
- /**
- * @hidden
- * If the particle systems emitter should be disposed when the particle system is disposed
- */
- public _disposeEmitterOnDispose = false;
- /**
- * The current active Sub-systems, this property is used by the root particle system only.
- */
- public activeSubSystems: Array<ParticleSystem>;
- private _rootParticleSystem: ParticleSystem;
- //end of Sub-emitter
- /**
- * Gets the current list of active particles
- */
- public get particles(): Particle[] {
- return this._particles;
- }
- /**
- * Returns the string "ParticleSystem"
- * @returns a string containing the class name
- */
- public getClassName(): string {
- return "ParticleSystem";
- }
- /**
- * Instantiates a particle system.
- * Particles are often small sprites used to simulate hard-to-reproduce phenomena like fire, smoke, water, or abstract visual effects like magic glitter and faery dust.
- * @param name The name of the particle system
- * @param capacity The max number of particles alive at the same time
- * @param scene The scene the particle system belongs to
- * @param customEffect a custom effect used to change the way particles are rendered by default
- * @param isAnimationSheetEnabled Must be true if using a spritesheet to animate the particles texture
- * @param epsilon Offset used to render the particles
- */
- constructor(name: string, capacity: number, scene: Scene, customEffect: Nullable<Effect> = null, isAnimationSheetEnabled: boolean = false, epsilon: number = 0.01) {
- super(name);
- this._capacity = capacity;
- this._epsilon = epsilon;
- this._isAnimationSheetEnabled = isAnimationSheetEnabled;
- this._scene = scene || Engine.LastCreatedScene;
- // Setup the default processing configuration to the scene.
- this._attachImageProcessingConfiguration(null);
- this._customEffect = customEffect;
- this._scene.particleSystems.push(this);
- this._useInstancing = this._scene.getEngine().getCaps().instancedArrays;
- this._createIndexBuffer();
- this._createVertexBuffers();
- // Default emitter type
- this.particleEmitterType = new BoxParticleEmitter();
- this.updateFunction = (particles: Particle[]): void => {
- let noiseTextureData: Nullable<Uint8Array> = null;
- let noiseTextureSize: Nullable<ISize> = null;
- if (this.noiseTexture) { // We need to get texture data back to CPU
- noiseTextureData = <Nullable<Uint8Array>>(this.noiseTexture.readPixels());
- noiseTextureSize = this.noiseTexture.getSize();
- }
- for (var index = 0; index < particles.length; index++) {
- var particle = particles[index];
- particle.age += this._scaledUpdateSpeed;
- if (particle.age >= particle.lifeTime) { // Recycle by swapping with last particle
- this._emitFromParticle(particle);
- if (particle._attachedSubEmitters) {
- particle._attachedSubEmitters.forEach((subEmitter) => {
- subEmitter.particleSystem.disposeOnStop = true;
- subEmitter.particleSystem.stop();
- });
- particle._attachedSubEmitters = null;
- }
- this.recycleParticle(particle);
- index--;
- continue;
- }
- else {
- let ratio = particle.age / particle.lifeTime;
- // Color
- if (this._colorGradients && this._colorGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._colorGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== particle._currentColorGradient) {
- particle._currentColor1.copyFrom(particle._currentColor2);
- (<ColorGradient>nextGradient).getColorToRef(particle._currentColor2);
- particle._currentColorGradient = (<ColorGradient>currentGradient);
- }
- Color4.LerpToRef(particle._currentColor1, particle._currentColor2, scale, particle.color);
- });
- }
- else {
- particle.colorStep.scaleToRef(this._scaledUpdateSpeed, this._scaledColorStep);
- particle.color.addInPlace(this._scaledColorStep);
- if (particle.color.a < 0) {
- particle.color.a = 0;
- }
- }
- // Angular speed
- if (this._angularSpeedGradients && this._angularSpeedGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._angularSpeedGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== particle._currentAngularSpeedGradient) {
- particle._currentAngularSpeed1 = particle._currentAngularSpeed2;
- particle._currentAngularSpeed2 = (<FactorGradient>nextGradient).getFactor();
- particle._currentAngularSpeedGradient = (<FactorGradient>currentGradient);
- }
- particle.angularSpeed = Scalar.Lerp(particle._currentAngularSpeed1, particle._currentAngularSpeed2, scale);
- });
- }
- particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
- // Direction
- let directionScale = this._scaledUpdateSpeed;
- /// Velocity
- if (this._velocityGradients && this._velocityGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._velocityGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== particle._currentVelocityGradient) {
- particle._currentVelocity1 = particle._currentVelocity2;
- particle._currentVelocity2 = (<FactorGradient>nextGradient).getFactor();
- particle._currentVelocityGradient = (<FactorGradient>currentGradient);
- }
- directionScale *= Scalar.Lerp(particle._currentVelocity1, particle._currentVelocity2, scale);
- });
- }
- particle.direction.scaleToRef(directionScale, this._scaledDirection);
- /// Limit velocity
- if (this._limitVelocityGradients && this._limitVelocityGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._limitVelocityGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== particle._currentLimitVelocityGradient) {
- particle._currentLimitVelocity1 = particle._currentLimitVelocity2;
- particle._currentLimitVelocity2 = (<FactorGradient>nextGradient).getFactor();
- particle._currentLimitVelocityGradient = (<FactorGradient>currentGradient);
- }
- let limitVelocity = Scalar.Lerp(particle._currentLimitVelocity1, particle._currentLimitVelocity2, scale);
- let currentVelocity = particle.direction.length();
- if (currentVelocity > limitVelocity) {
- particle.direction.scaleInPlace(this.limitVelocityDamping);
- }
- });
- }
- /// Drag
- if (this._dragGradients && this._dragGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._dragGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== particle._currentDragGradient) {
- particle._currentDrag1 = particle._currentDrag2;
- particle._currentDrag2 = (<FactorGradient>nextGradient).getFactor();
- particle._currentDragGradient = (<FactorGradient>currentGradient);
- }
- let drag = Scalar.Lerp(particle._currentDrag1, particle._currentDrag2, scale);
- this._scaledDirection.scaleInPlace(1.0 - drag);
- });
- }
- particle.position.addInPlace(this._scaledDirection);
- // Noise
- if (noiseTextureData && noiseTextureSize) {
- let localPosition = Tmp.Vector3[0];
- let emitterPosition = Tmp.Vector3[1];
- this._emitterWorldMatrix.getTranslationToRef(emitterPosition);
- particle.position.subtractToRef(emitterPosition, localPosition);
- let fetchedColorR = this._fetchR(localPosition.y, localPosition.z, noiseTextureSize.width, noiseTextureSize.height, noiseTextureData);
- let fetchedColorG = this._fetchR(localPosition.x + 0.33, localPosition.z + 0.33, noiseTextureSize.width, noiseTextureSize.height, noiseTextureData);
- let fetchedColorB = this._fetchR(localPosition.x - 0.33, localPosition.y - 0.33, noiseTextureSize.width, noiseTextureSize.height, noiseTextureData);
- let force = Tmp.Vector3[0];
- let scaledForce = Tmp.Vector3[1];
- force.copyFromFloats((2 * fetchedColorR - 1) * this.noiseStrength.x, (2 * fetchedColorG - 1) * this.noiseStrength.y, (2 * fetchedColorB - 1) * this.noiseStrength.z);
- force.scaleToRef(this._scaledUpdateSpeed, scaledForce);
- particle.direction.addInPlace(scaledForce);
- }
- // Gravity
- this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
- particle.direction.addInPlace(this._scaledGravity);
- // Size
- if (this._sizeGradients && this._sizeGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._sizeGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== particle._currentSizeGradient) {
- particle._currentSize1 = particle._currentSize2;
- particle._currentSize2 = (<FactorGradient>nextGradient).getFactor();
- particle._currentSizeGradient = (<FactorGradient>currentGradient);
- }
- particle.size = Scalar.Lerp(particle._currentSize1, particle._currentSize2, scale);
- });
- }
- // Remap data
- if (this._useRampGradients) {
- if (this._colorRemapGradients && this._colorRemapGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._colorRemapGradients, (currentGradient, nextGradient, scale) => {
- let min = Scalar.Lerp((<FactorGradient>currentGradient).factor1, (<FactorGradient>nextGradient).factor1, scale);
- let max = Scalar.Lerp((<FactorGradient>currentGradient).factor2!, (<FactorGradient>nextGradient).factor2!, scale);
- particle.remapData.x = min;
- particle.remapData.y = max - min;
- });
- }
- if (this._alphaRemapGradients && this._alphaRemapGradients.length > 0) {
- Tools.GetCurrentGradient(ratio, this._alphaRemapGradients, (currentGradient, nextGradient, scale) => {
- let min = Scalar.Lerp((<FactorGradient>currentGradient).factor1, (<FactorGradient>nextGradient).factor1, scale);
- let max = Scalar.Lerp((<FactorGradient>currentGradient).factor2!, (<FactorGradient>nextGradient).factor2!, scale);
- particle.remapData.z = min;
- particle.remapData.w = max - min;
- });
- }
- }
- if (this._isAnimationSheetEnabled) {
- particle.updateCellIndex();
- }
- // Update the position of the attached sub-emitters to match their attached particle
- if (particle._attachedSubEmitters && particle._attachedSubEmitters.length > 0) {
- particle._attachedSubEmitters.forEach((subEmitter) => {
- ParticleSystem._InheritParticleInfoToSubEmitter(subEmitter, particle);
- });
- }
- }
- }
- }
- }
- private static _InheritParticleInfoToSubEmitter(subEmitter: SubEmitter, particle: Particle) {
- if ((<AbstractMesh>subEmitter.particleSystem.emitter).position) {
- var emitterMesh = (<AbstractMesh>subEmitter.particleSystem.emitter);
- emitterMesh.position.copyFrom(particle.position);
- if (subEmitter.inheritDirection) {
- emitterMesh.position.subtractToRef(particle.direction, BABYLON.Tmp.Vector3[0]);
- // Look at using Y as forward
- emitterMesh.lookAt(BABYLON.Tmp.Vector3[0], 0, Math.PI / 2);
- }
- } else {
- var emitterPosition = (<Vector3>subEmitter.particleSystem.emitter);
- emitterPosition.copyFrom(particle.position);
- if (subEmitter.inheritDirection) {
- Tools.Warn("subEmitter.inheritDirection is not supported with non-mesh emitter type");
- }
- }
- // Set inheritedVelocityOffset to be used when new particles are created
- particle.direction.scaleToRef(subEmitter.inheritedVelocityAmount / 2, Tmp.Vector3[0]);
- subEmitter.particleSystem._inheritedVelocityOffset.copyFrom(Tmp.Vector3[0]);
- }
- private _addFactorGradient(factorGradients: FactorGradient[], gradient: number, factor: number, factor2?: number) {
- let newGradient = new FactorGradient();
- newGradient.gradient = gradient;
- newGradient.factor1 = factor;
- newGradient.factor2 = factor2;
- factorGradients.push(newGradient);
- factorGradients.sort((a, b) => {
- if (a.gradient < b.gradient) {
- return -1;
- } else if (a.gradient > b.gradient) {
- return 1;
- }
- return 0;
- });
- }
- private _removeFactorGradient(factorGradients: Nullable<FactorGradient[]>, gradient: number) {
- if (!factorGradients) {
- return;
- }
- let index = 0;
- for (var factorGradient of factorGradients) {
- if (factorGradient.gradient === gradient) {
- factorGradients.splice(index, 1);
- break;
- }
- index++;
- }
- }
- /**
- * Adds a new life time gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the life time factor to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addLifeTimeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._lifeTimeGradients) {
- this._lifeTimeGradients = [];
- }
- this._addFactorGradient(this._lifeTimeGradients, gradient, factor, factor2);
- return this;
- }
- /**
- * Remove a specific life time gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeLifeTimeGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._lifeTimeGradients, gradient);
- return this;
- }
- /**
- * Adds a new size gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the size factor to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addSizeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._sizeGradients) {
- this._sizeGradients = [];
- }
- this._addFactorGradient(this._sizeGradients, gradient, factor, factor2);
- return this;
- }
- /**
- * Remove a specific size gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeSizeGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._sizeGradients, gradient);
- return this;
- }
- /**
- * Adds a new color remap gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param min defines the color remap minimal range
- * @param max defines the color remap maximal range
- * @returns the current particle system
- */
- public addColorRemapGradient(gradient: number, min: number, max: number): IParticleSystem {
- if (!this._colorRemapGradients) {
- this._colorRemapGradients = [];
- }
- this._addFactorGradient(this._colorRemapGradients, gradient, min, max);
- return this;
- }
- /**
- * Remove a specific color remap gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeColorRemapGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._colorRemapGradients, gradient);
- return this;
- }
- /**
- * Adds a new alpha remap gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param min defines the alpha remap minimal range
- * @param max defines the alpha remap maximal range
- * @returns the current particle system
- */
- public addAlphaRemapGradient(gradient: number, min: number, max: number): IParticleSystem {
- if (!this._alphaRemapGradients) {
- this._alphaRemapGradients = [];
- }
- this._addFactorGradient(this._alphaRemapGradients, gradient, min, max);
- return this;
- }
- /**
- * Remove a specific alpha remap gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeAlphaRemapGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._alphaRemapGradients, gradient);
- return this;
- }
- /**
- * Adds a new angular speed gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the angular speed to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addAngularSpeedGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._angularSpeedGradients) {
- this._angularSpeedGradients = [];
- }
- this._addFactorGradient(this._angularSpeedGradients, gradient, factor, factor2);
- return this;
- }
- /**
- * Remove a specific angular speed gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeAngularSpeedGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._angularSpeedGradients, gradient);
- return this;
- }
- /**
- * Adds a new velocity gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the velocity to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addVelocityGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._velocityGradients) {
- this._velocityGradients = [];
- }
- this._addFactorGradient(this._velocityGradients, gradient, factor, factor2);
- return this;
- }
- /**
- * Remove a specific velocity gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeVelocityGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._velocityGradients, gradient);
- return this;
- }
- /**
- * Adds a new limit velocity gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the limit velocity value to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addLimitVelocityGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._limitVelocityGradients) {
- this._limitVelocityGradients = [];
- }
- this._addFactorGradient(this._limitVelocityGradients, gradient, factor, factor2);
- return this;
- }
- /**
- * Remove a specific limit velocity gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeLimitVelocityGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._limitVelocityGradients, gradient);
- return this;
- }
- /**
- * Adds a new drag gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the drag value to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addDragGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._dragGradients) {
- this._dragGradients = [];
- }
- this._addFactorGradient(this._dragGradients, gradient, factor, factor2);
- return this;
- }
- /**
- * Remove a specific drag gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeDragGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._dragGradients, gradient);
- return this;
- }
- /**
- * Adds a new emit rate gradient (please note that this will only work if you set the targetStopDuration property)
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the emit rate value to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addEmitRateGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._emitRateGradients) {
- this._emitRateGradients = [];
- }
- this._addFactorGradient(this._emitRateGradients, gradient, factor, factor2);
- if (!this._currentEmitRateGradient) {
- this._currentEmitRateGradient = this._emitRateGradients[0];
- this._currentEmitRate1 = this._currentEmitRateGradient.getFactor();
- this._currentEmitRate2 = this._currentEmitRate1;
- }
- if (this._emitRateGradients.length === 2) {
- this._currentEmitRate2 = this._emitRateGradients[1].getFactor();
- }
- return this;
- }
- /**
- * Remove a specific emit rate gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeEmitRateGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._emitRateGradients, gradient);
- return this;
- }
- /**
- * Adds a new start size gradient (please note that this will only work if you set the targetStopDuration property)
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param factor defines the start size value to affect to the specified gradient
- * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
- * @returns the current particle system
- */
- public addStartSizeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem {
- if (!this._startSizeGradients) {
- this._startSizeGradients = [];
- }
- this._addFactorGradient(this._startSizeGradients, gradient, factor, factor2);
- if (!this._currentStartSizeGradient) {
- this._currentStartSizeGradient = this._startSizeGradients[0];
- this._currentStartSize1 = this._currentStartSizeGradient.getFactor();
- this._currentStartSize2 = this._currentStartSize1;
- }
- if (this._startSizeGradients.length === 2) {
- this._currentStartSize2 = this._startSizeGradients[1].getFactor();
- }
- return this;
- }
- /**
- * Remove a specific start size gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeStartSizeGradient(gradient: number): IParticleSystem {
- this._removeFactorGradient(this._emitRateGradients, gradient);
- return this;
- }
- private _createRampGradientTexture() {
- if (!this._rampGradients || !this._rampGradients.length || this._rampGradientsTexture) {
- return;
- }
- let data = new Uint8Array(this._rawTextureWidth * 4);
- let tmpColor = Tmp.Color3[0];
- for (var x = 0; x < this._rawTextureWidth; x++) {
- var ratio = x / this._rawTextureWidth;
- Tools.GetCurrentGradient(ratio, this._rampGradients, (currentGradient, nextGradient, scale) => {
- Color3.LerpToRef((<Color3Gradient>currentGradient).color, (<Color3Gradient>nextGradient).color, scale, tmpColor);
- data[x * 4] = tmpColor.r * 255;
- data[x * 4 + 1] = tmpColor.g * 255;
- data[x * 4 + 2] = tmpColor.b * 255;
- data[x * 4 + 3] = 255;
- });
- }
- this._rampGradientsTexture = RawTexture.CreateRGBATexture(data, this._rawTextureWidth, 1, this._scene, false, false, Texture.NEAREST_SAMPLINGMODE);
- }
- /**
- * Gets the current list of ramp gradients.
- * You must use addRampGradient and removeRampGradient to udpate this list
- * @returns the list of ramp gradients
- */
- public getRampGradients(): Nullable<Array<Color3Gradient>> {
- return this._rampGradients;
- }
- /**
- * Adds a new ramp gradient used to remap particle colors
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param color defines the color to affect to the specified gradient
- * @returns the current particle system
- */
- public addRampGradient(gradient: number, color: Color3): ParticleSystem {
- if (!this._rampGradients) {
- this._rampGradients = [];
- }
- let rampGradient = new Color3Gradient();
- rampGradient.gradient = gradient;
- rampGradient.color = color;
- this._rampGradients.push(rampGradient);
- this._rampGradients.sort((a, b) => {
- if (a.gradient < b.gradient) {
- return -1;
- } else if (a.gradient > b.gradient) {
- return 1;
- }
- return 0;
- });
- if (this._rampGradientsTexture) {
- this._rampGradientsTexture.dispose();
- (<any>this._rampGradientsTexture) = null;
- }
- this._createRampGradientTexture();
- return this;
- }
- /**
- * Remove a specific ramp gradient
- * @param gradient defines the gradient to remove
- * @returns the current particle system
- */
- public removeRampGradient(gradient: number): ParticleSystem {
- this._removeGradientAndTexture(gradient, this._rampGradients, this._rampGradientsTexture);
- (<any>this._rampGradientsTexture) = null;
- if (this._rampGradients && this._rampGradients.length > 0) {
- this._createRampGradientTexture();
- }
- return this;
- }
- /**
- * Adds a new color gradient
- * @param gradient defines the gradient to use (between 0 and 1)
- * @param color defines the color to affect to the specified gradient
- * @param color2 defines an additional color used to define a range ([color, color2]) with main color to pick the final color from
- */
- public addColorGradient(gradient: number, color: Color4, color2?: Color4): IParticleSystem {
- if (!this._colorGradients) {
- this._colorGradients = [];
- }
- let colorGradient = new ColorGradient();
- colorGradient.gradient = gradient;
- colorGradient.color1 = color;
- colorGradient.color2 = color2;
- this._colorGradients.push(colorGradient);
- this._colorGradients.sort((a, b) => {
- if (a.gradient < b.gradient) {
- return -1;
- } else if (a.gradient > b.gradient) {
- return 1;
- }
- return 0;
- });
- return this;
- }
- /**
- * Remove a specific color gradient
- * @param gradient defines the gradient to remove
- */
- public removeColorGradient(gradient: number): IParticleSystem {
- if (!this._colorGradients) {
- return this;
- }
- let index = 0;
- for (var colorGradient of this._colorGradients) {
- if (colorGradient.gradient === gradient) {
- this._colorGradients.splice(index, 1);
- break;
- }
- index++;
- }
- return this;
- }
- private _fetchR(u: number, v: number, width: number, height: number, pixels: Uint8Array): number {
- u = Math.abs(u) * 0.5 + 0.5;
- v = Math.abs(v) * 0.5 + 0.5;
- let wrappedU = ((u * width) % width) | 0;
- let wrappedV = ((v * height) % height) | 0;
- let position = (wrappedU + wrappedV * width) * 4;
- return pixels[position] / 255;
- }
- protected _reset() {
- this._resetEffect();
- }
- private _resetEffect() {
- if (this._vertexBuffer) {
- this._vertexBuffer.dispose();
- this._vertexBuffer = null;
- }
- if (this._spriteBuffer) {
- this._spriteBuffer.dispose();
- this._spriteBuffer = null;
- }
- this._createVertexBuffers();
- }
- private _createVertexBuffers() {
- this._vertexBufferSize = this._useInstancing ? 10 : 12;
- if (this._isAnimationSheetEnabled) {
- this._vertexBufferSize += 1;
- }
- if (!this._isBillboardBased) {
- this._vertexBufferSize += 3;
- }
- if (this._useRampGradients) {
- this._vertexBufferSize += 4;
- }
- let engine = this._scene.getEngine();
- this._vertexData = new Float32Array(this._capacity * this._vertexBufferSize * (this._useInstancing ? 1 : 4));
- this._vertexBuffer = new Buffer(engine, this._vertexData, true, this._vertexBufferSize);
- let dataOffset = 0;
- var positions = this._vertexBuffer.createVertexBuffer(VertexBuffer.PositionKind, dataOffset, 3, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers[VertexBuffer.PositionKind] = positions;
- dataOffset += 3;
- var colors = this._vertexBuffer.createVertexBuffer(VertexBuffer.ColorKind, dataOffset, 4, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers[VertexBuffer.ColorKind] = colors;
- dataOffset += 4;
- var options = this._vertexBuffer.createVertexBuffer("angle", dataOffset, 1, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers["angle"] = options;
- dataOffset += 1;
- var size = this._vertexBuffer.createVertexBuffer("size", dataOffset, 2, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers["size"] = size;
- dataOffset += 2;
- if (this._isAnimationSheetEnabled) {
- var cellIndexBuffer = this._vertexBuffer.createVertexBuffer("cellIndex", dataOffset, 1, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers["cellIndex"] = cellIndexBuffer;
- dataOffset += 1;
- }
- if (!this._isBillboardBased) {
- var directionBuffer = this._vertexBuffer.createVertexBuffer("direction", dataOffset, 3, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers["direction"] = directionBuffer;
- dataOffset += 3;
- }
- if (this._useRampGradients) {
- var rampDataBuffer = this._vertexBuffer.createVertexBuffer("remapData", dataOffset, 4, this._vertexBufferSize, this._useInstancing);
- this._vertexBuffers["remapData"] = rampDataBuffer;
- dataOffset += 4;
- }
- var offsets: VertexBuffer;
- if (this._useInstancing) {
- var spriteData = new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]);
- this._spriteBuffer = new Buffer(engine, spriteData, false, 2);
- offsets = this._spriteBuffer.createVertexBuffer("offset", 0, 2);
- } else {
- offsets = this._vertexBuffer.createVertexBuffer("offset", dataOffset, 2, this._vertexBufferSize, this._useInstancing);
- dataOffset += 2;
- }
- this._vertexBuffers["offset"] = offsets;
- }
- private _createIndexBuffer() {
- if (this._useInstancing) {
- return;
- }
- var indices = [];
- var index = 0;
- for (var count = 0; count < this._capacity; count++) {
- indices.push(index);
- indices.push(index + 1);
- indices.push(index + 2);
- indices.push(index);
- indices.push(index + 2);
- indices.push(index + 3);
- index += 4;
- }
- this._indexBuffer = this._scene.getEngine().createIndexBuffer(indices);
- }
- /**
- * Gets the maximum number of particles active at the same time.
- * @returns The max number of active particles.
- */
- public getCapacity(): number {
- return this._capacity;
- }
- /**
- * Gets whether there are still active particles in the system.
- * @returns True if it is alive, otherwise false.
- */
- public isAlive(): boolean {
- return this._alive;
- }
- /**
- * Gets if the system has been started. (Note: this will still be true after stop is called)
- * @returns True if it has been started, otherwise false.
- */
- public isStarted(): boolean {
- return this._started;
- }
- /**
- * Starts the particle system and begins to emit
- * @param delay defines the delay in milliseconds before starting the system (0 by default)
- */
- public start(delay = 0): void {
- if (delay) {
- setTimeout(() => {
- this.start(0);
- }, delay);
- return;
- }
- // Convert the subEmitters field to the constant type field _subEmitters
- this._subEmitters = new Array<Array<SubEmitter>>();
- if (this.subEmitters) {
- this.subEmitters.forEach((subEmitter) => {
- if (subEmitter instanceof ParticleSystem) {
- this._subEmitters.push([new SubEmitter(subEmitter)]);
- } else if (subEmitter instanceof SubEmitter) {
- this._subEmitters.push([subEmitter]);
- } else if (subEmitter instanceof Array) {
- this._subEmitters.push(subEmitter);
- }
- });
- }
- this._started = true;
- this._stopped = false;
- this._actualFrame = 0;
- if (this._subEmitters && this._subEmitters.length != 0) {
- this.activeSubSystems = new Array<ParticleSystem>();
- }
- if (this.preWarmCycles) {
- for (var index = 0; index < this.preWarmCycles; index++) {
- this.animate(true);
- }
- }
- }
- /**
- * Stops the particle system.
- * @param stopSubEmitters if true it will stop the current system and all created sub-Systems if false it will stop the current root system only, this param is used by the root particle system only. the default value is true.
- */
- public stop(stopSubEmitters = true): void {
- this._stopped = true;
- if (stopSubEmitters) {
- this._stopSubEmitters();
- }
- }
- // animation sheet
- /**
- * Remove all active particles
- */
- public reset(): void {
- this._stockParticles = [];
- this._particles = [];
- }
- /**
- * @hidden (for internal use only)
- */
- public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number): void {
- var offset = index * this._vertexBufferSize;
- this._vertexData[offset++] = particle.position.x;
- this._vertexData[offset++] = particle.position.y;
- this._vertexData[offset++] = particle.position.z;
- this._vertexData[offset++] = particle.color.r;
- this._vertexData[offset++] = particle.color.g;
- this._vertexData[offset++] = particle.color.b;
- this._vertexData[offset++] = particle.color.a;
- this._vertexData[offset++] = particle.angle;
- this._vertexData[offset++] = particle.scale.x * particle.size;
- this._vertexData[offset++] = particle.scale.y * particle.size;
- if (this._isAnimationSheetEnabled) {
- this._vertexData[offset++] = particle.cellIndex;
- }
- if (!this._isBillboardBased) {
- if (particle._initialDirection) {
- this._vertexData[offset++] = particle._initialDirection.x;
- this._vertexData[offset++] = particle._initialDirection.y;
- this._vertexData[offset++] = particle._initialDirection.z;
- } else {
- this._vertexData[offset++] = particle.direction.x;
- this._vertexData[offset++] = particle.direction.y;
- this._vertexData[offset++] = particle.direction.z;
- }
- }
- if (this._useRampGradients) {
- this._vertexData[offset++] = particle.remapData.x;
- this._vertexData[offset++] = particle.remapData.y;
- this._vertexData[offset++] = particle.remapData.z;
- this._vertexData[offset++] = particle.remapData.w;
- }
- if (!this._useInstancing) {
- if (this._isAnimationSheetEnabled) {
- if (offsetX === 0)
- offsetX = this._epsilon;
- else if (offsetX === 1)
- offsetX = 1 - this._epsilon;
- if (offsetY === 0)
- offsetY = this._epsilon;
- else if (offsetY === 1)
- offsetY = 1 - this._epsilon;
- }
- this._vertexData[offset++] = offsetX;
- this._vertexData[offset++] = offsetY;
- }
- }
- // start of sub system methods
- /**
- * "Recycles" one of the particle by copying it back to the "stock" of particles and removing it from the active list.
- * Its lifetime will start back at 0.
- */
- public recycleParticle: (particle: Particle) => void = (particle) => {
- // move particle from activeParticle list to stock particles
- var lastParticle = <Particle>this._particles.pop();
- if (lastParticle !== particle) {
- lastParticle.copyTo(particle);
- }
- this._stockParticles.push(lastParticle);
- };
- private _stopSubEmitters(): void {
- if (!this.activeSubSystems) {
- return;
- }
- this.activeSubSystems.forEach(subSystem => {
- subSystem.stop(true);
- });
- this.activeSubSystems = new Array<ParticleSystem>();
- }
- private _createParticle: () => Particle = () => {
- var particle: Particle;
- if (this._stockParticles.length !== 0) {
- particle = <Particle>this._stockParticles.pop();
- particle._reset();
- } else {
- particle = new Particle(this);
- }
- // Attach emitters
- if (this._subEmitters && this._subEmitters.length > 0) {
- var subEmitters = this._subEmitters[Math.floor(Math.random() * this._subEmitters.length)];
- particle._attachedSubEmitters = [];
- subEmitters.forEach((subEmitter) => {
- if (subEmitter.type === SubEmitterType.ATTACHED) {
- var newEmitter = subEmitter.clone();
- (<Array<SubEmitter>>particle._attachedSubEmitters).push(newEmitter);
- newEmitter.particleSystem.start();
- }
- })
- }
- return particle;
- }
- private _removeFromRoot(): void {
- if (!this._rootParticleSystem) {
- return;
- }
- let index = this._rootParticleSystem.activeSubSystems.indexOf(this);
- if (index !== -1) {
- this._rootParticleSystem.activeSubSystems.splice(index, 1);
- }
- }
- private _emitFromParticle: (particle: Particle) => void = (particle) => {
- if (!this._subEmitters || this._subEmitters.length === 0) {
- return;
- }
- var templateIndex = Math.floor(Math.random() * this._subEmitters.length);
- this._subEmitters[templateIndex].forEach((subEmitter) => {
- if (subEmitter.type === SubEmitterType.END) {
- var subSystem = subEmitter.clone();
- ParticleSystem._InheritParticleInfoToSubEmitter(subSystem, particle);
- subSystem.particleSystem._rootParticleSystem = this;
- this.activeSubSystems.push(subSystem.particleSystem);
- subSystem.particleSystem.start();
- }
- })
- }
- // End of sub system methods
- private _update(newParticles: number): void {
- // Update current
- this._alive = this._particles.length > 0;
- if ((<AbstractMesh>this.emitter).position) {
- var emitterMesh = (<AbstractMesh>this.emitter);
- this._emitterWorldMatrix = emitterMesh.getWorldMatrix();
- } else {
- var emitterPosition = (<Vector3>this.emitter);
- this._emitterWorldMatrix = Matrix.Translation(emitterPosition.x, emitterPosition.y, emitterPosition.z);
- }
- this.updateFunction(this._particles);
- // Add new ones
- var particle: Particle;
- for (var index = 0; index < newParticles; index++) {
- if (this._particles.length === this._capacity) {
- break;
- }
- particle = this._createParticle();
- this._particles.push(particle);
- // Emitter
- let emitPower = Scalar.RandomRange(this.minEmitPower, this.maxEmitPower);
- if (this.startPositionFunction) {
- this.startPositionFunction(this._emitterWorldMatrix, particle.position, particle);
- }
- else {
- this.particleEmitterType.startPositionFunction(this._emitterWorldMatrix, particle.position, particle);
- }
- if (this.startDirectionFunction) {
- this.startDirectionFunction(this._emitterWorldMatrix, particle.direction, particle);
- }
- else {
- this.particleEmitterType.startDirectionFunction(this._emitterWorldMatrix, particle.direction, particle);
- }
- if (emitPower === 0) {
- if (!particle._initialDirection) {
- particle._initialDirection = particle.direction.clone();
- } else {
- particle._initialDirection.copyFrom(particle.direction);
- }
- } else {
- particle._initialDirection = null;
- }
- particle.direction.scaleInPlace(emitPower);
- // Life time
- if (this.targetStopDuration && this._lifeTimeGradients && this._lifeTimeGradients.length > 0) {
- let ratio = Scalar.Clamp(this._actualFrame / this.targetStopDuration);
- Tools.GetCurrentGradient(ratio, this._lifeTimeGradients, (currentGradient, nextGradient, scale) => {
- let factorGradient1 = (<FactorGradient>currentGradient);
- let factorGradient2 = (<FactorGradient>nextGradient);
- let lifeTime1 = factorGradient1.getFactor();
- let lifeTime2 = factorGradient2.getFactor();
- let gradient = (ratio - factorGradient1.gradient) / (factorGradient2.gradient - factorGradient1.gradient);
- particle.lifeTime = Scalar.Lerp(lifeTime1, lifeTime2, gradient);
- });
- } else {
- particle.lifeTime = Scalar.RandomRange(this.minLifeTime, this.maxLifeTime);
- }
- // Size
- if (!this._sizeGradients || this._sizeGradients.length === 0) {
- particle.size = Scalar.RandomRange(this.minSize, this.maxSize);
- } else {
- particle._currentSizeGradient = this._sizeGradients[0];
- particle._currentSize1 = particle._currentSizeGradient.getFactor();
- particle.size = particle._currentSize1;
- if (this._sizeGradients.length > 1) {
- particle._currentSize2 = this._sizeGradients[1].getFactor();
- } else {
- particle._currentSize2 = particle._currentSize1;
- }
- }
- // Size and scale
- particle.scale.copyFromFloats(Scalar.RandomRange(this.minScaleX, this.maxScaleX), Scalar.RandomRange(this.minScaleY, this.maxScaleY));
- // Adjust scale by start size
- if (this._startSizeGradients && this._startSizeGradients[0]) {
- const ratio = this._actualFrame / this.targetStopDuration;
- Tools.GetCurrentGradient(ratio, this._startSizeGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== this._currentStartSizeGradient) {
- this._currentStartSize1 = this._currentStartSize2;
- this._currentStartSize2 = (<FactorGradient>nextGradient).getFactor();
- this._currentStartSizeGradient = (<FactorGradient>currentGradient);
- }
- var value = Scalar.Lerp(this._currentStartSize1, this._currentStartSize2, scale);
- particle.scale.scaleInPlace(value);
- });
- }
- // Angle
- if (!this._angularSpeedGradients || this._angularSpeedGradients.length === 0) {
- particle.angularSpeed = Scalar.RandomRange(this.minAngularSpeed, this.maxAngularSpeed);
- } else {
- particle._currentAngularSpeedGradient = this._angularSpeedGradients[0];
- particle.angularSpeed = particle._currentAngularSpeedGradient.getFactor();
- particle._currentAngularSpeed1 = particle.angularSpeed;
- if (this._angularSpeedGradients.length > 1) {
- particle._currentAngularSpeed2 = this._angularSpeedGradients[1].getFactor();
- } else {
- particle._currentAngularSpeed2 = particle._currentAngularSpeed1;
- }
- }
- particle.angle = Scalar.RandomRange(this.minInitialRotation, this.maxInitialRotation);
- // Velocity
- if (this._velocityGradients && this._velocityGradients.length > 0) {
- particle._currentVelocityGradient = this._velocityGradients[0];
- particle._currentVelocity1 = particle._currentVelocityGradient.getFactor();
- if (this._velocityGradients.length > 1) {
- particle._currentVelocity2 = this._velocityGradients[1].getFactor();
- } else {
- particle._currentVelocity2 = particle._currentVelocity1;
- }
- }
- // Limit velocity
- if (this._limitVelocityGradients && this._limitVelocityGradients.length > 0) {
- particle._currentLimitVelocityGradient = this._limitVelocityGradients[0];
- particle._currentLimitVelocity1 = particle._currentLimitVelocityGradient.getFactor();
- if (this._limitVelocityGradients.length > 1) {
- particle._currentLimitVelocity2 = this._limitVelocityGradients[1].getFactor();
- } else {
- particle._currentLimitVelocity2 = particle._currentLimitVelocity1;
- }
- }
- // Drag
- if (this._dragGradients && this._dragGradients.length > 0) {
- particle._currentDragGradient = this._dragGradients[0];
- particle._currentDrag1 = particle._currentDragGradient.getFactor();
- if (this._dragGradients.length > 1) {
- particle._currentDrag2 = this._dragGradients[1].getFactor();
- } else {
- particle._currentDrag2 = particle._currentDrag1;
- }
- }
- // Color
- if (!this._colorGradients || this._colorGradients.length === 0) {
- var step = Scalar.RandomRange(0, 1.0);
- Color4.LerpToRef(this.color1, this.color2, step, particle.color);
- this.colorDead.subtractToRef(particle.color, this._colorDiff);
- this._colorDiff.scaleToRef(1.0 / particle.lifeTime, particle.colorStep);
- } else {
- particle._currentColorGradient = this._colorGradients[0];
- particle._currentColorGradient.getColorToRef(particle.color);
- particle._currentColor1.copyFrom(particle.color);
- if (this._colorGradients.length > 1) {
- this._colorGradients[1].getColorToRef(particle._currentColor2);
- } else {
- particle._currentColor2.copyFrom(particle.color);
- }
- }
- // Sheet
- if (this._isAnimationSheetEnabled) {
- particle._initialStartSpriteCellID = this.startSpriteCellID;
- particle._initialEndSpriteCellID = this.endSpriteCellID;
- }
- // Inherited Velocity
- particle.direction.addInPlace(this._inheritedVelocityOffset);
- // Ramp
- if (this._useRampGradients) {
- particle.remapData = new Vector4(0, 1, 0, 1);
- }
- }
- }
- /** @hidden */
- public static _GetAttributeNamesOrOptions(isAnimationSheetEnabled = false, isBillboardBased = false, useRampGradients = false): string[] {
- var attributeNamesOrOptions = [VertexBuffer.PositionKind, VertexBuffer.ColorKind, "angle", "offset", "size"]
- if (isAnimationSheetEnabled) {
- attributeNamesOrOptions.push("cellIndex");
- }
- if (!isBillboardBased) {
- attributeNamesOrOptions.push("direction");
- }
- if (useRampGradients) {
- attributeNamesOrOptions.push("remapData");
- }
- return attributeNamesOrOptions;
- }
- public static _GetEffectCreationOptions(isAnimationSheetEnabled = false): string[] {
- var effectCreationOption = ["invView", "view", "projection", "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "textureMask", "translationPivot", "eyePosition"];
- if (isAnimationSheetEnabled) {
- effectCreationOption.push("particlesInfos")
- }
- return effectCreationOption;
- }
- private _getEffect(blendMode: number): Effect {
- if (this._customEffect) {
- return this._customEffect;
- };
- var defines = [];
- if (this._scene.clipPlane) {
- defines.push("#define CLIPPLANE");
- }
- if (this._scene.clipPlane2) {
- defines.push("#define CLIPPLANE2");
- }
- if (this._scene.clipPlane3) {
- defines.push("#define CLIPPLANE3");
- }
- if (this._scene.clipPlane4) {
- defines.push("#define CLIPPLANE4");
- }
- if (this._isAnimationSheetEnabled) {
- defines.push("#define ANIMATESHEET");
- }
- if (blendMode === ParticleSystem.BLENDMODE_MULTIPLY) {
- defines.push("#define BLENDMULTIPLYMODE");
- }
- if (this._useRampGradients) {
- defines.push("#define RAMPGRADIENT");
- }
- if (this._isBillboardBased) {
- defines.push("#define BILLBOARD");
- switch (this.billboardMode) {
- case AbstractMesh.BILLBOARDMODE_Y:
- defines.push("#define BILLBOARDY");
- break;
- case AbstractMesh.BILLBOARDMODE_ALL:
- default:
- break;
- }
- }
- if (this._imageProcessingConfiguration) {
- this._imageProcessingConfiguration.prepareDefines(this._imageProcessingConfigurationDefines);
- defines.push(this._imageProcessingConfigurationDefines.toString());
- }
- // Effect
- var join = defines.join("\n");
- if (this._cachedDefines !== join) {
- this._cachedDefines = join;
- var attributesNamesOrOptions = ParticleSystem._GetAttributeNamesOrOptions(this._isAnimationSheetEnabled, this._isBillboardBased, this._useRampGradients);
- var effectCreationOption = ParticleSystem._GetEffectCreationOptions(this._isAnimationSheetEnabled);
- var samplers = ["diffuseSampler", "rampSampler"];
- if (ImageProcessingConfiguration) {
- ImageProcessingConfiguration.PrepareUniforms(effectCreationOption, this._imageProcessingConfigurationDefines);
- ImageProcessingConfiguration.PrepareSamplers(samplers, this._imageProcessingConfigurationDefines);
- }
- this._effect = this._scene.getEngine().createEffect(
- "particles",
- attributesNamesOrOptions,
- effectCreationOption,
- samplers, join);
- }
- return this._effect;
- }
- /**
- * Animates the particle system for the current frame by emitting new particles and or animating the living ones.
- * @param preWarmOnly will prevent the system from updating the vertex buffer (default is false)
- */
- public animate(preWarmOnly = false): void {
- if (!this._started)
- return;
- if (!preWarmOnly) {
- // Check
- if (!this.isReady())
- return;
- if (this._currentRenderId === this._scene.getRenderId()) {
- return;
- }
- this._currentRenderId = this._scene.getRenderId();
- }
- this._scaledUpdateSpeed = this.updateSpeed * (preWarmOnly ? this.preWarmStepOffset : this._scene.getAnimationRatio());
- // Determine the number of particles we need to create
- var newParticles;
- if (this.manualEmitCount > -1) {
- newParticles = this.manualEmitCount;
- this._newPartsExcess = 0;
- this.manualEmitCount = 0;
- } else {
- let rate = this.emitRate;
- if (this._emitRateGradients && this._emitRateGradients.length > 0 && this.targetStopDuration) {
- const ratio = this._actualFrame / this.targetStopDuration;
- Tools.GetCurrentGradient(ratio, this._emitRateGradients, (currentGradient, nextGradient, scale) => {
- if (currentGradient !== this._currentEmitRateGradient) {
- this._currentEmitRate1 = this._currentEmitRate2;
- this._currentEmitRate2 = (<FactorGradient>nextGradient).getFactor();
- this._currentEmitRateGradient = (<FactorGradient>currentGradient);
- }
- rate = Scalar.Lerp(this._currentEmitRate1, this._currentEmitRate2, scale);
- });
- }
- newParticles = ((rate * this._scaledUpdateSpeed) >> 0);
- this._newPartsExcess += rate * this._scaledUpdateSpeed - newParticles;
- }
- if (this._newPartsExcess > 1.0) {
- newParticles += this._newPartsExcess >> 0;
- this._newPartsExcess -= this._newPartsExcess >> 0;
- }
- this._alive = false;
- if (!this._stopped) {
- this._actualFrame += this._scaledUpdateSpeed;
- if (this.targetStopDuration && this._actualFrame >= this.targetStopDuration)
- this.stop();
- } else {
- newParticles = 0;
- }
- this._update(newParticles);
- // Stopped?
- if (this._stopped) {
- if (!this._alive) {
- this._started = false;
- if (this.onAnimationEnd) {
- this.onAnimationEnd();
- }
- if (this.disposeOnStop) {
- this._scene._toBeDisposed.push(this);
- }
- }
- }
- if (!preWarmOnly) {
- // Update VBO
- var offset = 0;
- for (var index = 0; index < this._particles.length; index++) {
- var particle = this._particles[index];
- this._appendParticleVertices(offset, particle);
- offset += this._useInstancing ? 1 : 4;
- }
- if (this._vertexBuffer) {
- this._vertexBuffer.update(this._vertexData);
- }
- }
- if (this.manualEmitCount === 0 && this.disposeOnStop) {
- this.stop();
- }
- }
- private _appendParticleVertices(offset: number, particle: Particle) {
- this._appendParticleVertex(offset++, particle, 0, 0);
- if (!this._useInstancing) {
- this._appendParticleVertex(offset++, particle, 1, 0);
- this._appendParticleVertex(offset++, particle, 1, 1);
- this._appendParticleVertex(offset++, particle, 0, 1);
- }
- }
- /**
- * Rebuilds the particle system.
- */
- public rebuild(): void {
- this._createIndexBuffer();
- if (this._vertexBuffer) {
- this._vertexBuffer._rebuild();
- }
- }
- /**
- * Is this system ready to be used/rendered
- * @return true if the system is ready
- */
- public isReady(): boolean {
- if (!this.emitter || !this._imageProcessingConfiguration.isReady() || !this.particleTexture || !this.particleTexture.isReady()) {
- return false;
- }
- if (this.blendMode !== ParticleSystem.BLENDMODE_MULTIPLYADD) {
- if (!this._getEffect(this.blendMode).isReady()) {
- return false;
- }
- } else {
- if (!this._getEffect(ParticleSystem.BLENDMODE_MULTIPLY).isReady()) {
- return false;
- }
- if (!this._getEffect(ParticleSystem.BLENDMODE_ADD).isReady()) {
- return false;
- }
- }
- return true;
- }
- private _render(blendMode: number) {
- var effect = this._getEffect(blendMode);
- var engine = this._scene.getEngine();
- // Render
- engine.enableEffect(effect);
- var viewMatrix = this._scene.getViewMatrix();
- effect.setTexture("diffuseSampler", this.particleTexture);
- effect.setMatrix("view", viewMatrix);
- effect.setMatrix("projection", this._scene.getProjectionMatrix());
- if (this._isAnimationSheetEnabled && this.particleTexture) {
- var baseSize = this.particleTexture.getBaseSize();
- effect.setFloat3("particlesInfos", this.spriteCellWidth / baseSize.width, this.spriteCellHeight / baseSize.height, baseSize.width / this.spriteCellWidth);
- }
- effect.setVector2("translationPivot", this.translationPivot);
- effect.setFloat4("textureMask", this.textureMask.r, this.textureMask.g, this.textureMask.b, this.textureMask.a);
- if (this._isBillboardBased) {
- var camera = this._scene.activeCamera!;
- effect.setVector3("eyePosition", camera.globalPosition);
- }
- if (this._rampGradientsTexture) {
- effect.setTexture("rampSampler", this._rampGradientsTexture);
- }
- if (this._scene.clipPlane || this._scene.clipPlane2 || this._scene.clipPlane3 || this._scene.clipPlane4) {
- var invView = viewMatrix.clone();
- invView.invert();
- effect.setMatrix("invView", invView);
- MaterialHelper.BindClipPlane(effect, this._scene);
- }
- engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
- // image processing
- if (this._imageProcessingConfiguration && !this._imageProcessingConfiguration.applyByPostProcess) {
- this._imageProcessingConfiguration.bind(effect);
- }
- // Draw order
- switch (blendMode) {
- case ParticleSystem.BLENDMODE_ADD:
- engine.setAlphaMode(Engine.ALPHA_ADD);
- break;
- case ParticleSystem.BLENDMODE_ONEONE:
- engine.setAlphaMode(Engine.ALPHA_ONEONE);
- break;
- case ParticleSystem.BLENDMODE_STANDARD:
- engine.setAlphaMode(Engine.ALPHA_COMBINE);
- break;
- case ParticleSystem.BLENDMODE_MULTIPLY:
- engine.setAlphaMode(Engine.ALPHA_MULTIPLY);
- break;
- }
- if (this._useInstancing) {
- engine.drawArraysType(Material.TriangleFanDrawMode, 0, 4, this._particles.length);
- } else {
- engine.drawElementsType(Material.TriangleFillMode, 0, this._particles.length * 6);
- }
- return this._particles.length;
- }
- /**
- * Renders the particle system in its current state.
- * @returns the current number of particles
- */
- public render(): number {
- // Check
- if (!this.isReady() || !this._particles.length) {
- return 0;
- }
- var engine = this._scene.getEngine();
- engine.setState(false);
- if (this.forceDepthWrite) {
- engine.setDepthWrite(true);
- }
- let outparticles = 0;
- if (this.blendMode === ParticleSystem.BLENDMODE_MULTIPLYADD) {
- outparticles = this._render(ParticleSystem.BLENDMODE_MULTIPLY) + this._render(ParticleSystem.BLENDMODE_ADD);
- }
- outparticles = this._render(this.blendMode);
- engine.unbindInstanceAttributes();
- engine.setAlphaMode(Engine.ALPHA_DISABLE);
- return outparticles;
- }
- /**
- * Disposes the particle system and free the associated resources
- * @param disposeTexture defines if the particule texture must be disposed as well (true by default)
- */
- public dispose(disposeTexture = true): void {
- if (this._vertexBuffer) {
- this._vertexBuffer.dispose();
- this._vertexBuffer = null;
- }
- if (this._spriteBuffer) {
- this._spriteBuffer.dispose();
- this._spriteBuffer = null;
- }
- if (this._indexBuffer) {
- this._scene.getEngine()._releaseBuffer(this._indexBuffer);
- this._indexBuffer = null;
- }
- if (disposeTexture && this.particleTexture) {
- this.particleTexture.dispose();
- this.particleTexture = null;
- }
- if (disposeTexture && this.noiseTexture) {
- this.noiseTexture.dispose();
- this.noiseTexture = null;
- }
- if (this._rampGradientsTexture) {
- this._rampGradientsTexture.dispose();
- this._rampGradientsTexture = null;
- }
- this._removeFromRoot();
- if (this._disposeEmitterOnDispose && this.emitter && (this.emitter as AbstractMesh).dispose && !(this.emitter as AbstractMesh).isDisposed === false) {
- (<AbstractMesh>this.emitter).dispose();
- }
- // Remove from scene
- var index = this._scene.particleSystems.indexOf(this);
- if (index > -1) {
- this._scene.particleSystems.splice(index, 1);
- }
- // Callback
- this.onDisposeObservable.notifyObservers(this);
- this.onDisposeObservable.clear();
- }
- // Clone
- /**
- * Clones the particle system.
- * @param name The name of the cloned object
- * @param newEmitter The new emitter to use
- * @returns the cloned particle system
- */
- public clone(name: string, newEmitter: any): ParticleSystem {
- var custom: Nullable<Effect> = null;
- var program: any = null;
- if (this.customShader != null) {
- program = this.customShader;
- var defines: string = (program.shaderOptions.defines.length > 0) ? program.shaderOptions.defines.join("\n") : "";
- custom = this._scene.getEngine().createEffectForParticles(program.shaderPath.fragmentElement, program.shaderOptions.uniforms, program.shaderOptions.samplers, defines);
- } else if (this._customEffect) {
- custom = this._customEffect;
- }
- var result = new ParticleSystem(name, this._capacity, this._scene, custom);
- result.customShader = program;
- Tools.DeepCopy(this, result, ["particles", "customShader"]);
- if (newEmitter === undefined) {
- newEmitter = this.emitter;
- }
- result.emitter = newEmitter;
- if (this.particleTexture) {
- result.particleTexture = new Texture(this.particleTexture.url, this._scene);
- }
- // Clone gradients
- if (this._colorGradients) {
- this._colorGradients.forEach((v) => {
- result.addColorGradient(v.gradient, v.color1, v.color2);
- });
- }
- if (this._dragGradients) {
- this._dragGradients.forEach((v) => {
- result.addDragGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._angularSpeedGradients) {
- this._angularSpeedGradients.forEach((v) => {
- result.addAngularSpeedGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._emitRateGradients) {
- this._emitRateGradients.forEach((v) => {
- result.addEmitRateGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._lifeTimeGradients) {
- this._lifeTimeGradients.forEach((v) => {
- result.addLifeTimeGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._limitVelocityGradients) {
- this._limitVelocityGradients.forEach((v) => {
- result.addLimitVelocityGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._sizeGradients) {
- this._sizeGradients.forEach((v) => {
- result.addSizeGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._startSizeGradients) {
- this._startSizeGradients.forEach((v) => {
- result.addStartSizeGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._velocityGradients) {
- this._velocityGradients.forEach((v) => {
- result.addVelocityGradient(v.gradient, v.factor1, v.factor2);
- });
- }
- if (this._rampGradients) {
- this._rampGradients.forEach((v) => {
- result.addRampGradient(v.gradient, v.color);
- });
- }
- if (this._colorRemapGradients) {
- this._colorRemapGradients.forEach((v) => {
- result.addColorRemapGradient(v.gradient, v.factor1, v.factor2!);
- });
- }
- if (this._alphaRemapGradients) {
- this._alphaRemapGradients.forEach((v) => {
- result.addAlphaRemapGradient(v.gradient, v.factor1, v.factor2!);
- });
- }
- if (!this.preventAutoStart) {
- result.start();
- }
- return result;
- }
- /**
- * Serializes the particle system to a JSON object.
- * @returns the JSON object
- */
- public serialize(): any {
- var serializationObject: any = {};
- ParticleSystem._Serialize(serializationObject, this);
- serializationObject.textureMask = this.textureMask.asArray();
- serializationObject.customShader = this.customShader;
- serializationObject.preventAutoStart = this.preventAutoStart;
- serializationObject.isAnimationSheetEnabled = this._isAnimationSheetEnabled;
- return serializationObject;
- }
- /** @hidden */
- public static _Serialize(serializationObject: any, particleSystem: IParticleSystem) {
- serializationObject.name = particleSystem.name;
- serializationObject.id = particleSystem.id;
- serializationObject.capacity = particleSystem.getCapacity();
- // Emitter
- if ((<AbstractMesh>particleSystem.emitter).position) {
- var emitterMesh = (<AbstractMesh>particleSystem.emitter);
- serializationObject.emitterId = emitterMesh.id;
- } else {
- var emitterPosition = (<Vector3>particleSystem.emitter);
- serializationObject.emitter = emitterPosition.asArray();
- }
- // Emitter
- if (particleSystem.particleEmitterType) {
- serializationObject.particleEmitterType = particleSystem.particleEmitterType.serialize();
- }
- if (particleSystem.particleTexture) {
- serializationObject.textureName = particleSystem.particleTexture.name;
- }
- // Animations
- Animation.AppendSerializedAnimations(particleSystem, serializationObject);
- // Particle system
- serializationObject.renderingGroupId = particleSystem.renderingGroupId;
- serializationObject.isBillboardBased = particleSystem.isBillboardBased;
- serializationObject.minAngularSpeed = particleSystem.minAngularSpeed;
- serializationObject.maxAngularSpeed = particleSystem.maxAngularSpeed;
- serializationObject.minSize = particleSystem.minSize;
- serializationObject.maxSize = particleSystem.maxSize;
- serializationObject.minScaleX = particleSystem.minScaleX;
- serializationObject.maxScaleX = particleSystem.maxScaleX;
- serializationObject.minScaleY = particleSystem.minScaleY;
- serializationObject.maxScaleY = particleSystem.maxScaleY;
- serializationObject.minEmitPower = particleSystem.minEmitPower;
- serializationObject.maxEmitPower = particleSystem.maxEmitPower;
- serializationObject.minLifeTime = particleSystem.minLifeTime;
- serializationObject.maxLifeTime = particleSystem.maxLifeTime;
- serializationObject.emitRate = particleSystem.emitRate;
- serializationObject.gravity = particleSystem.gravity.asArray();
- serializationObject.noiseStrength = particleSystem.noiseStrength.asArray();
- serializationObject.color1 = particleSystem.color1.asArray();
- serializationObject.color2 = particleSystem.color2.asArray();
- serializationObject.colorDead = particleSystem.colorDead.asArray();
- serializationObject.updateSpeed = particleSystem.updateSpeed;
- serializationObject.targetStopDuration = particleSystem.targetStopDuration;
- serializationObject.blendMode = particleSystem.blendMode;
- serializationObject.preWarmCycles = particleSystem.preWarmCycles;
- serializationObject.preWarmStepOffset = particleSystem.preWarmStepOffset;
- serializationObject.minInitialRotation = particleSystem.minInitialRotation;
- serializationObject.maxInitialRotation = particleSystem.maxInitialRotation;
- serializationObject.startSpriteCellID = particleSystem.startSpriteCellID;
- serializationObject.endSpriteCellID = particleSystem.endSpriteCellID;
- serializationObject.spriteCellChangeSpeed = particleSystem.spriteCellChangeSpeed;
- serializationObject.spriteCellWidth = particleSystem.spriteCellWidth;
- serializationObject.spriteCellHeight = particleSystem.spriteCellHeight;
- let colorGradients = particleSystem.getColorGradients();
- if (colorGradients) {
- serializationObject.colorGradients = [];
- for (var colorGradient of colorGradients) {
- var serializedGradient: any = {
- gradient: colorGradient.gradient,
- color1: colorGradient.color1.asArray()
- };
- if (colorGradient.color2) {
- serializedGradient.color2 = colorGradient.color2.asArray();
- }
- serializationObject.colorGradients.push(serializedGradient);
- }
- }
- let rampGradients = particleSystem.getRampGradients();
- if (rampGradients) {
- serializationObject.rampGradients = [];
- for (var rampGradient of rampGradients) {
- var serializedGradient: any = {
- gradient: rampGradient.gradient,
- color: rampGradient.color.asArray()
- };
- serializationObject.rampGradients.push(serializedGradient);
- }
- }
- let colorRemapGradients = particleSystem.getColorRemapGradients();
- if (colorRemapGradients) {
- serializationObject.colorRemapGradients = [];
- for (var colorRemapGradient of colorRemapGradients) {
- var serializedGradient: any = {
- gradient: colorRemapGradient.gradient,
- factor1: colorRemapGradient.factor1
- };
- if (colorRemapGradient.factor2 !== undefined) {
- serializedGradient.factor2 = colorRemapGradient.factor2;
- }
- serializationObject.colorRemapGradients.push(serializedGradient);
- }
- }
- let alphaRemapGradients = particleSystem.getAlphaRemapGradients();
- if (alphaRemapGradients) {
- serializationObject.alphaRemapGradients = [];
- for (var alphaRemapGradient of alphaRemapGradients) {
- var serializedGradient: any = {
- gradient: alphaRemapGradient.gradient,
- factor1: alphaRemapGradient.factor1
- };
- if (alphaRemapGradient.factor2 !== undefined) {
- serializedGradient.factor2 = alphaRemapGradient.factor2;
- }
- serializationObject.alphaRemapGradients.push(serializedGradient);
- }
- }
- let sizeGradients = particleSystem.getSizeGradients();
- if (sizeGradients) {
- serializationObject.sizeGradients = [];
- for (var sizeGradient of sizeGradients) {
- var serializedGradient: any = {
- gradient: sizeGradient.gradient,
- factor1: sizeGradient.factor1
- };
- if (sizeGradient.factor2 !== undefined) {
- serializedGradient.factor2 = sizeGradient.factor2;
- }
- serializationObject.sizeGradients.push(serializedGradient);
- }
- }
- let angularSpeedGradients = particleSystem.getAngularSpeedGradients();
- if (angularSpeedGradients) {
- serializationObject.angularSpeedGradients = [];
- for (var angularSpeedGradient of angularSpeedGradients) {
- var serializedGradient: any = {
- gradient: angularSpeedGradient.gradient,
- factor1: angularSpeedGradient.factor1
- };
- if (angularSpeedGradient.factor2 !== undefined) {
- serializedGradient.factor2 = angularSpeedGradient.factor2;
- }
- serializationObject.angularSpeedGradients.push(serializedGradient);
- }
- }
- let velocityGradients = particleSystem.getVelocityGradients();
- if (velocityGradients) {
- serializationObject.velocityGradients = [];
- for (var velocityGradient of velocityGradients) {
- var serializedGradient: any = {
- gradient: velocityGradient.gradient,
- factor1: velocityGradient.factor1
- };
- if (velocityGradient.factor2 !== undefined) {
- serializedGradient.factor2 = velocityGradient.factor2;
- }
- serializationObject.velocityGradients.push(serializedGradient);
- }
- }
- let dragGradients = particleSystem.getDragGradients();
- if (dragGradients) {
- serializationObject.dragyGradients = [];
- for (var dragGradient of dragGradients) {
- var serializedGradient: any = {
- gradient: dragGradient.gradient,
- factor1: dragGradient.factor1
- };
- if (dragGradient.factor2 !== undefined) {
- serializedGradient.factor2 = dragGradient.factor2;
- }
- serializationObject.dragGradients.push(serializedGradient);
- }
- }
- let emitRateGradients = particleSystem.getEmitRateGradients();
- if (emitRateGradients) {
- serializationObject.emitRateGradients = [];
- for (var emitRateGradient of emitRateGradients) {
- var serializedGradient: any = {
- gradient: emitRateGradient.gradient,
- factor1: emitRateGradient.factor1
- };
- if (emitRateGradient.factor2 !== undefined) {
- serializedGradient.factor2 = emitRateGradient.factor2;
- }
- serializationObject.emitRateGradients.push(serializedGradient);
- }
- }
- let startSizeGradients = particleSystem.getStartSizeGradients();
- if (startSizeGradients) {
- serializationObject.startSizeGradients = [];
- for (var startSizeGradient of startSizeGradients) {
- var serializedGradient: any = {
- gradient: startSizeGradient.gradient,
- factor1: startSizeGradient.factor1
- };
- if (startSizeGradient.factor2 !== undefined) {
- serializedGradient.factor2 = startSizeGradient.factor2;
- }
- serializationObject.startSizeGradients.push(serializedGradient);
- }
- }
- let limitVelocityGradients = particleSystem.getLimitVelocityGradients();
- if (limitVelocityGradients) {
- serializationObject.limitVelocityGradients = [];
- for (var limitVelocityGradient of limitVelocityGradients) {
- var serializedGradient: any = {
- gradient: limitVelocityGradient.gradient,
- factor1: limitVelocityGradient.factor1
- };
- if (limitVelocityGradient.factor2 !== undefined) {
- serializedGradient.factor2 = limitVelocityGradient.factor2;
- }
- serializationObject.limitVelocityGradients.push(serializedGradient);
- }
- serializationObject.limitVelocityDamping = particleSystem.limitVelocityDamping;
- }
- if (ProceduralTexture && particleSystem.noiseTexture && particleSystem.noiseTexture instanceof ProceduralTexture) {
- const noiseTexture = particleSystem.noiseTexture as ProceduralTexture;
- serializationObject.noiseTexture = noiseTexture.serialize();
- }
- }
- /** @hidden */
- public static _Parse(parsedParticleSystem: any, particleSystem: IParticleSystem, scene: Scene, rootUrl: string) {
- // Texture
- if (parsedParticleSystem.textureName) {
- particleSystem.particleTexture = new Texture(rootUrl + parsedParticleSystem.textureName, scene);
- particleSystem.particleTexture.name = parsedParticleSystem.textureName;
- }
- // Emitter
- if (parsedParticleSystem.emitterId === undefined) {
- particleSystem.emitter = Vector3.Zero();
- }
- else if (parsedParticleSystem.emitterId) {
- particleSystem.emitter = scene.getLastMeshByID(parsedParticleSystem.emitterId);
- } else {
- particleSystem.emitter = Vector3.FromArray(parsedParticleSystem.emitter);
- }
- // Misc.
- if (parsedParticleSystem.renderingGroupId !== undefined) {
- particleSystem.renderingGroupId = parsedParticleSystem.renderingGroupId;
- }
- if (parsedParticleSystem.isBillboardBased !== undefined) {
- particleSystem.isBillboardBased = parsedParticleSystem.isBillboardBased;
- }
- // Animations
- if (parsedParticleSystem.animations) {
- for (var animationIndex = 0; animationIndex < parsedParticleSystem.animations.length; animationIndex++) {
- var parsedAnimation = parsedParticleSystem.animations[animationIndex];
- particleSystem.animations.push(Animation.Parse(parsedAnimation));
- }
- }
- if (parsedParticleSystem.autoAnimate) {
- scene.beginAnimation(particleSystem, parsedParticleSystem.autoAnimateFrom, parsedParticleSystem.autoAnimateTo, parsedParticleSystem.autoAnimateLoop, parsedParticleSystem.autoAnimateSpeed || 1.0);
- }
- // Particle system
- particleSystem.minAngularSpeed = parsedParticleSystem.minAngularSpeed;
- particleSystem.maxAngularSpeed = parsedParticleSystem.maxAngularSpeed;
- particleSystem.minSize = parsedParticleSystem.minSize;
- particleSystem.maxSize = parsedParticleSystem.maxSize;
- if (parsedParticleSystem.minScaleX) {
- particleSystem.minScaleX = parsedParticleSystem.minScaleX;
- particleSystem.maxScaleX = parsedParticleSystem.maxScaleX;
- particleSystem.minScaleY = parsedParticleSystem.minScaleY;
- particleSystem.maxScaleY = parsedParticleSystem.maxScaleY;
- }
- if (parsedParticleSystem.preWarmCycles !== undefined) {
- particleSystem.preWarmCycles = parsedParticleSystem.preWarmCycles;
- particleSystem.preWarmStepOffset = parsedParticleSystem.preWarmStepOffset;
- }
- if (parsedParticleSystem.minInitialRotation !== undefined) {
- particleSystem.minInitialRotation = parsedParticleSystem.minInitialRotation;
- particleSystem.maxInitialRotation = parsedParticleSystem.maxInitialRotation;
- }
- particleSystem.minLifeTime = parsedParticleSystem.minLifeTime;
- particleSystem.maxLifeTime = parsedParticleSystem.maxLifeTime;
- particleSystem.minEmitPower = parsedParticleSystem.minEmitPower;
- particleSystem.maxEmitPower = parsedParticleSystem.maxEmitPower;
- particleSystem.emitRate = parsedParticleSystem.emitRate;
- particleSystem.gravity = Vector3.FromArray(parsedParticleSystem.gravity);
- if (parsedParticleSystem.noiseStrength) {
- particleSystem.noiseStrength = Vector3.FromArray(parsedParticleSystem.noiseStrength);
- }
- particleSystem.color1 = Color4.FromArray(parsedParticleSystem.color1);
- particleSystem.color2 = Color4.FromArray(parsedParticleSystem.color2);
- particleSystem.colorDead = Color4.FromArray(parsedParticleSystem.colorDead);
- particleSystem.updateSpeed = parsedParticleSystem.updateSpeed;
- particleSystem.targetStopDuration = parsedParticleSystem.targetStopDuration;
- particleSystem.blendMode = parsedParticleSystem.blendMode;
- if (parsedParticleSystem.colorGradients) {
- for (var colorGradient of parsedParticleSystem.colorGradients) {
- particleSystem.addColorGradient(colorGradient.gradient, Color4.FromArray(colorGradient.color1), colorGradient.color2 ? Color4.FromArray(colorGradient.color2) : undefined);
- }
- }
- if (parsedParticleSystem.rampGradients) {
- for (var rampGradient of parsedParticleSystem.rampGradients) {
- particleSystem.addRampGradient(rampGradient.gradient, Color3.FromArray(rampGradient.color1));
- }
- }
- if (parsedParticleSystem.colorRemapGradients) {
- for (var colorRemapGradient of parsedParticleSystem.colorRemapGradients) {
- particleSystem.addColorRemapGradient(colorRemapGradient.gradient, colorRemapGradient.factor1 !== undefined ? colorRemapGradient.factor1 : colorRemapGradient.factor, colorRemapGradient.factor2);
- }
- }
- if (parsedParticleSystem.alphaRemapGradients) {
- for (var alphaRemapGradient of parsedParticleSystem.alphaRemapGradients) {
- particleSystem.addAlphaRemapGradient(alphaRemapGradient.gradient, alphaRemapGradient.factor1 !== undefined ? alphaRemapGradient.factor1 : alphaRemapGradient.factor, alphaRemapGradient.factor2);
- }
- }
- if (parsedParticleSystem.sizeGradients) {
- for (var sizeGradient of parsedParticleSystem.sizeGradients) {
- particleSystem.addSizeGradient(sizeGradient.gradient, sizeGradient.factor1 !== undefined ? sizeGradient.factor1 : sizeGradient.factor, sizeGradient.factor2);
- }
- }
- if (parsedParticleSystem.sizeGradients) {
- for (var sizeGradient of parsedParticleSystem.sizeGradients) {
- particleSystem.addSizeGradient(sizeGradient.gradient, sizeGradient.factor1 !== undefined ? sizeGradient.factor1 : sizeGradient.factor, sizeGradient.factor2);
- }
- }
- if (parsedParticleSystem.angularSpeedGradients) {
- for (var angularSpeedGradient of parsedParticleSystem.angularSpeedGradients) {
- particleSystem.addAngularSpeedGradient(angularSpeedGradient.gradient, angularSpeedGradient.factor1 !== undefined ? angularSpeedGradient.factor1 : angularSpeedGradient.factor, angularSpeedGradient.factor2);
- }
- }
- if (parsedParticleSystem.velocityGradients) {
- for (var velocityGradient of parsedParticleSystem.velocityGradients) {
- particleSystem.addVelocityGradient(velocityGradient.gradient, velocityGradient.factor1 !== undefined ? velocityGradient.factor1 : velocityGradient.factor, velocityGradient.factor2);
- }
- }
- if (parsedParticleSystem.dragGradients) {
- for (var dragGradient of parsedParticleSystem.dragGradients) {
- particleSystem.addDragGradient(dragGradient.gradient, dragGradient.factor1 !== undefined ? dragGradient.factor1 : dragGradient.factor, dragGradient.factor2);
- }
- }
- if (parsedParticleSystem.emitRateGradients) {
- for (var emitRateGradient of parsedParticleSystem.emitRateGradients) {
- particleSystem.addEmitRateGradient(emitRateGradient.gradient, emitRateGradient.factor1 !== undefined ? emitRateGradient.factor1 : emitRateGradient.factor, emitRateGradient.factor2);
- }
- }
- if (parsedParticleSystem.startSizeGradients) {
- for (var startSizeGradient of parsedParticleSystem.startSizeGradients) {
- particleSystem.addStartSizeGradient(startSizeGradient.gradient, startSizeGradient.factor1 !== undefined ? startSizeGradient.factor1 : startSizeGradient.factor, startSizeGradient.factor2);
- }
- }
- if (parsedParticleSystem.limitVelocityGradients) {
- for (var limitVelocityGradient of parsedParticleSystem.limitVelocityGradients) {
- particleSystem.addLimitVelocityGradient(limitVelocityGradient.gradient, limitVelocityGradient.factor1 !== undefined ? limitVelocityGradient.factor1 : limitVelocityGradient.factor, limitVelocityGradient.factor2);
- }
- particleSystem.limitVelocityDamping = parsedParticleSystem.limitVelocityDamping;
- }
- if (parsedParticleSystem.noiseTexture) {
- particleSystem.noiseTexture = ProceduralTexture.Parse(parsedParticleSystem.noiseTexture, scene, rootUrl);
- }
- // Emitter
- let emitterType: IParticleEmitterType;
- if (parsedParticleSystem.particleEmitterType) {
- switch (parsedParticleSystem.particleEmitterType.type) {
- case "SphereParticleEmitter":
- emitterType = new SphereParticleEmitter();
- break;
- case "SphereDirectedParticleEmitter":
- emitterType = new SphereDirectedParticleEmitter();
- break;
- case "ConeEmitter":
- case "ConeParticleEmitter":
- emitterType = new ConeParticleEmitter();
- break;
- case "BoxEmitter":
- case "BoxParticleEmitter":
- default:
- emitterType = new BoxParticleEmitter();
- break;
- }
- emitterType.parse(parsedParticleSystem.particleEmitterType);
- } else {
- emitterType = new BoxParticleEmitter();
- emitterType.parse(parsedParticleSystem);
- }
- particleSystem.particleEmitterType = emitterType;
- // Animation sheet
- particleSystem.startSpriteCellID = parsedParticleSystem.startSpriteCellID;
- particleSystem.endSpriteCellID = parsedParticleSystem.endSpriteCellID;
- particleSystem.spriteCellWidth = parsedParticleSystem.spriteCellWidth;
- particleSystem.spriteCellHeight = parsedParticleSystem.spriteCellHeight;
- particleSystem.spriteCellChangeSpeed = parsedParticleSystem.spriteCellChangeSpeed;
- }
- /**
- * Parses a JSON object to create a particle system.
- * @param parsedParticleSystem The JSON object to parse
- * @param scene The scene to create the particle system in
- * @param rootUrl The root url to use to load external dependencies like texture
- * @returns the Parsed particle system
- */
- public static Parse(parsedParticleSystem: any, scene: Scene, rootUrl: string): ParticleSystem {
- var name = parsedParticleSystem.name;
- var custom: Nullable<Effect> = null;
- var program: any = null;
- if (parsedParticleSystem.customShader) {
- program = parsedParticleSystem.customShader;
- var defines: string = (program.shaderOptions.defines.length > 0) ? program.shaderOptions.defines.join("\n") : "";
- custom = scene.getEngine().createEffectForParticles(program.shaderPath.fragmentElement, program.shaderOptions.uniforms, program.shaderOptions.samplers, defines);
- }
- var particleSystem = new ParticleSystem(name, parsedParticleSystem.capacity, scene, custom, parsedParticleSystem.isAnimationSheetEnabled);
- particleSystem.customShader = program;
- if (parsedParticleSystem.id) {
- particleSystem.id = parsedParticleSystem.id;
- }
- // Auto start
- if (parsedParticleSystem.preventAutoStart) {
- particleSystem.preventAutoStart = parsedParticleSystem.preventAutoStart;
- }
- ParticleSystem._Parse(parsedParticleSystem, particleSystem, scene, rootUrl);
- particleSystem.textureMask = Color4.FromArray(parsedParticleSystem.textureMask);
- if (!particleSystem.preventAutoStart) {
- particleSystem.start();
- }
- return particleSystem;
- }
- }
- }
|