csg: solids

To use this module, require it in a generator script (see Geometry modules):

require csg

Constructive solid geometry for model and mesh generators: primitives, booleans, cuts, transforms and face attributes. A Solid is an expression that the engine builds after the generator returns: return a Solid, or a scene Node of solids. Units are meters and degrees, Y is up.

Constants

CSG_UNION = 1

The kind of a union node; the module uses it inside.

CSG_SUBTRACT = 2

The kind of a subtraction node; the module uses it inside.

CSG_INTERSECT = 3

The kind of an intersection node; the module uses it inside.

weighted = 1

The mode of normals(): smooth normals weight each face by its area.

Structures

Solid

A csg solid: a handle of a lazy expression. A call returns a new solid and keeps its input, except name, which writes on its input and returns it; the engine builds the model after the generator returns.

Fields:
  • id : int - The index of the expression in the model; 0 is the empty solid

Primitives

box(size: float3; bevel: float = 0f): Solid

A box of full sizes size meters, centered at the origin.

Arguments:
  • size : float3 - the full sizes along x, y and z in meters

  • bevel : float - a 45 degree cut of every edge in meters, at most half the smallest size; 0 keeps the edges sharp

box(sx: float; sy: float; sz: float; bevel: float = 0f): Solid

A box of full sizes sx, sy, sz meters, centered at the origin.

Arguments:
  • sx : float - the full size along x in meters

  • sy : float - the full size along y in meters

  • sz : float - the full size along z in meters

  • bevel : float - a 45 degree cut of every edge in meters, at most half the smallest size; 0 keeps the edges sharp

capsule(r: float; h: float; seg: int = 0): Solid

A capsule along Y, centered at the origin: a cylinder with a half sphere at each end.

Arguments:
  • r : float - the radius in meters

  • h : float - the full height with the domes in meters, at least 2 r

  • seg : int - segments per full turn around the axis at quality 1, 0 picks them by size and quality

ccyl(r: float; h: float; chamfer: float; seg: int = 0): Solid

A cylinder along Y with chamfered rims, centered at the origin.

Arguments:
  • r : float - the radius in meters

  • h : float - the full height in meters

  • chamfer : float - the size of the chamfer in meters, below r and at most h / 2

  • seg : int - segments per full turn around the axis at quality 1, 0 picks them by size and quality

cone(rBottom: float; rTop: float; h: float; seg: int = 0): Solid

A cone or a truncated cone along Y, centered at the origin.

Arguments:
  • rBottom : float - the radius at y = -h / 2 in meters

  • rTop : float - the radius at y = h / 2 in meters, 0 for a point

  • h : float - the full height in meters

  • seg : int - segments per full turn around the axis at quality 1, 0 picks them by size and quality

convex(points: array<float3>; round: float = 0f; seg: int = 0): Solid

The convex hull of points in model space; round grows it by a ball, which rounds its edges and corners.

Arguments:
  • points : array<float3> - the points in meters

  • round : float - the radius of the ball in meters, 0 for sharp edges

  • seg : int - segments per full turn of a rounding at quality 1, 0 picks them by size and quality

cubesphere(r: float; seg: int = 0): Solid

A sphere of radius r meters at the origin made from a subdivided cube, with one UV chart per cube face.

Arguments:
  • r : float - the radius in meters

  • seg : int - segments per full turn at quality 1, 0 picks them by size and quality

cylinder(r: float; h: float; seg: int = 0): Solid

A cylinder along Y, centered at the origin.

Arguments:
  • r : float - the radius in meters

  • h : float - the full height in meters

  • seg : int - segments per full turn around the axis at quality 1, 0 picks them by size and quality

ellipsoid(rx: float; ry: float; rz: float; seg: int = 0): Solid

An ellipsoid with the radii rx, ry, rz meters, centered at the origin.

Arguments:
  • rx : float - the radius along x in meters

  • ry : float - the radius along y in meters

  • rz : float - the radius along z in meters

  • seg : int - segments around the Y axis at quality 1, 0 picks them by size and quality

empty(): Solid

The empty solid: a start value for a union in a loop.

extrude(poly: array<float2>; h: float; bevel: float = 0f; round: float = 0f; seg: int = 0): Solid

A prism: the polygon in the (x, z) plane extruded along Y, its height centered on y = 0.

Arguments:
  • poly : array<float2> - the outline as (x, z) points in meters, any winding, no holes, not self-crossing

  • h : float - the full height in meters

  • bevel : float - a 45 degree cut of the rims of both caps in meters, at most h / 2; 0 for none

  • round : float - a quarter circle rounding of the rims of both caps in meters, at most h / 2; 0 for none; use bevel or round, not both

  • seg : int - segments per full turn of a rounding at quality 1, 0 picks them by size and quality

icosphere(r: float; level: int = 3): Solid

A geodesic sphere of radius r meters at the origin: an icosahedron, subdivided.

Arguments:
  • r : float - the radius in meters

  • level : int - the subdivision depth, 0 to 7

loft(sections: array<array<float3>>; closed: bool = false): Solid

