Bläddra i källkod

moving animation to particle

DESKTOP-QJU4N0L\mityh 8 år sedan
förälder
incheckning
7d34eb4ff9

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 4763 - 4751
dist/preview release/babylon.d.ts


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 19 - 19
dist/preview release/babylon.js


+ 66 - 37
dist/preview release/babylon.max.js

@@ -40844,8 +40844,36 @@ var BABYLON;
 
 var BABYLON;
 (function (BABYLON) {
+    var IParticleAnimation = (function () {
+        function IParticleAnimation() {
+        }
+        return IParticleAnimation;
+    }());
+    BABYLON.IParticleAnimation = IParticleAnimation;
     var Particle = (function () {
-        function Particle() {
+        function Particle(cellWidth, cellHeight, cellIndex, invertU, invertV, _loopAnimation, _fromIndex, _toIndex, _delay, _sheetDirection, _time) {
+            if (cellWidth === void 0) { cellWidth = 0; }
+            if (cellHeight === void 0) { cellHeight = 0; }
+            if (cellIndex === void 0) { cellIndex = 0; }
+            if (invertU === void 0) { invertU = 0; }
+            if (invertV === void 0) { invertV = 0; }
+            if (_loopAnimation === void 0) { _loopAnimation = false; }
+            if (_fromIndex === void 0) { _fromIndex = 0; }
+            if (_toIndex === void 0) { _toIndex = 0; }
+            if (_delay === void 0) { _delay = 0; }
+            if (_sheetDirection === void 0) { _sheetDirection = 1; }
+            if (_time === void 0) { _time = 0; }
+            this.cellWidth = cellWidth;
+            this.cellHeight = cellHeight;
+            this.cellIndex = cellIndex;
+            this.invertU = invertU;
+            this.invertV = invertV;
+            this._loopAnimation = _loopAnimation;
+            this._fromIndex = _fromIndex;
+            this._toIndex = _toIndex;
+            this._delay = _delay;
+            this._sheetDirection = _sheetDirection;
+            this._time = _time;
             this.position = BABYLON.Vector3.Zero();
             this.direction = BABYLON.Vector3.Zero();
             this.color = new BABYLON.Color4(0, 0, 0, 0);
@@ -40856,6 +40884,22 @@ var BABYLON;
             this.angle = 0;
             this.angularSpeed = 0;
         }
+        Particle.prototype._animate = function (deltaTime) {
+            this._time += deltaTime;
+            if (this._time > this._delay) {
+                this._time = this._time % this._delay;
+                this.cellIndex += this._sheetDirection;
+                if (this.cellIndex > this._toIndex) {
+                    if (this._loopAnimation) {
+                        this.cellIndex = this._fromIndex;
+                    }
+                    else {
+                        this.cellIndex = this._toIndex;
+                        // stop and remove from scene
+                    }
+                }
+            }
+        };
         Particle.prototype.copyTo = function (other) {
             other.position.copyFrom(this.position);
             other.direction.copyFrom(this.direction);
@@ -40946,7 +40990,6 @@ var BABYLON;
             this._toIndex = 0;
             this._delay = 0;
             this._sheetDirection = 1;
-            this._frameCount = 0;
             this._time = 0;
             this.id = name;
             this._capacity = capacity;
@@ -41074,26 +41117,8 @@ var BABYLON;
         ParticleSystem.prototype.stopAnimation = function () {
             this._animationStarted = false;
         };
-        ParticleSystem.prototype._animateSheet = function (deltaTime) {
-            if (!this._animationStarted)
-                return;
-            this._time += deltaTime;
-            if (this._time > this._delay) {
-                this._time = this._time % this._delay;
-                this.cellIndex += this._sheetDirection;
-                if (this.cellIndex === this._toIndex + 1) {
-                    if (this._loopAnimation) {
-                        this.cellIndex = this._fromIndex;
-                    }
-                    else {
-                        this._animationStarted = false;
-                        this.stop();
-                    }
-                }
-            }
-        };
         // animation sheet
-        ParticleSystem.prototype._appendParticleVertex = function (index, particle, offsetX, offsetY, intertU, interV, columnOffset, rowOffset) {
+        ParticleSystem.prototype._appendParticleVertex = function (index, particle, offsetX, offsetY, rowSize) {
             if (offsetX === 0)
                 offsetX = this._epsilon;
             else if (offsetX === 1)
@@ -41102,6 +41127,10 @@ var BABYLON;
                 offsetY = this._epsilon;
             else if (offsetY === 1)
                 offsetY = 1 - this._epsilon;
+            var rowOffset = (particle.cellIndex / rowSize) >> 0;
+            var columnOffset = particle.cellIndex - rowOffset * rowSize;
+            var intertU = particle.invertU ? 1 : 0;
+            var interV = particle.invertV ? 1 : 0;
             var offset = index * 15;
             this._vertexData[offset] = particle.position.x;
             this._vertexData[offset + 1] = particle.position.y;
@@ -41143,7 +41172,12 @@ var BABYLON;
                     particle.age = 0;
                 }
                 else {
-                    particle = new BABYLON.Particle();
+                    if (this.cellSize) {
+                        particle = new BABYLON.Particle(this.cellWidth, this.cellHeight, this.cellIndex, this.invertU, this.invertV, this._loopAnimation, this._fromIndex, this._toIndex, this._delay, this._sheetDirection, this._time);
+                    }
+                    else {
+                        particle = new BABYLON.Particle();
+                    }
                 }
                 this.particles.push(particle);
                 var emitPower = randomNumber(this.minEmitPower, this.maxEmitPower);
@@ -41228,31 +41262,26 @@ var BABYLON;
                 }
             }
             // Animation sheet
-            var rowOffset = 0;
-            var columnOffset = 0;
-            var intertU = 0;
-            var interV = 0;
+            var deltaTime = 0;
             if (this.cellSize) {
                 var baseSize = this.particleTexture.getBaseSize();
                 var rowSize = baseSize.width / this.cellWidth;
-                var rowOffset = (this.cellIndex / rowSize) >> 0;
-                var columnOffset = this.cellIndex - rowOffset * rowSize;
-                var intertU = this.invertU ? 1 : 0;
-                var interV = this.invertV ? 1 : 0;
+                var engine = this._scene.getEngine();
+                deltaTime = engine.getDeltaTime();
             }
             // Update VBO
             var offset = 0;
             for (var index = 0; index < this.particles.length; index++) {
                 var particle = this.particles[index];
-                this._appendParticleVertex(offset++, particle, 0, 0, intertU, interV, columnOffset, rowOffset);
-                this._appendParticleVertex(offset++, particle, 1, 0, intertU, interV, columnOffset, rowOffset);
-                this._appendParticleVertex(offset++, particle, 1, 1, intertU, interV, columnOffset, rowOffset);
-                this._appendParticleVertex(offset++, particle, 0, 1, intertU, interV, columnOffset, rowOffset);
+                this._appendParticleVertex(offset++, particle, 0, 0, rowSize);
+                this._appendParticleVertex(offset++, particle, 1, 0, rowSize);
+                this._appendParticleVertex(offset++, particle, 1, 1, rowSize);
+                this._appendParticleVertex(offset++, particle, 0, 1, rowSize);
+                if (this.cellSize) {
+                    particle._animate(deltaTime);
+                }
             }
             this._vertexBuffer.update(this._vertexData);
-            var engine = this._scene.getEngine();
-            var deltaTime = engine.getDeltaTime();
-            this._animateSheet(deltaTime);
         };
         ParticleSystem.prototype.rebuild = function () {
             this._createIndexBuffer();

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 4763 - 4751
dist/preview release/babylon.module.d.ts


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 19 - 19
dist/preview release/babylon.worker.js


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

@@ -1,4 +1,9 @@
 module BABYLON {
+
+    export class IParticleAnimation {
+
+    }
+
     export class Particle {
         public position = Vector3.Zero();
         public direction = Vector3.Zero();
@@ -10,6 +15,26 @@
         public angle = 0;
         public angularSpeed = 0;
 
+        constructor(public cellWidth: number = 0, public cellHeight: number = 0, public cellIndex: number = 0, public invertU: number = 0, public invertV: number = 0, private _loopAnimation = false, private _fromIndex = 0, private _toIndex = 0, private _delay = 0, private _sheetDirection = 1, private _time = 0) {
+        }
+
+        public _animate(deltaTime: number): void {
+            this._time += deltaTime;
+            if (this._time > this._delay) {
+                this._time = this._time % this._delay;
+                this.cellIndex += this._sheetDirection;
+                if (this.cellIndex > this._toIndex) {
+                    if (this._loopAnimation) {
+                        this.cellIndex = this._fromIndex;
+                    }
+                    else {
+                        this.cellIndex = this._toIndex;
+                        // stop and remove from scene
+                    }
+                }
+            }
+        }
+
         public copyTo(other: Particle) {
             other.position.copyFrom(this.position);
             other.direction.copyFrom(this.direction);

+ 23 - 39
src/Particles/babylon.particleSystem.ts

@@ -65,7 +65,6 @@
         public cellIndex: number;
         public invertU: number;
         public invertV: number;
-        public disposeWhenFinishedAnimating: boolean;
         private _epsilon: number;
 
 
@@ -135,7 +134,6 @@
         private _toIndex = 0;
         private _delay = 0;
         private _sheetDirection = 1;
-        private _frameCount = 0;
         private _time = 0;
         // end of sheet animation
 
@@ -284,28 +282,9 @@
         public stopAnimation(): void {
             this._animationStarted = false;
         }
-
-        public _animateSheet(deltaTime: number): void {
-            if (!this._animationStarted)
-                return;
-
-            this._time += deltaTime;
-            if (this._time > this._delay) {
-                this._time = this._time % this._delay;
-                this.cellIndex += this._sheetDirection;
-                if (this.cellIndex === this._toIndex + 1) {
-                    if (this._loopAnimation) {
-                        this.cellIndex = this._fromIndex;
-                    } else {
-                        this._animationStarted = false;
-                        this.stop();
-                    }
-                }
-            }
-        }
         // animation sheet
 
-        public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number, intertU: number, interV: number, columnOffset: number, rowOffset: number): void {
+        public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number, rowSize: number): void {
 
             if (offsetX === 0)
                 offsetX = this._epsilon;
@@ -317,6 +296,11 @@
             else if (offsetY === 1)
                 offsetY = 1 - this._epsilon;
 
+            var rowOffset = (particle.cellIndex / rowSize) >> 0;
+            var columnOffset = particle.cellIndex - rowOffset * rowSize;
+            var intertU = particle.invertU ? 1 : 0;
+            var interV = particle.invertV ? 1 : 0;
+
             var offset = index * 15;
             this._vertexData[offset] = particle.position.x;
             this._vertexData[offset + 1] = particle.position.y;
@@ -361,7 +345,12 @@
                     particle = this._stockParticles.pop();
                     particle.age = 0;
                 } else {
-                    particle = new Particle();
+                    if (this.cellSize) {
+                        particle = new Particle(this.cellWidth, this.cellHeight, this.cellIndex, this.invertU, this.invertV, this._loopAnimation, this._fromIndex, this._toIndex, this._delay, this._sheetDirection, this._time);
+                    }
+                    else {
+                        particle = new Particle();
+                    }
                 }
                 this.particles.push(particle);
 
@@ -477,17 +466,12 @@
             }
 
             // Animation sheet
-            var rowOffset = 0;
-            var columnOffset = 0;
-            var intertU = 0;
-            var interV = 0;
+            var deltaTime = 0;
             if (this.cellSize) {
                 var baseSize = this.particleTexture.getBaseSize();
                 var rowSize = baseSize.width / this.cellWidth;
-                var rowOffset = (this.cellIndex / rowSize) >> 0;
-                var columnOffset = this.cellIndex - rowOffset * rowSize;
-                var intertU = this.invertU ? 1 : 0;
-                var interV = this.invertV ? 1 : 0;
+                var engine = this._scene.getEngine();
+                deltaTime = engine.getDeltaTime();
             }
 
             // Update VBO
@@ -495,17 +479,17 @@
             for (var index = 0; index < this.particles.length; index++) {
                 var particle = this.particles[index];
 
-                this._appendParticleVertex(offset++, particle, 0, 0, intertU, interV, columnOffset, rowOffset);
-                this._appendParticleVertex(offset++, particle, 1, 0, intertU, interV, columnOffset, rowOffset);
-                this._appendParticleVertex(offset++, particle, 1, 1, intertU, interV, columnOffset, rowOffset);
-                this._appendParticleVertex(offset++, particle, 0, 1, intertU, interV, columnOffset, rowOffset);
+                this._appendParticleVertex(offset++, particle, 0, 0, rowSize);
+                this._appendParticleVertex(offset++, particle, 1, 0, rowSize);
+                this._appendParticleVertex(offset++, particle, 1, 1, rowSize);
+                this._appendParticleVertex(offset++, particle, 0, 1, rowSize);
+
+                if (this.cellSize) {
+                    particle._animate(deltaTime);
+                }
             }
 
             this._vertexBuffer.update(this._vertexData);
-
-            var engine = this._scene.getEngine();
-            var deltaTime = engine.getDeltaTime();
-            this._animateSheet(deltaTime);
         }
 
         public rebuild(): void {