Explorar o código

motion blur properly works with PBR

Benjamin Guignabert %!s(int64=5) %!d(string=hai) anos
pai
achega
996fbc0525

+ 11 - 7
src/Materials/PBR/pbrAdditionnalPrePassConfiguration.ts

@@ -1,10 +1,11 @@
 import { UniformBuffer } from "../../Materials/uniformBuffer";
 import { Matrix } from "../../Maths/math.vector";
+import { Mesh } from "../../Meshes/mesh";
 import { Scene } from "../../scene";
 import { PrePassRenderer } from "../../Rendering/prePassRenderer";
 
 export class PBRAdditionnalPrePassConfiguration {
-    public previousWorld: Matrix;
+    public previousWorldMatrices: { [index: number]: Matrix } = {};
     public previousViewProjection: Matrix;
 
     constructor() {
@@ -45,18 +46,21 @@ export class PBRAdditionnalPrePassConfiguration {
      * @param lodBasedMicrosurface defines whether the material relies on lod based microsurface or not.
      * @param realTimeFiltering defines whether the textures should be filtered on the fly.
      */
-    public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene, world: Matrix, isFrozen: boolean): void {
+    public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene, mesh: Mesh, world: Matrix, isFrozen: boolean): void {
 
         if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
             if (scene.prePassRenderer && scene.prePassRenderer.enabled) {
                 // TODO : cache for better performance
                 if (scene.prePassRenderer.getIndex(PrePassRenderer.VELOCITY_TEXTURE_TYPE) !== -1) {
-                    if (this.previousWorld) {
-                        uniformBuffer.updateMatrix("previousWorld", this.previousWorld);
-                        uniformBuffer.updateMatrix("previousViewProjection", this.previousViewProjection);
+                    if (!this.previousWorldMatrices[mesh.uniqueId]) {
+                        this.previousWorldMatrices[mesh.uniqueId] = Matrix.Identity();
+                        // TODO move out in own test
+                        this.previousViewProjection = scene.getTransformMatrix();
                     }
-
-                    this.previousWorld = world.clone();
+                    uniformBuffer.updateMatrix("previousWorld", this.previousWorldMatrices[mesh.uniqueId]);
+                    uniformBuffer.updateMatrix("previousViewProjection", this.previousViewProjection);
+                    
+                    this.previousWorldMatrices[mesh.uniqueId] = world.clone();
                     this.previousViewProjection = scene.getTransformMatrix().clone();
                 }
 

+ 2 - 2
src/Materials/PBR/pbrBaseMaterial.ts

@@ -1758,10 +1758,12 @@ export abstract class PBRBaseMaterial extends PushMaterial {
         }
 
         this._activeEffect = effect;
+        let ubo = this._uniformBuffer;
 
         // Matrices
         if (!defines.INSTANCES || defines.THIN_INSTANCES) {
             this.bindOnlyWorldMatrix(world);
+            this.additionnalPrePass.bindForSubMesh(ubo, scene, mesh, world, this.isFrozen);
         }
 
         // Normal Matrix
@@ -1776,7 +1778,6 @@ export abstract class PBRBaseMaterial extends PushMaterial {
         MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
 
         let reflectionTexture: Nullable<BaseTexture> = null;
-        let ubo = this._uniformBuffer;
         if (mustRebind) {
             var engine = scene.getEngine();
             ubo.bindToEffect(effect, "Material");
@@ -2019,7 +2020,6 @@ export abstract class PBRBaseMaterial extends PushMaterial {
             this.clearCoat.bindForSubMesh(ubo, scene, engine, this._disableBumpMap, this.isFrozen, this._invertNormalMapX, this._invertNormalMapY);
             this.anisotropy.bindForSubMesh(ubo, scene, this.isFrozen);
             this.sheen.bindForSubMesh(ubo, scene, this.isFrozen);
-            this.additionnalPrePass.bindForSubMesh(ubo, scene, world, this.isFrozen);
 
             // Clip plane
             MaterialHelper.BindClipPlane(this._activeEffect, scene);

+ 0 - 74
src/Materials/PBR/pbrPrePassConfiguration.ts

@@ -1,74 +0,0 @@
-import { UniformBuffer } from "../../Materials/uniformBuffer";
-import { Matrix } from "../../Maths/math.vector";
-import { Scene } from "../../scene";
-import { PrePassRenderer } from "../../Rendering/prePassRenderer";
-
-export class PBRAdditionnalPrePassConfiguration {
-    public previousWorld: Matrix;
-    public previousViewProjection: Matrix;
-
-    constructor() {
-
-    }
-
-    /**
-     * Add the required uniforms to the current buffer.
-     * @param uniformBuffer defines the current uniform buffer.
-     */
-    public static PrepareUniformBuffer(uniformBuffer: UniformBuffer): void {
-        uniformBuffer.addUniform("previousWorld", 16);
-        uniformBuffer.addUniform("previousViewProjection", 16);
-    }
-
-    /**
-     * Add the required uniforms to the current list.
-     * @param uniforms defines the current uniform list.
-     */
-    public static AddUniforms(uniforms: string[]): void {
-        uniforms.push("previousWorld", "previousViewProjection");
-    }
-
-    /**
-     * Add the required samplers to the current list.
-     * @param samplers defines the current sampler list.
-     */
-    public static AddSamplers(samplers: string[]): void {
-        // pass
-    }
-
-    /**
-     * Binds the material data.
-     * @param uniformBuffer defines the Uniform buffer to fill in.
-     * @param scene defines the scene the material belongs to.
-     * @param engine defines the engine the material belongs to.
-     * @param isFrozen defines whether the material is frozen or not.
-     * @param lodBasedMicrosurface defines whether the material relies on lod based microsurface or not.
-     * @param realTimeFiltering defines whether the textures should be filtered on the fly.
-     */
-    public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene, world: Matrix, isFrozen: boolean): void {
-
-        if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
-            if (scene.prePassRenderer && scene.prePassRenderer.enabled) {
-                // TODO : cache for better performance
-                if (scene.prePassRenderer.getIndex(PrePassRenderer.VELOCITY_TEXTURE_TYPE) !== -1) {
-                    if (this.previousWorld) {
-                        uniformBuffer.updateMatrix("previousWorld", this.previousWorld);
-                        uniformBuffer.updateMatrix("previousViewProjection", this.previousViewProjection);
-                    }
-
-                    this.previousWorld = world.clone();
-                    this.previousViewProjection = scene.getTransformMatrix().clone();
-                }
-
-                // if (scene.prePassRenderer.getIndex(PrePassRenderer.REFLECTIVITY_TEXTURE_TYPE) !== -1) {
-                //     // Roughness
-                //     if (material instanceof PBRMaterial && material.reflectivityTexture) {
-                //         this._effect.setMatrix("reflectivityMatrix", material.reflectivityTexture.getTextureMatrix());
-                //         this._effect.setTexture("reflectivitySampler", material.reflectivityTexture);
-                //     }
-                // }
-            }
-        }
-
-    }
-}

+ 0 - 18
src/Rendering/prePassRenderer.ts

@@ -5,7 +5,6 @@ import { Constants } from "../Engines/constants";
 import { ImageProcessingPostProcess } from "../PostProcesses/imageProcessingPostProcess";
 import { PostProcess } from "../PostProcesses/postProcess";
 import { Effect } from "../Materials/effect";
-import { Logger } from '../Misc/logger';
 import { _DevTools } from '../Misc/devTools';
 import { Color4 } from "../Maths/math.color";
 import { PrePassEffectConfiguration } from "./prePassEffectConfiguration";
@@ -287,16 +286,6 @@ export class PrePassRenderer {
         }
     }
 
-    private _checkTextureType(type: number) : boolean {
-        if (type < 0 || type >= this._textureFormats.length) {
-            Logger.Error("PrePassRenderer : Unknown texture type");
-            return false;
-        }
-
-        return true;
-
-    }
-
     /**
      * Adds an effect configuration
      */
@@ -318,10 +307,6 @@ export class PrePassRenderer {
      * @return The index
      */
     public getIndex(type: number) : number {
-        if (!this._checkTextureType(type)) {
-            return -1;
-        }
-
         return this._textureIndices[type];
     }
 
@@ -413,9 +398,6 @@ export class PrePassRenderer {
     private _enableTextures(types: number[]) {
         for (let i = 0; i < types.length; i++) {
             let type = types[i];
-            if (!this._checkTextureType(type)) {
-                return;
-            }
 
             if (this._textureIndices[type] === -1) {
                 this._textureIndices[type] = this._mrtLayout.length;