瀏覽代碼

Added 2 values to sizeGradients

David Catuhe 7 年之前
父節點
當前提交
4f9ae38bf9

+ 2 - 2
assets/particles/systems/sun.json

@@ -211,11 +211,11 @@
       "sizeGradients": [
       "sizeGradients": [
         {
         {
           "gradient": 0,
           "gradient": 0,
-          "factor": 0
+          "factor1": 0
         },
         },
         {
         {
           "gradient": 1,
           "gradient": 1,
-          "factor": 1
+          "factor1": 1
         }
         }
       ],
       ],
       "textureMask": [
       "textureMask": [

+ 2 - 1
src/Particles/babylon.IParticleSystem.ts

@@ -257,8 +257,9 @@ module BABYLON {
          * Adds a new size gradient
          * Adds a new size gradient
          * @param gradient defines the gradient to use (between 0 and 1)
          * @param gradient defines the gradient to use (between 0 and 1)
          * @param factor defines the size factor to affect to the specified gradient
          * @param factor defines the size factor to affect to the specified gradient
+         * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
          */
          */
-        addSizeGradient(gradient: number, factor: number): IParticleSystem;
+        addSizeGradient(gradient: number, factor: number, factor2?: number): IParticleSystem;
         /**
         /**
          * Remove a specific size gradient
          * Remove a specific size gradient
          * @param gradient defines the gradient to remove
          * @param gradient defines the gradient to remove

+ 12 - 0
src/Particles/babylon.particle.ts

@@ -73,6 +73,13 @@
         /** @hidden */
         /** @hidden */
         public _currentColor2 = new Color4(0, 0, 0, 0);
         public _currentColor2 = new Color4(0, 0, 0, 0);
 
 
+        /** @hidden */
+        public _currentSizeGradient: Nullable<FactorGradient>;
+        /** @hidden */
+        public _currentSize1 = 0;
+        /** @hidden */
+        public _currentSize2 = 0;        
+
         /**
         /**
          * Creates a new instance Particle
          * Creates a new instance Particle
          * @param particleSystem the particle system the particle belongs to
          * @param particleSystem the particle system the particle belongs to
@@ -134,6 +141,11 @@
                 other._currentColor1.copyFrom(this._currentColor1);
                 other._currentColor1.copyFrom(this._currentColor1);
                 other._currentColor2.copyFrom(this._currentColor2);
                 other._currentColor2.copyFrom(this._currentColor2);
             }
             }
+            if (this._currentSizeGradient) {
+                other._currentSizeGradient = this._currentSizeGradient;
+                other._currentSize1 = this._currentSize1;
+                other._currentSize2 = this._currentSize2;
+            }
         }
         }
     }
     }
 } 
 } 

+ 24 - 9
src/Particles/babylon.particleSystem.ts

@@ -186,6 +186,7 @@
 
 
         private _colorGradients: Nullable<Array<ColorGradient>> = null;
         private _colorGradients: Nullable<Array<ColorGradient>> = null;
         private _sizeGradients: Nullable<Array<FactorGradient>> = null;
         private _sizeGradients: Nullable<Array<FactorGradient>> = null;
+        private _lifeTimeGradients: Nullable<Array<FactorGradient>> = null;
 
 
         /**
         /**
          * Gets the current list of color gradients.
          * Gets the current list of color gradients.
@@ -513,9 +514,14 @@
                         particle.direction.addInPlace(this._scaledGravity);
                         particle.direction.addInPlace(this._scaledGravity);
 
 
                         // Gradient
                         // Gradient
-                        if (this._sizeGradients && this._sizeGradients.length > 0) {
+                        if (this._sizeGradients && this._sizeGradients.length > 0) {                  
                             Tools.GetCurrentGradient(ratio, this._sizeGradients, (currentGradient, nextGradient, scale) => {
                             Tools.GetCurrentGradient(ratio, this._sizeGradients, (currentGradient, nextGradient, scale) => {
-                                particle.size = particle._initialSize * Scalar.Lerp((<FactorGradient>currentGradient).factor, (<FactorGradient>nextGradient).factor, scale);
+                                if (currentGradient !== particle._currentSizeGradient) {
+                                    particle._currentSize1 = particle._currentSize2;
+                                    particle._currentSize2 = (<FactorGradient>nextGradient).getFactor();    
+                                    particle._currentSizeGradient = (<FactorGradient>currentGradient);
+                                }                                
+                                particle.size = particle._initialSize * Scalar.Lerp(particle._currentSize1, particle._currentSize2, scale);
                             });
                             });
                         }
                         }
 
 
@@ -530,16 +536,18 @@
         /**
         /**
          * Adds a new size gradient
          * Adds a new size gradient
          * @param gradient defines the gradient to use (between 0 and 1)
          * @param gradient defines the gradient to use (between 0 and 1)
-         * @param factor defines the size factor to affect to the specified gradient
+         * @param factor defines the size factor to affect to the specified gradient         
+         * @param factor2 defines an additional factor used to define a range ([factor, factor2]) with main value to pick the final value from
          */
          */
-        public addSizeGradient(gradient: number, factor: number): ParticleSystem {
+        public addSizeGradient(gradient: number, factor: number, factor2?: number): ParticleSystem {
             if (!this._sizeGradients) {
             if (!this._sizeGradients) {
                 this._sizeGradients = [];
                 this._sizeGradients = [];
             }
             }
 
 
             let sizeGradient = new FactorGradient();
             let sizeGradient = new FactorGradient();
             sizeGradient.gradient = gradient;
             sizeGradient.gradient = gradient;
-            sizeGradient.factor = factor;
+            sizeGradient.factor1 = factor;
+            sizeGradient.factor2 = factor2;
             this._sizeGradients.push(sizeGradient);
             this._sizeGradients.push(sizeGradient);
 
 
             this._sizeGradients.sort((a, b) => {
             this._sizeGradients.sort((a, b) => {
@@ -1458,10 +1466,17 @@
             if (sizeGradients) {
             if (sizeGradients) {
                 serializationObject.sizeGradients = [];
                 serializationObject.sizeGradients = [];
                 for (var sizeGradient of sizeGradients) {
                 for (var sizeGradient of sizeGradients) {
-                    serializationObject.sizeGradients.push({
+
+                    var serializedGradient: any = {
                         gradient: sizeGradient.gradient,
                         gradient: sizeGradient.gradient,
-                        factor: sizeGradient.factor
-                    })
+                        factor1: sizeGradient.factor1
+                    };
+
+                    if (sizeGradient.factor2 !== undefined) {
+                        serializedGradient.factor2 = sizeGradient.factor2;
+                    }
+
+                    serializationObject.sizeGradients.push(serializedGradient);
                 }
                 }
             }            
             }            
 
 
@@ -1551,7 +1566,7 @@
 
 
             if (parsedParticleSystem.sizeGradients) {
             if (parsedParticleSystem.sizeGradients) {
                 for (var sizeGradient of parsedParticleSystem.sizeGradients) {
                 for (var sizeGradient of parsedParticleSystem.sizeGradients) {
-                    particleSystem.addSizeGradient(sizeGradient.gradient, sizeGradient.factor);
+                    particleSystem.addSizeGradient(sizeGradient.gradient, sizeGradient.factor1 !== undefined ?  sizeGradient.factor1 : sizeGradient.factor, sizeGradient.factor2);
                 }
                 }
             }       
             }       
             
             

+ 19 - 2
src/Tools/babylon.tools.ts

@@ -49,9 +49,26 @@
          */
          */
         public gradient: number;
         public gradient: number;
         /**
         /**
-         * Gets or sets associated factor
+         * Gets or sets first associated factor
          */        
          */        
-        public factor: number;
+        public factor1: number;
+        /**
+         * Gets or sets second associated factor
+         */        
+        public factor2?: number;    
+        
+        /** 
+         * Will get a number picked randomly between factor1 and factor2.
+         * If factor2 is undefined then factor1 will be used
+         * @returns the picked number
+         */
+        public getFactor(): number {
+            if (this.factor2 === undefined) {
+                return this.factor1;
+            }
+
+            return Scalar.Lerp(this.factor1, this.factor2, Math.random());
+        }        
     }  
     }  
 
 
     // See https://stackoverflow.com/questions/12915412/how-do-i-extend-a-host-object-e-g-error-in-typescript
     // See https://stackoverflow.com/questions/12915412/how-do-i-extend-a-host-object-e-g-error-in-typescript