From a5a3d1efa3ea9eed17c47756956a3a60e6c3fbe2 Mon Sep 17 00:00:00 2001
From: Kevin Ring <kevin@kotachrome.com>
Date: Wed, 25 Mar 2020 20:26:26 +1100
Subject: [PATCH 1/3] Fix polyline rendering with log depth.

---
 .../PolylineColorAppearanceVS.glsl            |  4 ----
 .../PolylineMaterialAppearanceVS.glsl         |  4 ----
 Source/Shaders/PolylineCommon.glsl            | 19 +++++++++++++------
 Source/Shaders/PolylineVS.glsl                |  4 ----
 4 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl
index 17ce3ec48e27..5bb9d0188828 100644
--- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl
+++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl
@@ -25,8 +25,4 @@ void main()
     float angle;
     vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle);
     gl_Position = czm_viewportOrthographic * positionWC;
-
-#ifdef LOG_DEPTH
-    czm_vertexLogDepth(czm_modelViewProjectionRelativeToEye * p);
-#endif
 }
diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl
index bdfae0e27569..6b61f09369c6 100644
--- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl
+++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl
@@ -27,8 +27,4 @@ void main()
 
     vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle);
     gl_Position = czm_viewportOrthographic * positionWC;
-
-#ifdef LOG_DEPTH
-    czm_vertexLogDepth(czm_modelViewProjectionRelativeToEye * p);
-#endif
 }
diff --git a/Source/Shaders/PolylineCommon.glsl b/Source/Shaders/PolylineCommon.glsl
index d4a4e89a402d..cfdd87cca723 100644
--- a/Source/Shaders/PolylineCommon.glsl
+++ b/Source/Shaders/PolylineCommon.glsl
@@ -3,7 +3,8 @@ void clipLineSegmentToNearPlane(
     vec3 p1,
     out vec4 positionWC,
     out bool clipped,
-    out bool culledByNearPlane)
+    out bool culledByNearPlane,
+    out vec4 clippedPositionEC)
 {
     culledByNearPlane = false;
     clipped = false;
@@ -18,7 +19,7 @@ void clipLineSegmentToNearPlane(
     {
         culledByNearPlane = true;
     }
-    else if (endPoint0Distance < 0.0 && abs(denominator) > czm_epsilon7)
+    else if (endPoint0Distance < 0.0)
     {
         // t = (-plane distance - dot(plane normal, ray origin)) / dot(plane normal, ray direction)
         float t = (czm_currentFrustum.x + p0.z) / denominator;
@@ -33,7 +34,8 @@ void clipLineSegmentToNearPlane(
         }
     }
 
-    positionWC = czm_eyeToWindowCoordinates(vec4(p0, 1.0));
+    clippedPositionEC = vec4(p0, 1.0);
+    positionWC = czm_eyeToWindowCoordinates(clippedPositionEC);
 }
 
 vec4 getPolylineWindowCoordinatesEC(vec4 positionEC, vec4 prevEC, vec4 nextEC, float expandDirection, float width, bool usePrevious, out float angle)
@@ -61,9 +63,14 @@ vec4 getPolylineWindowCoordinatesEC(vec4 positionEC, vec4 prevEC, vec4 nextEC, f
     angle = floor(angle / czm_piOverFour + 0.5) * czm_piOverFour;
 #endif
 
-    clipLineSegmentToNearPlane(prevEC.xyz, positionEC.xyz, p0, clipped, culledByNearPlane);
-    clipLineSegmentToNearPlane(nextEC.xyz, positionEC.xyz, p1, clipped, culledByNearPlane);
-    clipLineSegmentToNearPlane(positionEC.xyz, usePrevious ? prevEC.xyz : nextEC.xyz, endPointWC, clipped, culledByNearPlane);
+    vec4 clippedPositionEC;
+    clipLineSegmentToNearPlane(prevEC.xyz, positionEC.xyz, p0, clipped, culledByNearPlane, clippedPositionEC);
+    clipLineSegmentToNearPlane(nextEC.xyz, positionEC.xyz, p1, clipped, culledByNearPlane, clippedPositionEC);
+    clipLineSegmentToNearPlane(positionEC.xyz, usePrevious ? prevEC.xyz : nextEC.xyz, endPointWC, clipped, culledByNearPlane, clippedPositionEC);
+
+#ifdef LOG_DEPTH
+    czm_vertexLogDepth(czm_projection * clippedPositionEC);
+#endif
 
     if (culledByNearPlane)
     {
diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl
index 818272033561..972e0862fb17 100644
--- a/Source/Shaders/PolylineVS.glsl
+++ b/Source/Shaders/PolylineVS.glsl
@@ -96,8 +96,4 @@ void main()
     v_st = vec2(texCoord, clamp(expandDir, 0.0, 1.0));
     v_width = width;
     v_pickColor = pickColor;
-
-#ifdef LOG_DEPTH
-    czm_vertexLogDepth(czm_modelViewProjectionRelativeToEye * p);
-#endif
 }

From 3521dc78de34536f4a3c64c44019aa1b5791d624 Mon Sep 17 00:00:00 2001
From: Kevin Ring <kevin@kotachrome.com>
Date: Fri, 27 Mar 2020 10:08:47 +1100
Subject: [PATCH 2/3] Update CHANGES.md.

---
 CHANGES.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGES.md b/CHANGES.md
index bdb953e9f767..de67217320ac 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -17,6 +17,7 @@ Change Log
 * Fixed ground atmosphere rendering when using a samller ellipsoid. [#8683](https://github.com/CesiumGS/cesium/issues/8683)
 * Fixed globe incorrectly occluding objects when using a smaller ellipsoid. [#7124](https://github.com/CesiumGS/cesium/issues/7124)
 * Fixed a regression introduced in 1.67 which caused overlapping colored ground geometry to have visual artifacts. [#8694](https://github.com/CesiumGS/cesium/pull/8694)
+* Fixed a clipping problem when viewing a polyline up close with the logarithmic depth buffer enabled, which is the default on most systems. [#8600](https://github.com/CesiumGS/cesium/pull/8600)
 
 ### 1.67.0 - 2020-03-02
 

From aae531a512625d49202459e08e37c4d87fb841db Mon Sep 17 00:00:00 2001
From: Kevin Ring <kevin@kotachrome.com>
Date: Sat, 28 Mar 2020 08:10:39 +1100
Subject: [PATCH 3/3] Update CHANGES.md

Co-Authored-By: Sean Lilley <lilleyse@gmail.com>
---
 CHANGES.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CHANGES.md b/CHANGES.md
index de67217320ac..4ee5a483352b 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -17,7 +17,7 @@ Change Log
 * Fixed ground atmosphere rendering when using a samller ellipsoid. [#8683](https://github.com/CesiumGS/cesium/issues/8683)
 * Fixed globe incorrectly occluding objects when using a smaller ellipsoid. [#7124](https://github.com/CesiumGS/cesium/issues/7124)
 * Fixed a regression introduced in 1.67 which caused overlapping colored ground geometry to have visual artifacts. [#8694](https://github.com/CesiumGS/cesium/pull/8694)
-* Fixed a clipping problem when viewing a polyline up close with the logarithmic depth buffer enabled, which is the default on most systems. [#8600](https://github.com/CesiumGS/cesium/pull/8600)
+* Fixed a clipping problem when viewing a polyline up close with the logarithmic depth buffer enabled, which is the default on most systems. [#8703](https://github.com/CesiumGS/cesium/pull/8703)
 
 ### 1.67.0 - 2020-03-02