PShape createShape(GROUP) ignoring noStroke(), fill() when contained in PGraphics?

Not sure if I’m missing something … I’m having trouble getting shapes (approx. 100 shapes, generated by a for loop in setup(), using the method .addChild() to create a group that is visible OK in draw()) to accept any styling when contained within PGraphics beginDraw() ~ endDraw().

If I draw a primitive within the PGraphics say, beginDraw(), pg.rect(), endDraww() then I can alter styling with pg.fill() and pg.noStroke() etc. etc… … but my shape group remains stuck on default styling no matter what I do (black stroke, white fill).

Are there any caveats when using shape groups in PGraphics? Happy to paste some of my code here.

Hello,

I cooked this up:

Example
PShape house;
PGraphics pg;

boolean toggle;

void setup() 
  {
  size(200, 200);

  // Make a group PShape
  
  pg = createGraphics(200, 200);
  house = createShape(GROUP);
  
  // Make three shapes
  PShape path = createShape();
  path.beginShape();
  path.vertex(-20, -20);
  path.vertex(0, -40);
  path.vertex(20, -20);
  path.endShape();
  
  //fill(128);
  PShape rectangle = createShape(RECT, -20, -20, 40, 40);
  PShape circle = createShape(ELLIPSE, 0, 0, 20, 20);
  
  // Add all three as children
  house.addChild(path);
  house.addChild(rectangle);
  house.addChild(circle);
  
  //pg.beginDraw();
  ////house.disableStyle();
  //pg.fill(color(255, 0, 0));
  //pg.stroke(color(255, 255, 0));
  //pg.shapeMode(CENTER);
  //pg.shape(house, pg.width/2, pg.height/2);
  //pg.endDraw();
  }

void draw() 
  {
  background(52);
  
  pg.beginDraw();
  if (toggle) 
    house.disableStyle();
  else
    house.enableStyle();
  pg.fill(color(255, 0, 0));
  pg.stroke(color(255, 255, 0));
  pg.shapeMode(CENTER);
  pg.shape(house, pg.width/2, pg.height/2);
  pg.endDraw();
  
  image(pg, mouseX-width/2, mouseY-height/2);
  }
  
void keyPressed()
  {
  toggle = !toggle;
  }

I modified this example:
https://processing.org/reference/PShape_addChild_.html

Reference:
https://processing.org/reference/PShape_disableStyle_.html

:)

@glv – thanks for the reply. Your example demo got me in the right direction.

I had declared disableStyle() elsewhere during setup() which for some reason was not passing into the draw() PGraphics beginDraw() ~ endDraw() part of my code.

So thanks to you, I got the result I wanted via this:

void draw() {

int liveKey = int(key);
if (keyPressed && liveKey >= 97 && liveKey <= 122) { // lowercase QWERTY: 97 = key a, 122 = key z
   liveKey = liveKey - 97;
   shape = shapeGroup[liveKey];
   shape.disableStyle(); // disableStyle only works for me when declared before beginDraw() ?
      pg.beginDraw();
      pg.colorMode(RGB, 1.0);
      pg.background(0, 0, 0, 1);
      pg.fill(1, 1, 1, 1);
      pg.stroke(1, 1, 1, 1);
      pg.shape(shape);
      pg.endDraw();
      blendMode(MULTIPLY);
   image(pg, 0, 0);
}
// etc. etc.
}

And with this, my shapes are now rendered how I actually want.

Without declaring disableStyle() as above, the shapes would always revert to default styling no matter what code I wrote after beginDraw()

Cheers again!

1 Like