Skip to content

Instantly share code, notes, and snippets.

@vormplus
Created January 11, 2014 15:36
Show Gist options
  • Save vormplus/8372326 to your computer and use it in GitHub Desktop.
Save vormplus/8372326 to your computer and use it in GitHub Desktop.
Code to add color to mesh. The color is stored in the label field of each face. Tested with HE_Mesh 1.8.2 (http://hemesh.wblut.com/) and Processing 1.5.1 (http://processing.org/)
import wblut.math.*;
import wblut.processing.*;
import wblut.core.*;
import wblut.*;
import wblut.hemesh.*;
import wblut.geom.*;
import processing.opengl.*;
HE_Mesh mesh;
WB_Render render;
// A color palette with 6 colors, one for each side of the cube
color[] colors = { #ECD078, #D95B43, #C02942, #542437, #53777A, #FFFFFF };
void setup()
{
size( 800, 800, OPENGL );
HEC_Cube cube = new HEC_Cube().setEdge( 200 );
mesh = new HE_Mesh( cube );
// loop through the faces of the cube, assign a color to each face
// and store it in the label field of the HE_Face
// Colors in Processing are integers, and the label field of HE_Face
// is also an integer, so this will work fine!
int currentColor = 0;
for ( HE_Face f : mesh.getFacesAsList() ) {
f.setLabel( colors[ currentColor ] );
currentColor++;
}
// If we subdivide the mesh, the newly created faces will still be
// the same color as the old face
HES_Planar planar = new HES_Planar();
planar.setRandom( true );
planar.setRange( 0.2 );
mesh.subdivide( planar, 2 );
// You can even triangulate the mesh to exportg it to VRML or AMF
// for 3D printing. The colors will stay in place.
mesh.triangulate();
render = new WB_Render( this );
}
void draw()
{
background( 0 );
translate( width/2, height/2 );
lights();
rotateY( radians( frameCount ) );
rotateX( radians( frameCount ) );
// Draw each face separately instead of drawing all faces at once
// Use the label of the face as the fill color.
for ( HE_Face f : mesh.getFacesAsList() ) {
noStroke();
fill( f.getLabel() );
render.drawFace( f );
}
noFill();
stroke( 0 );
render.drawEdges( mesh );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment