From b5ba89342588ec7d3e7e6858f3579af237d38a05 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 27 Apr 2018 14:18:09 -0400 Subject: [PATCH] Fix z-fighting for classification primitives. --- Source/Scene/ClassificationPrimitive.js | 3 ++- Source/Shaders/Builtin/Functions/vertexLogDepth.glsl | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Source/Scene/ClassificationPrimitive.js b/Source/Scene/ClassificationPrimitive.js index 5252f357f8e..f83253d3aea 100644 --- a/Source/Scene/ClassificationPrimitive.js +++ b/Source/Scene/ClassificationPrimitive.js @@ -530,7 +530,8 @@ define([ // Tesselation on ClassificationPrimitives tends to be low, // which causes problems when interpolating log depth from vertices. // So force computing and writing logarithmic depth in the fragment shader. - var disableGlPositionLogDepth = 'DISABLE_GL_POSITION_LOG_DEPTH'; + // Re-enable at far distances to avoid z-fighting. + var disableGlPositionLogDepth = 'ENABLE_GL_POSITION_LOG_DEPTH_AT_HEIGHT'; var vsSource = new ShaderSource({ defines : [extrudedDefine, disableGlPositionLogDepth], diff --git a/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl b/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl index 1a16d725f5c..fffc82156b3 100644 --- a/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl +++ b/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl @@ -7,6 +7,13 @@ void czm_updatePositionDepth() { #if defined(LOG_DEPTH) && !defined(DISABLE_GL_POSITION_LOG_DEPTH) v_logPositionEC = (czm_inverseProjection * gl_Position).xyz; +#ifdef ENABLE_GL_POSITION_LOG_DEPTH_AT_HEIGHT + if (length(v_logPositionEC) < 2.0e6) + { + return; + } +#endif + gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; gl_Position.z *= gl_Position.w; #endif