Shapes3D library's Shape3D fill() method

edited November 2017 in Library Questions

Hello,

I can't get the Shape3D fill() method to return anything other than white ((R,G,B)=(250.0,250.0,250.0)) when I'm picking a Box.

A slightly adapted version of the Shapes3D example sketch S3D4P_Picking.pde is below.

Am I missing something?

Thanks,

Jim

/**
 Simple program to demonstrate the shape picking feature
 of this library.

 Click on the slowly revolving cubes to change their colour.

 created by Peter Lager
 */

import shapes3d.utils.*;
import shapes3d.animation.*;
import shapes3d.*;
import peasy.*;

PeasyCam cam;

Box[] box = new Box[20];
Shape3D picked = null;
boolean clicked = false;

float bsize, a, d = 50, c = 120;

void setup() {
  size(400, 400, P3D);
  //cursor(CROSS);
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(10);
  cam.setMaximumDistance(500);  
  for (int i = 0; i < box.length; i++) {
    bsize = 5 + (int)random(12);
    box[i] = new Box(this, bsize, bsize, bsize);
    box[i].moveTo(random(-d, d), random(-d, d), random(-d, d));
    box[i].fill(randomColor());
    box[i].stroke(color(64, 0, 64));
    box[i].strokeWeight(0.6);
    box[i].drawMode(S3D.SOLID | S3D.WIRE);
    box[i].tag = "Box " + i;
  }
}

void draw() {
  background(128);
  pushMatrix();
  //camera(c * sin(a), 10, c * cos(a), 0, 0, 0, 0, 1, 0);
  if (clicked) {
   clicked = false;
   picked = Shape3D.pickShape(this, mouseX, mouseY);
   println("picked: " + picked);
     if (picked != null) {
       picked.fill(randomColor());
       int pickedFillColour = picked.fill();
       println("Picked colour: (R,G,B)=(" + red(pickedFillColour) + ", " + green(pickedFillColour) + ", " + blue(pickedFillColour) + ")");
    }
  }
  for (int i = 0; i < box.length; i++)
    box[i].draw();
  popMatrix();
  a += 0.002;
}

void mouseClicked() {
  clicked = true;
}

int randomColor() {
  return color(random(100, 220), random(100, 220), random(100, 220));
}

Answers

  • Answer ✓

    All the other shapes, except Box, have a single fill colour for the whole shape so the statement shape.fill(); returns that colour.

    In the case of Box each of the six sides can have its own colour so it doesn't know which one to return so it returns 'white'.

  • edited November 2017 Answer ✓

    Thanks quark.

    I wanted to give each shape a clicked or not clicked status, and allow the user to toggle these by picking a shape and changing its colour.

    The boolean for the toggle was originally the shape's colour, but I've now discovered the tag member variable.

    Cheers,

    Jim

Sign In or Register to comment.