Bläddra i källkod

Adding active sub systems

Ibraheem Osama 7 år sedan
förälder
incheckning
0304dfcbe8

+ 61 - 37
src/Particles/babylon.particleSystem.ts

@@ -347,15 +347,10 @@
         public subEmitters: ParticleSystem[];
 
         // TODO need to lazy loaded
-        public createdSubSystems = new Array<SubParticleSystem>();
-        public stockSubSystems = new StringDictionary<Array<SubParticleSystem>>();
+        public activeSubSystems: StringDictionary<SubParticleSystem>;
+        public stockSubSystems: StringDictionary<Array<SubParticleSystem>>;
 
         private _isEmitting = false;
-        private _isUsed = false;
-        // to be overriden by subSystems
-        public stoppedEmitting(): void {
-
-        }
         //end of sub emitter
 
         /**
@@ -467,15 +462,6 @@
             ParticleSystem.recycleParticle(this, this, particle);
         }
 
-        public static recycleParticle(rootSystem: ParticleSystem, currentSystem: ParticleSystem, particle: Particle) {
-            var lastParticle = <Particle>currentSystem._particles.pop();
-
-            if (lastParticle !== particle) {
-                lastParticle.copyTo(particle);
-                rootSystem._stockParticles.push(lastParticle);
-            }
-        }
-
         /**
          * Gets the maximum number of particles active at the same time.
          * @returns The max number of active particles.
@@ -507,6 +493,10 @@
             this._started = true;
             this._stopped = false;
             this._actualFrame = 0;
+            if (this.subEmitters && this.subEmitters.length != 0) {
+                this.activeSubSystems = new StringDictionary<SubParticleSystem>();
+                this.stockSubSystems = new StringDictionary<Array<SubParticleSystem>>();
+            }
         }
 
         /**
@@ -517,12 +507,21 @@
         }
 
         public stopSubEmitters(): void {
-            if (this.createdSubSystems.length === 0)
+            if (this.activeSubSystems.count === 0)
                 return;
 
-            this.createdSubSystems.forEach(subSystem => {
+            this.stockSubSystems.forEach(generation => {
+                this.stockSubSystems.get(generation)!.forEach(subSystem => {
+                    subSystem.stop();
+                });
+            });
+
+            this.activeSubSystems.forEach(subSystemName => {
+                var subSystem = this.activeSubSystems.get(subSystemName)!;
                 subSystem.stop();
+                subSystem.stoppedEmitting(true);
             });
+            this.activeSubSystems = new StringDictionary<SubParticleSystem>();
         }
 
         public static emitFromGeneration(system: ParticleSystem, particle: Particle, generation: number): void {
@@ -533,21 +532,23 @@
             var generationString = generation.toString();
 
             if (!system.stockSubSystems.contains(generationString) || (system.stockSubSystems.get(generationString) as (Array<SubParticleSystem>)).length === 0) {
+
+                // get the current generation template and clone it to subSystem
                 var subSystem = system.subEmitters[generation].cloneToSubSystem(system.name, particle.position, generation, system);
-                system.createdSubSystems.push(subSystem);
+                system.activeSubSystems.add(subSystem.name, subSystem);
                 subSystem.start();
             }
             else {
-                var stockSubSystem = (system.stockSubSystems.get(generationString) as (Array<SubParticleSystem>)).pop() as SubParticleSystem;
+                var stockSubSystem = (system.stockSubSystems.get(generationString)!).pop()!;
                 stockSubSystem.emitter = particle.position;
+                system.activeSubSystems.add(stockSubSystem.name, stockSubSystem);
+                // reset the manual emit count
                 if (system.subEmitters[generation].manualEmitCount != -1)
                     stockSubSystem.manualEmitCount = system.subEmitters[generation].manualEmitCount;
+
+                stockSubSystem.start();
             }
         }
-        // sub emitter
-        public emitFromParticle(particle: Particle): void {
-            ParticleSystem.emitFromGeneration(this, particle, 0);
-        }
         // end of sub emitter
 
         // animation sheet
@@ -615,10 +616,30 @@
             return particle;
         }
 
-        public createParticle(): Particle {
+        // end of sub system methods
+        protected createParticle(): Particle {
             return ParticleSystem.createParticle(this, this);
         }
 
+        // to be overriden by subSystems
+        protected stoppedEmitting(): void {
+
+        }
+
+        protected emitFromParticle(particle: Particle): void {
+            ParticleSystem.emitFromGeneration(this, particle, 0);
+        }
+
+        public static recycleParticle(rootSystem: ParticleSystem, currentSystem: ParticleSystem, particle: Particle) {
+            var lastParticle = <Particle>currentSystem._particles.pop();
+
+            if (lastParticle !== particle) {
+                lastParticle.copyTo(particle);
+                rootSystem._stockParticles.push(lastParticle);
+            }
+        }
+        // end of sub system methods
+
         private _update(newParticles: number): void {
             // Update current
             this._alive = this._particles.length > 0;
@@ -629,7 +650,6 @@
 
             if (!this._alive && this._isEmitting) {
                 this._isEmitting = false;
-                this._isUsed = false;
                 this.stoppedEmitting();
             }
 
@@ -931,6 +951,20 @@
                     emitter.dispose();
                 });
             }
+
+            if (this.stockSubSystems) {
+                this.stockSubSystems.forEach(generation => {
+                    this.stockSubSystems.get(generation)!.forEach(subSystem => {
+                        subSystem.dispose();
+                    });
+                });
+            }
+
+            if (this.activeSubSystems) {
+                this.activeSubSystems.forEach(subSystemName => {
+                    this.activeSubSystems.get(subSystemName)!.dispose();
+                });
+            }
         }
 
         /**
@@ -988,16 +1022,6 @@
             return particleEmitter;
         }
 
-        public static randomNumber(min: number, max: number): number {
-            if (min === max) {
-                return (min);
-            }
-
-            var random = Math.random();
-
-            return ((random * (max - min)) + min);
-        }
-
         public cloneToSubSystem(name: string, newEmitter: Vector3, generation: number, root: ParticleSystem): SubParticleSystem {
             var custom: Nullable<Effect> = null;
             var program: any = null;
@@ -1009,7 +1033,7 @@
             var result = new SubParticleSystem(name, this._capacity, this._scene, generation, root, custom);
             result.customShader = program;
             Tools.DeepCopy(this, result, ["customShader"]);
-            result.name = name + "Child" + root.count++;
+            result.name = name + "_Child_" + root.count++ + "_" + generation;
             result.id = result.name;
             result.emitter = newEmitter;
             result.particleEmitterType = this.particleEmitterType;

+ 7 - 7
src/Particles/babylon.sphereParticleEmitter.ts

@@ -31,13 +31,13 @@ module BABYLON {
          */
         startDirectionFunction(emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void {
             var direction = particle.position.subtract(worldMatrix.getTranslation()).normalize();
-            // var randX = Scalar.RandomRange(0, this.directionRandomizer);
-            // var randY = Scalar.RandomRange(0, this.directionRandomizer);
-            // var randZ = Scalar.RandomRange(0, this.directionRandomizer);
-            // direction.x += randX;
-            // direction.y += randY;
-            // direction.z += randZ;
-            // direction.normalize();
+            var randX = Scalar.RandomRange(0, this.directionRandomizer);
+            var randY = Scalar.RandomRange(0, this.directionRandomizer);
+            var randZ = Scalar.RandomRange(0, this.directionRandomizer);
+            direction.x += randX;
+            direction.y += randY;
+            direction.z += randZ;
+            direction.normalize();
 
             Vector3.TransformNormalFromFloatsToRef(direction.x * emitPower, direction.y * emitPower, direction.z * emitPower, worldMatrix, directionToUpdate);
         }

+ 14 - 9
src/Particles/babylon.subParticleSystem.ts

@@ -1,31 +1,36 @@
 module BABYLON {
     export class SubParticleSystem extends ParticleSystem {
-        constructor(name: string, capacity: number, scene: Scene, private _generation: number, private _parentParticleSystem: ParticleSystem, customEffect: Nullable<Effect> = null, _isAnimationSheetEnabled: boolean = false, epsilon: number = 0.01) {
+        public generationString: string;
+        constructor(name: string, capacity: number, scene: Scene, private _generation: number, private _rootParticleSystem: ParticleSystem, customEffect: Nullable<Effect> = null, _isAnimationSheetEnabled: boolean = false, epsilon: number = 0.01) {
             super(name, capacity, scene, customEffect, _isAnimationSheetEnabled, epsilon);
+            this.generationString = this._generation.toString();
         }
 
-        public stoppedEmitting(): void {
-            var generationString = this._generation.toString();
-            if (this._parentParticleSystem.stockSubSystems.contains(generationString)) {
-                (this._parentParticleSystem.stockSubSystems.get(generationString) as (Array<SubParticleSystem>)).push(this);
+        public stoppedEmitting(overrideRemove = false): void {
+
+            if (overrideRemove)
+                this._rootParticleSystem.activeSubSystems.remove(this.name);
+                
+            if (this._rootParticleSystem.stockSubSystems.contains(this.generationString)) {
+                (this._rootParticleSystem.stockSubSystems.get(this.generationString)!).push(this);
             }
             else {
                 var subSysArray = new Array<SubParticleSystem>();
                 subSysArray.push(this);
-                this._parentParticleSystem.stockSubSystems.add(generationString, subSysArray);
+                this._rootParticleSystem.stockSubSystems.add(this.generationString, subSysArray);
             }
         }
 
         public emitFromParticle(particle: Particle): void {
-            ParticleSystem.emitFromGeneration(this._parentParticleSystem, particle, this._generation + 1);
+            ParticleSystem.emitFromGeneration(this._rootParticleSystem, particle, this._generation + 1);
         }
 
         public recycleParticle(particle: Particle): void {
-            ParticleSystem.recycleParticle(this._parentParticleSystem, this, particle);
+            ParticleSystem.recycleParticle(this._rootParticleSystem, this, particle);
         }
 
         public createParticle(): Particle {
-            return ParticleSystem.createParticle(this._parentParticleSystem, this);
+            return ParticleSystem.createParticle(this._rootParticleSystem, this);
         }
     }
 }