Skip to content

Commit 9a81d77

Browse files
committed
Line.vert fix for small units
Fixes #7200
1 parent fb068f7 commit 9a81d77

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/webgl/shaders/line.vert

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,33 @@ void main() {
9090

9191
// Moving vertices slightly toward the camera
9292
// 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
9499
// the line disappearing behind the geometry faces.
100+
float zDistance = -posp.z;
101+
float distanceFactor = smoothstep(0.0, 800.0, zDistance);
95102

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
96114
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;
100120

101121
vec4 p = uProjectionMatrix * posp;
102122
vec4 qIn = uProjectionMatrix * posqIn;

0 commit comments

Comments
 (0)