setFill(indx,color) may be used to set vertex colors for a PShape. However, it appears to be able to do this only once as shown in the following demo. It will change the colors if the triangle is redrawn, but the desired result is that it will reset the color of a pre-existing PShape without drawing a new one.
PShape t;
void setup() {
size(400, 400, P2D);
surface.setTitle("Should change color when mouse clicked.");
t = createShape();
t.beginShape();
t.vertex(200, 100);
t.vertex(100, 300);
t.vertex(300, 300);
t.endShape(CLOSE);
for (int i = 0; i < t.getVertexCount(); i++) {
t.setFill(i, color(random(255), random(255), random(255)));
}
}
void draw() {
background(209);
shape(t);
}
void mousePressed() {
for (int i = 0; i < t.getVertexCount(); i++) {
t.getVertex(i);
t.setFill(i, color(random(255), random(255), random(255)));
}
}
Thank you for bringing it up @svan! Cheers @glv! I was trying to avoid instantiating an new PShape object every time I want to change its colors, in order to gain some performance… But it seems that this is a known limitation, after the shape is drawn the “OpenGL tesselation” is fixed…
There is a mention to an update() method for PShape at PShape, but I couldn’t find it. If I have the time and energy I might investigate PShapeOpenGL PShapeOpenGL more.
That does work, but it creates two objects; the goal is to get it to work with a single object. Good reference. I can’t get the original post to run in version 3.5.4 due to a NSWindow error (macos), but it runs ok in version 2.2.1 which indeed points to a change in the runtime. Now we just need to run that down. Thanks.
Thank you for the additional reference which provided very helpful insight to tessellation in Processing. The shortened code also works just fine and I will edit the solution to use it. Hopefully the runtime code can also be amended and will carry over to py5. I think vertex coloring is cool and we are able to do this without writing openGL code which can get formidable. Your input allowed us to solve this issue and is much appreciated.
I’ve already done Processing’s BOX and SPHERE. I think the reason that vertex coloring has not been used more in Processing is the lack of documentation for setFill(indx, color). Most examples just use setFill(color) which gives a single solid color.