Skip to content

feat(renderer): implement per-polygon fills for 2D shape #7722 #7729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
88 changes: 41 additions & 47 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,37 +928,36 @@ class Renderer2D extends p5.Renderer {
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
this.drawingContext.closePath();
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
this._setFill(vertices[i + 2][5]);
this.drawingContext.fill();
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
this._setStroke(vertices[i + 2][6]);
this.drawingContext.stroke();
}
}
} else if (shapeKind === constants.TRIANGLE_STRIP) {
for (i = 0; i + 1 < numVerts; i++) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 1][5]);
}
if (i + 2 < numVerts) {
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
}
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
}
if (numVerts > 2) {
for (let i = 2; i < numVerts; i++) {
if (!this._clipping) {
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[i - 2][0], vertices[i - 2][1]);
this.drawingContext.lineTo(vertices[i - 1][0], vertices[i - 1][1]);
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
this.drawingContext.closePath();
if (this._doFill) {
this._setFill(vertices[i][5]);
this.drawingContext.fill();
}
this._doFillStrokeClose(closeShape);
if (this._doStroke) {
this._setStroke(vertices[i][6]);
this.drawingContext.stroke();
}
}
}
}


} else if (shapeKind === constants.TRIANGLE_FAN) {
if (numVerts > 2) {
// For performance reasons, try to batch as many of the
Expand Down Expand Up @@ -1001,40 +1000,35 @@ class Renderer2D extends p5.Renderer {
for (j = 1; j < 4; j++) {
this.drawingContext.lineTo(vertices[i + j][0], vertices[i + j][1]);
}
this.drawingContext.lineTo(v[0], v[1]);
this.drawingContext.closePath();
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 3][5]);
this._setFill(vertices[i + 3][5]);
this.drawingContext.fill();
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 3][6]);
this._setStroke(vertices[i + 3][6]);
this.drawingContext.stroke();
}
this._doFillStrokeClose(closeShape);
}
} else if (shapeKind === constants.QUAD_STRIP) {
if (numVerts > 3) {
for (i = 0; i + 1 < numVerts; i += 2) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
if (i + 3 < numVerts) {
this.drawingContext.moveTo(
vertices[i + 2][0], vertices[i + 2][1]);
this.drawingContext.lineTo(v[0], v[1]);
this.drawingContext.lineTo(
vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(
vertices[i + 3][0], vertices[i + 3][1]);
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 3][5]);
for (i = 0; i + 3 < numVerts; i += 2) {
if (!this._clipping) {
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[i][0], vertices[i][1]);
this.drawingContext.lineTo(vertices[i+1][0], vertices[i+1][1]);
this.drawingContext.lineTo(vertices[i+3][0], vertices[i+3][1]);
this.drawingContext.lineTo(vertices[i+2][0], vertices[i+2][1]);
this.drawingContext.closePath();
if (this._doFill) {
this._setFill(vertices[i + 3][5]);
this.drawingContext.fill();
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 3][6]);
if (this._doStroke) {
this._setStroke(vertices[i + 3][6]);
this.drawingContext.stroke();
}
} else {
this.drawingContext.moveTo(v[0], v[1]);
this.drawingContext.lineTo(
vertices[i + 1][0], vertices[i + 1][1]);
}
this._doFillStrokeClose(closeShape);
}
}
}
} else {
Expand Down