David Catuhe преди 6 години
родител
ревизия
ec958d68a9

+ 5 - 0
src/Particles/baseParticleSystem.ts

@@ -271,6 +271,11 @@ export class BaseParticleSystem {
     public beginAnimationLoop = false;
 
     /**
+     * Gets or sets a world offset applied to all particles
+     */
+    public worldOffset = new Vector3(0, 0, 0);
+
+    /**
      * Gets or sets whether an animation sprite sheet is enabled or not on the particle system
      */
     public get isAnimationSheetEnabled(): boolean {

+ 3 - 2
src/Particles/gpuParticleSystem.ts

@@ -1064,7 +1064,7 @@ export class GPUParticleSystem extends BaseParticleSystem implements IDisposable
             return;
         }
 
-        var uniforms = ["view", "projection", "colorDead", "invView", "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "sheetInfos", "translationPivot", "eyePosition"];
+        var uniforms = ["worldOffset", "view", "projection", "colorDead", "invView", "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "sheetInfos", "translationPivot", "eyePosition"];
         var samplers = ["textureSampler", "colorGradientSampler"];
 
         if (ImageProcessingConfiguration) {
@@ -1295,7 +1295,8 @@ export class GPUParticleSystem extends BaseParticleSystem implements IDisposable
             this._renderEffect.setMatrix("view", viewMatrix);
             this._renderEffect.setMatrix("projection", this._scene.getProjectionMatrix());
             this._renderEffect.setTexture("textureSampler", this.particleTexture);
-            this._renderEffect.setVector2("translationPivot", this.translationPivot);
+            this._renderEffect.setVector2("translationPivot", this.translationPivot);            
+            this._renderEffect.setVector3("worldOffset", this.worldOffset);
             if (this._colorGradientsTexture) {
                 this._renderEffect.setTexture("colorGradientSampler", this._colorGradientsTexture);
             } else {

+ 3 - 3
src/Particles/particleSystem.ts

@@ -1121,9 +1121,9 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
     public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number): void {
         var offset = index * this._vertexBufferSize;
 
-        this._vertexData[offset++] = particle.position.x;
-        this._vertexData[offset++] = particle.position.y;
-        this._vertexData[offset++] = particle.position.z;
+        this._vertexData[offset++] = particle.position.x + this.worldOffset.x;
+        this._vertexData[offset++] = particle.position.y + this.worldOffset.y;
+        this._vertexData[offset++] = particle.position.z + this.worldOffset.z;
         this._vertexData[offset++] = particle.color.r;
         this._vertexData[offset++] = particle.color.g;
         this._vertexData[offset++] = particle.color.b;

+ 6 - 5
src/Shaders/gpuRenderParticles.vertex.fx

@@ -4,6 +4,7 @@
 uniform mat4 view;
 uniform mat4 projection;
 uniform vec2 translationPivot;
+uniform vec3 worldOffset;
 
 // Particles state
 in vec3 position;
@@ -59,7 +60,7 @@ vec3 rotate(vec3 yaxis, vec3 rotatedCorner) {
 
 	vec3 alignedCorner = rotMatrix * rotatedCorner;
 
-	return position + alignedCorner;
+	return (position + worldOffset) + alignedCorner;
 }
 
 #ifdef BILLBOARDSTRETCHED
@@ -75,7 +76,7 @@ vec3 rotateAlign(vec3 toCamera, vec3 rotatedCorner) {
 	mat3 rotMatrix =  mat3(row0, row1, row2);
 
 	vec3 alignedCorner = rotMatrix * rotatedCorner;
-	return position + alignedCorner; 
+	return (position + worldOffset) + alignedCorner; 
 }
 #endif
 
@@ -109,7 +110,7 @@ void main() {
 		rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 		rotatedCorner.y = 0.;
 
-		vec3 yaxis = position - eyePosition;
+		vec3 yaxis = (position + worldOffset) - eyePosition;
 		yaxis.y = 0.;
 		vec3 worldPos = rotate(normalize(yaxis), rotatedCorner.xyz);
 
@@ -119,7 +120,7 @@ void main() {
 		rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 		rotatedCorner.z = 0.;
 
-		vec3 toCamera = position - eyePosition;	
+		vec3 toCamera = (position + worldOffset) - eyePosition;	
 		vec3 worldPos = rotateAlign(toCamera, rotatedCorner.xyz);
 		
 		vec4 viewPosition = (view * vec4(worldPos, 1.0)); 	
@@ -130,7 +131,7 @@ void main() {
 		rotatedCorner.z = 0.;
 
 		// Expand position
-		vec4 viewPosition = view * vec4(position, 1.0) + rotatedCorner;
+		vec4 viewPosition = view * vec4((position + worldOffset), 1.0) + rotatedCorner;
 	#endif
 
 #else

+ 1 - 0
src/Shaders/gpuUpdateParticles.vertex.fx

@@ -10,6 +10,7 @@ uniform vec2 lifeTime;
 uniform vec2 emitPower;
 uniform vec2 sizeRange;
 uniform vec4 scaleRange;
+
 #ifndef COLORGRADIENTS
 uniform vec4 color1;
 uniform vec4 color2;