Bläddra i källkod

Merge pull request #9357 from CraigFeldspar/fix-db

Fixing prepass + bounding box renderer
David Catuhe 4 år sedan
förälder
incheckning
7c6ee63a73

+ 5 - 10
src/Meshes/mesh.ts

@@ -1861,10 +1861,6 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
             engine.setAlphaMode(this._effectiveMaterial.alphaMode);
         }
 
-        for (let step of scene._beforeRenderingMeshStage) {
-            step.action(this, subMesh, batch);
-        }
-
         var effect: Nullable<Effect>;
         if (this._effectiveMaterial._storeEffectOnSubMeshes) {
             effect = subMesh.effect;
@@ -1872,13 +1868,12 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
             effect = this._effectiveMaterial.getEffect();
         }
 
-        if (!effect) {
-            return this;
+        for (let step of scene._beforeRenderingMeshStage) {
+            step.action(this, subMesh, batch, effect);
         }
 
-        // Render to MRT
-        if (scene.prePassRenderer) {
-            scene.prePassRenderer.bindAttachmentsForEffect(effect, subMesh);
+        if (!effect) {
+            return this;
         }
 
         const effectiveMesh = effectiveMeshReplacement || this._effectiveMesh;
@@ -1938,7 +1933,7 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
         this._effectiveMaterial.unbind();
 
         for (let step of scene._afterRenderingMeshStage) {
-            step.action(this, subMesh, batch);
+            step.action(this, subMesh, batch, effect);
         }
 
         if (this._internalMeshDataInfo._onAfterRenderObservable) {

+ 1 - 1
src/PostProcesses/RenderPipeline/Pipelines/ssao2RenderingPipeline.ts

@@ -190,7 +190,7 @@ export class SSAO2RenderingPipeline extends PostProcessRenderPipeline {
      * @param cameras The array of cameras that the rendering pipeline will be attached to
      * @param forceGeometryBuffer Set to true if you want to use the legacy geometry buffer renderer
      */
-    constructor(name: string, scene: Scene, ratio: any, cameras?: Camera[], forceGeometryBuffer = false) {
+    constructor(name: string, scene: Scene, ratio: any, cameras?: Camera[], forceGeometryBuffer = true) {
         super(scene.getEngine(), name);
 
         this._scene = scene;

+ 2 - 2
src/PostProcesses/motionBlurPostProcess.ts

@@ -81,10 +81,10 @@ export class MotionBlurPostProcess extends PostProcess {
      * @param engine The engine which the post process will be applied. (default: current engine)
      * @param reusable If the post process can be reused on the same frame. (default: false)
      * @param textureType Type of textures used when performing the post process. (default: 0)
-     * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
+     * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: true)
      * @param forceGeometryBuffer If this post process should use geometry buffer instead of prepass (default: false)
      */
-    constructor(name: string, scene: Scene, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false, forceGeometryBuffer = false) {
+    constructor(name: string, scene: Scene, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false, forceGeometryBuffer = true) {
         super(name, "motionBlur", ["motionStrength", "motionScale", "screenSize"], ["velocitySampler"], options, camera, samplingMode, engine, reusable, "#define GEOMETRY_SUPPORTED\n#define SAMPLES 64.0", textureType, undefined, null, blockCompilation);
 
         this._forceGeometryBuffer = forceGeometryBuffer;

+ 2 - 2
src/PostProcesses/screenSpaceReflectionPostProcess.ts

@@ -70,10 +70,10 @@ export class ScreenSpaceReflectionPostProcess extends PostProcess {
      * @param engine The engine which the post process will be applied. (default: current engine)
      * @param reusable If the post process can be reused on the same frame. (default: false)
      * @param textureType Type of textures used when performing the post process. (default: 0)
-     * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
+     * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: true)
      * @param forceGeometryBuffer If this post process should use geometry buffer instead of prepass (default: false)
      */
-    constructor(name: string, scene: Scene, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false, forceGeometryBuffer = false) {
+    constructor(name: string, scene: Scene, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false, forceGeometryBuffer = true) {
         super(name, "screenSpaceReflection", [
             "projection", "view", "threshold", "reflectionSpecularFalloffExponent", "strength", "step", "roughnessFactor"
         ], [

+ 10 - 1
src/Rendering/prePassRenderer.ts

@@ -131,7 +131,7 @@ export class PrePassRenderer {
     }
 
     private _geometryBuffer: Nullable<GeometryBufferRenderer>;
-    private _useGeometryBufferFallback = true;
+    private _useGeometryBufferFallback = false;
     /**
      * Uses the geometry buffer renderer as a fallback for non prepass capable effects
      */
@@ -246,6 +246,15 @@ export class PrePassRenderer {
     }
 
     /**
+     * Restores attachments for single texture draw.
+     */
+    public restoreAttachments() {
+        if (this.enabled && this._defaultAttachments) {
+            this._engine.bindAttachments(this._defaultAttachments);
+        }
+    }
+
+    /**
      * @hidden
      */
     public _beforeCameraDraw() {

+ 26 - 0
src/Rendering/prePassRendererSceneComponent.ts

@@ -3,6 +3,10 @@ import { Scene } from "../scene";
 import { ISceneComponent, SceneComponentConstants } from "../sceneComponent";
 import { PrePassRenderer } from "./prePassRenderer";
 import { Logger } from "../Misc/logger";
+import { AbstractMesh } from "../Meshes/abstractMesh";
+import { SubMesh } from "../Meshes/subMesh";
+import { _InstancesBatch } from "../Meshes/mesh";
+import { Effect } from "../Materials/effect";
 
 declare module "../abstractScene" {
     export interface AbstractScene {
@@ -96,6 +100,8 @@ export class PrePassRendererSceneComponent implements ISceneComponent {
         this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_PREPASS, this, this._beforeCameraDraw);
         this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_PREPASS, this, this._afterCameraDraw);
         this.scene._beforeClearStage.registerStep(SceneComponentConstants.STEP_BEFORECLEARSTAGE_PREPASS, this, this._beforeClearStage);
+        this.scene._beforeRenderingMeshStage.registerStep(SceneComponentConstants.STEP_BEFORERENDERINGMESH_PREPASS, this, this._beforeRenderingMeshStage);
+        this.scene._afterRenderingMeshStage.registerStep(SceneComponentConstants.STEP_AFTERRENDERINGMESH_PREPASS, this, this._afterRenderingMeshStage);
     }
 
     private _beforeCameraDraw() {
@@ -116,6 +122,26 @@ export class PrePassRendererSceneComponent implements ISceneComponent {
         }
     }
 
+    private _beforeRenderingMeshStage(mesh: AbstractMesh, subMesh: SubMesh, batch: _InstancesBatch, effect: Nullable<Effect>) {
+        if (!effect) {
+            return;
+        }
+
+        // Render to MRT
+        const scene = mesh.getScene();
+        if (scene.prePassRenderer) {
+            scene.prePassRenderer.bindAttachmentsForEffect(effect, subMesh);
+        }
+    }
+
+    private _afterRenderingMeshStage(mesh: AbstractMesh) {
+        const scene = mesh.getScene();
+
+        if (scene.prePassRenderer) {
+            scene.prePassRenderer.restoreAttachments();
+        }
+    }
+
     /**
      * Rebuilds the elements related to this component in case of
      * context lost for instance.

+ 6 - 3
src/sceneComponent.ts

@@ -10,6 +10,7 @@ import { PickingInfo } from "./Collisions/pickingInfo";
 import { AbstractScene } from "./abstractScene";
 
 declare type Mesh = import("./Meshes/mesh").Mesh;
+declare type Effect = import("./Materials/effect").Effect;
 
 /**
  * Groups all the scene component constants in one place to ease maintenance.
@@ -52,9 +53,11 @@ export class SceneComponentConstants {
 
     public static readonly STEP_BEFORERENDERTARGETDRAW_LAYER = 0;
 
-    public static readonly STEP_BEFORERENDERINGMESH_OUTLINE = 0;
+    public static readonly STEP_BEFORERENDERINGMESH_PREPASS = 0;
+    public static readonly STEP_BEFORERENDERINGMESH_OUTLINE = 1;
 
-    public static readonly STEP_AFTERRENDERINGMESH_OUTLINE = 0;
+    public static readonly STEP_AFTERRENDERINGMESH_PREPASS = 0;
+    public static readonly STEP_AFTERRENDERINGMESH_OUTLINE = 1;
 
     public static readonly STEP_AFTERRENDERINGGROUPDRAW_EFFECTLAYER_DRAW = 0;
     public static readonly STEP_AFTERRENDERINGGROUPDRAW_BOUNDINGBOXRENDERER = 1;
@@ -186,7 +189,7 @@ export type RenderingGroupStageAction = (renderingGroupId: number) => void;
 /**
  * Strong typing of a Mesh Render related stage step action
  */
-export type RenderingMeshStageAction = (mesh: Mesh, subMesh: SubMesh, batch: _InstancesBatch) => void;
+export type RenderingMeshStageAction = (mesh: Mesh, subMesh: SubMesh, batch: _InstancesBatch, effect: Nullable<Effect>) => void;
 
 /**
  * Strong typing of a simple stage step action