Creating 3D Shapes with Hemesh

Posted on 2010-12-14 by Jan Vantomme
Tags: processing, tutorial

EDIT: Some things have changed in the latest version of Hemesh. These examples don't work with this version. I wrote an article that explains how to use the new WB_Render class to draw the meshes to the screen. Read it over here.

Hemesh is a library for Processing created by Frederik Vanhoutte (W:Blut). In this article I'm going to show you how you can use this library to create complex 3D shapes with only a few lines of code. The libary does the heavy lifting for you. Download hemesh and install it in your Processing libraries folder to get started.

Creators

The HEC_Creator class is the base class for creating a mesh. All 3D shapes in the libary are based on this. I'm going to show you how you can construct most of the shapes in this library. You will usually start with creating a new object and call of the methods needed to setup the shape. The code needs to be called in the setup() function and will look somewhat like this:

HEC_Creator creator = new HEC_Modeltype(this).methodOne(200).methodTwo(200);

Box

  • new HEC_Box(this) Constructor for the box.
  • setWidth() Set the width of the box. (float)
  • setHeight() Set the height of the box. (float)
  • setDepth() Set the depth of the box. . (float)
  • setWidthSegments() Set the number of segments for the width. (integer)
  • setHeightSegments() Set the number of segments for the height. (integer)
  • setDepthSegments() Set the number of segments for the depth. (integer)

A box created with Hemesh

The code to recreate this image can be found in the sketch hemesh_box.pde.

Cone

  • new HEC_Cone(this) Constructor for a cone.
  • setFacets() Set the number of sides for the bottom face. (integer)
  • setRadius() Set the radius of the bottom face. (float)
  • setHeight() Set the height of the cone. (float)
  • setSteps() Set the number of vertical divisions of the cone. (integer)

A cone created with Hemesh

The code to recreate this image can be found in the sketch hemesh_cone.pde.

Cube

  • new HEC_Cube(this) Constructor for a cube.
  • setEdge() Set the length of the edge of the cube. (float)
  • setDepthSegments() Set the number of segments for the depth of the cube. (integer)
  • setHeightSegments() Set the number of segments for the height of the cube. (integer)
  • setWidthSegments() Set the number of segments for the width of the cube. (integer)

A cube created with Hemesh

The code to recreate this image can be found in the sketch hemesh_cube.pde.

Cylinder

  • new HEC_Cylinder(this) Constructor for a cylinder.
  • setFacets() Set the number of facets for the ground and top plane of the cylinder. (integer)
  • setHeight() Set the height of the cylinder. (float)
  • setRadius() If you use one parameter, it will be used to set the radius of both top and bottom planes, if you use two parameters, the first one will be used for the top plane, the second one for the bottom plane. (float)
  • setSteps() Set the number of vertical divisions for the cylinder. (integer)

A cylinder created with Hemesh

The code to recreate this image can be found in the sketch hemesh_cylinder.pde.

Dodecahedron

  • new HEC_Dodecahedron(this) Constructor for a dodecahedron.
  • setEdge() Set the length of the edge of the dodecahedron. (float)

A dodecahedron created with Hemesh

The code to recreate this image can be found in the sketch hemesh_dodecahedron.pde.

Geodesic

  • new HEC_Geodesic(this) Constructor for a geodesic sphere.
  • setLevel() Set the level of recursive divisions. Don't set this too high, try with a number below 10. (integer)
  • setRadius() Set the radius for the geodesic sphere. (float)

A geodesic sphere created with Hemesh

The code to recreate this image can be found in the sketch hemesh_geodesic.pde.

Grid

  • new HEC_Grid(this) Constructor for a grid.
  • setSizeU() Set the width of the plane. (float)
  • setSizeV() Set the height of the plane. (float)
  • setResU() Set the number of divisions for the width. (integer)
  • setResV() Set the number of divisions for the height. (integer)

A grid created with Hemesh

The code to recreate this image can be found in the sketch hemesh_grid.pde.

Icosahedron

  • HEC_Icosahedron(this) Constructor for an icosahedron.
  • setEdge() Set the length of the edge for the icosahedron.

An icosahedron created with Hemesh

The code to recreate this image can be found in the sketch hemesh_icosahedron.pde.

Octahedron

  • HEC_Octahedron(this) Constructor for an octahedron.
  • setEdge() Set the length of the edge for the octahedron. (float)

An octahedron created with Hemesh

The code to recreate this image can be found in the sketch hemesh_octahedron.pde.

Sphere

  • HEC_Sphere(this) Constructor for a sphere.
  • setRadius() Set the radius of the sphere. (float)
  • setUFacets() Set the number of divisions along the equator of the sphere. (integer)
  • setVFacets() Set the number of divisions along the meridian of the sphere. (integer)

A sphere created with Hemesh

The code to recreate this image can be found in the sketch hemesh_sphere.pde.

Tetrahedron

  • HEC_Tetrahedron(this) Constructor for an tetrahedron.
  • setEdge() Set the length of the edge for the tetrahedron.

A tetrahedron created with Hemesh

The code to recreate this image can be found in the sketch hemesh_tetrahedron.pde.

Display the Mesh

Initialize a variable before the setup() function.

HE_Mesh mesh;

Us a creator inside the setup and pass it to the new HE_Mesh object.

HEC_Creator creator = new HEC_Box(this);
mesh = new HE_Mesh( creator );

To display the mesh, you'll need to add this to the draw function:

mesh.drawFaces();

Download

Download all examples from this tutorial on hemesh.

Tweet this article