• fullscreen
  • control.pde
  • controlPanel.pde
  • createFloor.pde
  • drawMesh.pde
  • loadColors.pde
  • loadMesh.pde
  • meshViewer.pde
  • /*
     * Peasycam style scene controls
     */
    
    void mouseDragged(){
      rotX += map(mouseY-pmouseY,0,height,0,TWO_PI);
      rotY += map(mouseX-pmouseX,0,width,0,TWO_PI);
    }
    
    void keyPressed() {
      switch(keyCode){
        case UP:
          zoom *= 1.02;
          break;
        case DOWN:
          zoom /= 1.02;
          break;
        case ' ':
          psychedelic = !psychedelic;
          break;
      }
      if(key == 's'){
        save("pic.tiff");
      } 
    }
    
    
    /*
     * ControlP5 selection panel
     */
    
    void controlPanel(){
      cp5 = new ControlP5(this);
      cp5.addButton("mesh 1",0, 40,40,20,20).setCaptionLabel("");
      cp5.addButton("mesh 2",0, 70,40,20,20).setCaptionLabel("");
      cp5.addButton("mesh 3",0,100,40,20,20).setCaptionLabel("");
      cp5.addButton("mesh 4",0,130,40,20,20).setCaptionLabel("");
      cp5.addButton("mesh 5",0,160,40,20,20).setCaptionLabel("");
      cp5.setAutoDraw(false);
    }
    
    void controlEvent(ControlEvent theControlEvent){
      if(theControlEvent.isFrom("mesh 1")){
        mesh = mesh1;
        colors = col1;
      }
      if(theControlEvent.isFrom("mesh 2")){
        mesh = mesh2;
        colors = col2;
      }
      if(theControlEvent.isFrom("mesh 3")){
        mesh = mesh3;
        colors = col3;
      }
      if(theControlEvent.isFrom("mesh 4")){
        mesh = mesh4;
        colors = col4;
      }
      if(theControlEvent.isFrom("mesh 5")){
        mesh = mesh5;
        colors = col5;
      }
    }
    
    
    /*
     * Creates an image with a color gradient in the direction of the y-axis
     */
    
    PImage createFloor(color backgroundColor){
      PImage img = createImage(width,height,RGB);
    
      for (int y = 0; y < height; y++){
        for (int x = 0; x < width; x++){
          int loc = x + y*width;
          float gradientR = map(y,0,height,red(backgroundColor),40);
          float gradientG = map(y,0,height,green(backgroundColor),40);
          float gradientB = map(y,0,height,blue(backgroundColor),40);
          img.pixels[loc] = color(gradientR-15*y/height,gradientG-15*y/height,gradientB);
        }
      }
      
      return img;
    }
    
    
    /*
     * Draws the mesh with or without some psychedelia ;)
     */
    
    void drawMesh(HE_Mesh mesh, color[] colors, boolean psy){
      noStroke();
    
      // The front side
      if(psy){
        Iterator<HE_Face> iter = mesh.fItr();
        for(int i = 0; iter.hasNext(); i++){
          HE_Face f = iter.next();
          color c = colors[i];
          fill(blendColor(c,color(random(-150,150),random(-150,150),0),ADD));
          render.drawFace(f);
        }
      } 
      else{
        Iterator<HE_Face> iter = mesh.fItr();
        for(int i = 0; iter.hasNext(); i++){
          HE_Face f = iter.next();
          color c = colors[i];
          fill(c);
          render.drawFace(f);
        }
      }
      // The back side
      pushMatrix();
        fill(255);
        translate(0,0,0.1);
        render.drawFaces(mesh);
      popMatrix();
    }
    
    
    /*
     * Reads the color file created with the Kinect 3D scanner
     */
    
    color[] loadColors(String file){
      String[] colLines = loadStrings(file);
      color[] col = new color[colLines.length];
      
      for(int i = 0; i < colLines.length; i++){
        String[] c = split(colLines[i]," ");
        col[i] = color(float(c[0]),float(c[1]),float(c[2]));
      }
      return col;
    }
    
    
    /*
     * Reads the mesh file created with the Kinect 3D scanner
     */
    
    HE_Mesh loadMesh(String file){
      HEC_FromHemeshFile meshData = new HEC_FromHemeshFile(sketchPath(file));
      HE_Mesh mesh = new HE_Mesh(meshData);
      mesh.moveTo(0,0,0);
      return mesh;
    }
    
    
    /* 
     * Mesh viewer (JGC).
     *
     * Reads and draws various meshes obtained with the Kinect 3D scanner.
     * 
     * Mouse controls like in Peasycam.
     * Press the space bar for some psychedelic effects.
     *
     */
    
    import controlP5.*;
    import wblut.hemesh.modifiers.*;
    import wblut.core.processing.*;
    import wblut.hemesh.creators.*;
    import wblut.hemesh.core.*;
    
    // Prefix of the mesh and color files.
    String file = "data/face";
    
    ControlP5 cp5;
    WB_Render render;
    HE_Mesh   mesh, mesh1, mesh2, mesh3, mesh4, mesh5;
    color[]   colors, col1, col2, col3, col4, col5;
    boolean   psychedelic = false;
    PImage    fakeFloor;       
    
    float zoom = 1;
    float rotX = PI;
    float rotY = 0;
    
    void setup(){
      size(800,600,P3D);
      //smooth();
      
      // Read the mesh files
      mesh1 = loadMesh(file+"0"+".hemesh");
      mesh2 = loadMesh(file+"1"+".hemesh");
      mesh3 = loadMesh(file+"2"+".hemesh");
      mesh4 = loadMesh(file+"3"+".hemesh");
      mesh5 = loadMesh(file+"4"+".hemesh");
    
      // Read the color files
      col1 = loadColors(file+"0"+".col");
      col2 = loadColors(file+"1"+".col");
      col3 = loadColors(file+"2"+".col");
      col4 = loadColors(file+"3"+".col");
      col5 = loadColors(file+"4"+".col");
    
      // Smooth the meshes
      HEM_Smooth modifier = new HEM_Smooth();
      modifier.setIterations(2);
      modifier.setAutoRescale(true);
      mesh1.modify(modifier);
      mesh2.modify(modifier);
      mesh3.modify(modifier);
      mesh4.modify(modifier);
      mesh5.modify(modifier);
    
      // draw() will start with this mesh
      mesh = mesh1;
      colors = col1;
      render = new WB_Render(this);
      
      // Create an image to represent the floor
      fakeFloor = createFloor(color(200));
     
      controlPanel();
    }
    
    void draw(){
      background(200);
      
      // Draw the floor image in an inclined plane
      beginShape();
        texture(fakeFloor);
        vertex(-width,0.75*height,-300,0,0);
        vertex(2*width,0.75*height,-300,width,0);
        vertex(width,height,0,width,height);
        vertex(0,height,0,0,height);
      endShape();
    
      // Draw the control panel
      cp5.draw();
    
      // Position the scene
      lights();
      translate(width/2,height/2,0);
      rotateX(rotX);
      rotateY(rotY);
      scale(zoom);
    
      drawMesh(mesh,colors,psychedelic);
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Javier Graciá Carpio

    Kinect mesh viewer

    Add to Faves Me Likey@! 3
    You must login/register to add this sketch to your favorites.

    Reads and draws various meshes obtained with the Kinect 3D scanner (http://www.openprocessing.org/sketch/62534).
    This video shows it in action:
    https://vimeo.com/42342434

    Doesn't work in the browser. Download the code and run it in your computer. The data directory includes some examples. This way you can try it, even if you don't have a kinect.

    Mouse controls like in Peasycam.
    (Press the space bar for some psychedelic effects)

    Uses the hemesh and controlP5 libraries.
    It only works with this version of controlP5:
    http://code.google.com/p/controlp5/downloads/detail?name=controlP5-1.5.1.zip

    Brendan Flynn
    24 May 2012
    any idea why import wblut.hemesh.options.*; doesnt work in any of the libraries examples. tryed the above program looks cool, must play with it later , for have to hit the weeds now (in garden) sun is a shinning bright in Ireland today. (rare thing)
    Brendan Flynn
    24 May 2012
    question: does it take long to load the scans, and where do they save? ok got to split.
    The Hemesh library examples are not up-to-date. You have to remove the imports in the examples and include the library with the "sketch" -> "import library" option in processing.

    Taking the scans should be fast (few seconds). However, the sketch sometimes gets stuck creating the mesh. Don't know the reason, but i think is related with the Hemesh library. If this happens to you also, just run the sketch again. The files are saved in the sketch directory. You have to copy those files to the viewer sketch directory or indicate the path to them in the sketch.
    Brendan Flynn
    27 May 2012
    cool man, will run it later, lovely day in ireland again today, so bringing the little one to the lake for a swim :) rain promised to come back in tomorrow.
    I found the explanation for the problem when creating the meshes in the processing forums:
    https://forum.processing.org/topic/3d-text-extrude-type-to-form-a-3d-object-detect-edges
    To fix it copy the contents of the Hemesh files HEC_FromFacelist.java and HEC_FromTriangles.java to the sketch, and comment the two following lines:

    package wblut.hemesh.creators;
    mesh.resolvePinchPoints();

    That should work.
    You need to login/register to comment.