How do I fill a 3D PShape Cube with color?

edited March 2018 in Library Questions

I was able to create the PShape Cube with a stroke the color that I want, but I am having trouble adding color to my cube. I have also tried moving "cube.setFill(color(100,200,0));" after "PShape cube = createShape();". Any help is greatly appreciated.

import peasy.*;
import peasy.org.apache.commons.math.*;
import peasy.org.apache.commons.math.geometry.*;

PeasyCam cam;

void setup(){
  size(600,600, P3D);
  cam = new PeasyCam(this, 600);
}
void draw(){
  background(255);

  PVector p1 = new PVector(0, 0, 0);
  PVector p2 = new PVector(0, -100, 0);
  PVector p3 = new PVector(-100, -100, 0);
  PVector p4 = new PVector(-100, 0, 0);
  PVector p5 = new PVector(0, 0, -100); 
  PVector p6 = new PVector(0, -100, -100);
  PVector p7 = new PVector(-100, -100, -100);
  PVector p8 = new PVector(-100, 0, -100);

  PShape cube = createShape();
  cube.beginShape();
  cube.vertex(p1.x, p1.y, p1.z);
  cube.vertex(p2.x, p2.y, p2.z);
  cube.vertex(p3.x, p3.y, p3.z);
  cube.vertex(p4.x, p4.y, p4.z);

  cube.vertex(p1.x, p1.y, p1.z);
  cube.vertex(p5.x, p5.y, p5.z);
  cube.vertex(p6.x, p6.y, p6.z);
  cube.vertex(p2.x, p2.y, p2.z);

  cube.vertex(p3.x, p3.y, p3.z);
  cube.vertex(p7.x, p7.y, p7.z);
  cube.vertex(p8.x, p8.y, p8.z);
  cube.vertex(p4.x, p4.y, p4.z);

  cube.vertex(p8.x, p8.y, p8.z);
  cube.vertex(p5.x, p5.y, p5.z);
  cube.vertex(p6.x, p6.y, p6.z);
  cube.vertex(p7.x, p7.y, p7.z);

  cube.vertex(p8.x, p8.y, p8.z);
  cube.vertex(p4.x, p4.y, p4.z);

  cube.endShape(CLOSE);

  cube.setFill(color(100,200,0));
  stroke(100,200,0);
  shape(cube);
}

Answers

  • This after line 24

    cube.setFill(color(100,200,0));

  • This seems to give me the same output as before.

  • Answer ✓

    https://processing.org/reference/beginShape_.html

    Two ways illustrated below. Check the link above for options. Not clear if the documentation list the requirements to set a fill on a PShape. Notice you do not need to CLOSE the shape if you specify a parameter.

    Kf

    import peasy.*;
    import peasy.org.apache.commons.math.*;
    import peasy.org.apache.commons.math.geometry.*;
    
    PeasyCam cam;
    
    void setup() {
      size(600, 600, P3D);
      cam = new PeasyCam(this, 600);
    }
    void draw() {
      background(255);
    
      PVector q1 = new PVector(0, 0, 0);
      PVector q2 = new PVector(0, -100, 0);
      PVector q3 = new PVector(-100, -100, 0);
      PVector q4 = new PVector(-100, 0, 0);
    
    
      PShape cube2 = createShape();
    
      cube2.beginShape();
      cube2.fill(0, 255, 250);
      cube2.vertex(q1.x, q1.y, q1.z);
      cube2.vertex(q2.x, q2.y, q2.z);
      cube2.vertex(q3.x, q3.y, q3.z);
      cube2.vertex(q4.x, q4.y, q4.z);
      cube2.endShape(CLOSE);
    
    
    
      PVector p1 = new PVector(0, 0, 0);
      PVector p2 = new PVector(0, -100, 0);
      PVector p3 = new PVector(-100, -100, 0);
      PVector p4 = new PVector(-100, 0, 0);
      PVector p5 = new PVector(0, 0, -100); 
      PVector p6 = new PVector(0, -100, -100);
      PVector p7 = new PVector(-100, -100, -100);
      PVector p8 = new PVector(-100, 0, -100);
    
      PShape cube = createShape();
    
      cube.beginShape(TRIANGLE_STRIP);
      cube.fill(0, 255, 25);
      cube.vertex(p1.x, p1.y, p1.z);
      cube.vertex(p5.x, p5.y, p5.z);
      cube.vertex(p6.x, p6.y, p6.z);
      cube.vertex(p2.x, p2.y, p2.z);
    
      cube.vertex(p3.x, p3.y, p3.z);
      cube.vertex(p7.x, p7.y, p7.z);
      cube.vertex(p8.x, p8.y, p8.z);
      cube.vertex(p4.x, p4.y, p4.z);
    
      cube.vertex(p8.x, p8.y, p8.z);
      cube.vertex(p5.x, p5.y, p5.z);
      cube.vertex(p6.x, p6.y, p6.z);
      cube.vertex(p7.x, p7.y, p7.z);
    
      cube.vertex(p8.x, p8.y, p8.z);
      cube.vertex(p4.x, p4.y, p4.z);
    
      cube.endShape();
    
      shape(cube);
      translate(0, 0, 200);  
      shape(cube2);
    }
    
  • I appreciate the reference and explanation, thank you!

  • edited March 2018
    • AFAIK, setFill() is used to modify the fill color of an already existing PShape.
    • But when building 1 using beginShape(), a simple fill() is enough.
    • I'd even risk to say that if we don't call the fill() method at all, the newly PShape is probably gonna use the main canvas' current fill color.
    • The same applies to stroke() & setStroke() too. Plus noFill() & noStroke().
Sign In or Register to comment.