Skip to content

Commit 6b6a731

Browse files
committed
Support log and reversed depth
1 parent 443043c commit 6b6a731

File tree

7 files changed

+148
-38
lines changed

7 files changed

+148
-38
lines changed

src/materials/glsl/circle-of-confusion.frag

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ float readDepth(const in vec2 uv) {
3030

3131
#endif
3232

33-
#ifdef LOG_DEPTH
33+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
3434

3535
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
3636
float a = cameraFar / (cameraFar - cameraNear);
3737
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
3838
depth = a + b / d;
3939

40+
#elif defined(USE_REVERSED_DEPTH_BUFFER)
41+
42+
depth = 1.0 - depth;
43+
4044
#endif
4145

4246
return depth;

src/materials/glsl/convolution.box.frag

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <packing>
1414

1515
uniform vec2 cameraNearFar;
16+
#define cameraNear cameraNearFar.x
17+
#define cameraFar cameraNearFar.y
1618

1719
#ifdef NORMAL_DEPTH
1820

@@ -28,7 +30,22 @@
2830

2931
float readDepth(const in vec2 uv) {
3032

31-
return texture2D(normalDepthBuffer, uv).a;
33+
float depth = texture2D(normalDepthBuffer, uv).a;
34+
35+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
36+
37+
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
38+
float a = cameraFar / (cameraFar - cameraNear);
39+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
40+
depth = a + b / d;
41+
42+
#elif defined(USE_REVERSED_DEPTH_BUFFER)
43+
44+
depth = 1.0 - depth;
45+
46+
#endif
47+
48+
return depth;
3249

3350
}
3451

@@ -52,14 +69,29 @@
5269

5370
#if DEPTH_PACKING == 3201
5471

55-
return unpackRGBAToDepth(texture2D(depthBuffer, uv));
72+
float depth = unpackRGBAToDepth(texture2D(depthBuffer, uv));
5673

5774
#else
5875

59-
return texture2D(depthBuffer, uv).r;
76+
float depth = texture2D(depthBuffer, uv).r;
6077

6178
#endif
6279

80+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
81+
82+
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
83+
float a = cameraFar / (cameraFar - cameraNear);
84+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
85+
depth = a + b / d;
86+
87+
#elif defined(USE_REVERSED_DEPTH_BUFFER)
88+
89+
depth = 1.0 - depth;
90+
91+
#endif
92+
93+
return depth;
94+
6395
}
6496

6597
#endif

