浏览代码

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 6 年之前
父节点
当前提交
358be8af1e

+ 31 - 3
src/Rendering/depthRenderer.ts

@@ -25,6 +25,11 @@ export class DepthRenderer {
     private _scene: Scene;
     private _depthMap: RenderTargetTexture;
     private _effect: Effect;
+    private readonly _storeNonLinearDepth: boolean;
+    private readonly _clearColor: Color4;
+
+    /** Get if the depth renderer is using packed depth or not */
+    public readonly isPacked: boolean;
 
     private _cachedDefines: string;
     private _camera: Nullable<Camera>;
@@ -46,16 +51,29 @@ export class DepthRenderer {
      * @param scene The scene the renderer belongs to
      * @param type The texture type of the depth map (default: Engine.TEXTURETYPE_FLOAT)
      * @param camera The camera to be used to render the depth map (default: scene's active camera)
+     * @param storeNonLinearDepth Defines whether the depth is stored linearly like in Babylon Shadows or directly like glFragCoord.z
      */
-    constructor(scene: Scene, type: number = Constants.TEXTURETYPE_FLOAT, camera: Nullable<Camera> = null) {
+    constructor(scene: Scene, type: number = Constants.TEXTURETYPE_FLOAT, camera: Nullable<Camera> = null, storeNonLinearDepth = false) {
         this._scene = scene;
+        this._storeNonLinearDepth = storeNonLinearDepth;
+        this.isPacked = type === Constants.TEXTURETYPE_UNSIGNED_BYTE;
+        if (this.isPacked) {
+            this._clearColor = new Color4(1.0, 1.0, 1.0, 1.0);
+        }
+        else {
+            this._clearColor = new Color4(1.0, 0.0, 0.0, 1.0);
+        }
+
         DepthRenderer._SceneComponentInitialization(this._scene);
 
         this._camera = camera;
         var engine = scene.getEngine();
 
         // Render target
-        this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type);
+        var format = (this.isPacked || engine.webGLVersion === 1) ? Constants.TEXTUREFORMAT_RGBA : Constants.TEXTUREFORMAT_R;
+        this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type,
+            false, undefined, undefined, undefined, undefined,
+            format);
         this._depthMap.wrapU = Texture.CLAMP_ADDRESSMODE;
         this._depthMap.wrapV = Texture.CLAMP_ADDRESSMODE;
         this._depthMap.refreshRate = 1;
@@ -69,7 +87,7 @@ export class DepthRenderer {
 
         // set default depth value to 1.0 (far away)
         this._depthMap.onClearObservable.add((engine) => {
-            engine.clear(new Color4(1.0, 1.0, 1.0, 1.0), true, true, true);
+            engine.clear(this._clearColor, true, true, true);
         });
 
         // Custom render function
@@ -214,6 +232,16 @@ export class DepthRenderer {
             MaterialHelper.PushAttributesForInstances(attribs);
         }
 
+        // None linear depth
+        if (this._storeNonLinearDepth) {
+            defines.push("#define NONLINEARDEPTH");
+        }
+
+        // Float Mode
+        if (this.isPacked) {
+            defines.push("#define PACKED");
+        }
+
         // Get correct effect
         var join = defines.join("\n");
         if (this._cachedDefines !== join) {

+ 5 - 4
src/Rendering/depthRendererSceneComponent.ts

@@ -15,9 +15,10 @@ declare module "../scene" {
         /**
          * Creates a depth renderer a given camera which contains a depth map which can be used for post processing.
          * @param camera The camera to create the depth renderer on (default: scene's active camera)
+         * @param storeNonLinearDepth Defines whether the depth is stored linearly like in Babylon Shadows or directly like glFragCoord.z
          * @returns the created depth renderer
          */
-        enableDepthRenderer(camera?: Nullable<Camera>): DepthRenderer;
+        enableDepthRenderer(camera?: Nullable<Camera>, storeNonLinearDepth?: boolean): DepthRenderer;
 
         /**
          * Disables a depth renderer for a given camera
@@ -27,7 +28,7 @@ declare module "../scene" {
     }
 }
 
-Scene.prototype.enableDepthRenderer = function(camera?: Nullable<Camera>): DepthRenderer {
+Scene.prototype.enableDepthRenderer = function(camera?: Nullable<Camera>, storeNonLinearDepth = false): DepthRenderer {
     camera = camera || this.activeCamera;
     if (!camera) {
         throw "No camera available to enable depth renderer";
@@ -43,9 +44,9 @@ Scene.prototype.enableDepthRenderer = function(camera?: Nullable<Camera>): Depth
         else if (this.getEngine().getCaps().textureFloatRender) {
             textureType = Constants.TEXTURETYPE_FLOAT;
         } else {
-            throw "Depth renderer does not support int texture type";
+            textureType = Constants.TEXTURETYPE_UNSIGNED_BYTE;
         }
-        this._depthRenderer[camera.id] = new DepthRenderer(this, textureType, camera);
+        this._depthRenderer[camera.id] = new DepthRenderer(this, textureType, camera, storeNonLinearDepth);
     }
 
     return this._depthRenderer[camera.id];

+ 16 - 0
src/Shaders/ShadersInclude/packingFunctions.fx

@@ -0,0 +1,16 @@
+vec4 pack(float depth)
+{
+    const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
+    const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
+
+    vec4 res = fract(depth * bit_shift);
+    res -= res.xxyz * bit_mask;
+
+    return res;
+}
+
+float unpack(vec4 color)
+{
+    const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
+    return dot(color, bit_shift);
+}

+ 1 - 0
src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx

@@ -1,5 +1,6 @@
 #ifdef SHADOWS
     #ifndef SHADOWFLOAT
+        // Dupplicate to prevent include in include issues
         float unpack(vec4 color)
         {
             const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);

+ 17 - 1
src/Shaders/depth.fragment.fx

@@ -5,6 +5,10 @@ uniform sampler2D diffuseSampler;
 
 varying float vDepthMetric;
 
+#ifdef PACKED
+	#include<packingFunctions>
+#endif
+
 void main(void)
 {
 #ifdef ALPHATEST
@@ -12,5 +16,17 @@ void main(void)
 		discard;
 #endif
 
-	gl_FragColor = vec4(vDepthMetric, vDepthMetric * vDepthMetric, 0.0, 1.0);
+#ifdef NONLINEARDEPTH
+	#ifdef PACKED
+		gl_FragColor = pack(gl_FragCoord.z);
+	#else
+		gl_FragColor = vec4(gl_FragCoord.z, 0.0, 0.0, 0.0);
+	#endif
+#else
+	#ifdef PACKED
+		gl_FragColor = pack(vDepthMetric);
+	#else
+		gl_FragColor = vec4(vDepthMetric, 0.0, 0.0, 1.0);
+	#endif
+#endif
 }

+ 1 - 16
src/Shaders/kernelBlur.fragment.fx

@@ -23,22 +23,7 @@ varying vec2 sampleCenter;
 #include<kernelBlurVaryingDeclaration>[0..varyingCount]
 
 #ifdef PACKEDFLOAT
-    vec4 pack(float depth)
-    {
-        const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-        const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-        vec4 res = fract(depth * bit_shift);
-        res -= res.xxyz * bit_mask;
-
-        return res;
-    }
-
-    float unpack(vec4 color)
-    {
-        const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
-        return dot(color, bit_shift);
-    }
+	#include<packingFunctions>
 #endif
 
 void main(void)

+ 1 - 10
src/Shaders/shadowMap.fragment.fx

@@ -1,14 +1,5 @@
 #ifndef FLOAT
-vec4 pack(float depth)
-{
-    const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-    const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-    vec4 res = fract(depth * bit_shift);
-    res -= res.xxyz * bit_mask;
-
-    return res;
-}
+	#include<packingFunctions>
 #endif
 
 varying float vDepthMetric;

+ 1 - 9
src/Shaders/standard.fragment.fx

@@ -212,15 +212,7 @@ uniform vec2 dsOffsets[9];
 uniform float halfDestPixelSize;
 
 #ifdef FINAL_DOWN_SAMPLER
-vec4 pack(float value) {
-	const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
-	const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
-
-	vec4 res = fract(value * bit_shift);
-	res -= res.xxyz * bit_mask;
-
-	return res;
-}
+	#include<packingFunctions>
 #endif
 
 void main()

二进制
tests/validation/ReferenceImages/depthRenderer.png