The colouring of 3D models in most of the standard file formats is usually achieved via texturing or setting a base color/material properties for the entire (sub)mesh. On the other hand, the direct coloring of individual vertices or faces without textures is not that commonly supported. (Binary) STL supports the individual coloring of faces,
however there're 2 major variations/formats of how this is done and they're not compatible with each other. For
toxiclibs I've implemented both of these color models, since I didn't know which software was used by the 3D print studio I was working with back then.
Even though these color models are supported, they're not used by default when exporting a TriangleMesh instance. The main reason for this is the current lack of color/texture support by the mesh class itself, but you could trivially create your own version of saveAsSTL() and do something like:
Code:/* a custom mesh STL exporter */
import toxi.geom.*;
import toxi.geom.util.*;
// assume this has been populated somehow...
TriangleMesh mesh;
// create stl color model with mesh base color
// the true flag means facets can have their own RGB value
MaterialiseSTLColorModel colModel=new MaterialiseSTLColorModel(0x112233,true);
// create STLWriter instance
STLWriter stl = new STLWriter(colModel);
// write the file header
stl.beginSave(sketchPath("color.stl"), mesh.getNumFaces());
int k=0;
// iterate over all mesh faces
for (Iterator i=mesh.faces.iterator(); i.hasNext();) {
TriangleMesh.Face f=(TriangleMesh.Face)i.next();
// tint every 2nd face in alternating colors
// btw. a RGB value of -1 will disable the face color
// and revert to the default mesh color
stl.face(f.b, f.a, f.c, f.normal, 0==k%2 ? 0xff00ff : 0xffff00);
k++;
}
stl.endSave();
Using the Materialise color model will only be successfully picked up by their Magics software (incl. MiniMagics, the only version I've ever tested with). As far as I know, MeshLab and a lot of the other STL viewers don't even check color info for STL files...
If you want to use this with
Shapeways, then you're correct that you will need VRML or X3D files for this eventually. However, other 3D print studios might not be that limited, especially if they're using the Materialise tool chain (as a lot of them seem to do)... shop around!