Skip to content

Commit 4f81bdb

Browse files
committed
Skip adding degenerate faces in textToModel
1 parent 63375bb commit 4f81bdb

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/type/p5.Font.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,11 @@ export class Font {
537537
({ width, height, options } = this._parseArgs(width, height, options));
538538
const extrude = options?.extrude || 0;
539539
const contours = this.textToContours(str, x, y, width, height, options);
540+
540541
const geom = this._pInst.buildGeometry(() => {
541542
if (extrude === 0) {
543+
const prevValidateFaces = this._pInst._renderer._validateFaces;
544+
this._pInst._renderer._validateFaces = true;
542545
this._pInst.beginShape();
543546
this._pInst.normal(0, 0, 1);
544547
for (const contour of contours) {
@@ -549,7 +552,11 @@ export class Font {
549552
this._pInst.endContour(this._pInst.CLOSE);
550553
}
551554
this._pInst.endShape();
555+
this._pInst._renderer._validateFaces = prevValidateFaces;
552556
} else {
557+
const prevValidateFaces = this._pInst._renderer._validateFaces;
558+
this._pInst._renderer._validateFaces = true;
559+
553560
// Draw front faces
554561
for (const side of [1, -1]) {
555562
this._pInst.beginShape();
@@ -561,8 +568,9 @@ export class Font {
561568
this._pInst.endContour(this._pInst.CLOSE);
562569
}
563570
this._pInst.endShape();
564-
this._pInst.beginShape();
565571
}
572+
this._pInst._renderer._validateFaces = prevValidateFaces;
573+
566574
// Draw sides
567575
for (const contour of contours) {
568576
this._pInst.beginShape(this._pInst.QUAD_STRIP);

src/webgl/GeometryBuilder.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class GeometryBuilder {
108108
* Adds geometry from the renderer's immediate mode into the builder's
109109
* combined geometry.
110110
*/
111-
addImmediate(geometry, shapeMode) {
111+
addImmediate(geometry, shapeMode, { validateFaces = false } = {}) {
112112
const faces = [];
113113

114114
if (this.renderer.states.fillColor) {
@@ -129,7 +129,14 @@ class GeometryBuilder {
129129
}
130130
} else {
131131
for (let i = 0; i < geometry.vertices.length; i += 3) {
132-
faces.push([i, i + 1, i + 2]);
132+
if (
133+
!validateFaces ||
134+
geometry.vertices[i].copy().sub(geometry.vertices[i+1])
135+
.cross(geometry.vertices[i].copy().sub(geometry.vertices[i+2]))
136+
.magSq() > 0
137+
) {
138+
faces.push([i, i + 1, i + 2]);
139+
}
133140
}
134141
}
135142
}

src/webgl/p5.RendererGL.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ class RendererGL extends Renderer {
455455
}
456456
return this._internalDisable.call(this.drawingContext, key);
457457
};
458+
459+
// Whether or not to remove degenerate faces from geometry. This is usually
460+
// set to false for performance.
461+
this._validateFaces = false;
458462
}
459463

460464
remove() {
@@ -562,7 +566,8 @@ class RendererGL extends Renderer {
562566
if (this.geometryBuilder) {
563567
this.geometryBuilder.addImmediate(
564568
this.shapeBuilder.geometry,
565-
this.shapeBuilder.shapeMode
569+
this.shapeBuilder.shapeMode,
570+
{ validateFaces: this._validateFaces }
566571
);
567572
} else if (this.states.fillColor || this.states.strokeColor) {
568573
if (this.shapeBuilder.shapeMode === constants.POINTS) {
@@ -2614,7 +2619,7 @@ function rendererGL(p5, fn) {
26142619
* }
26152620
* </code>
26162621
* </div>
2617-
*
2622+
*
26182623
* <div>
26192624
* <code>
26202625
* // Now with the antialias attribute set to true.

0 commit comments

Comments
 (0)