babylon.baseParticleSystem.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. module BABYLON {
  2. /**
  3. * This represents the base class for particle system in Babylon.
  4. * 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.
  5. * Particles can take different shapes while emitted like box, sphere, cone or you can write your custom function.
  6. * @example https://doc.babylonjs.com/babylon101/particles
  7. */
  8. export class BaseParticleSystem {
  9. /**
  10. * Source color is added to the destination color without alpha affecting the result.
  11. */
  12. public static BLENDMODE_ONEONE = 0;
  13. /**
  14. * Blend current color and particle color using particle’s alpha.
  15. */
  16. public static BLENDMODE_STANDARD = 1;
  17. /**
  18. * Add current color and particle color multiplied by particle’s alpha.
  19. */
  20. public static BLENDMODE_ADD = 2;
  21. /**
  22. * Multiply current color with particle color
  23. */
  24. public static BLENDMODE_MULTIPLY = 3;
  25. /**
  26. * List of animations used by the particle system.
  27. */
  28. public animations: Animation[] = [];
  29. /**
  30. * The id of the Particle system.
  31. */
  32. public id: string;
  33. /**
  34. * The friendly name of the Particle system.
  35. */
  36. public name: string;
  37. /**
  38. * The rendering group used by the Particle system to chose when to render.
  39. */
  40. public renderingGroupId = 0;
  41. /**
  42. * The emitter represents the Mesh or position we are attaching the particle system to.
  43. */
  44. public emitter: Nullable<AbstractMesh | Vector3> = null;
  45. /**
  46. * The maximum number of particles to emit per frame
  47. */
  48. public emitRate = 10;
  49. /**
  50. * If you want to launch only a few particles at once, that can be done, as well.
  51. */
  52. public manualEmitCount = -1;
  53. /**
  54. * The overall motion speed (0.01 is default update speed, faster updates = faster animation)
  55. */
  56. public updateSpeed = 0.01;
  57. /**
  58. * The amount of time the particle system is running (depends of the overall update speed).
  59. */
  60. public targetStopDuration = 0;
  61. /**
  62. * Specifies whether the particle system will be disposed once it reaches the end of the animation.
  63. */
  64. public disposeOnStop = false;
  65. /**
  66. * Minimum power of emitting particles.
  67. */
  68. public minEmitPower = 1;
  69. /**
  70. * Maximum power of emitting particles.
  71. */
  72. public maxEmitPower = 1;
  73. /**
  74. * Minimum life time of emitting particles.
  75. */
  76. public minLifeTime = 1;
  77. /**
  78. * Maximum life time of emitting particles.
  79. */
  80. public maxLifeTime = 1;
  81. /**
  82. * Minimum Size of emitting particles.
  83. */
  84. public minSize = 1;
  85. /**
  86. * Maximum Size of emitting particles.
  87. */
  88. public maxSize = 1;
  89. /**
  90. * Minimum scale of emitting particles on X axis.
  91. */
  92. public minScaleX = 1;
  93. /**
  94. * Maximum scale of emitting particles on X axis.
  95. */
  96. public maxScaleX = 1;
  97. /**
  98. * Minimum scale of emitting particles on Y axis.
  99. */
  100. public minScaleY = 1;
  101. /**
  102. * Maximum scale of emitting particles on Y axis.
  103. */
  104. public maxScaleY = 1;
  105. /**
  106. * Gets or sets the minimal initial rotation in radians.
  107. */
  108. public minInitialRotation = 0;
  109. /**
  110. * Gets or sets the maximal initial rotation in radians.
  111. */
  112. public maxInitialRotation = 0;
  113. /**
  114. * Minimum angular speed of emitting particles (Z-axis rotation for each particle).
  115. */
  116. public minAngularSpeed = 0;
  117. /**
  118. * Maximum angular speed of emitting particles (Z-axis rotation for each particle).
  119. */
  120. public maxAngularSpeed = 0;
  121. /**
  122. * The texture used to render each particle. (this can be a spritesheet)
  123. */
  124. public particleTexture: Nullable<Texture>;
  125. /**
  126. * The layer mask we are rendering the particles through.
  127. */
  128. public layerMask: number = 0x0FFFFFFF;
  129. /**
  130. * This can help using your own shader to render the particle system.
  131. * The according effect will be created
  132. */
  133. public customShader: any = null;
  134. /**
  135. * By default particle system starts as soon as they are created. This prevents the
  136. * automatic start to happen and let you decide when to start emitting particles.
  137. */
  138. public preventAutoStart: boolean = false;
  139. /**
  140. * Gets or sets a texture used to add random noise to particle positions
  141. */
  142. public noiseTexture: Nullable<BaseTexture>;
  143. /** Gets or sets the strength to apply to the noise value (default is (10, 10, 10)) */
  144. public noiseStrength = new Vector3(10, 10, 10);
  145. /**
  146. * Callback triggered when the particle animation is ending.
  147. */
  148. public onAnimationEnd: Nullable<() => void> = null;
  149. /**
  150. * Blend mode use to render the particle, it can be either ParticleSystem.BLENDMODE_ONEONE or ParticleSystem.BLENDMODE_STANDARD.
  151. */
  152. public blendMode = ParticleSystem.BLENDMODE_ONEONE;
  153. /**
  154. * Forces the particle to write their depth information to the depth buffer. This can help preventing other draw calls
  155. * to override the particles.
  156. */
  157. public forceDepthWrite = false;
  158. /** Gets or sets a value indicating how many cycles (or frames) must be executed before first rendering (this value has to be set before starting the system). Default is 0 */
  159. public preWarmCycles = 0;
  160. /** Gets or sets a value indicating the time step multiplier to use in pre-warm mode (default is 1) */
  161. public preWarmStepOffset = 1;
  162. /**
  163. * If using a spritesheet (isAnimationSheetEnabled) defines the speed of the sprite loop (default is 1 meaning the animation will play once during the entire particle lifetime)
  164. */
  165. public spriteCellChangeSpeed = 1;
  166. /**
  167. * If using a spritesheet (isAnimationSheetEnabled) defines the first sprite cell to display
  168. */
  169. public startSpriteCellID = 0;
  170. /**
  171. * If using a spritesheet (isAnimationSheetEnabled) defines the last sprite cell to display
  172. */
  173. public endSpriteCellID = 0;
  174. /**
  175. * If using a spritesheet (isAnimationSheetEnabled), defines the sprite cell width to use
  176. */
  177. public spriteCellWidth = 0;
  178. /**
  179. * If using a spritesheet (isAnimationSheetEnabled), defines the sprite cell height to use
  180. */
  181. public spriteCellHeight = 0;
  182. /** Gets or sets a Vector2 used to move the pivot (by default (0,0)) */
  183. public translationPivot = new Vector2(0, 0);
  184. /** @hidden */
  185. protected _isAnimationSheetEnabled: boolean;
  186. /**
  187. * Gets or sets whether an animation sprite sheet is enabled or not on the particle system
  188. */
  189. public get isAnimationSheetEnabled(): boolean {
  190. return this._isAnimationSheetEnabled;
  191. }
  192. public set isAnimationSheetEnabled(value: boolean) {
  193. if (this._isAnimationSheetEnabled == value) {
  194. return;
  195. }
  196. this._isAnimationSheetEnabled = value;
  197. this._reset();
  198. }
  199. /**
  200. * Get hosting scene
  201. * @returns the scene
  202. */
  203. public getScene(): Scene {
  204. return this._scene;
  205. }
  206. /**
  207. * You can use gravity if you want to give an orientation to your particles.
  208. */
  209. public gravity = Vector3.Zero();
  210. protected _colorGradients: Nullable<Array<ColorGradient>> = null;
  211. protected _sizeGradients: Nullable<Array<FactorGradient>> = null;
  212. protected _lifeTimeGradients: Nullable<Array<FactorGradient>> = null;
  213. protected _angularSpeedGradients: Nullable<Array<FactorGradient>> = null;
  214. protected _velocityGradients: Nullable<Array<FactorGradient>> = null;
  215. protected _limitVelocityGradients: Nullable<Array<FactorGradient>> = null;
  216. protected _dragGradients: Nullable<Array<FactorGradient>> = null;
  217. protected _emitRateGradients: Nullable<Array<FactorGradient>> = null;
  218. protected _startSizeGradients: Nullable<Array<FactorGradient>> = null;
  219. /**
  220. * Gets the current list of drag gradients.
  221. * You must use addDragGradient and removeDragGradient to udpate this list
  222. * @returns the list of drag gradients
  223. */
  224. public getDragGradients(): Nullable<Array<FactorGradient>> {
  225. return this._dragGradients;
  226. }
  227. /** Gets or sets a value indicating the damping to apply if the limit velocity factor is reached */
  228. public limitVelocityDamping = 0.4;
  229. /**
  230. * Gets the current list of limit velocity gradients.
  231. * You must use addLimitVelocityGradient and removeLimitVelocityGradient to udpate this list
  232. * @returns the list of limit velocity gradients
  233. */
  234. public getLimitVelocityGradients(): Nullable<Array<FactorGradient>> {
  235. return this._limitVelocityGradients;
  236. }
  237. /**
  238. * Gets the current list of color gradients.
  239. * You must use addColorGradient and removeColorGradient to udpate this list
  240. * @returns the list of color gradients
  241. */
  242. public getColorGradients(): Nullable<Array<ColorGradient>> {
  243. return this._colorGradients;
  244. }
  245. /**
  246. * Gets the current list of size gradients.
  247. * You must use addSizeGradient and removeSizeGradient to udpate this list
  248. * @returns the list of size gradients
  249. */
  250. public getSizeGradients(): Nullable<Array<FactorGradient>> {
  251. return this._sizeGradients;
  252. }
  253. /**
  254. * Gets the current list of life time gradients.
  255. * You must use addLifeTimeGradient and removeLifeTimeGradient to udpate this list
  256. * @returns the list of life time gradients
  257. */
  258. public getLifeTimeGradients(): Nullable<Array<FactorGradient>> {
  259. return this._lifeTimeGradients;
  260. }
  261. /**
  262. * Gets the current list of angular speed gradients.
  263. * You must use addAngularSpeedGradient and removeAngularSpeedGradient to udpate this list
  264. * @returns the list of angular speed gradients
  265. */
  266. public getAngularSpeedGradients(): Nullable<Array<FactorGradient>> {
  267. return this._angularSpeedGradients;
  268. }
  269. /**
  270. * Gets the current list of velocity gradients.
  271. * You must use addVelocityGradient and removeVelocityGradient to udpate this list
  272. * @returns the list of velocity gradients
  273. */
  274. public getVelocityGradients(): Nullable<Array<FactorGradient>> {
  275. return this._velocityGradients;
  276. }
  277. /**
  278. * Gets the current list of start size gradients.
  279. * You must use addStartSizeGradient and removeStartSizeGradient to udpate this list
  280. * @returns the list of start size gradients
  281. */
  282. public getStartSizeGradients(): Nullable<Array<FactorGradient>> {
  283. return this._startSizeGradients;
  284. }
  285. /**
  286. * Gets the current list of emit rate gradients.
  287. * You must use addEmitRateGradient and removeEmitRateGradient to udpate this list
  288. * @returns the list of emit rate gradients
  289. */
  290. public getEmitRateGradients(): Nullable<Array<FactorGradient>> {
  291. return this._emitRateGradients;
  292. }
  293. /**
  294. * Random direction of each particle after it has been emitted, between direction1 and direction2 vectors.
  295. * This only works when particleEmitterTyps is a BoxParticleEmitter
  296. */
  297. public get direction1(): Vector3 {
  298. if ((<BoxParticleEmitter>this.particleEmitterType).direction1) {
  299. return (<BoxParticleEmitter>this.particleEmitterType).direction1;
  300. }
  301. return Vector3.Zero();
  302. }
  303. public set direction1(value: Vector3) {
  304. if ((<BoxParticleEmitter>this.particleEmitterType).direction1) {
  305. (<BoxParticleEmitter>this.particleEmitterType).direction1 = value;
  306. }
  307. }
  308. /**
  309. * Random direction of each particle after it has been emitted, between direction1 and direction2 vectors.
  310. * This only works when particleEmitterTyps is a BoxParticleEmitter
  311. */
  312. public get direction2(): Vector3 {
  313. if ((<BoxParticleEmitter>this.particleEmitterType).direction2) {
  314. return (<BoxParticleEmitter>this.particleEmitterType).direction2;
  315. }
  316. return Vector3.Zero();
  317. }
  318. public set direction2(value: Vector3) {
  319. if ((<BoxParticleEmitter>this.particleEmitterType).direction2) {
  320. (<BoxParticleEmitter>this.particleEmitterType).direction2 = value;
  321. }
  322. }
  323. /**
  324. * Minimum box point around our emitter. Our emitter is the center of particles source, but if you want your particles to emit from more than one point, then you can tell it to do so.
  325. * This only works when particleEmitterTyps is a BoxParticleEmitter
  326. */
  327. public get minEmitBox(): Vector3 {
  328. if ((<BoxParticleEmitter>this.particleEmitterType).minEmitBox) {
  329. return (<BoxParticleEmitter>this.particleEmitterType).minEmitBox;
  330. }
  331. return Vector3.Zero();
  332. }
  333. public set minEmitBox(value: Vector3) {
  334. if ((<BoxParticleEmitter>this.particleEmitterType).minEmitBox) {
  335. (<BoxParticleEmitter>this.particleEmitterType).minEmitBox = value;
  336. }
  337. }
  338. /**
  339. * Maximum box point around our emitter. Our emitter is the center of particles source, but if you want your particles to emit from more than one point, then you can tell it to do so.
  340. * This only works when particleEmitterTyps is a BoxParticleEmitter
  341. */
  342. public get maxEmitBox(): Vector3 {
  343. if ((<BoxParticleEmitter>this.particleEmitterType).maxEmitBox) {
  344. return (<BoxParticleEmitter>this.particleEmitterType).maxEmitBox;
  345. }
  346. return Vector3.Zero();
  347. }
  348. public set maxEmitBox(value: Vector3) {
  349. if ((<BoxParticleEmitter>this.particleEmitterType).maxEmitBox) {
  350. (<BoxParticleEmitter>this.particleEmitterType).maxEmitBox = value;
  351. }
  352. }
  353. /**
  354. * Random color of each particle after it has been emitted, between color1 and color2 vectors
  355. */
  356. public color1 = new Color4(1.0, 1.0, 1.0, 1.0);
  357. /**
  358. * Random color of each particle after it has been emitted, between color1 and color2 vectors
  359. */
  360. public color2 = new Color4(1.0, 1.0, 1.0, 1.0);
  361. /**
  362. * Color the particle will have at the end of its lifetime
  363. */
  364. public colorDead = new Color4(0, 0, 0, 1.0);
  365. /**
  366. * An optional mask to filter some colors out of the texture, or filter a part of the alpha channel
  367. */
  368. public textureMask = new Color4(1.0, 1.0, 1.0, 1.0);
  369. /**
  370. * The particle emitter type defines the emitter used by the particle system.
  371. * It can be for example box, sphere, or cone...
  372. */
  373. public particleEmitterType: IParticleEmitterType;
  374. /**
  375. * Gets or sets the billboard mode to use when isBillboardBased = true.
  376. * Only BABYLON.AbstractMesh.BILLBOARDMODE_ALL and AbstractMesh.BILLBOARDMODE_Y are supported so far
  377. */
  378. public billboardMode = AbstractMesh.BILLBOARDMODE_ALL;
  379. protected _isBillboardBased = true;
  380. /**
  381. * Gets or sets a boolean indicating if the particles must be rendered as billboard or aligned with the direction
  382. */
  383. public get isBillboardBased(): boolean {
  384. return this._isBillboardBased;
  385. }
  386. public set isBillboardBased(value: boolean) {
  387. if (this._isBillboardBased === value) {
  388. return;
  389. }
  390. this._isBillboardBased = value;
  391. this._reset();
  392. }
  393. /**
  394. * The scene the particle system belongs to.
  395. */
  396. protected _scene: Scene;
  397. /**
  398. * Local cache of defines for image processing.
  399. */
  400. protected _imageProcessingConfigurationDefines = new ImageProcessingConfigurationDefines();
  401. /**
  402. * Default configuration related to image processing available in the standard Material.
  403. */
  404. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  405. /**
  406. * Gets the image processing configuration used either in this material.
  407. */
  408. public get imageProcessingConfiguration(): ImageProcessingConfiguration {
  409. return this._imageProcessingConfiguration;
  410. }
  411. /**
  412. * Sets the Default image processing configuration used either in the this material.
  413. *
  414. * If sets to null, the scene one is in use.
  415. */
  416. public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
  417. this._attachImageProcessingConfiguration(value);
  418. }
  419. /**
  420. * Attaches a new image processing configuration to the Standard Material.
  421. * @param configuration
  422. */
  423. protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>): void {
  424. if (configuration === this._imageProcessingConfiguration) {
  425. return;
  426. }
  427. // Pick the scene configuration if needed.
  428. if (!configuration) {
  429. this._imageProcessingConfiguration = this._scene.imageProcessingConfiguration;
  430. }
  431. else {
  432. this._imageProcessingConfiguration = configuration;
  433. }
  434. }
  435. /** @hidden */
  436. protected _reset() {
  437. }
  438. /**
  439. * Instantiates a particle system.
  440. * 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.
  441. * @param name The name of the particle system
  442. */
  443. public constructor(name: string) {
  444. this.id = name;
  445. this.name = name;
  446. }
  447. /**
  448. * Creates a Point Emitter for the particle system (emits directly from the emitter position)
  449. * @param direction1 Particles are emitted between the direction1 and direction2 from within the box
  450. * @param direction2 Particles are emitted between the direction1 and direction2 from within the box
  451. * @returns the emitter
  452. */
  453. public createPointEmitter(direction1: Vector3, direction2: Vector3): PointParticleEmitter {
  454. var particleEmitter = new PointParticleEmitter();
  455. particleEmitter.direction1 = direction1;
  456. particleEmitter.direction2 = direction2;
  457. this.particleEmitterType = particleEmitter;
  458. return particleEmitter;
  459. }
  460. /**
  461. * Creates a Hemisphere Emitter for the particle system (emits along the hemisphere radius)
  462. * @param radius The radius of the hemisphere to emit from
  463. * @param radiusRange The range of the hemisphere to emit from [0-1] 0 Surface Only, 1 Entire Radius
  464. * @returns the emitter
  465. */
  466. public createHemisphericEmitter(radius = 1, radiusRange = 1): HemisphericParticleEmitter {
  467. var particleEmitter = new HemisphericParticleEmitter(radius, radiusRange);
  468. this.particleEmitterType = particleEmitter;
  469. return particleEmitter;
  470. }
  471. /**
  472. * Creates a Sphere Emitter for the particle system (emits along the sphere radius)
  473. * @param radius The radius of the sphere to emit from
  474. * @param radiusRange The range of the sphere to emit from [0-1] 0 Surface Only, 1 Entire Radius
  475. * @returns the emitter
  476. */
  477. public createSphereEmitter(radius = 1, radiusRange = 1): SphereParticleEmitter {
  478. var particleEmitter = new SphereParticleEmitter(radius, radiusRange);
  479. this.particleEmitterType = particleEmitter;
  480. return particleEmitter;
  481. }
  482. /**
  483. * Creates a Directed Sphere Emitter for the particle system (emits between direction1 and direction2)
  484. * @param radius The radius of the sphere to emit from
  485. * @param direction1 Particles are emitted between the direction1 and direction2 from within the sphere
  486. * @param direction2 Particles are emitted between the direction1 and direction2 from within the sphere
  487. * @returns the emitter
  488. */
  489. public createDirectedSphereEmitter(radius = 1, direction1 = new Vector3(0, 1.0, 0), direction2 = new Vector3(0, 1.0, 0)): SphereDirectedParticleEmitter {
  490. var particleEmitter = new SphereDirectedParticleEmitter(radius, direction1, direction2)
  491. this.particleEmitterType = particleEmitter;
  492. return particleEmitter;
  493. }
  494. /**
  495. * Creates a Cylinder Emitter for the particle system (emits from the cylinder to the particle position)
  496. * @param radius The radius of the emission cylinder
  497. * @param height The height of the emission cylinder
  498. * @param radiusRange The range of emission [0-1] 0 Surface only, 1 Entire Radius
  499. * @param directionRandomizer How much to randomize the particle direction [0-1]
  500. * @returns the emitter
  501. */
  502. public createCylinderEmitter(radius = 1, height = 1, radiusRange = 1, directionRandomizer = 0): CylinderParticleEmitter {
  503. var particleEmitter = new CylinderParticleEmitter(radius, height, radiusRange, directionRandomizer);
  504. this.particleEmitterType = particleEmitter;
  505. return particleEmitter;
  506. }
  507. /**
  508. * Creates a Cone Emitter for the particle system (emits from the cone to the particle position)
  509. * @param radius The radius of the cone to emit from
  510. * @param angle The base angle of the cone
  511. * @returns the emitter
  512. */
  513. public createConeEmitter(radius = 1, angle = Math.PI / 4): ConeParticleEmitter {
  514. var particleEmitter = new ConeParticleEmitter(radius, angle);
  515. this.particleEmitterType = particleEmitter;
  516. return particleEmitter;
  517. }
  518. /**
  519. * Creates a Box Emitter for the particle system. (emits between direction1 and direction2 from withing the box defined by minEmitBox and maxEmitBox)
  520. * @param direction1 Particles are emitted between the direction1 and direction2 from within the box
  521. * @param direction2 Particles are emitted between the direction1 and direction2 from within the box
  522. * @param minEmitBox Particles are emitted from the box between minEmitBox and maxEmitBox
  523. * @param maxEmitBox Particles are emitted from the box between minEmitBox and maxEmitBox
  524. * @returns the emitter
  525. */
  526. public createBoxEmitter(direction1: Vector3, direction2: Vector3, minEmitBox: Vector3, maxEmitBox: Vector3): BoxParticleEmitter {
  527. var particleEmitter = new BoxParticleEmitter();
  528. this.particleEmitterType = particleEmitter;
  529. this.direction1 = direction1;
  530. this.direction2 = direction2;
  531. this.minEmitBox = minEmitBox;
  532. this.maxEmitBox = maxEmitBox;
  533. return particleEmitter;
  534. }
  535. }
  536. }