src/materials/glsl/depth-comparison.frag

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,37 @@ void main() {
2323

2424
// Transform into Cartesian coordinates (not mirrored).
2525
vec2 projTexCoord = (vProjTexCoord.xy / vProjTexCoord.w) * 0.5 + 0.5;
26-
projTexCoord = clamp(projTexCoord, 0.002, 0.998);
2726

2827
#if DEPTH_PACKING == 3201
2928

30-
float fragCoordZ = unpackRGBAToDepth(texture2D(depthBuffer, projTexCoord));
29+
float depth = unpackRGBAToDepth(texture2D(depthBuffer, projTexCoord));
3130

3231
#else
3332

34-
float fragCoordZ = texture2D(depthBuffer, projTexCoord).r;
33+
float depth = texture2D(depthBuffer, projTexCoord).r;
34+
35+
#endif
36+
37+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
38+
39+
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
40+
float a = cameraFar / (cameraFar - cameraNear);
41+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
42+
depth = a + b / d;
43+
44+
#elif defined(USE_REVERSED_DEPTH_BUFFER)
45+
46+
depth = 1.0 - depth;
3547

3648
#endif
3749

3850
#ifdef PERSPECTIVE_CAMERA
3951

40-
float viewZ = perspectiveDepthToViewZ(fragCoordZ, cameraNear, cameraFar);
52+
float viewZ = perspectiveDepthToViewZ(depth, cameraNear, cameraFar);
4153

4254
#else
4355

44-
float viewZ = orthographicDepthToViewZ(fragCoordZ, cameraNear, cameraFar);
56+
float viewZ = orthographicDepthToViewZ(depth, cameraNear, cameraFar);
4557

4658
#endif
4759

src/materials/glsl/depth-mask.frag

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ void main() {
4444

4545
depth.x = texture2D(depthBuffer0, vUv).r;
4646

47-
#ifdef LOG_DEPTH
48-
49-
float d = pow(2.0, depth.x * log2(cameraNearFar.y + 1.0)) - 1.0;
50-
float a = cameraNearFar.y / (cameraNearFar.y - cameraNearFar.x);
51-
float b = cameraNearFar.y * cameraNearFar.x / (cameraNearFar.x - cameraNearFar.y);
52-
depth.x = a + b / d;
53-
54-
#endif
55-
5647
#endif
5748

5849
#if DEPTH_PACKING_1 == 3201
@@ -63,14 +54,23 @@ void main() {
6354

6455
depth.y = texture2D(depthBuffer1, vUv).r;
6556

66-
#ifdef LOG_DEPTH
57+
#endif
58+
59+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
60+
61+
float a = cameraNearFar.y / (cameraNearFar.y - cameraNearFar.x);
62+
float b = cameraNearFar.y * cameraNearFar.x / (cameraNearFar.x - cameraNearFar.y);
63+
float c = log2(cameraNearFar.y + 1.0);
64+
float d = pow(2.0, depth.x * c) - 1.0;
65+
depth.x = a + b / d;
66+
67+
d = pow(2.0, depth.y * c) - 1.0;
68+
depth.y = a + b / d;
6769

68-
float d = pow(2.0, depth.y * log2(cameraNearFar.y + 1.0)) - 1.0;
69-
float a = cameraNearFar.y / (cameraNearFar.y - cameraNearFar.x);
70-
float b = cameraNearFar.y * cameraNearFar.x / (cameraNearFar.x - cameraNearFar.y);
71-
depth.y = a + b / d;
70+
#elif defined(USE_REVERSED_DEPTH_BUFFER)
7271

73-
#endif
72+
depth.x = 1.0 - depth.x;
73+
depth.y = 1.0 - depth.y;
7474

7575
#endif
7676

src/materials/glsl/effect.frag

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,29 @@ float readDepth(const in vec2 uv) {
5353

5454
#if DEPTH_PACKING == 3201
5555

56-
return unpackRGBAToDepth(texture2D(depthBuffer, uv));
56+
float depth = unpackRGBAToDepth(texture2D(depthBuffer, uv));
5757

5858
#else
5959

60-
return texture2D(depthBuffer, uv).r;
60+
float depth = texture2D(depthBuffer, uv).r;
6161

6262
#endif
6363

64+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
65+
66+
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
67+
float a = cameraFar / (cameraFar - cameraNear);
68+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
69+
depth = a + b / d;
70+
71+
#elif defined(USE_REVERSED_DEPTH_BUFFER)
72+
73+
depth = 1.0 - depth;
74+
75+
#endif
76+
77+
return depth;
78+
6479
}
6580

6681
float getViewZ(const in float depth) {

src/materials/glsl/ssao.frag

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <common>
22
#include <packing>
33

4+
uniform vec2 cameraNearFar;
5+
#define cameraNear cameraNearFar.x
6+
#define cameraFar cameraNearFar.y
7+
48
#ifdef NORMAL_DEPTH
59

610
#ifdef GL_FRAGMENT_PRECISION_HIGH
@@ -15,7 +19,18 @@
1519

1620
float readDepth(const in vec2 uv) {
1721

18-
return texture2D(normalDepthBuffer, uv).a;
22+
float depth = texture2D(normalDepthBuffer, uv).a;
23+
24+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
25+
26+
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
27+
float a = cameraFar / (cameraFar - cameraNear);
28+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
29+
depth = a + b / d;
30+
31+
#endif
32+
33+
return depth;
1934

2035
}
2136

@@ -41,14 +56,25 @@
4156

4257
#if DEPTH_PACKING == 3201
4358

44-
return unpackRGBAToDepth(texture2D(depthBuffer, uv));
59+
float depth = unpackRGBAToDepth(texture2D(depthBuffer, uv));
4560

4661
#else
4762

48-
return texture2D(depthBuffer, uv).r;
63+
float depth = texture2D(depthBuffer, uv).r;
4964

5065
#endif
5166

67+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
68+
69+
float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
70+
float a = cameraFar / (cameraFar - cameraNear);
71+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
72+
depth = a + b / d;
73+
74+
#endif
75+
76+
return depth;
77+
5278
}
5379

5480
#endif
@@ -58,7 +84,6 @@ uniform lowp sampler2D noiseTexture;
5884
uniform mat4 inverseProjectionMatrix;
5985
uniform mat4 projectionMatrix;
6086
uniform vec2 texelSize;
61-
uniform vec2 cameraNearFar;
6287

6388
uniform float intensity;
6489
uniform float minRadiusScale;
@@ -75,11 +100,11 @@ float getViewZ(const in float depth) {
75100

76101
#ifdef PERSPECTIVE_CAMERA
77102

78-
return perspectiveDepthToViewZ(depth, cameraNearFar.x, cameraNearFar.y);
103+
return perspectiveDepthToViewZ(depth, cameraNear, cameraFar);
79104

80105
#else
81106

82-
return orthographicDepthToViewZ(depth, cameraNearFar.x, cameraNearFar.y);
107+
return orthographicDepthToViewZ(depth, cameraNear, cameraFar);
83108

84109
#endif
85110

@@ -135,7 +160,7 @@ float getAmbientOcclusion(const in vec3 p, const in vec3 n, const in float depth
135160

136161
#ifdef PERSPECTIVE_CAMERA
137162

138-
float linearSampleDepth = viewZToOrthographicDepth(viewZ, cameraNearFar.x, cameraNearFar.y);
163+
float linearSampleDepth = viewZToOrthographicDepth(viewZ, cameraNear, cameraFar);
139164

140165
#else
141166

@@ -174,6 +199,15 @@ void main() {
174199

175200
vec4 normalDepth = texture2D(normalDepthBuffer, vUv);
176201

202+
#if defined(USE_LOGARITHMIC_DEPTH_BUFFER) || defined(LOG_DEPTH)
203+
204+
float d = pow(2.0, normalDepth.a * log2(cameraFar + 1.0)) - 1.0;
205+
float a = cameraFar / (cameraFar - cameraNear);
206+
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
207+
normalDepth.a = a + b / d;
208+
209+
#endif
210+
177211
#else
178212

179213
vec4 normalDepth = vec4(texture2D(normalBuffer, vUv).xyz, readDepth(vUv));
@@ -186,7 +220,7 @@ void main() {
186220

187221
#ifdef PERSPECTIVE_CAMERA
188222

189-
float linearDepth = viewZToOrthographicDepth(viewZ, cameraNearFar.x, cameraNearFar.y);
223+
float linearDepth = viewZToOrthographicDepth(viewZ, cameraNear, cameraFar);
190224

191225
#else
192226

src/passes/DepthPass.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ export class DepthPass extends Pass {
5151
renderPass.skipShadowMapUpdate = true;
5252
renderPass.ignoreBackground = true;
5353

54-
const clearPass = renderPass.clearPass;
55-
clearPass.overrideClearColor = new Color(0xffffff);
56-
clearPass.overrideClearAlpha = 1;
57-
5854
/**
5955
* A render target that contains the scene depth.
6056
*
@@ -194,4 +190,21 @@ export class DepthPass extends Pass {
194190

195191
}
196192

193+
/**
194+
* Performs initialization tasks.
195+
*
196+
* @param {WebGLRenderer} renderer - The renderer.
197+
* @param {Boolean} alpha - Whether the renderer uses the alpha channel or not.
198+
* @param {Number} frameBufferType - The type of the main frame buffers.
199+
*/
200+
201+
initialize(renderer, alpha, frameBufferType) {
202+
203+
const clearColor = renderer.capabilities.reversedDepthBuffer ? 0x000000 : 0xffffff;
204+
const clearPass = this.renderPass.clearPass;
205+
clearPass.overrideClearColor = new Color(clearColor);
206+
clearPass.overrideClearAlpha = 1;
207+
208+
}
209+
197210
}

0 commit comments

Comments
 (0)