Meshdef Language by Chris Meub on 8/15/2015


Meshdef is a tool for generating 3D meshes and scenes using a simple syntax.

I sometimes have a need or desire to create 3D models. Not being an artist, I have always found it difficult to convert the representation in my mind into the operations of 3DS Max or Blender. It is easier for me to think of an algorithm to create the structure using vertices, edges, and faces. Meshdef is an experimental domain-specific language to fill that role.

The approach I took was inspired from turtle graphics. However, it might be more helpful to think of it as "spider graphics". Like a spider, you create edges. Edge lists of equal dimension combine to generate faces.

The language is built for fast prototyping. All commands are one or two letters. Loops implicitly create their own control variables. A state machine manages various settings such as color and draw mode. The language may appear declarative, but it is imperative at its core. It is sort of like an SVG path language with control statements thrown in. The compiler does the work of welding similar vertices together (with a configurable epsilon value) and creating index lists. This makes it easy to create optimized meshes without any gaps.

Have a look at an example:

C100,120,220,0
X16
(
    [
        X10( Vz-1 Rx180/(#-1) ) E
    ]
    Rz360/#
)
Ef

Here is the model generated:

Blue 3D Sphere

Here is the same code with annotations:

// Plan: Compose a sphere made of 16 vertical slices.
// Each slice will have 10 faces.

// set the pen color using R,G,B,A.
C100,120,220,0

// repeat the following command 16 times.
X16
( // (parenthesis are used to group a set of commands as one.)

    [// push the transform stack.

        // repeat the following command 10 times.
        X10
        (
            // push a vertex with z=-1 and x,y=0,0
            Vz-1 

            // rotate about the x-axis 180 degrees
            // divided by the repeat number (10) minus 1.
            Rx180/(#-1) 
        )
        E // flush the vertex list as an edge

    ] // pop the transform stack.

    // rotate about the z-axis by 360 degrees
    // divided by the repeat number (16).
    Rz360/#
)
Ef // flush the edge list as a triangle list

Here is a more complex model:

Striated Blue 3D Crown

And the script which builds it (The alpha values are being used to control a glow shader):

C110,180,180
X10
(
    [ 
        Ty10
        X15
        (
            Ca0

            { X4( Vz-1 V1,1,-1 V1,1,1 Vz1 E Rz90 ) Ef }

            S0.89
            Tz1.9
            Rx10

            Ca7

            { X4( Vz-1 V1,1,-1 V1,1,1 Vz1 E Rz90 ) Ef }

            S0.89
            Tz1.9
            Rx10
        )
    ]
    Rz360/#
)

If you have interest in using a tool like this, let me know.

I'll leave you with some images of meshes I've written using this tool:

Red Five-petal Flower Model

Brown 3D Bench Model

Recursive Mesh Model

Curved 3D Vase Model