# PShape setFill() issue

**URL:** <https://discourse.processing.org/t/pshape-setfill-issue/45536>\
**Category:** Coding Questions\
**Created:** [January 5, 2025, 8:01pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536 "2025-01-05T20:01:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 5, 2025, 8:01pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/1 "2025-01-05T20:01:16Z")

</div>

**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.

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 5, 2025, 8:57pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/2 "2025-01-05T20:57:29Z")

</div>

Hello @svan,

I have come across this before

This will work:

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

```

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 5, 2025, 9:24pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/3 "2025-01-05T21:24:43Z")

</div>

Hello @svan ,

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

This may be related:

> <https://github.com/processing/processing4/issues/677>
>
> \*Created by: cacheflowe\*
> 
> This used to work in Processing 3.x, but no longer wor…ks in recent Processing 4 versions. 
> 
> Here's a complete sketch. In Processing 3.x and up to 4.0.b2, we see the box updates to random vertex colors. Since Processing 4.0.b4 (or maybe beta 3), it doesn't have an effect.
> 
> \`\`\`java
> PShape shape;
>   
> public void setup() {
> size(640, 360, P3D);
> shape = createShape(BOX, 100, 100, 100).getTessellation();
> updateFill();
> }
> 
> public void updateFill() {
> int numVerts = shape.getVertexCount();
> for (int i = 0; i \< numVerts; i++) {
> shape.setFill(i, color(random(255), random(255), random(255)));
> }
> }
> 
> public void draw() {
> background(0);
> translate(width/2, height/2, 0);
> rotateY(frameCount \* 0.01);
> shape(shape);
> }
> \`\`\`

`:)`

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [January 5, 2025, 9:27pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/4 "2025-01-05T21:27:36Z")

</div>

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](https://processing.github.io/processing-javadocs/core/processing/core/PShape.html), but I couldn’t find it. If I have the time and energy I might investigate PShapeOpenGL [PShapeOpenGL](https://processing.github.io/processing-javadocs/core/processing/opengl/PShapeOpenGL.html) more.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 5, 2025, 9:34pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/5 "2025-01-05T21:34:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 7, 2025, 6:44am UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/6 "2025-01-07T06:44:30Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 8, 2025, 1:03am UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/7 "2025-01-08T01:03:39Z")

</div>

> [@svan](#):
>
> I re-submitted a bug report and this fix was posted:

This is a link to the issue on Github:

- [PShape setFill(indx, color) not working in Processing 4 · Issue #900 · processing/processing4 · GitHub](https://github.com/processing/processing4/issues/900)

A bit more research on _tessellation update_ came across this:

> <https://github.com/processing/processing4/issues/196>
>
> \*Created by: codeanticode\*
> 
> Performance was quite good with GLModel (from the \[G…LGraphics\](http://glgraphics.sourceforge.net/) library) in certain instances where PShape still struggles (for example when modifying the coordinates of many vertices at once), because GLModel used a technique called \[buffer object streaming\](https://www.khronos.org/opengl/wiki/Buffer\_Object\_Streaming). This technique allows one to write data directly into a regular Java buffer, which internally is mapped to video memory. This relies on the function \[glMapBufferRange\](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glMapBufferRange.xhtml), which was available only on desktop, not on mobile (OpenGL ES), back when the OpenGL renderer in Processing 2 & 3 was implemented it. In order to keep code parity between desktop and mobile versions of the OpenGL renderer Processing, buffer streaming was not used in PShapeOpenGL. It turns out that glMapBufferRange eventually became available in \[OpenGL ES 3.0\](https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glMapBufferRange.xhtml). So it could be possible to re-implement buffer streaming to improve PShape performance, perhaps in combination with other PShape enhancements such as this one https://github.com/processing/processing/issues/2280, for both Processing 4 and new versions of the Android mode. \[to be tagged as proposed enhancement\]

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

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

```auto
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();
  }  
}

```

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 8, 2025, 3:17am UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/8 "2025-01-08T03:17:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2025, 11:13pm UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/9 "2025-01-11T23:13:19Z")

</div>

> [@svan](#):
>
> I think vertex coloring is cool

It is very cool!

Some gems here:  
_[P3D / Processing.org](https://processing.org/tutorials/p3d)_

Look for:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/optimized/3X/e/b/eb8337a3089c33108e3ebe6633b84215580a1e59_2_690x53.png)

The RGB cube is one of my favorites!

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 12, 2025, 1:42am UTC](https://discourse.processing.org/t/pshape-setfill-issue/45536/10 "2025-01-12T01:42:27Z")

</div>

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.

 ![BOX](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/e/9e408670b7c683080b9110e001660e666fe778e9.png)  
 ![SPHERE](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/2/52abcb987d7da571a7167e95a66c4e86ec53039a.png)
