Sfoglia il codice sorgente

Merge pull request #6175 from julien-moreau/master

fixed velocity for motion blur by computing transformations deltas be…
David Catuhe 6 anni fa
parent
commit
2efd05b5da
2 ha cambiato i file con 21 aggiunte e 9 eliminazioni
  1. 14 4
      src/Rendering/geometryBufferRenderer.ts
  2. 7 5
      src/Shaders/geometry.vertex.fx

+ 14 - 4
src/Rendering/geometryBufferRenderer.ts

@@ -104,6 +104,11 @@ export class GeometryBufferRenderer {
      */
     public set enableVelocity(enable: boolean) {
         this._enableVelocity = enable;
+
+        if (!enable) {
+            this._previousTransformationMatrices = {};
+        }
+
         this.dispose();
         this._createRenderTargets();
     }
@@ -218,7 +223,7 @@ export class GeometryBufferRenderer {
             this._cachedDefines = join;
             this._effect = this._scene.getEngine().createEffect("geometry",
                 attribs,
-                ["world", "mBones", "viewProjection", "diffuseMatrix", "view", "previousWorldViewProjection"],
+                ["world", "mBones", "viewProjection", "diffuseMatrix", "view", "previousWorldViewProjection", "currentWorldViewProjection"],
                 ["diffuseSampler"], join,
                 undefined, undefined, undefined,
                 { buffersCount: this._enablePosition ? 3 : 2 });
@@ -299,7 +304,7 @@ export class GeometryBufferRenderer {
             }
 
             // Velocity
-            if (!this._previousTransformationMatrices[mesh.uniqueId]) {
+            if (this._enableVelocity && !this._previousTransformationMatrices[mesh.uniqueId]) {
                 this._previousTransformationMatrices[mesh.uniqueId] = Matrix.Identity();
             }
 
@@ -338,7 +343,10 @@ export class GeometryBufferRenderer {
                 }
 
                 // Velocity
-                this._effect.setMatrix("previousWorldViewProjection", this._previousTransformationMatrices[mesh.uniqueId]);
+                if (this._enableVelocity) {
+                    this._effect.setMatrix("currentWorldViewProjection", mesh.getWorldMatrix().multiply(this._scene.getTransformMatrix()));
+                    this._effect.setMatrix("previousWorldViewProjection", this._previousTransformationMatrices[mesh.uniqueId]);
+                }
 
                 // Draw
                 mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
@@ -346,7 +354,9 @@ export class GeometryBufferRenderer {
             }
 
             // Velocity
-            this._previousTransformationMatrices[mesh.uniqueId] = mesh.getWorldMatrix().multiply(this._scene.getTransformMatrix());
+            if (this._enableVelocity) {
+                this._previousTransformationMatrices[mesh.uniqueId] = mesh.getWorldMatrix().multiply(this._scene.getTransformMatrix());
+            }
         };
 
         this._multiRenderTarget.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {

+ 7 - 5
src/Shaders/geometry.vertex.fx

@@ -31,6 +31,7 @@ varying vec3 vPosition;
 
 #ifdef VELOCITY
 uniform mat4 previousWorldViewProjection;
+uniform mat4 currentWorldViewProjection;
 varying vec4 vCurrentPosition;
 varying vec4 vPreviousPosition;
 #endif
@@ -39,6 +40,12 @@ void main(void)
 {
 #include<instancesVertex>
 
+	#ifdef VELOCITY
+	// Compute velocity before bones computation
+	vCurrentPosition = currentWorldViewProjection * vec4(position, 1.0);
+	vPreviousPosition = previousWorldViewProjection * vec4(position, 1.0);
+	#endif
+
 #include<bonesVertex>
 	vec4 pos = vec4(finalWorld * vec4(position, 1.0));
 
@@ -49,11 +56,6 @@ void main(void)
 	vPosition = pos.xyz / pos.w;
 	#endif
 
-	#ifdef VELOCITY
-	vCurrentPosition = viewProjection * finalWorld * vec4(position, 1.0);
-	vPreviousPosition = previousWorldViewProjection * vec4(position, 1.0);
-	#endif
-
 	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 
 #if defined(ALPHATEST) || defined(BASIC_RENDER)