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
26 changes: 26 additions & 0 deletions src/webgl/ShapeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ export class ShapeBuilder {
}
}

// Normalize nearly identical consecutive vertices to prevent tessellation artifacts
// This addresses numerical precision issues in libtess when consecutive vertices
// have coordinates that are almost (but not exactly) equal (e.g., differing by ~1e-8)
const epsilon = 1e-6;
for (const contour of contours) {
const stride = this.tessyVertexSize;
for (let i = stride; i < contour.length; i += stride) {
const prevX = contour[i - stride];
const prevY = contour[i - stride + 1];
const prevZ = contour[i - stride + 2];
const currX = contour[i];
const currY = contour[i + 1];
const currZ = contour[i + 2];

if (Math.abs(currX - prevX) < epsilon) {
contour[i] = prevX;
}
if (Math.abs(currY - prevY) < epsilon) {
contour[i + 1] = prevY;
}
if (Math.abs(currZ - prevZ) < epsilon) {
contour[i + 2] = prevZ;
}
Comment on lines +336 to +338
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was there a reason for snapping the z coordinates as well? I don't think we need to do that for z?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm actually not 100% sure how libtess works internally, but maybe we could update my test sketch from the original issue to like, swap the x and z axes, and then draw it with a 90 degree rotation to see if we notice the same issue happening? If so, we probably need to do this for z too, otherwise maybe we don't

}
}

const polyTriangles = this._triangulate(contours);

// If there were no valid faces, we still want to use the original vertices
Expand Down
31 changes: 31 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,35 @@ visualSuite('WebGL', function() {
screenshot();
});
});

visualSuite('Tessellation', function() {
Copy link
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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Worst case we can just use the exact same tests as in the original issue. I don't fully understand either why this case seems OK but not the one in the issue haha. I wonder if my numbers were bigger in the original and that causes more numerical precision issues

Copy link
Author

@avinxshKD avinxshKD Nov 5, 2025

Choose a reason for hiding this comment

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

Worst case we can just use the exact same tests as in the original issue. I don't fully understand either why this case seems OK but not the one in the issue haha. I wonder if my numbers were bigger in the original and that causes more numerical precision issues

Hello @perminder-17 @davepagurek thanks for the review

  1. As you said that Visual test not showing the bug
    Should I use the exact textToContours() example from issue [p5.js 2.0 Bug Report]: Tessellation of beginShape/endShape with multiple contours sometimes produces weird results #8186?
    That one clearly shows the glitchy tessellation before the fix. I can add that as the visual test.

  2. Also about Z coordinates
    I included Z because I thought it would be safer to normalize all three axes consistently. But you're right that the original issue only mentions X/Y coordinates. Should I:

    • Test if the issue happens with Z as Dave suggested (rotate the shape)?
    • Or remove Z normalization to keep the fix minimal?

Let me know and I'll update the PR accordingly and will also add similar changes to #8204

visualTest('Handles nearly identical consecutive vertices', function(p5, screenshot) {
p5.createCanvas(100, 100, p5.WEBGL);
p5.pixelDensity(1);
p5.background(255);
p5.fill(0);
p5.noStroke();

// Contours with nearly identical consecutive vertices (as can occur with textToContours)
// Outer contour
p5.beginShape();
p5.vertex(-30, -30, 0);
p5.vertex(30, -30, 0);
p5.vertex(30, 30, 0);
p5.vertex(-30, 30, 0);

// Inner contour (hole) with nearly identical vertices
p5.beginContour();
p5.vertex(-10, -10, 0);
p5.vertex(-10, 10, 0);
// This vertex has x coordinate almost equal to previous (10.00000001 vs 10)
p5.vertex(10.00000001, 10, 0);
p5.vertex(10, -10, 0);
p5.endContour();

p5.endShape(p5.CLOSE);

screenshot();
});
});
});