Geometry modules

A model or mesh generator can build its geometry with the geometry modules instead of filling vertex arrays by hand. This page tells what each module is for and the rules that make a model build. The function reference is on the module pages:

Module

What it gives

csg

Solids: primitives, booleans, cuts, transforms, colors, material ids, UV

scene

A tree of named model nodes, each with a solid, a pivot and colliders

material

Material rows: a name, a look, a color scale, a texture for a material id

sdf

Signed distance fields with smooth blends and noise, meshed into a solid

surf

Curves and surfaces (lofts, sweeps, patches), thickened or closed into a solid

model_structures

ModelGeometry, the [model_generator] annotation, collider shapes

Units are meters and degrees, and Y is up. A Solid is an expression: each call returns a new one and keeps its input, and nothing is built while the generator runs. The one exception is name: it writes the name on its input and returns the same solid, so a part used twice has the last name. The engine builds the model after the generator returns, on a worker thread.

An example

A model generator is a das file in the generators/ folder of the project. It requires the modules it uses and engine.gen_geometry.model_structures, which brings the [model_generator] annotation. It returns a csg Solid (one model node), the root of a scene Node tree, or a ModelGeometry that it fills by hand (see model generators). A mesh generator, for generate_mesh, has a [mesh_generator] function and requires engine.gen_geometry.mesh_structures instead.

A model generator:

// generators/stool.das
options gen2
require csg
require scene
require material
require engine.gen_geometry.model_structures

[model_generator]
def stool(height : float = 0.45; radius : float = 0.2) : Node {
    material(5, "wood").look("matte")
    // the legs reach 1 cm into the seat: parts that join must overlap
    let legHeight = height - 0.03
    let leg = cylinder(0.02, legHeight).at(radius * 0.7, legHeight * 0.5, 0.0)
    let legs = node("stool", leg.radial(3).rgb(0.1, 0.1, 0.1).mat(4))
    legs.collider(cylinder_shape(radius = radius, height = height, translation = float3(0.0, height * 0.5, 0.0)))
    // a child node: its solid is in model space, its pivot is the origin of the node
    let seat = cylinder(radius, 0.04).at(0.0, height - 0.02, 0.0).rgb(0.55, 0.35, 0.2).mat(5)
    legs.child("seat", seat, pivot(float3(0.0, height, 0.0)))
    return legs
}

The game makes a prefab of it and puts it in the scene:

instantiate_prefab(generate_model("%file/generators/stool.das", (height = 0.6)), NodeData(position = float3(0, 0, 2)))

Primitives are centered at the origin and their axis is Y (extrude keeps the (x, z) of its polygon, revolve, convex, tube and loft use their points as given). Place a part with at, rot_* and scale; repeat it with radial, linear and sym_*. rgb colors the faces (linear RGB), and mat picks the material id: 0 paint, 1 chrome, 2 matte, 3 glass, 4 metallic, or a row of the material module. Booleans, a cut and a side profile:

[model_generator]
def bracket(holes : int = 3) : Solid {
    var plate = box(1.2, 0.1, 0.4, 0.01)
    // the cutters are longer than the plate is thick: no face of a cutter lies on a face of the plate
    plate -= cylinder(0.05, 0.2).at(-0.4, 0.0, 0.12).linear(holes, float3(0.4, 0.0, 0.0)).sym_z()
    // a side profile in (x, y): the polygon is in (x, z), rot_x(-90.0) turns z to y
    let profile = [float2(-0.3, 0.0), float2(0.3, 0.0), float2(-0.3, 0.3)]
    let rib = extrude(profile, 0.04).rot_x(-90.0).at(0.0, 0.04, 0.0)
    // the color goes on after the cut, so the walls of the holes get it too
    return plate.rgb(0.3, 0.3, 0.35) | rib.rgb(0.8, 0.3, 0.1).mat(4)
}

sdf and surf give a csg Solid too, so their shapes join the others:

[model_generator]
def rock(seed : int = 7) : Solid {
    var field = sdf_sphere(0.5) | sdf_box(0.6, 0.3, 0.4).at(0.2, 0.2, 0.0)
    field = field.smooth_union(sdf_capsule(0.15, 0.8).rot_z(60.0), 0.1)
    // solid() meshes the field into a csg Solid; a coarse cell keeps the build fast
    return solid(field.noise(seed, 0.03, 4.0).rgb(0.4, 0.35, 0.3).cell(0.03)).mat(2)
}

[model_generator]
def vase(height : float = 0.6) : Solid {
    // a profile in the xy plane: x is the radius, y the height
    let profile = curve([float2(0.12, 0.0), float2(0.2, height * 0.3), float2(0.08, height * 0.8), float2(0.11, height)], xy)
    let wall = revolve(profile, float3(0.0, 1.0, 0.0))
    // thicken() makes the open surface a closed Solid; the base starts below the wall, so no faces coincide
    let base = cylinder(0.125, 0.02)
    return (thicken(wall, 0.01) | base).rgb(0.2, 0.4, 0.7)
}

The test samples/tests/success/geometry_modules_docs runs these examples.

Rules for a model that builds

  • Parts that join overlap a little (1 mm or more, more for coarse round parts), or keep a clear gap. Parts that only touch, and coincident coplanar faces (a cutter that ends on a face, two boxes side by side), can fail the build or leave seams. Make a cutter longer than the part it cuts.

  • A subtraction colors its cut faces as the cutter: color the cutter like the part, or color the result.

  • A rotation that is not a multiple of 90 degrees makes the contacts inexact: rotate a finished part, not the parts before they join.

  • The build first tries the fast kernel and, when that one refuses a part, the exact kernel, which can be ten times slower: a warning in the log names the part. A union of parts whose bounding boxes do not overlap needs no boolean; radial and linear add one union per copy, as a loop does.

  • set_quality(q) scales the segment counts of the whole model; a primitive takes seg for its own count. sdf builds at the cell of the final field: half the cell gives up to four times the triangles and the build time, less where flat faces merge.

  • A result has 500000 triangles and 64 materials at most. 250000 triangles is the budget of one call of the exact kernel, not of the fast one: the build can split a larger boolean region into batches for the exact kernel, and a region that no batch fits is refused by it. The das code of one run of a generator has 100 MB of heap and 10 MB of strings, and about 15 s in the editor and 30 s in a player; the compile, the conversion of the returned geometry and the csg build do not count. Past the limit the run fails with Generator '<path>' ran for more than <N> s and was stopped.

  • A module-level var starts each run as declared. A global with an initializer that runs code (let SIZES = [1.0, 2.0]) fails the compile with [init] is disabled: put the array in a function. Daslang keywords such as label, type and default cannot be names.

  • Colliders are explicit: .collider(shape) on a scene node; a Solid result has none. See Colliders and materials.

Colliders and materials

How the result becomes a model (the nodes, the materials and their names, the colliders, the limits) is in CSG models of the generator page. When the build fails, the log has a line <generator>: csg: <reason>, often with the file and the line of the part. A good csg build logs csg model <generator>: ... triangles, ... ms or csg mesh <generator>: ... triangles, ... ms to the editor console: read its time and triangle count when you tune seg, set_quality, cell and error. A generator that returns a ModelGeometry or a MeshGeometry logs no such line.