Skip to content

Introduction to ShapeJS

Published: at 06:51 PM

So today I got the chance to play around a bit with ShapeJS, an attempt of Shapeways to make parametric 3D modelling more accessible for programmers, and to make conventional 3D modelling more flexible for designers. It’s still in a beta phase, so what you get is a few examples and limited documentation. But this was more than enough for me to take a look at it.

After playing around for a bit, I started working on a prototype as well. It is basically a ring made up from spheres, all with variable radii. On top of that, I added an adjustable text on the ring, which even wraps along the ring itself. As the name suggests, it is all Javascript based, resulting in a really flexible (and simple)  way of programming a 3D model, especially if you already have some experience in javascript.

Below I posted the code to create the ring, documented as well as I could to make my whole process as clear as possible. I did this partly because I found that some examples currently on the ShapeJS website weren’t really explanatory, and I think another example might help interested people (like me) to get started.

To create the sample, copy the code below and paste it into the ShapeJS Development Environment.

//CONFIG PARAMETERS
//-----------------
var voxelSize = 0.2*MM; //resolution of final rendering, lower->high-res
var a = 40*MM; //Grid size, represents a box twice the width of this parameter where the final object should fit in

//must-have when doing things in circles
function degtoRad(deg) {
 return (deg*(Math.PI/180));
}
//helpful constants
XAXIS = new Vector3d(1,0,0);
YAXIS = new Vector3d(0,1,0);
ZAXIS = new Vector3d(0,0,1);

//function which you can fool around with
function createObject() {

  var radius = 5*MM; //default radius of all balls
  var circleRadius = 20*MM; //radius of balls in circle
  var union = new Union();

  var pos = new Vector3d(0,0,0); //start in center
  var zindex = 0; //parameter to add some depth to creation
  for(var i=0; i<480; i+=20) { //more than 360 degrees, to get the spiral effect

    //calculate new position
    pos.x = circleRadius * Math.sin(degtoRad(i));
    pos.y = circleRadius * Math.cos(degtoRad(i));
    pos.z += 0.5*MM; //each ball shifts in depth this much

   var ball = new Sphere(pos,radius);
   union.add(ball);
  }
  //----------TEXT on the RING
  //add text to ring, just for illustration
  var ringText = new Text("Awesome!","Arial",circleRadius*1.5,circleRadius,5*MM,voxelSize);
  //add transformations (rotate and translate) otherwise it will be put in the center
  var transform = new CompositeTransform();

  //so there's even the possibility to "bend" our text to make it fit
  //around the ring even better, using a ringwrap transform
  //This should happen first, because it can only wrap around the Y-axis
  transform.add(new RingWrap(circleRadius*1.2)); //radius of our ring

  //define what axis to rotate, and the angle (in radians)and add it to our ring
  transform.add(new Rotation(XAXIS,degtoRad(-90)));
  //translate so it fits at the top of the ring
  transform.add(new Translation(0,0.15*radius,2*radius));
  ringText.setTransform(transform);

  //add it to the rest of our ring
  union.add(ringText);
  return union;
}

//Don't mess with this, just standard stuff
function main(args) {
  var maker = new GridMaker();
  //create a grid with the config values at the top of this script
  var grid = createGrid(-a,a,-a,a,-a,a,voxelSize);
  maker.setSource(new createObject()); //the object
  maker.makeGrid(grid);
  return grid;
}

If you did everything correctly and press the “run script” button, you’ll see the following:

ShapeJS screenshot. On the left the editor, on the right the model preview

Congrats, you created a parametric ring!

Conclusion

That was fun, but will this ShapeJS develop itself as the new standard for design for 3D printing? It depends, do I have all the tools available that other parametric design packages currently have? (Think Grasshopper and SolidWorks). No I don’t have all those tools; but this is the first open-source solution which is nicely integrated with an already amazing 3D printing platform and community (And one of the few tools that let me do loads of boolean operations without nagging).

I know it is still in beta, but the editor in specific could use some extra attention, to make it easier for starters.  The code editor was a bit narrow, which made it difficult to switch between editing and looking at the result, especially as the script grew bigger.

In conclusion, I’m pretty excited about this tool, and I think it will push the boundaries of 3d printing even further. I’ll post some more about this when I’ve had the chance to work with this some more.


Previous Post
Retrospectify: a tool for doing collaborative retrospectives
Next Post
Simple streamgraph Code sample