Browse Source

New ParticleSystem.updateFunction
Tools.GetFfps and Tools.GetDeltaTime were moved to engine.getFps and engine.getDeltaTime

David Catuhe 10 years ago
parent
commit
fe16904584

+ 2 - 1
Babylon/Cameras/babylon.targetCamera.js

@@ -73,7 +73,8 @@ var BABYLON;
 
         // Methods
         TargetCamera.prototype._computeLocalCameraSpeed = function () {
-            return this.speed * ((BABYLON.Tools.GetDeltaTime() / (BABYLON.Tools.GetFps() * 10.0)));
+            var engine = this.getEngine();
+            return this.speed * ((engine.getDeltaTime() / (engine.getFps() * 10.0)));
         };
 
         // Target

+ 3 - 2
Babylon/Cameras/babylon.targetCamera.ts

@@ -76,8 +76,9 @@
         }
 
         // Methods
-        public _computeLocalCameraSpeed():number {
-            return this.speed * ((BABYLON.Tools.GetDeltaTime() / (BABYLON.Tools.GetFps() * 10.0)));
+        public _computeLocalCameraSpeed(): number {
+            var engine = this.getEngine();
+            return this.speed * ((engine.getDeltaTime() / (engine.getFps() * 10.0)));
         }
 
         // Target

File diff suppressed because it is too large
+ 1 - 1
Babylon/Debug/babylon.debugLayer.js


File diff suppressed because it is too large
+ 1 - 1
Babylon/Debug/babylon.debugLayer.js.map


+ 1 - 1
Babylon/Debug/babylon.debugLayer.ts

@@ -637,7 +637,7 @@
             var scene = this._scene;
             var engine = scene.getEngine();
 
-            this._statsSubsetDiv.innerHTML = "Babylon.js v" + Engine.Version + " - <b>" + Tools.Format(Tools.GetFps(), 0) + " fps</b><br><br>"
+            this._statsSubsetDiv.innerHTML = "Babylon.js v" + Engine.Version + " - <b>" + Tools.Format(engine.getFps(), 0) + " fps</b><br><br>"
             + "Total meshes: " + scene.meshes.length + "<br>"
             + "Total vertices: " + scene.getTotalVertices() + "<br>"
             + "Active meshes: " + scene.getActiveMeshes().length + "<br>"

+ 31 - 29
Babylon/Particles/babylon.particleSystem.js

@@ -97,6 +97,34 @@
 
                 BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
             };
