@@ -90,13 +90,33 @@ void main() {
90
90
91
91
// Moving vertices slightly toward the camera
92
92
// to avoid depth-fighting with the fill triangles.
93
- // This prevents popping effects due to half of
93
+ // A mix of scaling and offsetting is used based on distance
94
+ // Discussion here:
95
+ // https://github.com/processing/p5.js/issues/7200
96
+
97
+ // using a scale <1 moves the lines towards nearby camera
98
+ // in order to prevent popping effects due to half of
94
99
// the line disappearing behind the geometry faces.
100
+ float zDistance = - posp.z;
101
+ float distanceFactor = smoothstep (0.0 , 800.0 , zDistance);
95
102
103
+ // Discussed here:
104
+ // http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=252848
105
+ float scale = mix (1 ., 0.995 , facingCamera);
106
+ float dynamicScale = mix (scale, 1.0 , distanceFactor); // Closer = more scale, farther = less
107
+
108
+ posp.xyz = posp.xyz * dynamicScale;
109
+ posqIn.xyz = posqIn.xyz * dynamicScale;
110
+ posqOut.xyz = posqOut.xyz * dynamicScale;
111
+
112
+ // Moving vertices slightly toward camera when far away
113
+ // https://github.com/processing/p5.js/issues/6956
96
114
float zOffset = mix (- 0.00045 , - 1 ., facingCamera);
97
- posp.z -= zOffset;
98
- posqIn.z -= zOffset;
99
- posqOut.z -= zOffset;
115
+ float dynamicZAdjustment = mix (0.0 , zOffset, distanceFactor); // Closer = less zAdjustment, farther = more
116
+
117
+ posp.z -= dynamicZAdjustment;
118
+ posqIn.z -= dynamicZAdjustment;
119
+ posqOut.z -= dynamicZAdjustment;
100
120
101
121
vec4 p = uProjectionMatrix * posp;
102
122
vec4 qIn = uProjectionMatrix * posqIn;
0 commit comments