Browse Source

Merge pull request #17442 from gkjohnson/orthographic-logarithmic-depth

Fix logarithmic depth buffer result when rendering with OrthographicCamera
Mr.doob 5 years ago
parent
commit
416e7c858e

+ 6 - 0
src/renderers/shaders/ShaderChunk/common.glsl.js

@@ -106,5 +106,11 @@ float linearToRelativeLuminance( const in vec3 color ) {
 
 	return dot( weights, color.rgb );
 
+}
+
+bool isPerspectiveMatrix( mat4 projectionMatrix ) {
+
+  return projectionMatrix[ 2 ][ 3 ] == - 1.0;
+
 }
 `;

+ 1 - 1
src/renderers/shaders/ShaderChunk/logdepthbuf_fragment.glsl.js

@@ -1,7 +1,7 @@
 export default /* glsl */`
 #if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )
 
-	gl_FragDepthEXT = log2( vFragDepth ) * logDepthBufFC * 0.5;
+	gl_FragDepthEXT = vIsPerspective == 1.0 ? log2( vFragDepth ) * logDepthBufFC * 0.5 : gl_FragCoord.z;
 
 #endif
 `;

+ 1 - 0
src/renderers/shaders/ShaderChunk/logdepthbuf_pars_fragment.glsl.js

@@ -3,6 +3,7 @@ export default /* glsl */`
 
 	uniform float logDepthBufFC;
 	varying float vFragDepth;
+	varying float vIsPerspective;
 
 #endif
 `;

+ 1 - 0
src/renderers/shaders/ShaderChunk/logdepthbuf_pars_vertex.glsl.js

@@ -4,6 +4,7 @@ export default /* glsl */`
 	#ifdef USE_LOGDEPTHBUF_EXT
 
 		varying float vFragDepth;
+		varying float vIsPerspective;
 
 	#else
 

+ 7 - 2
src/renderers/shaders/ShaderChunk/logdepthbuf_vertex.glsl.js

@@ -4,12 +4,17 @@ export default /* glsl */`
 	#ifdef USE_LOGDEPTHBUF_EXT
 
 		vFragDepth = 1.0 + gl_Position.w;
+		vIsPerspective = float( isPerspectiveMatrix( projectionMatrix ) );
 
 	#else
 
-		gl_Position.z = log2( max( EPSILON, gl_Position.w + 1.0 ) ) * logDepthBufFC - 1.0;
+		if ( isPerspectiveMatrix( projectionMatrix ) ) {
 
-		gl_Position.z *= gl_Position.w;
+			gl_Position.z = log2( max( EPSILON, gl_Position.w + 1.0 ) ) * logDepthBufFC - 1.0;
+
+			gl_Position.z *= gl_Position.w;
+
+		}
 
 	#endif
 

+ 1 - 1
src/renderers/shaders/ShaderLib/points_vert.glsl.js

@@ -20,7 +20,7 @@ void main() {
 
 	#ifdef USE_SIZEATTENUATION
 
-		bool isPerspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 );
+		bool isPerspective = isPerspectiveMatrix( projectionMatrix );
 
 		if ( isPerspective ) gl_PointSize *= ( scale / - mvPosition.z );
 

+ 1 - 1
src/renderers/shaders/ShaderLib/sprite_vert.glsl.js

@@ -20,7 +20,7 @@ void main() {
 
 	#ifndef USE_SIZEATTENUATION
 
-		bool isPerspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 );
+		bool isPerspective = isPerspectiveMatrix( projectionMatrix );
 
 		if ( isPerspective ) scale *= - mvPosition.z;