Skip to content

Commit 8f23e69

Browse files
authored
Merge pull request #8207 from processing/fix/buildgeom-no-faces
Fix buildGeometry with no faces
2 parents dc448e5 + 9efb13c commit 8f23e69

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/webgl/GeometryBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class GeometryBuilder {
137137
for (let i = 2; i < geometry.vertices.length; i++) {
138138
faces.push([0, i - 1, i]);
139139
}
140-
} else {
140+
} else if (shapeMode === constants.TRIANGLES) {
141141
for (let i = 0; i < geometry.vertices.length; i += 3) {
142142
if (
143143
!validateFaces ||

src/webgl/ShapeBuilder.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ export class ShapeBuilder {
280280
* @private
281281
*/
282282
_tesselateShape() {
283-
// TODO: handle non-PATH shape modes that have contours
284-
this.shapeMode = constants.TRIANGLES;
285283
// const contours = [[]];
286284
const contours = [];
287285
for (let i = 0; i < this.geometry.vertices.length; i++) {
@@ -316,6 +314,15 @@ export class ShapeBuilder {
316314
}
317315

318316
const polyTriangles = this._triangulate(contours);
317+
318+
// If there were no valid faces, we still want to use the original vertices
319+
// for strokes, so we'll stop here.
320+
if (polyTriangles.length === 0) {
321+
return;
322+
}
323+
324+
// TODO: handle non-PATH shape modes that have contours
325+
this.shapeMode = constants.TRIANGLES;
319326
const originalVertices = this.geometry.vertices;
320327
this.geometry.vertices = [];
321328
this.geometry.vertexNormals = [];

test/unit/webgl/p5.Geometry.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,31 @@ suite('p5.Geometry', function() {
302302
myp5.model(geom);
303303
assert.deepEqual(myp5.get(5, 5), [0, 0, 255, 255]);
304304
});
305+
306+
test('buildGeometry() with no fill works', () => {
307+
myp5.createCanvas(10, 10, myp5.WEBGL);
308+
const geom = myp5.buildGeometry(() => {
309+
myp5.noFill();
310+
myp5.beginShape();
311+
myp5.vertex(0, 0);
312+
myp5.vertex(0, 5);
313+
myp5.vertex(5, 10);
314+
myp5.endShape();
315+
});
316+
expect(geom.vertices.length).toBeGreaterThan(0);
317+
expect(geom.faces.length).toEqual(0);
318+
});
319+
320+
test('buildGeometry() with just a line works', () => {
321+
myp5.createCanvas(10, 10, myp5.WEBGL);
322+
const geom = myp5.buildGeometry(() => {
323+
myp5.beginShape();
324+
myp5.vertex(0, 0);
325+
myp5.vertex(0, 10);
326+
myp5.endShape();
327+
});
328+
expect(geom.vertices.length).toBeGreaterThan(0);
329+
expect(geom.faces.length).toEqual(0);
330+
});
305331
});
306332
});

0 commit comments

Comments
 (0)