particleSystemComponent.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Mesh } from "Meshes/mesh";
  2. import { IParticleSystem } from "./IParticleSystem";
  3. import { GPUParticleSystem } from "./gpuParticleSystem";
  4. import { AbstractScene } from "abstractScene";
  5. import { Effect, EffectFallbacks } from "Materials/effect";
  6. import { Engine } from "Engines/engine";
  7. import { ParticleSystem } from "./particleSystem";
  8. import { Scene } from "scene";
  9. import { SceneComponentConstants } from "sceneComponent";
  10. import { AssetContainer } from "assetContainer";
  11. import "Shaders/particles.vertex";
  12. // Adds the parsers to the scene parsers.
  13. AbstractScene.AddParser(SceneComponentConstants.NAME_PARTICLESYSTEM, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
  14. let individualParser = AbstractScene.GetIndividualParser(SceneComponentConstants.NAME_PARTICLESYSTEM);
  15. if (!individualParser) {
  16. return;
  17. }
  18. // Particles Systems
  19. if (parsedData.particleSystems !== undefined && parsedData.particleSystems !== null) {
  20. for (var index = 0, cache = parsedData.particleSystems.length; index < cache; index++) {
  21. var parsedParticleSystem = parsedData.particleSystems[index];
  22. container.particleSystems.push(individualParser(parsedParticleSystem, scene, rootUrl));
  23. }
  24. }
  25. });
  26. AbstractScene.AddIndividualParser(SceneComponentConstants.NAME_PARTICLESYSTEM, (parsedParticleSystem: any, scene: Scene, rootUrl: string) => {
  27. if (parsedParticleSystem.activeParticleCount) {
  28. let ps = GPUParticleSystem.Parse(parsedParticleSystem, scene, rootUrl);
  29. return ps;
  30. } else {
  31. let ps = ParticleSystem.Parse(parsedParticleSystem, scene, rootUrl);
  32. return ps;
  33. }
  34. });
  35. declare module "Engines/Engine" {
  36. export interface Engine {
  37. /**
  38. * Create an effect to use with particle systems.
  39. * Please note that some parameters like animation sheets or not being billboard are not supported in this configuration
  40. * @param fragmentName defines the base name of the effect (The name of file without .fragment.fx)
  41. * @param uniformsNames defines a list of attribute names
  42. * @param samplers defines an array of string used to represent textures
  43. * @param defines defines the string containing the defines to use to compile the shaders
  44. * @param fallbacks defines the list of potential fallbacks to use if shader conmpilation fails
  45. * @param onCompiled defines a function to call when the effect creation is successful
  46. * @param onError defines a function to call when the effect creation has failed
  47. * @returns the new Effect
  48. */
  49. createEffectForParticles(fragmentName: string, uniformsNames: string[], samplers: string[], defines: string, fallbacks?: EffectFallbacks,
  50. onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void): Effect;
  51. }
  52. }
  53. Engine.prototype.createEffectForParticles = function(fragmentName: string, uniformsNames: string[] = [], samplers: string[] = [], defines = "", fallbacks?: EffectFallbacks,
  54. onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void): Effect {
  55. var attributesNamesOrOptions = ParticleSystem._GetAttributeNamesOrOptions();
  56. var effectCreationOption = ParticleSystem._GetEffectCreationOptions();
  57. if (defines.indexOf(" BILLBOARD") === -1) {
  58. defines += "\n#define BILLBOARD\n";
  59. }
  60. if (samplers.indexOf("diffuseSampler") === -1) {
  61. samplers.push("diffuseSampler");
  62. }
  63. return this.createEffect(
  64. {
  65. vertex: "particles",
  66. fragmentElement: fragmentName
  67. },
  68. attributesNamesOrOptions,
  69. effectCreationOption.concat(uniformsNames),
  70. samplers, defines, fallbacks, onCompiled, onError);
  71. };
  72. declare module "Meshes/mesh" {
  73. export interface Mesh {
  74. /**
  75. * Returns an array populated with IParticleSystem objects whose the mesh is the emitter
  76. * @returns an array of IParticleSystem
  77. */
  78. getEmittedParticleSystems(): IParticleSystem[];
  79. /**
  80. * Returns an array populated with IParticleSystem objects whose the mesh or its children are the emitter
  81. * @returns an array of IParticleSystem
  82. */
  83. getHierarchyEmittedParticleSystems(): IParticleSystem[];
  84. }
  85. }
  86. Mesh.prototype.getEmittedParticleSystems = function(): IParticleSystem[] {
  87. var results = new Array<IParticleSystem>();
  88. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  89. var particleSystem = this.getScene().particleSystems[index];
  90. if (particleSystem.emitter === this) {
  91. results.push(particleSystem);
  92. }
  93. }
  94. return results;
  95. };
  96. Mesh.prototype.getHierarchyEmittedParticleSystems = function(): IParticleSystem[] {
  97. var results = new Array<IParticleSystem>();
  98. var descendants = this.getDescendants();
  99. descendants.push(this);
  100. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  101. var particleSystem = this.getScene().particleSystems[index];
  102. let emitter: any = particleSystem.emitter;
  103. if (emitter.position && descendants.indexOf(emitter) !== -1) {
  104. results.push(particleSystem);
  105. }
  106. }
  107. return results;
  108. };