Skip to content

Commit fe0cae3

Browse files
authored
Fix issues with stroke depth ordering (#7369)
* Will high precision floats make lines render on top? * Merge pull request #7206 from TiborUdvari/fix/line.vert-v1.10.0 Line.vert fix for small units * Apply no z offset to lines when facing the camera Line.vert fix for small units * Add test case for ortho strokes
1 parent 5b12d51 commit fe0cae3

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

src/webgl/shaders/line.frag

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
precision mediump int;
1+
precision highp int;
2+
precision highp float;
23

34
uniform vec4 uMaterialColor;
45
uniform int uStrokeCap;

src/webgl/shaders/line.vert

+27-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
#define PROCESSING_LINE_SHADER
2020

21-
precision mediump int;
21+
precision highp int;
22+
precision highp float;
2223

2324
uniform mat4 uModelViewMatrix;
2425
uniform mat4 uProjectionMatrix;
@@ -95,13 +96,33 @@ void main() {
9596

9697
// Moving vertices slightly toward the camera
9798
// to avoid depth-fighting with the fill triangles.
98-
// This prevents popping effects due to half of
99+
// A mix of scaling and offsetting is used based on distance
100+
// Discussion here:
101+
// https://github.com/processing/p5.js/issues/7200
102+
103+
// using a scale <1 moves the lines towards nearby camera
104+
// in order to prevent popping effects due to half of
99105
// the line disappearing behind the geometry faces.
106+
float zDistance = -posp.z;
107+
float distanceFactor = smoothstep(0.0, 800.0, zDistance);
100108

101-
float zOffset = mix(-0.00045, -1., facingCamera);
102-
posp.z -= zOffset;
103-
posqIn.z -= zOffset;
104-
posqOut.z -= zOffset;
109+
// Discussed here:
110+
// http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=252848
111+
float scale = mix(1., 0.995, facingCamera);
112+
float dynamicScale = mix(scale, 1.0, distanceFactor); // Closer = more scale, farther = less
113+
114+
posp.xyz = posp.xyz * dynamicScale;
115+
posqIn.xyz = posqIn.xyz * dynamicScale;
116+
posqOut.xyz = posqOut.xyz * dynamicScale;
117+
118+
// Moving vertices slightly toward camera when far away
119+
// https://github.com/processing/p5.js/issues/6956
120+
float zOffset = mix(0., -1., facingCamera);
121+
float dynamicZAdjustment = mix(0.0, zOffset, distanceFactor); // Closer = less zAdjustment, farther = more
122+
123+
posp.z -= dynamicZAdjustment;
124+
posqIn.z -= dynamicZAdjustment;
125+
posqOut.z -= dynamicZAdjustment;
105126

106127
vec4 p = uProjectionMatrix * posp;
107128
vec4 qIn = uProjectionMatrix * posqIn;

test/unit/visual/cases/webgl.js

+13
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,17 @@ visualSuite('WebGL', function() {
315315
screenshot();
316316
});
317317
});
318+
319+
visualSuite('Strokes', function() {
320+
visualTest('Strokes do not cut into fills in ortho mode', (p5, screenshot) => {
321+
p5.createCanvas(50, 50, p5.WEBGL);
322+
p5.background(220);
323+
p5.stroke(8);
324+
p5.ortho();
325+
p5.rotateX(p5.PI/4);
326+
p5.rotateY(p5.PI/4);
327+
p5.box(30);
328+
screenshot();
329+
})
330+
})
318331
});
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)