particleSystemComponent.ts 5.5 KB

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