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