setVisible

edited December 2016 in Library Questions

I have the following program creates cubes and changes them. I am trying to make the cubes invisible until they are activated in updatecells, but I want them part of my overall VBO that is created during setup to help performance. I have tried putting the "setVisible( false )" code in many places, but cannot seem to get it to work. I have tried making each side of the cubes invisible separately, and that did not work either. Any suggestions are welcome.

import processing.opengl.*;
import peasy.*;

PeasyCam cam;
PShape world;
float CS = .25;
int CPS = 10;
int totalcubes = CPS*CPS*CPS;
float offset = CPS/2;
color colors[]= {0x88FF0000,0x8800FF00,0x880000FF,0x88FFFF00,0x8800FFFF,0x88FF00FF};
int dice_orientations[][][]={{{1,6,2,4,5,3},{1,6,4,5,3,2},{1,6,5,3,2,4},{1,6,3,2,4,5}},
                             {{6,1,2,3,5,4},{6,1,3,5,4,2},{6,1,5,4,2,3},{1,6,4,2,3,5}},
                             {{3,4,5,6,2,1},{3,4,6,2,1,5},{3,4,2,1,5,6},{3,4,1,5,6,2}},
                             {{4,3,1,2,6,5},{4,3,2,6,5,1},{4,3,6,5,1,2},{4,3,5,1,2,6}},
                             {{2,5,4,6,3,1},{2,5,6,3,1,4},{2,5,3,1,4,6},{2,5,1,4,6,3}},
                             {{5,2,1,3,6,4},{5,2,3,6,4,1},{5,2,6,4,1,3},{5,2,4,1,3,6}}};
int rcolor; 
void setup() { 
  size(2100,2100, P3D); 
  cam = new PeasyCam(this, 150*CPS);  
  noStroke(); 

  print("Making World   ");
  world = createShape(GROUP);      
  for (int i = 0; i < CPS; i++) {
    for (int j = 0; j < CPS; j++) {
      for (int k = 0; k < CPS; k++) {
        MyCube cube = new MyCube(i - offset, j - offset, k - offset);
        world.addChild(cube.output);
        }}print("-");}
  println("   done");} // end setup

 void draw() { 
  background(127);
  scale(95); 
  shape(world, 0, 0); 
  updatecells();
} // end draw


 void updatecells(){
  PShape cube = world.getChild(int(random(totalcubes)));
  color sidecolor;    
  int tempfront = int(random(6));
  int temptop = int(random(4));
  int tempside = 0;

  for (int side=0; side < 6; side++) {
    tempside=dice_orientations[tempfront][temptop][side]; 
    sidecolor=colors[tempside-1];
    for (int V=0; V < 4; V++) {
      cube.setFill(side*4+V ,sidecolor);
     } // end V
   } // end side
 } // end updatecells 



// could just be a method, returning PShape    
class MyCube {
  PShape output;

  MyCube(float x, float y, float z) { // <- new arguments
    int size = 1;
    output = createShape();
    output.translate(x, y, z); // <- new tanslate
    output.beginShape(QUADS);
    output.fill(colors[0]);
    output.vertex(-CS, CS, CS);output.vertex( CS, CS, CS);output.vertex( CS,-CS, CS);output.vertex(-CS,-CS, CS);
    output.fill(colors[1]);
    output.vertex( CS, CS, CS);output.vertex( CS, CS,-CS);output.vertex( CS,-CS,-CS);output.vertex( CS,-CS, CS);
    output.fill(colors[2]); 
    output.vertex( CS, CS,-CS);output.vertex(-CS, CS,-CS);output.vertex(-CS,-CS,-CS);output.vertex( CS,-CS,-CS);
    output.fill(colors[3]);
    output.vertex(-CS, CS,-CS);output.vertex(-CS, CS, CS);output.vertex(-CS,-CS, CS);output.vertex(-CS,-CS,-CS);
    output.fill(colors[4]);
    output.vertex(-CS, CS,-CS);output.vertex( CS, CS,-CS);output.vertex( CS, CS, CS);output.vertex(-CS, CS, CS);
    output.fill(colors[5]); 
    output.vertex(-CS,-CS,-CS);output.vertex( CS,-CS,-CS);output.vertex( CS,-CS, CS);output.vertex(-CS,-CS, CS);
    output.setVisible( false );
    output.endShape(CLOSE);
  } // end constructor
}// end MyCube

