Procházet zdrojové kódy

Pass the world position to the fragment shader

Popov72 před 5 roky
rodič
revize
8aad2f0d59

+ 8 - 1
src/Particles/particleSystem.ts

@@ -1656,6 +1656,8 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
                     defines.push("#define BILLBOARDSTRETCHED");
                     defines.push("#define BILLBOARDSTRETCHED");
                     break;
                     break;
                 case ParticleSystem.BILLBOARDMODE_ALL:
                 case ParticleSystem.BILLBOARDMODE_ALL:
+                    defines.push("#define BILLBOARDMODE_ALL");
+                    break;
                 default:
                 default:
                     break;
                     break;
             }
             }
@@ -1902,11 +1904,16 @@ export class ParticleSystem extends BaseParticleSystem implements IDisposable, I
             effect.setTexture("rampSampler", this._rampGradientsTexture);
             effect.setTexture("rampSampler", this._rampGradientsTexture);
         }
         }
 
 
+        const defines = effect.defines;
+
         if (this._scene.clipPlane || this._scene.clipPlane2 || this._scene.clipPlane3 || this._scene.clipPlane4 || this._scene.clipPlane5 || this._scene.clipPlane6) {
         if (this._scene.clipPlane || this._scene.clipPlane2 || this._scene.clipPlane3 || this._scene.clipPlane4 || this._scene.clipPlane5 || this._scene.clipPlane6) {
+            MaterialHelper.BindClipPlane(effect, this._scene);
+        }
+
+        if (defines.indexOf("#define BILLBOARDMODE_ALL") >= 0) {
             var invView = viewMatrix.clone();
             var invView = viewMatrix.clone();
             invView.invert();
             invView.invert();
             effect.setMatrix("invView", invView);
             effect.setMatrix("invView", invView);
-            MaterialHelper.BindClipPlane(effect, this._scene);
         }
         }
 
 
         engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
         engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);

+ 14 - 11
src/Shaders/gpuRenderParticles.vertex.fx

@@ -28,8 +28,9 @@ in vec2 uv;
 
 
 out vec2 vUV;
 out vec2 vUV;
 out vec4 vColor;
 out vec4 vColor;
+out vec3 vPositionW;
 
 
-#if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
+#if defined(BILLBOARD) && !defined(BILLBOARDY) && !defined(BILLBOARDSTRETCHED)
 uniform mat4 invView;
 uniform mat4 invView;
 #endif
 #endif
 
 
