gpuParticleSystem.d.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. import type { Immutable, Nullable } from "../types";
  2. import type { Color3Gradient, IValueGradient } from "../Misc/gradients";
  3. import { Observable } from "../Misc/observable";
  4. import { Vector3, Matrix } from "../Maths/math.vector";
  5. import { Color4 } from "../Maths/math.color";
  6. import { VertexBuffer } from "../Buffers/buffer";
  7. import type { IParticleSystem } from "./IParticleSystem";
  8. import { BaseParticleSystem } from "./baseParticleSystem";
  9. import { BoxParticleEmitter } from "../Particles/EmitterTypes/boxParticleEmitter";
  10. import type { IDisposable } from "../scene";
  11. import type { Effect } from "../Materials/effect";
  12. import { RawTexture } from "../Materials/Textures/rawTexture";
  13. import type { IAnimatable } from "../Animations/animatable.interface";
  14. import { AbstractEngine } from "../Engines/abstractEngine";
  15. import type { DataBuffer } from "../Buffers/dataBuffer";
  16. import { DrawWrapper } from "../Materials/drawWrapper";
  17. import { Scene } from "../scene";
  18. import "../Engines/Extensions/engine.transformFeedback";
  19. import "../Shaders/gpuRenderParticles.fragment";
  20. import "../Shaders/gpuRenderParticles.vertex";
  21. import type { PointParticleEmitter } from "./EmitterTypes/pointParticleEmitter";
  22. import type { HemisphericParticleEmitter } from "./EmitterTypes/hemisphericParticleEmitter";
  23. import type { SphereDirectedParticleEmitter, SphereParticleEmitter } from "./EmitterTypes/sphereParticleEmitter";
  24. import type { CylinderDirectedParticleEmitter, CylinderParticleEmitter } from "./EmitterTypes/cylinderParticleEmitter";
  25. import type { ConeParticleEmitter } from "./EmitterTypes/coneParticleEmitter";
  26. /**
  27. * This represents a GPU particle system in Babylon
  28. * This is the fastest particle system in Babylon as it uses the GPU to update the individual particle data
  29. * @see https://www.babylonjs-playground.com/#PU4WYI#4
  30. */
  31. export declare class GPUParticleSystem extends BaseParticleSystem implements IDisposable, IParticleSystem, IAnimatable {
  32. /**
  33. * The layer mask we are rendering the particles through.
  34. */
  35. layerMask: number;
  36. private _capacity;
  37. private _maxActiveParticleCount;
  38. private _currentActiveCount;
  39. private _accumulatedCount;
  40. private _updateBuffer;
  41. private _buffer0;
  42. private _buffer1;
  43. private _spriteBuffer;
  44. private _renderVertexBuffers;
  45. private _linesIndexBufferUseInstancing;
  46. private _targetIndex;
  47. private _sourceBuffer;
  48. private _targetBuffer;
  49. private _currentRenderId;
  50. private _currentRenderingCameraUniqueId;
  51. private _started;
  52. private _stopped;
  53. private _timeDelta;
  54. /** @internal */
  55. _randomTexture: RawTexture;
  56. /** @internal */
  57. _randomTexture2: RawTexture;
  58. /** Indicates that the update of particles is done in the animate function (and not in render). Default: false */
  59. updateInAnimate: boolean;
  60. private _attributesStrideSize;
  61. private _cachedUpdateDefines;
  62. private _randomTextureSize;
  63. private _actualFrame;
  64. private _drawWrappers;
  65. private _customWrappers;
  66. private readonly _rawTextureWidth;
  67. private _platform;
  68. private _rebuildingAfterContextLost;
  69. /**
  70. * Gets a boolean indicating if the GPU particles can be rendered on current browser
  71. */
  72. static get IsSupported(): boolean;
  73. /**
  74. * An event triggered when the system is disposed.
  75. */
  76. onDisposeObservable: Observable<IParticleSystem>;
  77. /**
  78. * An event triggered when the system is stopped
  79. */
  80. onStoppedObservable: Observable<IParticleSystem>;
  81. private _createIndexBuffer;
  82. /**
  83. * Gets the maximum number of particles active at the same time.
  84. * @returns The max number of active particles.
  85. */
  86. getCapacity(): number;
  87. /**
  88. * Forces the particle to write their depth information to the depth buffer. This can help preventing other draw calls
  89. * to override the particles.
  90. */
  91. forceDepthWrite: boolean;
  92. /**
  93. * Gets or set the number of active particles
  94. * The value cannot be greater than "capacity" (if it is, it will be limited to "capacity").
  95. */
  96. get maxActiveParticleCount(): number;
  97. set maxActiveParticleCount(value: number);
  98. /**
  99. * Gets or set the number of active particles
  100. * @deprecated Please use maxActiveParticleCount instead.
  101. */
  102. get activeParticleCount(): number;
  103. set activeParticleCount(value: number);
  104. private _preWarmDone;
  105. /**
  106. * Specifies if the particles are updated in emitter local space or world space.
  107. */
  108. isLocal: boolean;
  109. /** Indicates that the particle system is GPU based */
  110. readonly isGPU = true;
  111. /** Gets or sets a matrix to use to compute projection */
  112. defaultProjectionMatrix: Matrix;
  113. /**
  114. * Creates a Point Emitter for the particle system (emits directly from the emitter position)
  115. * @param direction1 Particles are emitted between the direction1 and direction2 from within the box
  116. * @param direction2 Particles are emitted between the direction1 and direction2 from within the box
  117. * @returns the emitter
  118. */
  119. createPointEmitter(direction1: Vector3, direction2: Vector3): PointParticleEmitter;
  120. /**
  121. * Creates a Hemisphere Emitter for the particle system (emits along the hemisphere radius)
  122. * @param radius The radius of the hemisphere to emit from
  123. * @param radiusRange The range of the hemisphere to emit from [0-1] 0 Surface Only, 1 Entire Radius
  124. * @returns the emitter
  125. */
  126. createHemisphericEmitter(radius?: number, radiusRange?: number): HemisphericParticleEmitter;
  127. /**
  128. * Creates a Sphere Emitter for the particle system (emits along the sphere radius)
  129. * @param radius The radius of the sphere to emit from
  130. * @param radiusRange The range of the sphere to emit from [0-1] 0 Surface Only, 1 Entire Radius
  131. * @returns the emitter
  132. */
  133. createSphereEmitter(radius?: number, radiusRange?: number): SphereParticleEmitter;
  134. /**
  135. * Creates a Directed Sphere Emitter for the particle system (emits between direction1 and direction2)
  136. * @param radius The radius of the sphere to emit from
  137. * @param direction1 Particles are emitted between the direction1 and direction2 from within the sphere
  138. * @param direction2 Particles are emitted between the direction1 and direction2 from within the sphere
  139. * @returns the emitter
  140. */
  141. createDirectedSphereEmitter(radius?: number, direction1?: Vector3, direction2?: Vector3): SphereDirectedParticleEmitter;
  142. /**
  143. * Creates a Cylinder Emitter for the particle system (emits from the cylinder to the particle position)
  144. * @param radius The radius of the emission cylinder
  145. * @param height The height of the emission cylinder
  146. * @param radiusRange The range of emission [0-1] 0 Surface only, 1 Entire Radius
  147. * @param directionRandomizer How much to randomize the particle direction [0-1]
  148. * @returns the emitter
  149. */
  150. createCylinderEmitter(radius?: number, height?: number, radiusRange?: number, directionRandomizer?: number): CylinderParticleEmitter;
  151. /**
  152. * Creates a Directed Cylinder Emitter for the particle system (emits between direction1 and direction2)
  153. * @param radius The radius of the cylinder to emit from
  154. * @param height The height of the emission cylinder
  155. * @param radiusRange the range of the emission cylinder [0-1] 0 Surface only, 1 Entire Radius (1 by default)
  156. * @param direction1 Particles are emitted between the direction1 and direction2 from within the cylinder
  157. * @param direction2 Particles are emitted between the direction1 and direction2 from within the cylinder
  158. * @returns the emitter
  159. */
  160. createDirectedCylinderEmitter(radius?: number, height?: number, radiusRange?: number, direction1?: Vector3, direction2?: Vector3): CylinderDirectedParticleEmitter;
  161. /**
  162. * Creates a Cone Emitter for the particle system (emits from the cone to the particle position)
  163. * @param radius The radius of the cone to emit from
  164. * @param angle The base angle of the cone
  165. * @returns the emitter
  166. */
  167. createConeEmitter(radius?: number, angle?: number): ConeParticleEmitter;
  168. /**
  169. * Creates a Box Emitter for the particle system. (emits between direction1 and direction2 from withing the box defined by minEmitBox and maxEmitBox)
  170. * @param direction1 Particles are emitted between the direction1 and direction2 from within the box
  171. * @param direction2 Particles are emitted between the direction1 and direction2 from within the box
  172. * @param minEmitBox Particles are emitted from the box between minEmitBox and maxEmitBox
  173. * @param maxEmitBox Particles are emitted from the box between minEmitBox and maxEmitBox
  174. * @returns the emitter
  175. */
  176. createBoxEmitter(direction1: Vector3, direction2: Vector3, minEmitBox: Vector3, maxEmitBox: Vector3): BoxParticleEmitter;
  177. /**
  178. * Is this system ready to be used/rendered
  179. * @returns true if the system is ready
  180. */
  181. isReady(): boolean;
  182. /**
  183. * Gets if the system has been started. (Note: this will still be true after stop is called)
  184. * @returns True if it has been started, otherwise false.
  185. */
  186. isStarted(): boolean;
  187. /**
  188. * Gets if the system has been stopped. (Note: rendering is still happening but the system is frozen)
  189. * @returns True if it has been stopped, otherwise false.
  190. */
  191. isStopped(): boolean;
  192. /**
  193. * Gets a boolean indicating that the system is stopping
  194. * @returns true if the system is currently stopping
  195. */
  196. isStopping(): boolean;
  197. /**
  198. * Gets the number of particles active at the same time.
  199. * @returns The number of active particles.
  200. */
  201. getActiveCount(): number;
  202. /**
  203. * Starts the particle system and begins to emit
  204. * @param delay defines the delay in milliseconds before starting the system (this.startDelay by default)
  205. */
  206. start(delay?: number): void;
  207. /**
  208. * Stops the particle system.
  209. */
  210. stop(): void;
  211. /**
  212. * Remove all active particles
  213. */
  214. reset(): void;
  215. /**
  216. * Returns the string "GPUParticleSystem"
  217. * @returns a string containing the class name
  218. */
  219. getClassName(): string;
  220. /**
  221. * Gets the custom effect used to render the particles
  222. * @param blendMode Blend mode for which the effect should be retrieved
  223. * @returns The effect
  224. */
  225. getCustomEffect(blendMode?: number): Nullable<Effect>;
  226. private _getCustomDrawWrapper;
  227. /**
  228. * Sets the custom effect used to render the particles
  229. * @param effect The effect to set
  230. * @param blendMode Blend mode for which the effect should be set
  231. */
  232. setCustomEffect(effect: Nullable<Effect>, blendMode?: number): void;
  233. /** @internal */
  234. protected _onBeforeDrawParticlesObservable: Nullable<Observable<Nullable<Effect>>>;
  235. /**
  236. * Observable that will be called just before the particles are drawn
  237. */
  238. get onBeforeDrawParticlesObservable(): Observable<Nullable<Effect>>;
  239. /**
  240. * Gets the name of the particle vertex shader
  241. */
  242. get vertexShaderName(): string;
  243. /**
  244. * Gets the vertex buffers used by the particle system
  245. * Should be called after render() has been called for the current frame so that the buffers returned are the ones that have been updated
  246. * in the current frame (there's a ping-pong between two sets of buffers - for a given frame, one set is used as the source and the other as the destination)
  247. */
  248. get vertexBuffers(): Immutable<{
  249. [key: string]: VertexBuffer;
  250. }>;
  251. /**
  252. * Gets the index buffer used by the particle system (null for GPU particle systems)
  253. */
  254. get indexBuffer(): Nullable<DataBuffer>;
  255. /** @internal */
  256. _colorGradientsTexture: RawTexture;
  257. protected _removeGradientAndTexture(gradient: number, gradients: Nullable<IValueGradient[]>, texture: RawTexture): BaseParticleSystem;
  258. /**
  259. * Adds a new color gradient
  260. * @param gradient defines the gradient to use (between 0 and 1)
  261. * @param color1 defines the color to affect to the specified gradient
  262. * @returns the current particle system
  263. */
  264. addColorGradient(gradient: number, color1: Color4): GPUParticleSystem;
  265. private _refreshColorGradient;
  266. /** Force the system to rebuild all gradients that need to be resync */
  267. forceRefreshGradients(): void;
  268. /**
  269. * Remove a specific color gradient
  270. * @param gradient defines the gradient to remove
  271. * @returns the current particle system
  272. */
  273. removeColorGradient(gradient: number): GPUParticleSystem;
  274. /**
  275. * Resets the draw wrappers cache
  276. */
  277. resetDrawCache(): void;
  278. /** @internal */
  279. _angularSpeedGradientsTexture: RawTexture;
  280. /** @internal */
  281. _sizeGradientsTexture: RawTexture;
  282. /** @internal */
  283. _velocityGradientsTexture: RawTexture;
  284. /** @internal */
  285. _limitVelocityGradientsTexture: RawTexture;
  286. /** @internal */
  287. _dragGradientsTexture: RawTexture;
  288. private _addFactorGradient;
  289. /**
  290. * Adds a new size gradient
  291. * @param gradient defines the gradient to use (between 0 and 1)
  292. * @param factor defines the size factor to affect to the specified gradient
  293. * @returns the current particle system
  294. */
  295. addSizeGradient(gradient: number, factor: number): GPUParticleSystem;
  296. /**
  297. * Remove a specific size gradient
  298. * @param gradient defines the gradient to remove
  299. * @returns the current particle system
  300. */
  301. removeSizeGradient(gradient: number): GPUParticleSystem;
  302. private _refreshFactorGradient;
  303. /**
  304. * Adds a new angular speed gradient
  305. * @param gradient defines the gradient to use (between 0 and 1)
  306. * @param factor defines the angular speed to affect to the specified gradient
  307. * @returns the current particle system
  308. */
  309. addAngularSpeedGradient(gradient: number, factor: number): GPUParticleSystem;
  310. /**
  311. * Remove a specific angular speed gradient
  312. * @param gradient defines the gradient to remove
  313. * @returns the current particle system
  314. */
  315. removeAngularSpeedGradient(gradient: number): GPUParticleSystem;
  316. /**
  317. * Adds a new velocity gradient
  318. * @param gradient defines the gradient to use (between 0 and 1)
  319. * @param factor defines the velocity to affect to the specified gradient
  320. * @returns the current particle system
  321. */
  322. addVelocityGradient(gradient: number, factor: number): GPUParticleSystem;
  323. /**
  324. * Remove a specific velocity gradient
  325. * @param gradient defines the gradient to remove
  326. * @returns the current particle system
  327. */
  328. removeVelocityGradient(gradient: number): GPUParticleSystem;
  329. /**
  330. * Adds a new limit velocity gradient
  331. * @param gradient defines the gradient to use (between 0 and 1)
  332. * @param factor defines the limit velocity value to affect to the specified gradient
  333. * @returns the current particle system
  334. */
  335. addLimitVelocityGradient(gradient: number, factor: number): GPUParticleSystem;
  336. /**
  337. * Remove a specific limit velocity gradient
  338. * @param gradient defines the gradient to remove
  339. * @returns the current particle system
  340. */
  341. removeLimitVelocityGradient(gradient: number): GPUParticleSystem;
  342. /**
  343. * Adds a new drag gradient
  344. * @param gradient defines the gradient to use (between 0 and 1)
  345. * @param factor defines the drag value to affect to the specified gradient
  346. * @returns the current particle system
  347. */
  348. addDragGradient(gradient: number, factor: number): GPUParticleSystem;
  349. /**
  350. * Remove a specific drag gradient
  351. * @param gradient defines the gradient to remove
  352. * @returns the current particle system
  353. */
  354. removeDragGradient(gradient: number): GPUParticleSystem;
  355. /**
  356. * Not supported by GPUParticleSystem
  357. * @returns the current particle system
  358. */
  359. addEmitRateGradient(): IParticleSystem;
  360. /**
  361. * Not supported by GPUParticleSystem
  362. * @returns the current particle system
  363. */
  364. removeEmitRateGradient(): IParticleSystem;
  365. /**
  366. * Not supported by GPUParticleSystem
  367. * @returns the current particle system
  368. */
  369. addStartSizeGradient(): IParticleSystem;
  370. /**
  371. * Not supported by GPUParticleSystem
  372. * @returns the current particle system
  373. */
  374. removeStartSizeGradient(): IParticleSystem;
  375. /**
  376. * Not supported by GPUParticleSystem
  377. * @returns the current particle system
  378. */
  379. addColorRemapGradient(): IParticleSystem;
  380. /**
  381. * Not supported by GPUParticleSystem
  382. * @returns the current particle system
  383. */
  384. removeColorRemapGradient(): IParticleSystem;
  385. /**
  386. * Not supported by GPUParticleSystem
  387. * @returns the current particle system
  388. */
  389. addAlphaRemapGradient(): IParticleSystem;
  390. /**
  391. * Not supported by GPUParticleSystem
  392. * @returns the current particle system
  393. */
  394. removeAlphaRemapGradient(): IParticleSystem;
  395. /**
  396. * Not supported by GPUParticleSystem
  397. * @returns the current particle system
  398. */
  399. addRampGradient(): IParticleSystem;
  400. /**
  401. * Not supported by GPUParticleSystem
  402. * @returns the current particle system
  403. */
  404. removeRampGradient(): IParticleSystem;
  405. /**
  406. * Not supported by GPUParticleSystem
  407. * @returns the list of ramp gradients
  408. */
  409. getRampGradients(): Nullable<Array<Color3Gradient>>;
  410. /**
  411. * Not supported by GPUParticleSystem
  412. * Gets or sets a boolean indicating that ramp gradients must be used
  413. * @see https://doc.babylonjs.com/features/featuresDeepDive/particles/particle_system/particle_system_intro#ramp-gradients
  414. */
  415. get useRampGradients(): boolean;
  416. set useRampGradients(value: boolean);
  417. /**
  418. * Not supported by GPUParticleSystem
  419. * @returns the current particle system
  420. */
  421. addLifeTimeGradient(): IParticleSystem;
  422. /**
  423. * Not supported by GPUParticleSystem
  424. * @returns the current particle system
  425. */
  426. removeLifeTimeGradient(): IParticleSystem;
  427. /**
  428. * Instantiates a GPU particle system.
  429. * 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.
  430. * @param name The name of the particle system
  431. * @param options The options used to create the system
  432. * @param sceneOrEngine The scene the particle system belongs to or the engine to use if no scene
  433. * @param customEffect a custom effect used to change the way particles are rendered by default
  434. * @param isAnimationSheetEnabled Must be true if using a spritesheet to animate the particles texture
  435. */
  436. constructor(name: string, options: Partial<{
  437. capacity: number;
  438. randomTextureSize: number;
  439. }>, sceneOrEngine: Scene | AbstractEngine, customEffect?: Nullable<Effect>, isAnimationSheetEnabled?: boolean);
  440. protected _reset(): void;
  441. private _createVertexBuffers;
  442. private _initialize;
  443. /** @internal */
  444. _recreateUpdateEffect(): boolean;
  445. /**
  446. * @internal
  447. */
  448. _getWrapper(blendMode: number): DrawWrapper;
  449. /**
  450. * @internal
  451. */
  452. static _GetAttributeNamesOrOptions(hasColorGradients?: boolean, isAnimationSheetEnabled?: boolean, isBillboardBased?: boolean, isBillboardStretched?: boolean): string[];
  453. /**
  454. * @internal
  455. */
  456. static _GetEffectCreationOptions(isAnimationSheetEnabled?: boolean, useLogarithmicDepth?: boolean, applyFog?: boolean): string[];
  457. /**
  458. * Fill the defines array according to the current settings of the particle system
  459. * @param defines Array to be updated
  460. * @param blendMode blend mode to take into account when updating the array
  461. */
  462. fillDefines(defines: Array<string>, blendMode?: number): void;
  463. /**
  464. * Fill the uniforms, attributes and samplers arrays according to the current settings of the particle system
  465. * @param uniforms Uniforms array to fill
  466. * @param attributes Attributes array to fill
  467. * @param samplers Samplers array to fill
  468. */
  469. fillUniformsAttributesAndSamplerNames(uniforms: Array<string>, attributes: Array<string>, samplers: Array<string>): void;
  470. /**
  471. * Animates the particle system for the current frame by emitting new particles and or animating the living ones.
  472. * @param preWarm defines if we are in the pre-warmimg phase
  473. */
  474. animate(preWarm?: boolean): void;
  475. private _createFactorGradientTexture;
  476. private _createSizeGradientTexture;
  477. private _createAngularSpeedGradientTexture;
  478. private _createVelocityGradientTexture;
  479. private _createLimitVelocityGradientTexture;
  480. private _createDragGradientTexture;
  481. private _createColorGradientTexture;
  482. private _render;
  483. /** @internal */
  484. _update(emitterWM?: Matrix): void;
  485. /**
  486. * Renders the particle system in its current state
  487. * @param preWarm defines if the system should only update the particles but not render them
  488. * @param forceUpdateOnly if true, force to only update the particles and never display them (meaning, even if preWarm=false, when forceUpdateOnly=true the particles won't be displayed)
  489. * @returns the current number of particles
  490. */
  491. render(preWarm?: boolean, forceUpdateOnly?: boolean): number;
  492. /**
  493. * Rebuilds the particle system
  494. */
  495. rebuild(): void;
  496. private _releaseBuffers;
  497. /**
  498. * Disposes the particle system and free the associated resources
  499. * @param disposeTexture defines if the particule texture must be disposed as well (true by default)
  500. */
  501. dispose(disposeTexture?: boolean): void;
  502. /**
  503. * Clones the particle system.
  504. * @param name The name of the cloned object
  505. * @param newEmitter The new emitter to use
  506. * @param cloneTexture Also clone the textures if true
  507. * @returns the cloned particle system
  508. */
  509. clone(name: string, newEmitter: any, cloneTexture?: boolean): GPUParticleSystem;
  510. /**
  511. * Serializes the particle system to a JSON object
  512. * @param serializeTexture defines if the texture must be serialized as well
  513. * @returns the JSON object
  514. */
  515. serialize(serializeTexture?: boolean): any;
  516. /**
  517. * Parses a JSON object to create a GPU particle system.
  518. * @param parsedParticleSystem The JSON object to parse
  519. * @param sceneOrEngine The scene or the engine to create the particle system in
  520. * @param rootUrl The root url to use to load external dependencies like texture
  521. * @param doNotStart Ignore the preventAutoStart attribute and does not start
  522. * @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
  523. * @returns the parsed GPU particle system
  524. */
  525. static Parse(parsedParticleSystem: any, sceneOrEngine: Scene | AbstractEngine, rootUrl: string, doNotStart?: boolean, capacity?: number): GPUParticleSystem;
  526. }