Toxiclibs explorations: 3D sketching app

Anyone who follows the developments around Processing, knows about the infamous toxiclibs. These are libraries for adding more and far more powerful functionalities to processing than ever before! The Toxiclibs website contains a bunch of these examples, which all demonstrate a particular part of the libraries. As the name suggests, the toxiclibs aren’t just one, but are a bunch of different libraries, most of them in the area of mesh generation.

After some googling, I found this example at GuruBlog; how to create a tentacle using processing and toxiclibs. He created a sketch that generated a 3D model of a tentacle and generated an stl file from it that was ready for 3D printing. I liked the idea of simple 3D model generation and sending it to a 3D printer right away, but why did it have to be a tentacle?

So to explore the possibilities of 3D modelling using toxiclibs, I started working on a sketch of my own. I started out with using the volumetric brush ( the same tool as the other guy used) to draw a cube, and let it rotate around a few axes.

3Dcube made using Toxiclibs

Next, I started out with creating an app that would let you draw up a complete 3D model, using the volumetric brush. This was more complicated, since it required me to map my X and Y mouse coordinates in a 3D space. After lots of sines and cosines I stuck with some sort of random navigation method, which actually is quite fun to play around with. The typical drawing I make looks like this.. So you might know a useful application for such kinds of 3D models, then please let me know!

Processing sketch running my clickBrush app

Improvements

I will think about improving the navigational system and maybe even adding features as increasing and decreasing brush size, to make even more fancy drawings. I hope to gain access to a 3D printer somewhere in the near future so I can actually verify the stl files ToxciLibs creates.

Source Code

/* ------------------------
ClickBrush sketch by Pepf
22nd of January 2012
----------------------------*/

import processing.opengl.*;
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.volume.*;
import toxi.processing.*;

ToxiclibsSupport gfx;
TriangleMesh mesh;
VolumetricBrush brush;
VolumetricSpace volume;
IsoSurface surface;
float yRot=0;
float xRot=0;
PFont myFont;
float mapY, mapX,xPos,yPos,zPos;

void setup() {
  size(600,600,OPENGL);
  noLoop();
  gfx = new ToxiclibsSupport(this);
  volume = new VolumetricSpaceArray( new Vec3D(600,600,600), 50, 50, 50 );
  surface = new ArrayIsoSurface( volume );
  mesh = new TriangleMesh();
  ellipseMode(CENTER);

  brush = new RoundBrush( volume, 50 );

  myFont = createFont("Arial", 16);
  textFont(myFont);

  volume.closeSides();
  surface.reset();
  surface.computeSurfaceMesh( mesh, .1 );
}

void draw() {
    //important stuff first
    xPos = mapX*cos(yRot);
    yPos = mapX*sin(yRot);
    zPos = -mapY*sin(xRot);

    background(0);
    lights();
    pushMatrix();
    translate( width/2, height/2, 0);
    rotateY( yRot);
    rotateX( xRot);
    scale(1);
    fill(255);
    //X
    stroke(255,0,0);
    line(0,0,300,0);
    line(0,yPos,xPos,yPos);
    //Y
    stroke(0,255,0);
    line(0,0,0,300);
    line(xPos,0,xPos,yPos);
    //Z
    stroke(0,0,250);
    line(0,0,0,0,0,300);
    line(xPos,yPos,0,xPos,yPos,zPos);
    noStroke();
    beginShape(TRIANGLES);
    gfx.mesh( mesh );
    endShape();
    translate(xPos,yPos,zPos);
    sphere(5);
    popMatrix();
    text("X:" + xRot + " - Y:" + yRot, 5,20);
    text("Xpos: " + xPos +"\n"+
        "Ypos: " + yPos + "\n"+
        "Zpos: " + zPos,5,80);
}

void mouseClicked() {
  brush.drawAtAbsolutePos( new Vec3D(xPos,yPos,zPos),0.2);
  volume.closeSides();
  surface.reset();
  surface.computeSurfaceMesh( mesh, .1 );
  redraw();
}
void mouseMoved() {
 mapY = map(mouseY,0,height,-(height/2),height/2);
 mapX = map(mouseX,0,width,-(width/2),width/2);
 redraw();
}
void keyPressed() {
  if (key==CODED) {
  if (keyCode==LEFT) {
      yRot+=0.1;
  }
  if (keyCode==RIGHT) {
     yRot-=0.1;
  }
  if (keyCode==UP) {
      xRot+=0.1;
  }
  if (keyCode==DOWN) {
     xRot-=0.1;
  }
  redraw();
  }
  if (key=='s') {
    mesh.saveAsSTL( sketchPath( "model.stl" ));
  }

}