PShape setFill() issue

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)));
  }
}

Hello @svan,

I have come across this before

This will work:

PShape t;

void setup() {
  size(400, 400, P3D);
  surface.setTitle("Should change color when mouse clicked.");
  updateShape();
}

boolean s;

void draw() {
  background(209);
  shape(t);
}

void mousePressed() {
  updateShape();
}

void updateShape() {
  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)));
  }
}

:)

1 Like

Hello @svan ,

Your code works as expected in Processing 3.5.4 but not in Processing 4.3.1.

This may be related:

:)

3 Likes

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.

1 Like

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.

3 Likes

I re-submitted a bug report and this fix was posted:

PShape t;

void setup() {
  size(400, 400, P2D);
  surface.setTitle("Should change color when mouse clicked.");
  t = createShape();
  t.beginShape();
  t.fill(color(random(255), random(255), random(255)));
  t.vertex(200, 100);
  t.vertex(100, 300);
  t.vertex(300, 300);
  t.endShape(CLOSE);
  for (int i = 0; i < 3; 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.beginTessellation();
    t.setFill(i, color(random(255), random(255), random(255)));
    t.endTessellation();
  }  
}

Addendum:
Works on createShape(BOX,) also. MousePressed() edited to reflect @glv’s findings.

This is a link to the issue on Github:

A bit more research on tessellation update came across this:

There are some links that are no longer valid in above!

This will also work (insight from GitHub issues above) in examples provided:

void mousePressed() {
  for (int i = 0; i < t.getVertexCount(); i++) {
    t.beginTessellation(POINTS);
    t.setFill(i, color(random(255), random(255), random(255)));
    t.endTessellation();
  }  
}

:)

2 Likes

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.

1 Like

It is very cool!

Some gems here:
P3D / Processing.org

Look for:
image

The RGB cube is one of my favorites!

:)

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.