A solid through cross sections in model space, with flat caps at the ends.

Arguments:
  • sections : array<array<float3>> - the sections, each an array of points in meters, all with the same point count

  • closed : bool - true joins the last section to the first, a ring with no caps

rbox(sx: float; sy: float; sz: float; radius: float; seg: int = 0): Solid

A box with rounded edges and corners, centered at the origin.

Arguments:
  • sx : float - the full size along x in meters

  • sy : float - the full size along y in meters

  • sz : float - the full size along z in meters

  • radius : float - the radius of the rounding in meters, at most half the smallest size

  • seg : int - segments per full turn of a rounding at quality 1, 0 picks them by size and quality

revolve(profile: array<float2>; angle: float = 360f; seg: int = 0): Solid

A solid of revolution around the Y axis.

Arguments:
  • profile : array<float2> - (radius, y) points in meters, an open line that starts and ends on the axis, or a closed loop off the axis

  • angle : float - the sweep in degrees; below 360 the ends get flat caps

  • seg : int - segments per full turn at quality 1, 0 picks them by size and quality

sphere(r: float; seg: int = 0): Solid

A sphere of radius r meters at the origin, with a latitude-longitude tessellation and UV.

Arguments:
  • r : float - the radius in meters

  • seg : int - segments around the Y axis at quality 1, 0 picks them by size and quality

torus(R: float; r: float; seg: int = 0; segMinor: int = 0): Solid

A torus around the Y axis, centered at the origin.

Arguments:
  • R : float - the radius of the ring center line in meters

  • r : float - the radius of the tube in meters, below R

  • seg : int - segments around the Y axis at quality 1, 0 picks them by size and quality

  • segMinor : int - segments around the tube at quality 1, 0 takes half of seg, or picks them by size and quality when seg is 0

tube(path: array<float3>; r: float; seg: int = 0): Solid

A round tube along a polyline, with round elbows and flat caps.

Arguments:
  • path : array<float3> - the points of the center line in model space, in meters; segments that are not neighbours must stay 2 r apart

  • r : float - the radius in meters

  • seg : int - segments around the tube at quality 1, 0 picks them by size and quality

Booleans and cuts

Solid&(a: Solid; b: Solid): Solid

The intersection of two solids: the volume inside both.

Arguments:
Solid&=(a: Solid; b: Solid)

Sets a to the intersection of a and b.

Arguments:
Solid-(a: Solid; b: Solid): Solid

Subtracts b from a. The faces that b cuts show the attributes of b: color the cutter like the part, or color the result.

Arguments:
Solid-=(a: Solid; b: Solid)

Subtracts b from a in place.

Arguments:
Solid|(a: Solid; b: Solid): Solid

The union of two solids. Parts that join must overlap a little; parts whose bounding boxes do not overlap are joined without a boolean.

Arguments:
Solid|=(a: Solid; b: Solid)

Sets a to the union of a and b.

Arguments:
chamfer(a: Solid; w: float; angle: float = 30f): Solid

Cuts the convex edges of the solid whose faces turn by more than angle; an edge with no room stays sharp, with a warning.

Arguments:
  • a : Solid - the solid

  • w : float - how far back along each face the cut goes, in meters

  • angle : float - the smallest turn of an edge that gets a chamfer, in degrees, between 0 and 180

cut(a: Solid; n: float3; d: float): Solid

Cuts the solid with a plane and keeps the side where dot(n, p) <= d.

Arguments:
  • a : Solid - the solid

  • n : float3 - the normal of the plane, toward the removed side

  • d : float - the offset of the plane along n, in meters when n is a unit vector

cut_above(a: Solid; y: float): Solid

Removes the part of the solid above the height y meters.

Arguments:
cut_below(a: Solid; y: float): Solid

Removes the part of the solid below the height y meters.

Arguments:
keep_x(a: Solid; min: float; max: float): Solid

Keeps the slab min <= x <= max of the solid, in meters.

Arguments:
  • a : Solid

  • min : float

  • max : float

keep_y(a: Solid; min: float; max: float): Solid

Keeps the slab min <= y <= max of the solid, in meters.

Arguments:
  • a : Solid

  • min : float

  • max : float

keep_z(a: Solid; min: float; max: float): Solid

Keeps the slab min <= z <= max of the solid, in meters.

Arguments:
  • a : Solid

  • min : float

  • max : float

Transforms and arrays

at(a: Solid; p: float3): Solid

Moves the solid by p meters.

Arguments:
at(a: Solid; x: float; y: float; z: float): Solid

Moves the solid by (x, y, z) meters.

Arguments:
  • a : Solid

  • x : float

  • y : float

  • z : float

linear(a: Solid; n: int; step: float3): Solid

The union of n copies of the solid, copy i moved by step * i meters.

Arguments:
  • a : Solid

  • n : int

  • step : float3

mirror_x(a: Solid): Solid

The mirror image of the solid across the plane x = 0.

Arguments:
mirror_y(a: Solid): Solid

The mirror image of the solid across the plane y = 0.

Arguments:
mirror_z(a: Solid): Solid

