thinParticleSystem.d.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import type { Immutable, Nullable } from "../types";
  2. import { FactorGradient, Color3Gradient } from "../Misc/gradients";
  3. import { Observable } from "../Misc/observable";
  4. import { Vector3, Matrix } from "../Maths/math.vector";
  5. import { VertexBuffer } from "../Buffers/buffer";
  6. import type { Effect } from "../Materials/effect";
  7. import type { IDisposable, Scene } from "../scene";
  8. import type { IParticleSystem } from "./IParticleSystem";
  9. import { BaseParticleSystem } from "./baseParticleSystem";
  10. import { Particle } from "./particle";
  11. import type { IAnimatable } from "../Animations/animatable.interface";
  12. import { DrawWrapper } from "../Materials/drawWrapper";
  13. import "../Shaders/particles.fragment";
  14. import "../Shaders/particles.vertex";
  15. import type { DataBuffer } from "../Buffers/dataBuffer";
  16. import { Color4, Color3 } from "../Maths/math.color";
  17. import type { AbstractEngine } from "../Engines/abstractEngine";
  18. import "../Engines/Extensions/engine.alpha";
  19. /**
  20. * This represents a thin particle system in Babylon.
  21. * 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.
  22. * Particles can take different shapes while emitted like box, sphere, cone or you can write your custom function.
  23. * This thin version contains a limited subset of the total features in order to provide users with a way to get particles but with a smaller footprint
  24. * @example https://doc.babylonjs.com/features/featuresDeepDive/particles/particle_system/particle_system_intro
  25. */
  26. export declare class ThinParticleSystem extends BaseParticleSystem implements IDisposable, IAnimatable, IParticleSystem {
  27. /**
  28. * This function can be defined to provide custom update for active particles.
  29. * This function will be called instead of regular update (age, position, color, etc.).
  30. * Do not forget that this function will be called on every frame so try to keep it simple and fast :)
  31. */
  32. updateFunction: (particles: Particle[]) => void;
  33. private _emitterWorldMatrix;
  34. private _emitterInverseWorldMatrix;
  35. /**
  36. * This function can be defined to specify initial direction for every new particle.
  37. * It by default use the emitterType defined function
  38. */
  39. startDirectionFunction: (worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle, isLocal: boolean) => void;
  40. /**
  41. * This function can be defined to specify initial position for every new particle.
  42. * It by default use the emitterType defined function
  43. */
  44. startPositionFunction: (worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle, isLocal: boolean) => void;
  45. /**
  46. * @internal
  47. */
  48. _inheritedVelocityOffset: Vector3;
  49. /**
  50. * An event triggered when the system is disposed
  51. */
  52. onDisposeObservable: Observable<IParticleSystem>;
  53. /**
  54. * An event triggered when the system is stopped
  55. */
  56. onStoppedObservable: Observable<IParticleSystem>;
  57. private _onDisposeObserver;
  58. /**
  59. * Sets a callback that will be triggered when the system is disposed
  60. */
  61. set onDispose(callback: () => void);
  62. private _particles;
  63. private _epsilon;
  64. private _capacity;
  65. private _stockParticles;
  66. private _newPartsExcess;
  67. private _vertexData;
  68. private _vertexBuffer;
  69. private _vertexBuffers;
  70. private _spriteBuffer;
  71. private _indexBuffer;
  72. private _linesIndexBuffer;
  73. private _linesIndexBufferUseInstancing;
  74. private _drawWrappers;
  75. /** @internal */
  76. _customWrappers: {
  77. [blendMode: number]: Nullable<DrawWrapper>;
  78. };
  79. private _scaledColorStep;
  80. private _colorDiff;
  81. private _scaledDirection;
  82. private _scaledGravity;
  83. private _currentRenderId;
  84. private _alive;
  85. private _useInstancing;
  86. private _vertexArrayObject;
  87. private _started;
  88. private _stopped;
  89. private _actualFrame;
  90. private _scaledUpdateSpeed;
  91. private _vertexBufferSize;
  92. /** @internal */
  93. _currentEmitRateGradient: Nullable<FactorGradient>;
  94. /** @internal */
  95. _currentEmitRate1: number;
  96. /** @internal */
  97. _currentEmitRate2: number;
  98. /** @internal */
  99. _currentStartSizeGradient: Nullable<FactorGradient>;
  100. /** @internal */
  101. _currentStartSize1: number;
  102. /** @internal */
  103. _currentStartSize2: number;
  104. /** Indicates that the update of particles is done in the animate function */
  105. readonly updateInAnimate = true;
  106. private readonly _rawTextureWidth;
  107. private _rampGradientsTexture;
  108. private _useRampGradients;
  109. /** Gets or sets a matrix to use to compute projection */
  110. defaultProjectionMatrix: Matrix;
  111. /** Gets or sets a matrix to use to compute view */
  112. defaultViewMatrix: Matrix;
  113. /** Gets or sets a boolean indicating that ramp gradients must be used
  114. * @see https://doc.babylonjs.com/features/featuresDeepDive/particles/particle_system/particle_system_intro#ramp-gradients
  115. */
  116. get useRampGradients(): boolean;
  117. set useRampGradients(value: boolean);
  118. /**
  119. * Specifies if the particles are updated in emitter local space or world space
  120. */
  121. isLocal: boolean;
  122. /** Indicates that the particle system is CPU based */
  123. readonly isGPU = false;
  124. /**
  125. * Gets the current list of active particles
  126. */
  127. get particles(): Particle[];
  128. /**
  129. * Gets the number of particles active at the same time.
  130. * @returns The number of active particles.
  131. */
  132. getActiveCount(): number;
  133. /**
  134. * Returns the string "ParticleSystem"
  135. * @returns a string containing the class name
  136. */
  137. getClassName(): string;
  138. /**
  139. * Gets a boolean indicating that the system is stopping
  140. * @returns true if the system is currently stopping
  141. */
  142. isStopping(): boolean;
  143. /**
  144. * Gets the custom effect used to render the particles
  145. * @param blendMode Blend mode for which the effect should be retrieved
  146. * @returns The effect
  147. */
  148. getCustomEffect(blendMode?: number): Nullable<Effect>;
  149. private _getCustomDrawWrapper;
  150. /**
  151. * Sets the custom effect used to render the particles
  152. * @param effect The effect to set
  153. * @param blendMode Blend mode for which the effect should be set
  154. */
  155. setCustomEffect(effect: Nullable<Effect>, blendMode?: number): void;
  156. /** @internal */
  157. private _onBeforeDrawParticlesObservable;
  158. /**
  159. * Observable that will be called just before the particles are drawn
  160. */
  161. get onBeforeDrawParticlesObservable(): Observable<Nullable<Effect>>;
  162. /**
  163. * Gets the name of the particle vertex shader
  164. */
  165. get vertexShaderName(): string;
  166. /**
  167. * Gets the vertex buffers used by the particle system
  168. */
  169. get vertexBuffers(): Immutable<{
  170. [key: string]: VertexBuffer;
  171. }>;
  172. /**
  173. * Gets the index buffer used by the particle system (or null if no index buffer is used (if _useInstancing=true))
  174. */
  175. get indexBuffer(): Nullable<DataBuffer>;
  176. /**
  177. * Instantiates a particle system.
  178. * 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.
  179. * @param name The name of the particle system
  180. * @param capacity The max number of particles alive at the same time
  181. * @param sceneOrEngine The scene the particle system belongs to or the engine to use if no scene
  182. * @param customEffect a custom effect used to change the way particles are rendered by default
  183. * @param isAnimationSheetEnabled Must be true if using a spritesheet to animate the particles texture
  184. * @param epsilon Offset used to render the particles
  185. */
  186. constructor(name: string, capacity: number, sceneOrEngine: Scene | AbstractEngine, customEffect?: Nullable<Effect>, isAnimationSheetEnabled?: boolean, epsilon?: number);
  187. /** @internal */
  188. _emitFromParticle: (particle: Particle) => void;
  189. serialize(serializeTexture: boolean): void;
  190. /**
  191. * Clones the particle system.
  192. * @param name The name of the cloned object
  193. * @param newEmitter The new emitter to use
  194. * @param cloneTexture Also clone the textures if true
  195. */
  196. clone(name: string, newEmitter: any, cloneTexture?: boolean): ThinParticleSystem;
  197. private _addFactorGradient;
  198. private _removeFactorGradient;
  199. /**
  200. * Adds a new life time gradient
  201. * @param gradient defines the gradient to use (between 0 and 1)
  202. * @param factor defines the life time factor to affect to the specified gradient
  203. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  204. * @returns the current particle system
  205. */
  206. addLifeTimeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  207. /**
  208. * Remove a specific life time gradient
  209. * @param gradient defines the gradient to remove
  210. * @returns the current particle system
  211. */
  212. removeLifeTimeGradient(gradient: number): IParticleSystem;
  213. /**
  214. * Adds a new size gradient
  215. * @param gradient defines the gradient to use (between 0 and 1)
  216. * @param factor defines the size factor to affect to the specified gradient
  217. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  218. * @returns the current particle system
  219. */
  220. addSizeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  221. /**
  222. * Remove a specific size gradient
  223. * @param gradient defines the gradient to remove
  224. * @returns the current particle system
  225. */
  226. removeSizeGradient(gradient: number): IParticleSystem;
  227. /**
  228. * Adds a new color remap gradient
  229. * @param gradient defines the gradient to use (between 0 and 1)
  230. * @param min defines the color remap minimal range
  231. * @param max defines the color remap maximal range
  232. * @returns the current particle system
  233. */
  234. addColorRemapGradient(gradient: number, min: number, max: number): IParticleSystem;
  235. /**
  236. * Remove a specific color remap gradient
  237. * @param gradient defines the gradient to remove
  238. * @returns the current particle system
  239. */
  240. removeColorRemapGradient(gradient: number): IParticleSystem;
  241. /**
  242. * Adds a new alpha remap gradient
  243. * @param gradient defines the gradient to use (between 0 and 1)
  244. * @param min defines the alpha remap minimal range
  245. * @param max defines the alpha remap maximal range
  246. * @returns the current particle system
  247. */
  248. addAlphaRemapGradient(gradient: number, min: number, max: number): IParticleSystem;
  249. /**
  250. * Remove a specific alpha remap gradient
  251. * @param gradient defines the gradient to remove
  252. * @returns the current particle system
  253. */
  254. removeAlphaRemapGradient(gradient: number): IParticleSystem;
  255. /**
  256. * Adds a new angular speed gradient
  257. * @param gradient defines the gradient to use (between 0 and 1)
  258. * @param factor defines the angular speed to affect to the specified gradient
  259. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  260. * @returns the current particle system
  261. */
  262. addAngularSpeedGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  263. /**
  264. * Remove a specific angular speed gradient
  265. * @param gradient defines the gradient to remove
  266. * @returns the current particle system
  267. */
  268. removeAngularSpeedGradient(gradient: number): IParticleSystem;
  269. /**
  270. * Adds a new velocity gradient
  271. * @param gradient defines the gradient to use (between 0 and 1)
  272. * @param factor defines the velocity to affect to the specified gradient
  273. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  274. * @returns the current particle system
  275. */
  276. addVelocityGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  277. /**
  278. * Remove a specific velocity gradient
  279. * @param gradient defines the gradient to remove
  280. * @returns the current particle system
  281. */
  282. removeVelocityGradient(gradient: number): IParticleSystem;
  283. /**
  284. * Adds a new limit velocity gradient
  285. * @param gradient defines the gradient to use (between 0 and 1)
  286. * @param factor defines the limit velocity value to affect to the specified gradient
  287. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  288. * @returns the current particle system
  289. */
  290. addLimitVelocityGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  291. /**
  292. * Remove a specific limit velocity gradient
  293. * @param gradient defines the gradient to remove
  294. * @returns the current particle system
  295. */
  296. removeLimitVelocityGradient(gradient: number): IParticleSystem;
  297. /**
  298. * Adds a new drag gradient
  299. * @param gradient defines the gradient to use (between 0 and 1)
  300. * @param factor defines the drag value to affect to the specified gradient
  301. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  302. * @returns the current particle system
  303. */
  304. addDragGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  305. /**
  306. * Remove a specific drag gradient
  307. * @param gradient defines the gradient to remove
  308. * @returns the current particle system
  309. */
  310. removeDragGradient(gradient: number): IParticleSystem;
  311. /**
  312. * Adds a new emit rate gradient (please note that this will only work if you set the targetStopDuration property)
  313. * @param gradient defines the gradient to use (between 0 and 1)
  314. * @param factor defines the emit rate value to affect to the specified gradient
  315. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  316. * @returns the current particle system
  317. */
  318. addEmitRateGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  319. /**
  320. * Remove a specific emit rate gradient
  321. * @param gradient defines the gradient to remove
  322. * @returns the current particle system
  323. */
  324. removeEmitRateGradient(gradient: number): IParticleSystem;
  325. /**
  326. * Adds a new start size gradient (please note that this will only work if you set the targetStopDuration property)
  327. * @param gradient defines the gradient to use (between 0 and 1)
  328. * @param factor defines the start size value to affect to the specified gradient
  329. * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
  330. * @returns the current particle system
  331. */
  332. addStartSizeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
  333. /**
  334. * Remove a specific start size gradient
  335. * @param gradient defines the gradient to remove
  336. * @returns the current particle system
  337. */
  338. removeStartSizeGradient(gradient: number): IParticleSystem;
  339. private _createRampGradientTexture;
  340. /**
  341. * Gets the current list of ramp gradients.
  342. * You must use addRampGradient and removeRampGradient to update this list
  343. * @returns the list of ramp gradients
  344. */
  345. getRampGradients(): Nullable<Array<Color3Gradient>>;
  346. /** Force the system to rebuild all gradients that need to be resync */
  347. forceRefreshGradients(): void;
  348. private _syncRampGradientTexture;
  349. /**
  350. * Adds a new ramp gradient used to remap particle colors
  351. * @param gradient defines the gradient to use (between 0 and 1)
  352. * @param color defines the color to affect to the specified gradient
  353. * @returns the current particle system
  354. */
  355. addRampGradient(gradient: number, color: Color3): ThinParticleSystem;
  356. /**
  357. * Remove a specific ramp gradient
  358. * @param gradient defines the gradient to remove
  359. * @returns the current particle system
  360. */
  361. removeRampGradient(gradient: number): ThinParticleSystem;
  362. /**
  363. * Adds a new color gradient
  364. * @param gradient defines the gradient to use (between 0 and 1)
  365. * @param color1 defines the color to affect to the specified gradient
  366. * @param color2 defines an additional color used to define a range ([color, color2]) with main color to pick the final color from
  367. * @returns this particle system
  368. */
  369. addColorGradient(gradient: number, color1: Color4, color2?: Color4): IParticleSystem;
  370. /**
  371. * Remove a specific color gradient
  372. * @param gradient defines the gradient to remove
  373. * @returns this particle system
  374. */
  375. removeColorGradient(gradient: number): IParticleSystem;
  376. /**
  377. * Resets the draw wrappers cache
  378. */
  379. resetDrawCache(): void;
  380. private _fetchR;
  381. protected _reset(): void;
  382. private _resetEffect;
  383. private _createVertexBuffers;
  384. private _createIndexBuffer;
  385. /**
  386. * Gets the maximum number of particles active at the same time.
  387. * @returns The max number of active particles.
  388. */
  389. getCapacity(): number;
  390. /**
  391. * Gets whether there are still active particles in the system.
  392. * @returns True if it is alive, otherwise false.
  393. */
  394. isAlive(): boolean;
  395. /**
  396. * Gets if the system has been started. (Note: this will still be true after stop is called)
  397. * @returns True if it has been started, otherwise false.
  398. */
  399. isStarted(): boolean;
  400. /** @internal */
  401. _preStart(): void;
  402. /**
  403. * Starts the particle system and begins to emit
  404. * @param delay defines the delay in milliseconds before starting the system (this.startDelay by default)
  405. */
  406. start(delay?: number): void;
  407. /**
  408. * Stops the particle system.
  409. * @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.
  410. */
  411. stop(stopSubEmitters?: boolean): void;
  412. /** @internal */
  413. _postStop(stopSubEmitters: boolean): void;
  414. /**
  415. * Remove all active particles
  416. */
  417. reset(): void;
  418. /**
  419. * @internal (for internal use only)
  420. */
  421. _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number): void;
  422. /**
  423. * "Recycles" one of the particle by copying it back to the "stock" of particles and removing it from the active list.
  424. * Its lifetime will start back at 0.
  425. * @param particle
  426. */
  427. recycleParticle: (particle: Particle) => void;
  428. private _createParticle;
  429. /** @internal */
  430. _prepareParticle(particle: Particle): void;
  431. private _update;
  432. /**
  433. * @internal
  434. */
  435. static _GetAttributeNamesOrOptions(isAnimationSheetEnabled?: boolean, isBillboardBased?: boolean, useRampGradients?: boolean): string[];
  436. /**
  437. * @internal
  438. */
  439. static _GetEffectCreationOptions(isAnimationSheetEnabled?: boolean, useLogarithmicDepth?: boolean, applyFog?: boolean): string[];
  440. /**
  441. * Fill the defines array according to the current settings of the particle system
  442. * @param defines Array to be updated
  443. * @param blendMode blend mode to take into account when updating the array
  444. */
  445. fillDefines(defines: Array<string>, blendMode: number): void;
  446. /**
  447. * Fill the uniforms, attributes and samplers arrays according to the current settings of the particle system
  448. * @param uniforms Uniforms array to fill
  449. * @param attributes Attributes array to fill
  450. * @param samplers Samplers array to fill
  451. */
  452. fillUniformsAttributesAndSamplerNames(uniforms: Array<string>, attributes: Array<string>, samplers: Array<string>): void;
  453. /**
  454. * @internal
  455. */
  456. private _getWrapper;
  457. /**
  458. * Animates the particle system for the current frame by emitting new particles and or animating the living ones.
  459. * @param preWarmOnly will prevent the system from updating the vertex buffer (default is false)
  460. */
  461. animate(preWarmOnly?: boolean): void;
  462. private _appendParticleVertices;
  463. /**
  464. * Rebuilds the particle system.
  465. */
  466. rebuild(): void;
  467. /**
  468. * Is this system ready to be used/rendered
  469. * @returns true if the system is ready
  470. */
  471. isReady(): boolean;
  472. private _render;
  473. /**
  474. * Renders the particle system in its current state.
  475. * @returns the current number of particles
  476. */
  477. render(): number;
  478. /** @internal */
  479. _onDispose(disposeAttachedSubEmitters?: boolean, disposeEndSubEmitters?: boolean): void;
  480. /**
  481. * Disposes the particle system and free the associated resources
  482. * @param disposeTexture defines if the particle texture must be disposed as well (true by default)
  483. * @param disposeAttachedSubEmitters defines if the attached sub-emitters must be disposed as well (false by default)
  484. * @param disposeEndSubEmitters defines if the end type sub-emitters must be disposed as well (false by default)
  485. */
  486. dispose(disposeTexture?: boolean, disposeAttachedSubEmitters?: boolean, disposeEndSubEmitters?: boolean): void;
  487. }