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
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
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;
5884uniform mat4 inverseProjectionMatrix;
5985uniform mat4 projectionMatrix;
6086uniform vec2 texelSize;
61- uniform vec2 cameraNearFar;
6287
6388uniform float intensity;
6489uniform 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
0 commit comments