particleHelper.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { Tools } from "../Misc/tools.js";
  2. import { Color4 } from "../Maths/math.color.js";
  3. import { Texture } from "../Materials/Textures/texture.js";
  4. import { EngineStore } from "../Engines/engineStore.js";
  5. import { GPUParticleSystem } from "./gpuParticleSystem.js";
  6. import { ParticleSystemSet } from "./particleSystemSet.js";
  7. import { ParticleSystem } from "./particleSystem.js";
  8. import { WebRequest } from "../Misc/webRequest.js";
  9. /**
  10. * This class is made for on one-liner static method to help creating particle system set.
  11. */
  12. export class ParticleHelper {
  13. /**
  14. * Create a default particle system that you can tweak
  15. * @param emitter defines the emitter to use
  16. * @param capacity defines the system capacity (default is 500 particles)
  17. * @param scene defines the hosting scene
  18. * @param useGPU defines if a GPUParticleSystem must be created (default is false)
  19. * @returns the new Particle system
  20. */
  21. static CreateDefault(emitter, capacity = 500, scene, useGPU = false) {
  22. let system;
  23. if (useGPU) {
  24. system = new GPUParticleSystem("default system", { capacity: capacity }, scene);
  25. }
  26. else {
  27. system = new ParticleSystem("default system", capacity, scene);
  28. }
  29. system.emitter = emitter;
  30. system.particleTexture = new Texture("https://assets.babylonjs.com/textures/flare.png", system.getScene());
  31. system.createConeEmitter(0.1, Math.PI / 4);
  32. // Particle color
  33. system.color1 = new Color4(1.0, 1.0, 1.0, 1.0);
  34. system.color2 = new Color4(1.0, 1.0, 1.0, 1.0);
  35. system.colorDead = new Color4(1.0, 1.0, 1.0, 0.0);
  36. // Particle Size
  37. system.minSize = 0.1;
  38. system.maxSize = 0.1;
  39. // Emission speed
  40. system.minEmitPower = 2;
  41. system.maxEmitPower = 2;
  42. // Update speed
  43. system.updateSpeed = 1 / 60;
  44. system.emitRate = 30;
  45. return system;
  46. }
  47. /**
  48. * This is the main static method (one-liner) of this helper to create different particle systems
  49. * @param type This string represents the type to the particle system to create
  50. * @param scene The scene where the particle system should live
  51. * @param gpu If the system will use gpu
  52. * @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
  53. * @returns the ParticleSystemSet created
  54. */
  55. static CreateAsync(type, scene, gpu = false, capacity) {
  56. if (!scene) {
  57. scene = EngineStore.LastCreatedScene;
  58. }
  59. const token = {};
  60. scene.addPendingData(token);
  61. return new Promise((resolve, reject) => {
  62. if (gpu && !GPUParticleSystem.IsSupported) {
  63. scene.removePendingData(token);
  64. return reject("Particle system with GPU is not supported.");
  65. }
  66. Tools.LoadFile(`${ParticleHelper.BaseAssetsUrl}/systems/${type}.json`, (data) => {
  67. scene.removePendingData(token);
  68. const newData = JSON.parse(data.toString());
  69. return resolve(ParticleSystemSet.Parse(newData, scene, gpu, capacity));
  70. }, undefined, undefined, undefined, () => {
  71. scene.removePendingData(token);
  72. return reject(`An error occurred with the creation of your particle system. Check if your type '${type}' exists.`);
  73. });
  74. });
  75. }
  76. /**
  77. * Static function used to export a particle system to a ParticleSystemSet variable.
  78. * Please note that the emitter shape is not exported
  79. * @param systems defines the particle systems to export
  80. * @returns the created particle system set
  81. */
  82. static ExportSet(systems) {
  83. const set = new ParticleSystemSet();
  84. for (const system of systems) {
  85. set.systems.push(system);
  86. }
  87. return set;
  88. }
  89. /**
  90. * Creates a particle system from a snippet saved in a remote file
  91. * @param name defines the name of the particle system to create (can be null or empty to use the one from the json data)
  92. * @param url defines the url to load from
  93. * @param scene defines the hosting scene
  94. * @param gpu If the system will use gpu
  95. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  96. * @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
  97. * @returns a promise that will resolve to the new particle system
  98. */
  99. static ParseFromFileAsync(name, url, scene, gpu = false, rootUrl = "", capacity) {
  100. return new Promise((resolve, reject) => {
  101. const request = new WebRequest();
  102. request.addEventListener("readystatechange", () => {
  103. if (request.readyState == 4) {
  104. if (request.status == 200) {
  105. const serializationObject = JSON.parse(request.responseText);
  106. let output;
  107. if (gpu) {
  108. output = GPUParticleSystem.Parse(serializationObject, scene, rootUrl, false, capacity);
  109. }
  110. else {
  111. output = ParticleSystem.Parse(serializationObject, scene, rootUrl, false, capacity);
  112. }
  113. if (name) {
  114. output.name = name;
  115. }
  116. resolve(output);
  117. }
  118. else {
  119. reject("Unable to load the particle system");
  120. }
  121. }
  122. });
  123. request.open("GET", url);
  124. request.send();
  125. });
  126. }
  127. /**
  128. * Creates a particle system from a snippet saved by the particle system editor
  129. * @param snippetId defines the snippet to load (can be set to _BLANK to create a default one)
  130. * @param scene defines the hosting scene
  131. * @param gpu If the system will use gpu
  132. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  133. * @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
  134. * @returns a promise that will resolve to the new particle system
  135. */
  136. static ParseFromSnippetAsync(snippetId, scene, gpu = false, rootUrl = "", capacity) {
  137. if (snippetId === "_BLANK") {
  138. const system = this.CreateDefault(null);
  139. system.start();
  140. return Promise.resolve(system);
  141. }
  142. return new Promise((resolve, reject) => {
  143. const request = new WebRequest();
  144. request.addEventListener("readystatechange", () => {
  145. if (request.readyState == 4) {
  146. if (request.status == 200) {
  147. const snippet = JSON.parse(JSON.parse(request.responseText).jsonPayload);
  148. const serializationObject = JSON.parse(snippet.particleSystem);
  149. let output;
  150. if (gpu) {
  151. output = GPUParticleSystem.Parse(serializationObject, scene, rootUrl, false, capacity);
  152. }
  153. else {
  154. output = ParticleSystem.Parse(serializationObject, scene, rootUrl, false, capacity);
  155. }
  156. output.snippetId = snippetId;
  157. resolve(output);
  158. }
  159. else {
  160. reject("Unable to load the snippet " + snippetId);
  161. }
  162. }
  163. });
  164. request.open("GET", this.SnippetUrl + "/" + snippetId.replace(/#/g, "/"));
  165. request.send();
  166. });
  167. }
  168. }
  169. /**
  170. * Gets or sets base Assets URL
  171. */
  172. ParticleHelper.BaseAssetsUrl = ParticleSystemSet.BaseAssetsUrl;
  173. /** Define the Url to load snippets */
  174. ParticleHelper.SnippetUrl = `https://snippet.babylonjs.com`;
  175. /**
  176. * Creates a particle system from a snippet saved by the particle system editor
  177. * @deprecated Please use ParseFromSnippetAsync instead
  178. * @param snippetId defines the snippet to load (can be set to _BLANK to create a default one)
  179. * @param scene defines the hosting scene
  180. * @param gpu If the system will use gpu
  181. * @param rootUrl defines the root URL to use to load textures and relative dependencies
  182. * @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
  183. * @returns a promise that will resolve to the new particle system
  184. */
  185. ParticleHelper.CreateFromSnippetAsync = ParticleHelper.ParseFromSnippetAsync;
  186. //# sourceMappingURL=particleHelper.js.map