The mirror image of the solid across the plane z = 0.

Arguments:
radial(a: Solid; n: int): Solid

The union of n copies of the solid, copy i turned about the Y axis by 360 * i / n degrees.

Arguments:
rot(a: Solid; axis: float3; deg: float): Solid

Rotates the solid about an axis through the origin by deg degrees, by the right-hand rule.

Arguments:
  • a : Solid

  • axis : float3

  • deg : float

rot_x(a: Solid; deg: float): Solid

Rotates the solid about the X axis by deg degrees: rot_x(90.0) turns +Y to +Z.

Arguments:
rot_y(a: Solid; deg: float): Solid

Rotates the solid about the Y axis by deg degrees: rot_y(90.0) turns +Z to +X.

Arguments:
rot_z(a: Solid; deg: float): Solid

Rotates the solid about the Z axis by deg degrees: rot_z(90.0) turns +X to +Y.

Arguments:
scale(a: Solid; s: float): Solid

Scales the solid about the origin by s.

Arguments:
scale(a: Solid; sx: float; sy: float; sz: float): Solid

Scales the solid about the origin, a factor per axis; a negative factor mirrors.

Arguments:
  • a : Solid

  • sx : float

  • sy : float

  • sz : float

sym_x(a: Solid): Solid

The union of the solid and its mirror image across the plane x = 0.

Arguments:
sym_y(a: Solid): Solid

The union of the solid and its mirror image across the plane y = 0.

Arguments:
sym_z(a: Solid): Solid

The union of the solid and its mirror image across the plane z = 0.

Arguments:

Attributes

color(a: Solid; c: uint): Solid

Sets the color of all faces of the solid from 0xRRGGBB: each byte / 255 is a linear value, as rgb takes, not sRGB.

Arguments:
color_uncolored(a: Solid; c: uint): Solid

Sets the color of the faces that no earlier color call colored, from 0xRRGGBB: each byte / 255 is a linear value, not sRGB.

Arguments:
flat(a: Solid): Solid

Hard shading: each face of the solid keeps its own normal.

Arguments:
mat(a: Solid; id: int): Solid

Sets the material id of all faces of the solid, 0 to 65535: 0 paint, 1 chrome, 2 matte, 3 glass, 4 metallic, or the id of a material row.

Arguments:
mat_unset(a: Solid; id: int): Solid

Sets the material id of the faces that no earlier mat call set.

Arguments:
name(a: Solid; text: string): Solid

Gives the solid a name for the errors and warnings of the build; returns the same solid.

Arguments:
  • a : Solid

  • text : string

normals(a: Solid; mode: int): Solid

With weighted, the smooth normals of the solid weight each face by its area; faces with normals of their own keep them.

Arguments:
rgb(a: Solid; r: float; g: float; b: float): Solid

Sets the color of all faces of the solid, linear RGB from 0 to 1; the default color is grey 0.8.

Arguments:
  • a : Solid

  • r : float

  • g : float

  • b : float

rgb_uncolored(a: Solid; r: float; g: float; b: float): Solid

Sets the color of the faces that no earlier color call colored, linear RGB from 0 to 1.

Arguments:
  • a : Solid

  • r : float

  • g : float

  • b : float

smooth(a: Solid): Solid

Smooth shading across the faces of the solid.

Arguments:
uv_box(a: Solid; scale: float): Solid

UV of the solid from a box projection, each face along its main axis: one UV unit is scale meters.

Arguments:
  • a : Solid

  • scale : float

uv_cyl(a: Solid; scale: float): Solid

UV of the solid from a projection on a cylinder around Y: u is one turn, v is y / scale.

Arguments:
  • a : Solid

  • scale : float

uv_offset(a: Solid; du: float; dv: float): Solid

Adds (du, dv) to the UV of the solid.

Arguments:
  • a : Solid

  • du : float

  • dv : float

uv_planar(a: Solid; axis: float3; scale: float): Solid

UV of the solid from a planar projection along axis: one UV unit is scale meters.

Arguments:
  • a : Solid

  • axis : float3

  • scale : float

uv_rotate(a: Solid; deg: float): Solid

Rotates the UV of the solid about (0, 0) by deg degrees, from u toward v.

Arguments:
uv_scale(a: Solid; su: float; sv: float): Solid

Multiplies the UV of the solid: u by su, v by sv.

Arguments:
  • a : Solid

  • su : float

  • sv : float

uv_sphere(a: Solid): Solid

UV of the solid from the angles around the origin.

Arguments:

Quality and helpers

cos_sin(deg: float): float2

The cosine and the sine of an angle in degrees, exact at multiples of 90 degrees.

Arguments:
  • deg : float

set_quality(q: float)

Sets the tessellation quality of the whole model: above 1 gives more segments, below 1 fewer; the last call wins.

Arguments:
  • q : float - the quality, finite and above 0; the default is 1

turned(v: float3; k: float3; cs: float2): float3

The vector v turned about the unit axis k by the angle whose cosine and sine are cs, counterclockwise seen from the tip of k.

Arguments:
  • v : float3

  • k : float3

  • cs : float2