Skip to content
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

PShape setFill(indx, color) not working in Processing 4 #900

Open
vsquared opened this issue Jan 6, 2025 · 3 comments
Open

PShape setFill(indx, color) not working in Processing 4 #900

vsquared opened this issue Jan 6, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@vsquared
Copy link
Contributor

vsquared commented Jan 6, 2025

Most appropriate sub-area of Processing 4?

OpenGL

Processing version

4.3

Operating system

MacOSX

Steps to reproduce this

"1. The source code below demonstrates the problem. The PShape should change color when the mouse is clicked.

  1. The source code runs correctly in versions 2.2.1 (macos) and 3.5.4 (reported by another forum member running Windows) indicating a runtime code change prior to version 4.

  2. There is a similar report from a year ago: PShape.setFill() no longer works #677"

  3. We would like to change the vertex colors of the original PShape object without having to create a second object.

snippet

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

Additional context

No response

@vsquared vsquared added the bug Something isn't working label Jan 6, 2025
@Junology
Copy link
Contributor

Junology commented Jan 7, 2025

You can update vertex colors in "Tessellation update mode" (though I don't know what it is supposed to mean):

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

It is also fixed by changing the core as follows:

--- a/core/src/processing/opengl/PShapeOpenGL.java
+++ b/core/src/processing/opengl/PShapeOpenGL.java
@@ -2005,12 +2005,12 @@ public class PShapeOpenGL extends PShape {
     }
 
     if (image == null) {
-      if (root.tessUpdate) {
+      inGeo.colors[index] = PGL.javaToNativeARGB(fill);
+      if (shapeCreated && tessellated && hasPolys) {
         int tessIdx = firstPolyVertex + index;
         tessGeo.polyColors[tessIdx] = PGL.javaToNativeARGB(fill);
         root.setModifiedPolyColors(tessIdx, tessIdx);
       } else {
-        inGeo.colors[index] = PGL.javaToNativeARGB(fill);
         markForTessellation();
       }
     }

Since I don't know what "Tessellation update mode" is for, I am hesitating to make PR for this fix.

@vsquared
Copy link
Contributor Author

vsquared commented Jan 7, 2025

Thank you so much for this. It works!

@hx2A
Copy link
Collaborator

hx2A commented Jan 7, 2025

Since I don't know what "Tessellation update mode" is for, I am hesitating to make PR for this fix.

I didn't know there was a "Tessellation update mode", but I do understand what tessellation means for PShape. The tessellated geometry is what gets sent to the GPU. Among other things, it divides up everything into little triangles. It also structures the data in a way so that the GPU can consume it. There is a cost to doing this, so Processing wants to do it once and not every frame. When a fill color is changed as with setFill(), it is updating arrays that store the pre-tessellated geometry data. The "Tessellation update mode" must be necessary so it knows to also update the tessellated data also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants