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/p5.RendererGL.Immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,32 @@ p5.RendererGL.prototype._tesselateShape = function() {
this.immediateMode.geometry.vertexNormals[i].z
);
}

// Normalize nearly identical consecutive vertices to avoid numerical issues in libtess.
// This workaround addresses tessellation artifacts when consecutive vertices have
// coordinates that are almost (but not exactly) equal, which can occur when drawing
// contours automatically sampled from fonts with font.textToContours().
const epsilon = 1e-6;
for (const contour of contours) {
for (
let i = p5.RendererGL.prototype.tessyVertexSize;
i < contour.length;
i += p5.RendererGL.prototype.tessyVertexSize
) {
const prevX = contour[i - p5.RendererGL.prototype.tessyVertexSize];
const prevY = contour[i - p5.RendererGL.prototype.tessyVertexSize + 1];
const currX = contour[i];
const currY = contour[i + 1];

if (Math.abs(prevX - currX) < epsilon) {
contour[i] = prevX;
}
if (Math.abs(prevY - currY) < epsilon) {
contour[i + 1] = prevY;
}
}
}

const polyTriangles = this._triangulate(contours);
const originalVertices = this.immediateMode.geometry.vertices;
this.immediateMode.geometry.vertices = [];
Expand Down
32 changes: 32 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,36 @@ 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.

visualTest('Handles nearly identical consecutive vertices from textToContours', async function(p5, screenshot) {
p5.createCanvas(200, 200, p5.WEBGL);
p5.background(255);
p5.fill(0);
p5.noStroke();

const font = await p5.loadFont('unit/assets/Inconsolata-Bold.ttf');
const contours = font.textToContours('p', 0, 0, 60);

if (contours && contours.length > 0) {
p5.translate(-p5.width / 4, -p5.height / 4);
p5.beginShape();
for (let contourIdx = 0; contourIdx < contours.length; contourIdx++) {
const contour = contours[contourIdx];
if (contourIdx > 0) {
p5.beginContour();
}
for (let i = 0; i < contour.length; i++) {
p5.vertex(contour[i].x, contour[i].y, 0);
}
if (contourIdx > 0) {
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
Loading