Answers

  • Help... Am I trying to do something not possible? I just want to turn parts of my PShape visible and invisible....

  • I'm no expert at PShapes, but I think that what you could do is call noStroke and noFill before the parts that should be invisible and go back to previous stroke and fill for the visible parts.

  • Thank you Lord, but to no avail. I had not thought of that and tried it in multiple configurations and still cannot get it to work.

  • I could only figure out one solution : keep each cube separate.
    P.S. The noFill(), if used at the start of the shape, causes the shape to be invisible, however you cannot call setFill() later on(no idea why).

  • Lord, Thank you for looking at this for me. I believe it is something about how it is added to the PShape, that it is added but is not a true child. When I do a children() on the PShape, nither the boxes or the vectors show up. I believe I am going to have to rewrite it using triangles or similar for the sides instead of vertex fills. I will try that and let everyone know.

  • Well, best of luck then.
    If I get time I will test some of my new but far-too-complex-to-explain-here ideas and let you know the results, but it may take about a week.

  • i think this may be another of those cases where not everything works with every flavour of PShape - PShape is used to handle SVG loading and display, OBJ loading and display AND as a wrapper around OpenGL style VOBs but not every method is supported for every type.

  • Thank you Lord and Koogs. Exactly what Koogs mentioned is what I am using the PShape for. I am using it as a wrapper/collection of OpenGL shapes in a VOB. This is a spare time project, so I will do some more investigation and let the Developers know if I find a solution also. If either of you find anything, please let me know.

  • My guess (without checking) would be that setVisible either only affects indexed children (int[]) or only affects named children (hashmap). Depending on how you load your Shape data, you may have one of the other; they are not kept in sync.

    For details, see:

    https://forum.processing.org/two/discussion/18095/how-to-name-a-pshape-child

    PShape shape keeps two unsynced lists of subshapes:

    1. an addChild() index (int)

    2. an addName() hashmap (String).

    Subshapes can be both indexed children and named or just indexed, or just named.

    One workaround might be to loop through your hashmap and add each item manually to the index (or vice-versa) and then call setVisible.

  • @jeremydouglass That's kind of what I was thinking about.
    Just a stupid question - how come you have ended up at so many questions I answer, that too so soon after I answer??

  • Thank you to both of you for all of the help that you have provided on my projects through the years. I will look into this and see if I can get it to work and let you all know.

  • @Lord_of_the_Galaxy -- I'm not sure. It looks like sometime in the last day or two you went through the unanswered list from the past 3 months and responded to a bunch in a sprint. Then last night I went to Recent and commented on several of them -- and most of those recents were all last posts from you.

  • @Schievelbein -- you are welcome, and good luck! I'm looking forward to learning more about this aspect of PShape if you figure it out.

  • @jeremydouglass A funny coincidence, I guess.
    @Schievelbein - Best of luck!

  • The problem is tougher than it seems.

  • I agree, I have been messing with the names vs IDs all week and still have not found a solution.

  • edited December 2016

    I just couldn't figure out anything, except that the problem is quite a common and irritating one.

  • Does anybody know how to put in a bug-fix or enhancement request for this item?

  • UPDATE I changed my code around and used addName() instead of addChild() and the setVisbile() works fine. The trouble is that my code now run 10-20 times slower. From what I can figure out, every time that I do a getChild(name), it goes through every child and compares the search name to the child name, making it so that I search through 50% of the children each time I do a query to modify. Is there any way to put the names in a hash table of sort the names or anything similar? From what I can tell, there is no way to name an indexed child. Is there a way to get the index of the named children? Thank you for the suggestions.

  • edited December 2016

    From what I can tell, there is no way to name an indexed child.

    This is from memory, I thought you could index a named child, which I think is really what you want for lookup speed.

    Did you try retrieving the names child with child = ps.getChild(name)and then adding it to the index with ps.addChild(child, idx)?

    is there a way to get the index of the named children?

    The index doesn't include them, unless you manually added a named child to the index. See my comment above:

    "PShape shape keeps two unsynced lists"

    If you mean the name list, I think is marked private in the Processing source for PShape.

  • Honestly, that source code has to be modified to make it easier to use for advanced users. (By advanced I mean anyone who wishes to modify children independently of the parent PShape)

  • edited December 2016

    When I try the above addChild, it complety obliterates my PShape. I tried to do a getChildIndex(name) and it always gives me -1. I wish they would fix this and make PShape(name) and PShape(idx) the same with just two different accesses, or is that what it already is and I am missing how to use it?

  • I don't know much about this issue but I was thinking, would it be a good idea to document this properly? Or at least to provide some guidance about index vs. name? I am thinking about the lines of a PShape tutorial, or updating the one in the processing website.

    Kf

  • Hopefully it is done in one of the next releases.
    Any way to tell them about it?

Sign In or Register to comment.