babylon.particleSystemComponent.ts 5.0 KB

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