@@ -99,7 +100,7 @@ void main() {
 		vec2 uvOffset = vec2(uv.x , 1.0 - uv.y);
 		vec2 uvOffset = vec2(uv.x , 1.0 - uv.y);
 		vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
 		vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
 	#else
 	#else
-   	vUV = uv;
+   	    vUV = uv;
 	#endif
 	#endif
   float ratio = age / life;
   float ratio = age / life;
 #ifdef COLORGRADIENTS
 #ifdef COLORGRADIENTS
@@ -121,18 +122,18 @@ void main() {
 
 
 		vec3 yaxis = (position + worldOffset) - eyePosition;
 		vec3 yaxis = (position + worldOffset) - eyePosition;
 		yaxis.y = 0.;
 		yaxis.y = 0.;
-		vec3 worldPos = rotate(normalize(yaxis), rotatedCorner.xyz);
+		vPositionW = rotate(normalize(yaxis), rotatedCorner.xyz);
 
 
-		vec4 viewPosition = (view * vec4(worldPos, 1.0));
+		vec4 viewPosition = (view * vec4(vPositionW, 1.0));
 	#elif defined(BILLBOARDSTRETCHED)
 	#elif defined(BILLBOARDSTRETCHED)
 		rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 		rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 		rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 		rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 		rotatedCorner.z = 0.;
 		rotatedCorner.z = 0.;
 
 
 		vec3 toCamera = (position + worldOffset) - eyePosition;
 		vec3 toCamera = (position + worldOffset) - eyePosition;
-		vec3 worldPos = rotateAlign(toCamera, rotatedCorner.xyz);
+		vPositionW = rotateAlign(toCamera, rotatedCorner.xyz);
 
 
-		vec4 viewPosition = (view * vec4(worldPos, 1.0));
+		vec4 viewPosition = (view * vec4(vPositionW, 1.0));
 	#else
 	#else
 		// Rotate
 		// Rotate
 		rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 		rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
@@ -145,26 +146,28 @@ void main() {
 		#else
 		#else
 			vec4 viewPosition = view * vec4((position + worldOffset), 1.0) + rotatedCorner;
 			vec4 viewPosition = view * vec4((position + worldOffset), 1.0) + rotatedCorner;
 		#endif
 		#endif
+
+        vPositionW = (invView * viewPosition).xyz;
 	#endif
 	#endif
 
 
 #else
 #else
-  // Rotate
+    // Rotate
 	vec3 rotatedCorner;
 	vec3 rotatedCorner;
 	rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 	rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 	rotatedCorner.y = 0.;
 	rotatedCorner.y = 0.;
 	rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 	rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 
 
 	vec3 yaxis = normalize(initialDirection);
 	vec3 yaxis = normalize(initialDirection);
-	vec3 worldPos = rotate(yaxis, rotatedCorner);
+	vPositionW = rotate(yaxis, rotatedCorner);
 
 
-  // Expand position
-  vec4 viewPosition = view * vec4(worldPos, 1.0);
+    // Expand position
+    vec4 viewPosition = view * vec4(vPositionW, 1.0);
 #endif
 #endif
 	gl_Position = projection * viewPosition;
 	gl_Position = projection * viewPosition;
 
 
 	// Clip plane
 	// Clip plane
 #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
 #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
-	vec4 worldPos = invView * viewPosition;
+    vec4 worldPos = vec4(vPositionW, 1.0);
 #endif
 #endif
 	#include<clipPlaneVertex>
 	#include<clipPlaneVertex>
 }
 }

+ 11 - 8
src/Shaders/particles.vertex.fx

@@ -29,12 +29,13 @@ uniform vec3 particlesInfos; // x (number of rows) y(number of columns) z(rowSiz
 // Output
 // Output
 varying vec2 vUV;
 varying vec2 vUV;
 varying vec4 vColor;
 varying vec4 vColor;
+varying vec3 vPositionW;
 
 
 #ifdef RAMPGRADIENT
 #ifdef RAMPGRADIENT
 varying vec4 remapRanges;
 varying vec4 remapRanges;
 #endif
 #endif
 
 
-#if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
+#if defined(BILLBOARD) && !defined(BILLBOARDY) && !defined(BILLBOARDSTRETCHED)
 uniform mat4 invView;
 uniform mat4 invView;
 #endif
 #endif
 #include<clipPlaneVertexDeclaration>
 #include<clipPlaneVertexDeclaration>
@@ -91,24 +92,26 @@ void main(void) {
 	vec3 yaxis = position - eyePosition;
 	vec3 yaxis = position - eyePosition;
 	yaxis.y = 0.;
 	yaxis.y = 0.;
 
 
-	vec3 worldPos = rotate(normalize(yaxis), rotatedCorner);
+	vPositionW = rotate(normalize(yaxis), rotatedCorner);
 
 
-	vec3 viewPos = (view * vec4(worldPos, 1.0)).xyz;
+	vec3 viewPos = (view * vec4(vPositionW, 1.0)).xyz;
 #elif defined(BILLBOARDSTRETCHED)
 #elif defined(BILLBOARDSTRETCHED)
 	rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 	rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 	rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 	rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 	rotatedCorner.z = 0.;
 	rotatedCorner.z = 0.;
 
 
 	vec3 toCamera = position - eyePosition;
 	vec3 toCamera = position - eyePosition;
-	vec3 worldPos = rotateAlign(toCamera, rotatedCorner);
+	vPositionW = rotateAlign(toCamera, rotatedCorner);
 
 
-	vec3 viewPos = (view * vec4(worldPos, 1.0)).xyz;
+	vec3 viewPos = (view * vec4(vPositionW, 1.0)).xyz;
 #else
 #else
 	rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 	rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
 	rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 	rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
 	rotatedCorner.z = 0.;
 	rotatedCorner.z = 0.;
 
 
 	vec3 viewPos = (view * vec4(position, 1.0)).xyz + rotatedCorner;
 	vec3 viewPos = (view * vec4(position, 1.0)).xyz + rotatedCorner;
+
+    vPositionW = (invView * vec4(viewPos, 1)).xyz;
 #endif
 #endif
 
 
 #ifdef RAMPGRADIENT
 #ifdef RAMPGRADIENT
@@ -125,9 +128,9 @@ void main(void) {
 	rotatedCorner.y = 0.;
 	rotatedCorner.y = 0.;
 
 
 	vec3 yaxis = normalize(direction);
 	vec3 yaxis = normalize(direction);
-	vec3 worldPos = rotate(yaxis, rotatedCorner);
+	vPositionW = rotate(yaxis, rotatedCorner);
 
 
-	gl_Position = projection * view * vec4(worldPos, 1.0);
+	gl_Position = projection * view * vec4(vPositionW, 1.0);
 #endif
 #endif
 	vColor = color;
 	vColor = color;
 
 
@@ -144,7 +147,7 @@ void main(void) {
 
 
 	// Clip plane
 	// Clip plane
 #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
 #if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
-	vec4 worldPos = invView * vec4(viewPos, 1.0);
+    vec4 worldPos = vec4(vPositionW, 1.0);
 #endif
 #endif
 	#include<clipPlaneVertex>
 	#include<clipPlaneVertex>