瀏覽代碼

some tweaks

Christopher Yovanovitch 7 年之前
父節點
當前提交
e414ae8870
共有 3 個文件被更改,包括 31 次插入68 次删除
  1. 1 3
      Tools/Gulp/config.json
  2. 1 1
      dist/preview release/what's new.md
  3. 29 64
      src/Helpers/babylon.particleHelper.ts

+ 1 - 3
Tools/Gulp/config.json

@@ -1176,9 +1176,7 @@
                 "../../src/Helpers/babylon.particleHelper.js"
             ],
             "dependUpon": [
-                "core",
-                "backgroundMaterial",
-                "additionalTextures"
+                "particles"
             ]
         },
         "videoDome": {

+ 1 - 1
dist/preview release/what's new.md

@@ -13,7 +13,7 @@
 - Added ability to not generate polynomials harmonics upon prefiltered texture creation ([sebavan](http://www.github.com/sebavan))
 - Added predicate function to customize the list of mesh included in the computation of bounding vectors in the ```getHierarchyBoundingVectors``` method ([sebavan](http://www.github.com/sebavan))
 - Added webVR constructor options: disable laser pointer toggle, teleportation floor meshes ([TrevorDev](https://github.com/TrevorDev))
-
+- Added a ParticleHelper class to create some pre-configured particle systems in a one-liner method style ([DevChris](https://github.com/yovanoc))
 
 ### glTF Loader
 

+ 29 - 64
src/Helpers/babylon.particleHelper.ts

@@ -3,7 +3,7 @@ module BABYLON {
      * Represents all the data needed to create a ParticleSystem.
      */
     export interface IParticleSystemData {
-        type: ParticleSystemType;
+        type: string;
         capacity: number;
         textureFile: string;
         minEmitBox: { x: number, y: number, z: number };
@@ -27,23 +27,6 @@ module BABYLON {
         updateSpeed: number;
     }
     /**
-     * ParticleSystemType
-     */
-    export enum ParticleSystemType {
-        /**
-         * None is to represents an error in parsing the type string in the create method.
-         */
-        None = "none",
-        /**
-         * Fire particle system.
-         */
-        Fire = "fire",
-        /**
-         * Smoke particle system.
-         */
-        Smoke = "smoke"
-    }
-    /**
      * This class is made for on one-liner static method to help creating particle systems.
      */
     export class ParticleHelper {
@@ -58,21 +41,16 @@ module BABYLON {
 
         /**
          * This is the main static method (one-liner) of this helper to create different particle systems.
-         * @param type This string will be parsed to a ParticleSystemType
+         * @param type This string represents the type to the particle system to create
          * @param emitter The object where the particle system will start to emit from.
          * @param scene The scene where the particle system should live.
          * @param gpu If the system will use gpu.
          * @returns the ParticleSystem created.
          */
-        public static Create(type: string, emitter: AbstractMesh,
+        public static CreateAsync(type: string, emitter: AbstractMesh,
                                    scene: Nullable<Scene> = Engine.LastCreatedScene, gpu: boolean = false): Promise<ParticleSystem> {
             
             return new Promise((resolve, reject) => {
-                const typeParsed = this._parseType(type);
-                if (typeParsed === ParticleSystemType.None) {
-                    throw new Error("This particle system type doesn't exist.");
-                }
-
                 if (scene) {
                     this._scene = scene;
                 } else {
@@ -81,75 +59,62 @@ module BABYLON {
 
                 this._emitter = emitter;
 
-                Tools.LoadFile(`${this._baseAssetsUrl}/systems/${typeParsed}.json`, (data, response) => {
+                Tools.LoadFile(`${this._baseAssetsUrl}/systems/${type}.json`, (data, response) => {
                     const newData = JSON.parse(data.toString()) as IParticleSystemData;
                     return resolve(this._createSystem(newData));
+                }, undefined, undefined, undefined, (req, exception) => {
+                    return reject(`An error occured while the creation of your particle system. Check if your type '${type}' exists.`);
                 });
 
             });
         }
 
-        private static _parseType(type: string): ParticleSystemType {
-            switch (type) {
-                case "fire":
-                case "Fire":
-                case "FIRE":
-                    return ParticleSystemType.Fire;
-                case "smoke":
-                case "Smoke":
-                case "SMOKE":
-                    return ParticleSystemType.Smoke;
-                default:
-                    return ParticleSystemType.None;
-            }
-        }
-
         private static _createSystem(data: IParticleSystemData): ParticleSystem {
             // Create a particle system
-            const fireSystem = new ParticleSystem(data.type, data.capacity, this._scene);
+            const system = new ParticleSystem(data.type, data.capacity, this._scene);
             // Texture of each particle
-            fireSystem.particleTexture = new Texture(`${this._baseAssetsUrl}/textures/${data.textureFile}`, this._scene);
+            system.particleTexture = new Texture(`${this._baseAssetsUrl}/textures/${data.textureFile}`, this._scene);
             // Where the particles come from
-            fireSystem.emitter = this._emitter; // the starting object, the emitter
-            fireSystem.minEmitBox = new Vector3(data.minEmitBox.x, data.minEmitBox.y, data.minEmitBox.z); // Starting all from
-            fireSystem.maxEmitBox = new Vector3(data.maxEmitBox.x, data.maxEmitBox.y, data.maxEmitBox.z); // To...
+            system.emitter = this._emitter; // the starting object, the emitter
+            system.minEmitBox = new Vector3(data.minEmitBox.x, data.minEmitBox.y, data.minEmitBox.z); // Starting all from
+            system.maxEmitBox = new Vector3(data.maxEmitBox.x, data.maxEmitBox.y, data.maxEmitBox.z); // To...
 
             // Colors of all particles
-            fireSystem.color1 = new Color4(data.color1.r, data.color1.g, data.color1.b, data.color1.a);
-            fireSystem.color2 = new Color4(data.color2.r, data.color2.g, data.color2.b, data.color2.a);
-            fireSystem.colorDead = new Color4(data.colorDead.r, data.colorDead.g, data.colorDead.b, data.colorDead.a);
+            system.color1 = new Color4(data.color1.r, data.color1.g, data.color1.b, data.color1.a);
+            system.color2 = new Color4(data.color2.r, data.color2.g, data.color2.b, data.color2.a);
+            system.colorDead = new Color4(data.colorDead.r, data.colorDead.g, data.colorDead.b, data.colorDead.a);
 
             // Size of each particle (random between...
-            fireSystem.minSize = data.minSize;
-            fireSystem.maxSize = data.maxSize;
+            system.minSize = data.minSize;
+            system.maxSize = data.maxSize;
 
             // Life time of each particle (random between...
-            fireSystem.minLifeTime = data.minLifeTime;
-            fireSystem.maxLifeTime = data.maxLifeTime;
+            system.minLifeTime = data.minLifeTime;
+            system.maxLifeTime = data.maxLifeTime;
 
             // Emission rate
-            fireSystem.emitRate = data.emitRate;
+            system.emitRate = data.emitRate;
 
             // Blend mode : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
-            fireSystem.blendMode = data.blendMode;
+            system.blendMode = data.blendMode;
 
             // Set the gravity of all particles
-            fireSystem.gravity = new Vector3(data.gravity.x, data.gravity.y, data.gravity.z);
+            system.gravity = new Vector3(data.gravity.x, data.gravity.y, data.gravity.z);
 
             // Direction of each particle after it has been emitted
-            fireSystem.direction1 = new Vector3(data.direction1.x, data.direction1.y, data.direction1.z);
-            fireSystem.direction2 = new Vector3(data.direction2.x, data.direction2.y, data.direction2.z);
+            system.direction1 = new Vector3(data.direction1.x, data.direction1.y, data.direction1.z);
+            system.direction2 = new Vector3(data.direction2.x, data.direction2.y, data.direction2.z);
 
             // Angular speed, in radians
-            fireSystem.minAngularSpeed = data.minAngularSpeed;
-            fireSystem.maxAngularSpeed = data.maxAngularSpeed;
+            system.minAngularSpeed = data.minAngularSpeed;
+            system.maxAngularSpeed = data.maxAngularSpeed;
 
             // Speed
-            fireSystem.minEmitPower = data.minEmitPower;
-            fireSystem.maxEmitPower = data.maxEmitPower;
-            fireSystem.updateSpeed = data.updateSpeed;
+            system.minEmitPower = data.minEmitPower;
+            system.maxEmitPower = data.maxEmitPower;
+            system.updateSpeed = data.updateSpeed;
 
-            return fireSystem;
+            return system;
         }
     }