+
+            this.updateFunction = function (particles) {
+                for (var index = 0; index < particles.length; index++) {
+                    var particle = particles[index];
+                    particle.age += _this._scaledUpdateSpeed;
+
+                    if (particle.age >= particle.lifeTime) {
+                        particles.splice(index, 1);
+                        _this._stockParticles.push(particle);
+                        index--;
+                        continue;
+                    } else {
+                        particle.colorStep.scaleToRef(_this._scaledUpdateSpeed, _this._scaledColorStep);
+                        particle.color.addInPlace(_this._scaledColorStep);
+
+                        if (particle.color.a < 0)
+                            particle.color.a = 0;
+
+                        particle.angle += particle.angularSpeed * _this._scaledUpdateSpeed;
+
+                        particle.direction.scaleToRef(_this._scaledUpdateSpeed, _this._scaledDirection);
+                        particle.position.addInPlace(_this._scaledDirection);
+
+                        _this.gravity.scaleToRef(_this._scaledUpdateSpeed, _this._scaledGravity);
+                        particle.direction.addInPlace(_this._scaledGravity);
+                    }
+                }
+            };
         }
         ParticleSystem.prototype.getCapacity = function () {
             return this._capacity;
@@ -138,34 +166,8 @@
         ParticleSystem.prototype._update = function (newParticles) {
             // Update current
             this._alive = this.particles.length > 0;
-            for (var index = 0; index < this.particles.length; index++) {
-                var particle = this.particles[index];
-                particle.age += this._scaledUpdateSpeed;
-
-                if (particle.age >= particle.lifeTime) {
-                    this._stockParticles.push(this.particles.splice(index, 1)[0]);
-                    index--;
-                    continue;
-                } else {
-                    particle.colorStep.scaleToRef(this._scaledUpdateSpeed, this._scaledColorStep);
-                    particle.color.addInPlace(this._scaledColorStep);
-
-                    if (particle.color.a < 0)
-                        particle.color.a = 0;
-
-                    particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
 
-                    particle.direction.scaleToRef(this._scaledUpdateSpeed, this._scaledDirection);
-                    particle.position.addInPlace(this._scaledDirection);
-
-                    this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
-                    particle.direction.addInPlace(this._scaledGravity);
-                }
-            }
-
-            if (this.customUpdateFunction) {
-                this.customUpdateFunction(this.particles);
-            }
+            this.updateFunction(this.particles);
 
             // Add new ones
             var worldMatrix;
@@ -176,13 +178,13 @@
                 worldMatrix = BABYLON.Matrix.Translation(this.emitter.x, this.emitter.y, this.emitter.z);
             }
 
-            for (index = 0; index < newParticles; index++) {
+            for (var index = 0; index < newParticles; index++) {
                 if (this.particles.length === this._capacity) {
                     break;
                 }
 
                 if (this._stockParticles.length !== 0) {
-                    particle = this._stockParticles.pop();
+                    var particle = this._stockParticles.pop();
                     particle.age = 0;
                 } else {
                     particle = new BABYLON.Particle();

+ 33 - 31
Babylon/Particles/babylon.particleSystem.ts

@@ -38,7 +38,7 @@
         public particleTexture: Texture;
 
         public onDispose: () => void;
-        public customUpdateFunction: (particles: Particle[]) => void;
+        public updateFunction: (particles: Particle[]) => void;
 
         public blendMode = ParticleSystem.BLENDMODE_ONEONE;
 
@@ -128,6 +128,35 @@
 
                 Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
             }
+
+            this.updateFunction = (particles: Particle[]): void => {
+                for (var index = 0; index < particles.length; index++) {
+                    var particle = particles[index];
+                    particle.age += this._scaledUpdateSpeed;
+
+                    if (particle.age >= particle.lifeTime) { // Recycle
+                        particles.splice(index, 1);
+                        this._stockParticles.push(particle);
+                        index--;
+                        continue;
+                    }
+                    else {
+                        particle.colorStep.scaleToRef(this._scaledUpdateSpeed, this._scaledColorStep);
+                        particle.color.addInPlace(this._scaledColorStep);
+
+                        if (particle.color.a < 0)
+                            particle.color.a = 0;
+
+                        particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
+
+                        particle.direction.scaleToRef(this._scaledUpdateSpeed, this._scaledDirection);
+                        particle.position.addInPlace(this._scaledDirection);
+
+                        this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
+                        particle.direction.addInPlace(this._scaledGravity);
+                    }
+                }
+            }
         }
 
         public getCapacity(): number {
@@ -170,35 +199,8 @@
         private _update(newParticles: number): void {
             // Update current
             this._alive = this.particles.length > 0;
-            for (var index = 0; index < this.particles.length; index++) {
-                var particle = this.particles[index];
-                particle.age += this._scaledUpdateSpeed;
-
-                if (particle.age >= particle.lifeTime) {
-                    this._stockParticles.push(this.particles.splice(index, 1)[0]);
-                    index--;
-                    continue;
-                }
-                else {
-                    particle.colorStep.scaleToRef(this._scaledUpdateSpeed, this._scaledColorStep);
-                    particle.color.addInPlace(this._scaledColorStep);
-
-                    if (particle.color.a < 0)
-                        particle.color.a = 0;
-
-                    particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
 
-                    particle.direction.scaleToRef(this._scaledUpdateSpeed, this._scaledDirection);
-                    particle.position.addInPlace(this._scaledDirection);
-
-                    this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
-                    particle.direction.addInPlace(this._scaledGravity);
-                }
-            }
-
-            if (this.customUpdateFunction) {
-                this.customUpdateFunction(this.particles);
-            }
+            this.updateFunction(this.particles);
 
             // Add new ones
             var worldMatrix;
@@ -209,13 +211,13 @@
                 worldMatrix = Matrix.Translation(this.emitter.x, this.emitter.y, this.emitter.z);
             }
 
-            for (index = 0; index < newParticles; index++) {
+            for (var index = 0; index < newParticles; index++) {
                 if (this.particles.length === this._capacity) {
                     break;
                 }
 
                 if (this._stockParticles.length !== 0) {
-                    particle = this._stockParticles.pop();
+                    var particle = this._stockParticles.pop();
                     particle.age = 0;
                 } else {
                     particle = new Particle();

+ 1 - 1
Babylon/Sprites/babylon.spriteManager.js

@@ -85,7 +85,7 @@
             var baseSize = this._spriteTexture.getBaseSize();
 
             // Sprites
-            var deltaTime = BABYLON.Tools.GetDeltaTime();
+            var deltaTime = engine.getDeltaTime();
             var max = Math.min(this._capacity, this.sprites.length);
             var rowSize = baseSize.width / this.cellSize;
 

+ 1 - 1
Babylon/Sprites/babylon.spriteManager.ts

@@ -102,7 +102,7 @@
             var baseSize = this._spriteTexture.getBaseSize();
 
             // Sprites
-            var deltaTime = BABYLON.Tools.GetDeltaTime();
+            var deltaTime = engine.getDeltaTime();
             var max = Math.min(this._capacity, this.sprites.length);
             var rowSize = baseSize.width / this.cellSize;
 

+ 1 - 1
Babylon/Tools/babylon.sceneOptimizer.js

@@ -313,7 +313,7 @@ var BABYLON;
         }
         SceneOptimizer._CheckCurrentState = function (scene, options, currentPriorityLevel, onSuccess, onFailure) {
             // TODO: add an epsilon
-            if (BABYLON.Tools.GetFps() >= options.targetFrameRate) {
+            if (scene.getEngine().getFps() >= options.targetFrameRate) {
                 if (onSuccess) {
                     onSuccess();
                 }

+ 1 - 1
Babylon/Tools/babylon.sceneOptimizer.ts

@@ -254,7 +254,7 @@
 
         static _CheckCurrentState(scene: Scene, options: SceneOptimizerOptions, currentPriorityLevel: number, onSuccess?: () => void, onFailure?: () => void) {
             // TODO: add an epsilon
-            if (Tools.GetFps() >= options.targetFrameRate) {
+            if (scene.getEngine().getFps() >= options.targetFrameRate) {
                 if (onSuccess) {
                     onSuccess();
                 }

+ 6 - 37
Babylon/Tools/babylon.tools.js

@@ -3,12 +3,6 @@
     // Screenshots
     var screenshotCanvas;
 
-    // FPS
-    var fpsRange = 60;
-    var previousFramesDuration = [];
-    var fps = 60;
-    var deltaTime = 0;
-
     var cloneValue = function (source, destinationObject) {
         if (!source)
             return null;
@@ -391,37 +385,6 @@
             }
         };
 
-        Tools.GetFps = function () {
-            return fps;
-        };
-
-        Tools.GetDeltaTime = function () {
-            return deltaTime;
-        };
-
-        Tools._MeasureFps = function () {
-            previousFramesDuration.push(Tools.Now);
-            var length = previousFramesDuration.length;
-
-            if (length >= 2) {
-                deltaTime = previousFramesDuration[length - 1] - previousFramesDuration[length - 2];
-            }
-
-            if (length >= fpsRange) {
-                if (length > fpsRange) {
-                    previousFramesDuration.splice(0, 1);
-                    length = previousFramesDuration.length;
-                }
-
-                var sum = 0;
-                for (var id = 0; id < length - 1; id++) {
-                    sum += previousFramesDuration[id + 1] - previousFramesDuration[id];
-                }
-
-                fps = 1000.0 / (sum / (length - 1));
-            }
-        };
-
         Tools.CreateScreenshot = function (engine, camera, size) {
             var width;
             var height;
@@ -795,6 +758,12 @@
             enumerable: true,
             configurable: true
         });
+
+        // Deprecated
+        Tools.GetFps = function () {
+            Tools.Warn("Tools.GetFps() is deprecated. Please use engine.getFps() instead");
+            return 0;
+        };
         Tools.BaseUrl = "";
 
         Tools.GetExponantOfTwo = function (value, max) {

+ 10 - 40
Babylon/Tools/babylon.tools.ts

@@ -11,12 +11,6 @@
     // Screenshots
     var screenshotCanvas: HTMLCanvasElement;
 
-    // FPS
-    var fpsRange = 60;
-    var previousFramesDuration = [];
-    var fps = 60;
-    var deltaTime = 0;
-
     var cloneValue = (source, destinationObject) => {
         if (!source)
             return null;
@@ -313,9 +307,9 @@
         }
 
         // Misc.   
-        public static Clamp(value:number, min = 0, max = 1): number {
+        public static Clamp(value: number, min = 0, max = 1): number {
             return Math.min(max, Math.max(min, value));
-        }     
+        }
 
         public static Format(value: number, decimals: number = 2): string {
             return value.toFixed(decimals);
@@ -422,38 +416,6 @@
             }
         }
 
-        public static GetFps(): number {
-            return fps;
-        }
-
-        public static GetDeltaTime(): number {
-            return deltaTime;
-        }
-
-        public static _MeasureFps(): void {
-            previousFramesDuration.push(Tools.Now);
-            var length = previousFramesDuration.length;
-
-            if (length >= 2) {
-                deltaTime = previousFramesDuration[length - 1] - previousFramesDuration[length - 2];
-            }
-
-            if (length >= fpsRange) {
-
-                if (length > fpsRange) {
-                    previousFramesDuration.splice(0, 1);
-                    length = previousFramesDuration.length;
-                }
-
-                var sum = 0;
-                for (var id = 0; id < length - 1; id++) {
-                    sum += previousFramesDuration[id + 1] - previousFramesDuration[id];
-                }
-
-                fps = 1000.0 / (sum / (length - 1));
-            }
-        }
-
         public static CreateScreenshot(engine: Engine, camera: Camera, size: any): void {
             var width: number;
             var height: number;
@@ -815,5 +777,13 @@
 
             return new Date().getTime();
         }
+
+        // Deprecated
+
+        public static GetFps(): number {
+            Tools.Warn("Tools.GetFps() is deprecated. Please use engine.getFps() instead");
+            return 0;
+        }
+
     }
 } 

+ 38 - 1
Babylon/babylon.engine.js

@@ -371,6 +371,11 @@
             this._runningLoop = false;
             this._loadingDivBackgroundColor = "black";
             this._drawCalls = 0;
+            // FPS
+            this.fpsRange = 60;
+            this.previousFramesDuration = [];
+            this.fps = 60;
+            this.deltaTime = 0;
             // States
             this._depthCullingState = new _DepthCullingState();
             this._alphaState = new _AlphaState();
@@ -708,7 +713,7 @@
         };
 
         Engine.prototype.beginFrame = function () {
-            BABYLON.Tools._MeasureFps();
+            this._measureFps();
         };
 
         Engine.prototype.endFrame = function () {
@@ -1903,6 +1908,38 @@
             this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
         };
 
+        // FPS
+        Engine.prototype.getFps = function () {
+            return this.fps;
+        };
+
+        Engine.prototype.getDeltaTime = function () {
+            return this.deltaTime;
+        };
+
+        Engine.prototype._measureFps = function () {
+            this.previousFramesDuration.push(BABYLON.Tools.Now);
+            var length = this.previousFramesDuration.length;
+
+            if (length >= 2) {
+                this.deltaTime = this.previousFramesDuration[length - 1] - this.previousFramesDuration[length - 2];
+            }
+
+            if (length >= this.fpsRange) {
+                if (length > this.fpsRange) {
+                    this.previousFramesDuration.splice(0, 1);
+                    length = this.previousFramesDuration.length;
+                }
+
+                var sum = 0;
+                for (var id = 0; id < length - 1; id++) {
+                    sum += this.previousFramesDuration[id + 1] - this.previousFramesDuration[id];
+                }
+
+                this.fps = 1000.0 / (sum / (length - 1));
+            }
+        };
+
         // Statics
         Engine.isSupported = function () {
             try  {

+ 40 - 2
Babylon/babylon.engine.ts

@@ -391,7 +391,6 @@
         public static CollisionsEpsilon = 0.001;
         public static ShadersRepository = "Babylon/Shaders/";
 
-
         // Public members
         public isFullscreen = false;
         public isPointerLock = false;
@@ -426,6 +425,12 @@
 
         private _drawCalls = 0;
 
+        // FPS
+        private fpsRange = 60;
+        private previousFramesDuration = [];
+        private fps = 60;
+        private deltaTime = 0;
+
         // States
         private _depthCullingState = new _DepthCullingState();
         private _alphaState = new _AlphaState();
@@ -717,7 +722,7 @@
         }
 
         public beginFrame(): void {
-            BABYLON.Tools._MeasureFps();
+            this._measureFps();
         }
 
         public endFrame(): void {
@@ -1910,6 +1915,39 @@
             this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
         }
 
+        // FPS
+        public getFps(): number {
+            return this.fps;
+        }
+
+        public getDeltaTime(): number {
+            return this.deltaTime;
+        }
+
+        private _measureFps(): void {
+            this.previousFramesDuration.push(Tools.Now);
+            var length = this.previousFramesDuration.length;
+
+            if (length >= 2) {
+                this.deltaTime = this.previousFramesDuration[length - 1] - this.previousFramesDuration[length - 2];
+            }
+
+            if (length >= this.fpsRange) {
+
+                if (length > this.fpsRange) {
+                    this.previousFramesDuration.splice(0, 1);
+                    length = this.previousFramesDuration.length;
+                }
+
+                var sum = 0;
+                for (var id = 0; id < length - 1; id++) {
+                    sum += this.previousFramesDuration[id + 1] - this.previousFramesDuration[id];
+                }
+
+                this.fps = 1000.0 / (sum / (length - 1));
+            }
+        }
+
         // Statics
         public static isSupported(): boolean {
             try {

+ 1 - 1
Babylon/babylon.scene.js

@@ -1055,7 +1055,7 @@
             }
 
             // Animations
-            var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(BABYLON.Tools.GetDeltaTime(), Scene.MaxDeltaTime));
+            var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime));
             this._animationRatio = deltaTime * (60.0 / 1000.0);
             this._animate();
 

+ 1 - 1
Babylon/babylon.scene.ts

@@ -1135,7 +1135,7 @@
             }
 
             // Animations
-            var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(Tools.GetDeltaTime(), Scene.MaxDeltaTime));
+            var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime));
             this._animationRatio = deltaTime * (60.0 / 1000.0);
             this._animate();
 

File diff suppressed because it is too large
+ 81 - 72
babylon.2.0-alpha.debug.js


File diff suppressed because it is too large
+ 13 - 13
babylon.2.0-alpha.js