Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/webgl/p5.RendererGL.Immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@ p5.RendererGL.prototype._tesselateShape = function() {
this.immediateMode.geometry.vertexNormals[i].z
);
}

// Snap nearly identical consecutive vertex coordinates to avoid libtess
// tessellation artifacts. Common with font.textToContours() output.
const stride = p5.RendererGL.prototype.tessyVertexSize;
const epsilon = 1e-6;
for (const contour of contours) {
for (let i = stride; i < contour.length; i += stride) {
if (Math.abs(contour[i] - contour[i - stride]) < epsilon) {
contour[i] = contour[i - stride];
}
if (Math.abs(contour[i + 1] - contour[i - stride + 1]) < epsilon) {
contour[i + 1] = contour[i - stride + 1];
}
}
}

const polyTriangles = this._triangulate(contours);
const originalVertices = this.immediateMode.geometry.vertices;
this.immediateMode.geometry.vertices = [];
Expand Down
29 changes: 29 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,33 @@ visualSuite('WebGL', function() {
screenshot();
});
});

visualSuite('Tessellation', function() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new visual test doesn’t actually cover this change. I see the same output before and after the patch. Please update the sketch to reproduce the bug which is fixed at your PR.

visualTest(
'Handles nearly identical consecutive vertices from textToContours',
function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(220);
p5.fill(40);
p5.noStroke();

p5.beginShape();
p5.vertex(-20, -20);
p5.vertex(20, -20);
p5.vertex(20, 20);
p5.vertex(-20, 20);

// Inner contour (hole) — vertex 2 has x nudged by 1e-8
p5.beginContour();
p5.vertex(8, -8);
p5.vertex(8, 8);
p5.vertex(-8.00000001, 8); // nearly identical x to next vertex
p5.vertex(-8, -8);
p5.endContour();
p5.endShape(p5.CLOSE);

screenshot();
}
);
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}