Skip to content

fixed geometry builder to accomodate vertexStrokeColors #7850

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 2 commits into
base: dev-2.0
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions src/webgl/GeometryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ class GeometryBuilder {
vertexColors.push(...this.renderer.states.curFillColor);
}
this.geometry.vertexColors.push(...vertexColors);
// After the existing vertexColors code, add this:
const vertexStrokeColors = [...input.vertexStrokeColors];
while (vertexStrokeColors.length < input.vertices.length * 4) {
if (this.renderer.states.strokeColor) {
vertexStrokeColors.push(...this.renderer.states.curStrokeColor);
} else {
// Use -1, -1, -1, -1 to indicate fallback to global stroke color
vertexStrokeColors.push(-1, -1, -1, -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

this.renderer.states.strokeColor will always exist, it might just be the default color. What we do for the fill colors is, when we start building a p5.Geometry, set the current color to -1, -1, -1, -1 so that we only get non-negative colors when someone calls fill() or stroke() inside of the buildGeometry callback:

beginGeometry() {
if (this.geometryBuilder) {
throw new Error(
"It looks like `beginGeometry()` is being called while another p5.Geometry is already being build."
);
}
this.geometryBuilder = new GeometryBuilder(this);
this.geometryBuilder.prevFillColor = this.states.fillColor;
this.fill(new Color([-1, -1, -1, -1]));

Can we do the same for strokes?

Copy link
Author

@Vaivaswat2244 Vaivaswat2244 May 27, 2025

Choose a reason for hiding this comment

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

okay so this should look something like this if I got this right...

beginGeometry() {
  if (this.geometryBuilder) {
    throw new Error(
      "It looks like `beginGeometry()` is being called while another p5.Geometry is already being build."
    );
  }
  this.geometryBuilder = new GeometryBuilder(this);
  
  this.geometryBuilder.prevFillColor = this.states.fillColor;
  this.geometryBuilder.prevStrokeColor = this.states.strokeColor; 
  this.fill(new Color([-1, -1, -1, -1]));
  this.stroke(new Color([-1, -1, -1, -1]));

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so, yep!

Copy link
Contributor

Choose a reason for hiding this comment

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

There's also an extra step at the end where we reset the previous fill color:

this.fill(this.geometryBuilder.prevFillColor);
So we'd do something similar for strokes there too

Copy link
Author

Choose a reason for hiding this comment

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

Right. Got it.

}
}
this.geometry.vertexStrokeColors.push(...vertexStrokeColors);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ class RendererGL extends Renderer {
this.geometryBuilder = new GeometryBuilder(this);
this.geometryBuilder.prevFillColor = this.states.fillColor;
this.fill(new Color([-1, -1, -1, -1]));
this.stroke(new Color([-1, -1, -1, -1]));
}

/**
Expand All @@ -508,6 +509,7 @@ class RendererGL extends Renderer {
}
const geometry = this.geometryBuilder.finish();
this.fill(this.geometryBuilder.prevFillColor);
this.stroke(this.geometryBuilder.prevStrokeColor);
this.geometryBuilder = undefined;
return geometry;
}
Expand Down
2 changes: 1 addition & 1 deletion src/webgl/shaders/line.vert
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void main() {

StrokeVertex inputs;
inputs.position = aPosition.xyz;
inputs.color = uUseLineColor ? aVertexColor : uMaterialColor;
inputs.color = (uUseLineColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor;
inputs.weight = uStrokeWeight;
inputs.tangentIn = aTangentIn;
inputs.tangentOut = aTangentOut;
Expand Down
53 changes: 53 additions & 0 deletions test/unit/visual/cases/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,57 @@ visualSuite('WebGL', function() {
screenshot();
});
});

visualSuite('buildGeometry stroke colors', () => {
visualTest('Geometry without stroke colors, global stroke override', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);

// Build geometry without any stroke() calls inside
const geom = p5.buildGeometry(() => {
p5.beginShape();
p5.vertex(-15, -15, 0);
p5.vertex(15, -15, 0);
p5.vertex(15, 15, 0);
p5.vertex(-15, 15, 0);
p5.endShape(p5.CLOSE);
});

p5.background(220);
p5.stroke('red'); // Should override and make all strokes red
p5.strokeWeight(2);
p5.noFill();
p5.model(geom);
screenshot();
});

visualTest('Geometry with internal stroke colors not overridden', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);

// Build geometry WITH stroke() calls inside
const geom = p5.buildGeometry(() => {
p5.beginShape();

p5.stroke('blue');
p5.vertex(-15, -15, 0);

p5.stroke('green');
p5.vertex(15, -15, 0);

p5.stroke('purple');
p5.vertex(15, 15, 0);

p5.stroke('orange');
p5.vertex(-15, 15, 0);

p5.endShape(p5.CLOSE);
});

p5.background(220);
p5.stroke('red'); // This should NOT override the internal colors
p5.strokeWeight(2);
p5.noFill();
p5.model(geom);
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