From dc20d97c284ce3cf86075057e97354a31613b9dc Mon Sep 17 00:00:00 2001 From: Vaivaswat Date: Tue, 27 May 2025 15:36:57 +0530 Subject: [PATCH 1/2] fixed geometry builder to accomodate vertexStrokeColors --- src/webgl/GeometryBuilder.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/webgl/GeometryBuilder.js b/src/webgl/GeometryBuilder.js index 83d2fb90de..a109988cef 100644 --- a/src/webgl/GeometryBuilder.js +++ b/src/webgl/GeometryBuilder.js @@ -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); + } + } + this.geometry.vertexStrokeColors.push(...vertexStrokeColors); } /** From 7f2edb8850602e5c792b06cab626ef30008d6e64 Mon Sep 17 00:00:00 2001 From: Vaivaswat Date: Mon, 2 Jun 2025 15:52:17 +0530 Subject: [PATCH 2/2] added tests for stroke colours and small changes --- src/webgl/p5.RendererGL.js | 2 + src/webgl/shaders/line.vert | 2 +- test/unit/visual/cases/webgl.js | 53 ++++++++++++++++++ .../000.png | Bin 0 -> 944 bytes .../metadata.json | 3 + .../000.png | Bin 0 -> 276 bytes .../metadata.json | 3 + 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/unit/visual/screenshots/WebGL/buildGeometry stroke colors/Geometry with internal stroke colors not overridden/000.png create mode 100644 test/unit/visual/screenshots/WebGL/buildGeometry stroke colors/Geometry with internal stroke colors not overridden/metadata.json create mode 100644 test/unit/visual/screenshots/WebGL/buildGeometry stroke colors/Geometry without stroke colors, global stroke override/000.png create mode 100644 test/unit/visual/screenshots/WebGL/buildGeometry stroke colors/Geometry without stroke colors, global stroke override/metadata.json diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 5e46d2d106..8797639648 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -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])); } /** @@ -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; } diff --git a/src/webgl/shaders/line.vert b/src/webgl/shaders/line.vert index de422ad6b6..3d3de09b15 100644 --- a/src/webgl/shaders/line.vert +++ b/src/webgl/shaders/line.vert @@ -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; diff --git a/test/unit/visual/cases/webgl.js b/test/unit/visual/cases/webgl.js index a670d34387..fe8c5ef0e8 100644 --- a/test/unit/visual/cases/webgl.js +++ b/test/unit/visual/cases/webgl.js @@ -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(); + }); + }); }); diff --git a/test/unit/visual/screenshots/WebGL/buildGeometry stroke colors/Geometry with internal stroke colors not overridden/000.png b/test/unit/visual/screenshots/WebGL/buildGeometry stroke colors/Geometry with internal stroke colors not overridden/000.png new file mode 100644 index 0000000000000000000000000000000000000000..f8a69d112b3bccfb35953500d52e20fa607d89ba GIT binary patch literal 944 zcmV;h15f;kP)Px&YDq*vRA@u(*HK7QaTEsd|K6y8k%ZvFEq%94tB@;v}qHZ3T~87S2z0=FEFqAaP1Tk8hM z8n&V!XQ1N#1#kS>@#8u=1$hKxfLC!#R=^1dd%gHE9D$-xl4^YQS2MTfq>U1Z- zO@&fXmQ=)9<^;&TWJf{HK!g4Wxba{*%94sWbPj+VA?YmF(Net+qlRZQ(UGJgUHUG7 zdV{?v$Qfv>*#T~FCI@9nMcS*{0UDSaKtawxw{Ziw9)AJKl8V?2(z}%_AiaCZb@V-2 zg;AaFi_wv!BGy{z^TRzYeV)j53^gsnsMgVPbR?-rv#}YV5&sAZat1~p&jZ)=t`cQQ zMa(8MKwh601vvu~PiKIue^Z09q#`D>^sU$DmA(bbb<8}UfKfHC>d}#;BF1JTK>iWw z{}Q>5`4^)ws`6zMI+9eR)>;eDw0jx_IRlH%A>hjGttd+>Vz3zi3b+C&$Qf9D-3MIp zvrd#H6{%{k0%&ev4h1;_8*jUTD|pg_vZNyVE(&AkwuVuVGjK6q0bD}m0?Lw#{5DF6SO-BRoY=B@P0lmsT9yCwdD?5%dws98fkQ^8-i6DH}l_k<5V`vyty`nyL8d Sw#tYA0000Lz7s#En}_~>R04m}qW&uo`tRj+H*uG$J$?wxrymU;g?N4sT8T{S)v_*?^) zbR5{f#b^VC$S`~Fi>NoJKK=RhN9-pLP;AZ7wDisES$@QO*YMm> zz9%Pm@Sb0Hgw-} X6G;0ZysKOj=raaSS3j3^P6