Browse Source

update particles

DESKTOP-QJU4N0L\mityh 8 năm trước cách đây
mục cha
commit
10a5069e54

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 521 - 524
dist/preview release/babylon.d.ts


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 16 - 16
dist/preview release/babylon.js


+ 21 - 34
dist/preview release/babylon.max.js

@@ -40858,9 +40858,7 @@ var BABYLON;
 var BABYLON;
 (function (BABYLON) {
     var Particle = (function () {
-        function Particle(particleSystem, cellWidth, cellHeight, cellIndex, invertU, invertV, _loopAnimation, _fromIndex, _toIndex, _delay, _sheetDirection, _time, disposeWhenFinishedAnimating) {
-            if (cellWidth === void 0) { cellWidth = 0; }
-            if (cellHeight === void 0) { cellHeight = 0; }
+        function Particle(particleSystem, cellIndex, invertU, invertV, _loopAnimation, _fromIndex, _toIndex, _delay, _sheetDirection, _time, disposeWhenFinishedAnimating) {
             if (cellIndex === void 0) { cellIndex = 0; }
             if (invertU === void 0) { invertU = 0; }
             if (invertV === void 0) { invertV = 0; }
@@ -40872,8 +40870,6 @@ var BABYLON;
             if (_time === void 0) { _time = 0; }
             if (disposeWhenFinishedAnimating === void 0) { disposeWhenFinishedAnimating = false; }
             this.particleSystem = particleSystem;
-            this.cellWidth = cellWidth;
-            this.cellHeight = cellHeight;
             this.cellIndex = cellIndex;
             this.invertU = invertU;
             this.invertV = invertV;
@@ -40894,7 +40890,7 @@ var BABYLON;
             this.angle = 0;
             this.angularSpeed = 0;
         }
-        Particle.prototype._animate = function (deltaTime) {
+        Particle.prototype.updateCellIndex = function (deltaTime) {
             this._time += deltaTime;
             if (this._time > this._delay) {
                 this._time = this._time % this._delay;
@@ -40906,7 +40902,7 @@ var BABYLON;
                     else {
                         this.cellIndex = this._toIndex;
                         if (this.disposeWhenFinishedAnimating) {
-                            this.ReadyForRecycling();
+                            this.readyForRecycling();
                         }
                     }
                 }
@@ -40923,8 +40919,6 @@ var BABYLON;
             other.angle = this.angle;
             other.angularSpeed = this.angularSpeed;
             other.particleSystem = this.particleSystem;
-            other.cellWidth = this.cellWidth;
-            other.cellHeight = this.cellHeight;
             other.cellIndex = this.cellIndex;
             other.invertU = this.invertU;
             other.invertV = this.invertV;
@@ -40936,7 +40930,7 @@ var BABYLON;
             other._time = this._time;
             other.disposeWhenFinishedAnimating = this.disposeWhenFinishedAnimating;
         };
-        Particle.prototype.ReadyForRecycling = function () {
+        Particle.prototype.readyForRecycling = function () {
             this.age = this.lifeTime;
         };
         return Particle;
@@ -41020,13 +41014,13 @@ var BABYLON;
             this._delay = 0;
             this._sheetDirection = 1;
             this._time = 0;
-            this._vertixBufferSize = 11;
+            this._vertexBufferSize = 11;
             this.appendParticleVertexes = null;
             this.id = name;
             this._capacity = capacity;
             this._epsilon = epsilon;
             if (cellSize) {
-                this._vertixBufferSize = 15;
+                this._vertexBufferSize = 15;
                 if (cellSize.width && cellSize.height) {
                     this.cellWidth = cellSize.width;
                     this.cellHeight = cellSize.height;
@@ -41041,8 +41035,8 @@ var BABYLON;
             scene.particleSystems.push(this);
             this._createIndexBuffer();
             // 11 floats per particle (x, y, z, r, g, b, a, angle, size, offsetX, offsetY) + 1 filler
-            this._vertexData = new Float32Array(capacity * this._vertixBufferSize * 4);
-            this._vertexBuffer = new BABYLON.Buffer(scene.getEngine(), this._vertexData, true, this._vertixBufferSize);
+            this._vertexData = new Float32Array(capacity * this._vertexBufferSize * 4);
+            this._vertexBuffer = new BABYLON.Buffer(scene.getEngine(), this._vertexData, true, this._vertexBufferSize);
             var positions = this._vertexBuffer.createVertexBuffer(BABYLON.VertexBuffer.PositionKind, 0, 3);
             var colors = this._vertexBuffer.createVertexBuffer(BABYLON.VertexBuffer.ColorKind, 3, 4);
             var options = this._vertexBuffer.createVertexBuffer("options", 7, 4);
@@ -41067,6 +41061,7 @@ var BABYLON;
                 BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
             };
             this.updateFunction = function (particles) {
+                var deltaTime = _this._scene.getEngine().getDeltaTime();
                 for (var index = 0; index < particles.length; index++) {
                     var particle = particles[index];
                     particle.age += _this._scaledUpdateSpeed;
@@ -41085,6 +41080,9 @@ var BABYLON;
                         particle.position.addInPlace(_this._scaledDirection);
                         _this.gravity.scaleToRef(_this._scaledUpdateSpeed, _this._scaledGravity);
                         particle.direction.addInPlace(_this._scaledGravity);
+                        if (_this.cellSize && _this._animationStarted) {
+                            particle.updateCellIndex(deltaTime);
+                        }
                     }
                 }
             };
@@ -41153,7 +41151,7 @@ var BABYLON;
         };
         // animation sheet
         ParticleSystem.prototype._appendParticleVertex = function (index, particle, offsetX, offsetY) {
-            var offset = index * this._vertixBufferSize;
+            var offset = index * this._vertexBufferSize;
             this._vertexData[offset] = particle.position.x;
             this._vertexData[offset + 1] = particle.position.y;
             this._vertexData[offset + 2] = particle.position.z;
@@ -41179,7 +41177,7 @@ var BABYLON;
             var columnOffset = particle.cellIndex - rowOffset * rowSize;
             var intertU = particle.invertU ? 1 : 0;
             var interV = particle.invertV ? 1 : 0;
-            var offset = index * this._vertixBufferSize;
+            var offset = index * this._vertexBufferSize;
             this._vertexData[offset] = particle.position.x;
             this._vertexData[offset + 1] = particle.position.y;
             this._vertexData[offset + 2] = particle.position.z;
@@ -41222,7 +41220,7 @@ var BABYLON;
                 }
                 else {
                     if (this.cellSize) {
-                        particle = new BABYLON.Particle(this, this.cellWidth, this.cellHeight, this.cellIndex, this.invertU, this.invertV, this._loopAnimation, this._fromIndex, this._toIndex, this._delay, this._sheetDirection, this._time, this.disposeParticlesWhenFinishedAnimating);
+                        particle = new BABYLON.Particle(this, this.cellIndex, this.invertU, this.invertV, this._loopAnimation, this._fromIndex, this._toIndex, this._delay, this._sheetDirection, this._time, this.disposeParticlesWhenFinishedAnimating);
                     }
                     else {
                         particle = new BABYLON.Particle(this);
@@ -41321,18 +41319,11 @@ var BABYLON;
                 }
             }
             // Animation sheet
-            var deltaTime = 0;
+            var rowSize = 0;
             if (this.cellSize) {
                 var baseSize = this.particleTexture.getBaseSize();
-                var rowSize = baseSize.width / this.cellWidth;
-                var engine = this._scene.getEngine();
-                deltaTime = engine.getDeltaTime();
-                if (this._animationStarted) {
-                    this.appendParticleVertexes = this.appenedAllParticleVertexesWithSheetAndAnimationStarted;
-                }
-                else {
-                    this.appendParticleVertexes = this.appenedAllParticleVertexesWithSheet;
-                }
+                rowSize = baseSize.width / this.cellWidth;
+                this.appendParticleVertexes = this.appenedParticleVertexesWithSheet;
             }
             else {
                 this.appendParticleVertexes = this.appenedParticleVertexesNoSheet;
@@ -41341,22 +41332,18 @@ var BABYLON;
             var offset = 0;
             for (var index = 0; index < this.particles.length; index++) {
                 var particle = this.particles[index];
-                this.appendParticleVertexes(offset, particle, rowSize, deltaTime);
+                this.appendParticleVertexes(offset, particle, rowSize);
                 offset += 4;
             }
             this._vertexBuffer.update(this._vertexData);
         };
-        ParticleSystem.prototype.appenedAllParticleVertexesWithSheet = function (offset, particle, rowSize, deltaTime) {
+        ParticleSystem.prototype.appenedParticleVertexesWithSheet = function (offset, particle, rowSize) {
             this._appendParticleVertexWithAnimation(offset++, particle, 0, 0, rowSize);
             this._appendParticleVertexWithAnimation(offset++, particle, 1, 0, rowSize);
             this._appendParticleVertexWithAnimation(offset++, particle, 1, 1, rowSize);
             this._appendParticleVertexWithAnimation(offset++, particle, 0, 1, rowSize);
         };
-        ParticleSystem.prototype.appenedAllParticleVertexesWithSheetAndAnimationStarted = function (offset, particle, rowSize, deltaTime) {
-            this.appenedAllParticleVertexesWithSheet(offset, particle, rowSize, deltaTime);
-            particle._animate(deltaTime);
-        };
-        ParticleSystem.prototype.appenedParticleVertexesNoSheet = function (offset, particle, rowSize, deltaTime) {
+        ParticleSystem.prototype.appenedParticleVertexesNoSheet = function (offset, particle, rowSize) {
             this._appendParticleVertex(offset++, particle, 0, 0);
             this._appendParticleVertex(offset++, particle, 1, 0);
             this._appendParticleVertex(offset++, particle, 1, 1);

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 521 - 524
dist/preview release/babylon.module.d.ts


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 26 - 26
dist/preview release/babylon.worker.js


+ 4 - 6
src/Particles/babylon.particle.ts

@@ -11,10 +11,10 @@
         public angle = 0;
         public angularSpeed = 0;
 
-        constructor(private particleSystem: ParticleSystem, 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, private disposeWhenFinishedAnimating = false) {
+        constructor(private particleSystem: ParticleSystem, 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, private disposeWhenFinishedAnimating = false) {
         }
 
-        public _animate(deltaTime: number): void {
+        public updateCellIndex(deltaTime: number): void {
             this._time += deltaTime;
             if (this._time > this._delay) {
                 this._time = this._time % this._delay;
@@ -26,7 +26,7 @@
                     else {
                         this.cellIndex = this._toIndex;
                         if (this.disposeWhenFinishedAnimating) {
-                            this.ReadyForRecycling();
+                            this.readyForRecycling();
                         }
                     }
                 }
@@ -44,8 +44,6 @@
             other.angle = this.angle;
             other.angularSpeed = this.angularSpeed;
             other.particleSystem = this.particleSystem;
-            other.cellWidth = this.cellWidth;
-            other.cellHeight = this.cellHeight;
             other.cellIndex = this.cellIndex;
             other.invertU = this.invertU;
             other.invertV = this.invertV;
@@ -58,7 +56,7 @@
             other.disposeWhenFinishedAnimating = this.disposeWhenFinishedAnimating;
         }
 
-        public ReadyForRecycling() {
+        public readyForRecycling() {
             this.age = this.lifeTime;
         }
     }

+ 21 - 26
src/Particles/babylon.particleSystem.ts

@@ -136,7 +136,7 @@
         private _delay = 0;
         private _sheetDirection = 1;
         private _time = 0;
-        private _vertixBufferSize = 11;
+        private _vertexBufferSize = 11;
         // end of sheet animation
 
         constructor(public name: string, capacity: number, scene: Scene, customEffect?: Effect, private cellSize?: any, epsilon: number = 0.01) {
@@ -145,7 +145,7 @@
 
             this._epsilon = epsilon;
             if (cellSize) {
-                this._vertixBufferSize = 15;
+                this._vertexBufferSize = 15;
                 if (cellSize.width && cellSize.height) {
                     this.cellWidth = cellSize.width;
                     this.cellHeight = cellSize.height;
@@ -164,8 +164,8 @@
             this._createIndexBuffer();
 
             // 11 floats per particle (x, y, z, r, g, b, a, angle, size, offsetX, offsetY) + 1 filler
-            this._vertexData = new Float32Array(capacity * this._vertixBufferSize * 4);
-            this._vertexBuffer = new Buffer(scene.getEngine(), this._vertexData, true, this._vertixBufferSize);
+            this._vertexData = new Float32Array(capacity * this._vertexBufferSize * 4);
+            this._vertexBuffer = new Buffer(scene.getEngine(), this._vertexData, true, this._vertexBufferSize);
 
             var positions = this._vertexBuffer.createVertexBuffer(VertexBuffer.PositionKind, 0, 3);
             var colors = this._vertexBuffer.createVertexBuffer(VertexBuffer.ColorKind, 3, 4);
@@ -198,6 +198,7 @@
             }
 
             this.updateFunction = (particles: Particle[]): void => {
+                var deltaTime = this._scene.getEngine().getDeltaTime();
                 for (var index = 0; index < particles.length; index++) {
                     var particle = particles[index];
                     particle.age += this._scaledUpdateSpeed;
@@ -221,6 +222,10 @@
 
                         this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
                         particle.direction.addInPlace(this._scaledGravity);
+
+                        if (this.cellSize && this._animationStarted) {
+                            particle.updateCellIndex(deltaTime);
+                        }
                     }
                 }
             }
@@ -291,7 +296,7 @@
         // animation sheet
 
         public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number): void {
-            var offset = index * this._vertixBufferSize;
+            var offset = index * this._vertexBufferSize;
             this._vertexData[offset] = particle.position.x;
             this._vertexData[offset + 1] = particle.position.y;
             this._vertexData[offset + 2] = particle.position.z;
@@ -322,7 +327,7 @@
             var intertU = particle.invertU ? 1 : 0;
             var interV = particle.invertV ? 1 : 0;
 
-            var offset = index * this._vertixBufferSize;
+            var offset = index * this._vertexBufferSize;
             this._vertexData[offset] = particle.position.x;
             this._vertexData[offset + 1] = particle.position.y;
             this._vertexData[offset + 2] = particle.position.z;
@@ -356,6 +361,7 @@
                 var emitterPosition = (<Vector3>this.emitter);
                 worldMatrix = Matrix.Translation(emitterPosition.x, emitterPosition.y, emitterPosition.z);
             }
+
             var particle: Particle;
             for (var index = 0; index < newParticles; index++) {
                 if (this.particles.length === this._capacity) {
@@ -368,12 +374,13 @@
                     particle.cellIndex = this._fromIndex;
                 } else {
                     if (this.cellSize) {
-                        particle = new Particle(this, this.cellWidth, this.cellHeight, this.cellIndex, this.invertU, this.invertV, this._loopAnimation, this._fromIndex, this._toIndex, this._delay, this._sheetDirection, this._time, this.disposeParticlesWhenFinishedAnimating);
+                        particle = new Particle(this, this.cellIndex, this.invertU, this.invertV, this._loopAnimation, this._fromIndex, this._toIndex, this._delay, this._sheetDirection, this._time, this.disposeParticlesWhenFinishedAnimating);
                     }
                     else {
                         particle = new Particle(this);
                     }
                 }
+
                 this.particles.push(particle);
 
                 var emitPower = randomNumber(this.minEmitPower, this.maxEmitPower);
@@ -500,19 +507,12 @@
             }
 
             // Animation sheet
-            var deltaTime = 0;
+            var rowSize = 0;
             if (this.cellSize) {
                 var baseSize = this.particleTexture.getBaseSize();
-                var rowSize = baseSize.width / this.cellWidth;
-                var engine = this._scene.getEngine();
-                deltaTime = engine.getDeltaTime();
+                rowSize = baseSize.width / this.cellWidth;
+                this.appendParticleVertexes = this.appenedParticleVertexesWithSheet;
 
-                if (this._animationStarted) {
-                    this.appendParticleVertexes = this.appenedAllParticleVertexesWithSheetAndAnimationStarted;
-                }
-                else {
-                    this.appendParticleVertexes = this.appenedAllParticleVertexesWithSheet;
-                }
             }
             else {
                 this.appendParticleVertexes = this.appenedParticleVertexesNoSheet;
@@ -522,28 +522,23 @@
             var offset = 0;
             for (var index = 0; index < this.particles.length; index++) {
                 var particle = this.particles[index];
-                this.appendParticleVertexes(offset, particle, rowSize, deltaTime);
+                this.appendParticleVertexes(offset, particle, rowSize);
                 offset += 4;
             }
 
             this._vertexBuffer.update(this._vertexData);
         }
 
-        public appendParticleVertexes: (offset: number, particle: Particle, rowSize: number, deltaTime: number) => void = null;
+        public appendParticleVertexes: (offset: number, particle: Particle, rowSize: number) => void = null;
 
-        private appenedAllParticleVertexesWithSheet(offset: number, particle: Particle, rowSize: number, deltaTime: number) {
+        private appenedParticleVertexesWithSheet(offset: number, particle: Particle, rowSize: number) {
             this._appendParticleVertexWithAnimation(offset++, particle, 0, 0, rowSize);
             this._appendParticleVertexWithAnimation(offset++, particle, 1, 0, rowSize);
             this._appendParticleVertexWithAnimation(offset++, particle, 1, 1, rowSize);
             this._appendParticleVertexWithAnimation(offset++, particle, 0, 1, rowSize);
         }
 
-        private appenedAllParticleVertexesWithSheetAndAnimationStarted(offset: number, particle: Particle, rowSize: number, deltaTime: number) {
-            this.appenedAllParticleVertexesWithSheet(offset, particle, rowSize, deltaTime);
-            particle._animate(deltaTime);
-        }
-
-        private appenedParticleVertexesNoSheet(offset: number, particle: Particle, rowSize: number, deltaTime: number) {
+        private appenedParticleVertexesNoSheet(offset: number, particle: Particle, rowSize: number) {
             this._appendParticleVertex(offset++, particle, 0, 0);
             this._appendParticleVertex(offset++, particle, 1, 0);
             this._appendParticleVertex(offset++, particle, 1, 1);