# graphics3d/toon/meshbuilder (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/graphics3d/toon/meshbuilder/

Building 3D models. (Builds basic shapes in code and puts them into one vertex array)

Builds the vertex array used to draw toon 3D (single-color clay-like material with outlines). It came from the in-board 3D of webgame-rimminer. (Same form as the three.js basic geometries)
  - Shapes are built in their own coordinates with the center at the origin. When a part is added, position, rotation and scale are multiplied in and it is put into one lump.
  - One vertex is position 3, normal 3, color 3 (sRGB), material 2 (glow strength, unlit), pattern 4 (pattern number, pattern color). The meaning of the pattern number and color is decided by the shader that draws it. (0 means no pattern)
  - Texture coordinates (UV) are stored separately, 2 per vertex, in uvData outside the vertex array. If even one vertex was given a UV, the rest are filled with 0. (uvs of a shape, uvs of addTriangles)
  - The second UV is stored the same way in uv1Data. (uvs1 of a shape, uvs1 of addTriangles, TEXCOORD_1 of GLB, for things that read a second unwrap such as the angel ring)
    (0, 0) is the top left of the image. (Same as glTF) A box is 0 ~ 1 per face, a sphere is 0 ~ 1 by longitude and latitude.
  - A material range (materialRanges) is the span of body vertex indices that holds one material. The GLB reader stores one per primitive. (Material name, extras, textures, indexStart and indexCount of ToonRenderer.drawMesh)
  - The outline is a slightly enlarged copy of the part. Since only its back faces are drawn, it is stored separately. (inverted hull)
  - Triangle winding is fixed by looking at the vertex normals so the outside becomes the front face. (Back-face culling and the outline rely on winding)

Example: Build and draw one model made of joined basic shapes

```js
import { ToonRenderer, MeshBuilder, createPartMatrix, createCapsuleGeometry, createSphereGeometry, createCylinderGeometry, createBoxGeometry, Camera3D, Matrix4, Vector3 } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const renderer = new ToonRenderer(webGL2RenderingContext);
const builder = new MeshBuilder();
const bodyMatrix = createPartMatrix(null, [0, 0.6, 0], null, null);
builder.addPart(createCapsuleGeometry(0.3, 0.5, 6, 16), bodyMatrix, "#f0c8a0");
builder.addPart(createSphereGeometry(0.28, 20, 14), createPartMatrix(bodyMatrix, [0, 0.65, 0], null, null), "#f8e0c8", { outlineWidth: 0.015 });
builder.addPart(createCylinderGeometry(0, 0.12, 0.3, 12), createPartMatrix(bodyMatrix, [0, 1.05, 0], null, null), "#d04040", { glow: 0.5 });
builder.addPart(createBoxGeometry(2, 0.1, 2), Matrix4.createTranslation(0, -0.05, 0), "#708060", { hasOutline: false });
const mesh = renderer.createMesh(builder.readArrays());
const camera = new Camera3D();
camera.setPerspective(0.8, 0.1, 100);
camera.lookAt(Vector3.create(0, 1.5, 3), Vector3.create(0, 0.8, 0), Vector3.create(0, 1, 0));
renderer.beginFrameWithCamera(camera);
renderer.drawMesh(mesh, Matrix4.createIdentity());
renderer.endFrame();
```

## MeshBuilder

Builder for one model chunk.

Example: Build several parts into one mesh and draw only one region of the body

```js
import { ToonRenderer, MeshBuilder, createRoundedBoxGeometry, createSphereGeometry, Camera3D, Matrix4, Vector3 } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const renderer = new ToonRenderer(webGL2RenderingContext);
const builder = new MeshBuilder();
builder.addPart(createRoundedBoxGeometry(1, 0.5, 0.6, 3, 0.1), Matrix4.createTranslation(0, 0.25, 0), "#d0d8e0");
const lampStart = builder.getIndexCount();
builder.addPart(createSphereGeometry(0.12, 12, 8), Matrix4.createTranslation(0.5, 0.35, 0), "#ffe080", { glow: 1.5, isUnlit: true });
const lampCount = builder.getIndexCount() - lampStart;
const mesh = renderer.createMesh(builder.readArrays());
const camera = new Camera3D();
camera.setPerspective(0.8, 0.1, 100);
camera.lookAt(Vector3.create(1.5, 1.2, 2), Vector3.create(0, 0.2, 0), Vector3.create(0, 1, 0));
renderer.beginFrameWithCamera(camera);
renderer.drawMesh(mesh, Matrix4.createIdentity(), { indexStart: 0, indexCount: lampStart });
renderer.drawMesh(mesh, Matrix4.createIdentity(), { indexStart: lampStart, indexCount: lampCount, tintColor: [1, 0.5, 0.5, 1], hasOutline: false });
renderer.endFrame();
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `isEmpty()` (method, since 0.4.0): Whether it is empty.
  - returns boolean: true if the body has no triangles.
- `getIndexCount()` (method, since 0.4.0): Number of body vertex indices stored so far. (read before and after a part when drawing that part's body span separately)
  - returns number: Number of body vertex indices. (Length of indexData)

  Example: Note down the span that holds the wheel region

  ```js
  import { MeshBuilder, createBoxGeometry, createCylinderGeometry, createPartMatrix, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createBoxGeometry(1.2, 0.4, 0.6), Matrix4.createTranslation(0, 0.4, 0), "#c04040");
  const wheelStart = builder.getIndexCount();
  builder.addPart(createCylinderGeometry(0.2, 0.2, 0.1, 16), createPartMatrix(null, [0.4, 0.2, 0.35], [Math.PI / 2, 0, 0], null), "#303030");
  const wheelRange = { indexStart: wheelStart, indexCount: builder.getIndexCount() - wheelStart };
  ```

- `getOutlineIndexCount()` (method, since 0.4.0): Number of outline shell vertex indices stored so far. (read before and after a part when drawing that part's shell span separately)
  - returns number: Number of vertex indices of the outline shell. (Length of outlineIndexData)

  Example: Span to draw only the outline shell of the window

  ```js
  import { MeshBuilder, createBoxGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createBoxGeometry(1, 1, 1), Matrix4.createIdentity(), "#a0a0a0");
  const windowOutlineStart = builder.getOutlineIndexCount();
  builder.addPart(createBoxGeometry(0.4, 0.3, 0.05), Matrix4.createTranslation(0, 0.1, 0.5), "#80c0ff", { outlineWidth: 0.01 });
  const windowOutlineRange = { outlineIndexStart: windowOutlineStart, outlineIndexCount: builder.getOutlineIndexCount() - windowOutlineStart };
  ```

- `addPart(geometry, matrix, colorText, options = {})` (method, since 0.4.0): Adds one part. (stores the shape multiplied by the matrix, and with an outline also stores a slightly enlarged copy)
  - `geometry` object: { positions, normals, indices, uvs (optional), uvs1 (second UV, optional) }
  - `matrix` Matrix4: Position, rotation and scale matrix to multiply into the shape.
  - `colorText` string: "#rrggbb"
  - `options` object: { glow glow (0 ~), isUnlit unlit, hasOutline outline (on by default), outlineWidth outline width }

  Example: Glowing eyes and a shadow plate without outline

  ```js
  import { MeshBuilder, createSphereGeometry, createCylinderGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createSphereGeometry(0.5, 24, 16), Matrix4.createTranslation(0, 0.5, 0), "#f0e0d0", { outlineWidth: 0.03 });
  builder.addPart(createSphereGeometry(0.06, 10, 8), Matrix4.createTranslation(0.15, 0.6, 0.45), "#40ffff", { glow: 2, isUnlit: true, hasOutline: false });
  builder.addPart(createCylinderGeometry(0.5, 0.5, 0.01, 24), Matrix4.createTranslation(0, 0.005, 0), "#202020", { hasOutline: false });
  ```

- `addQuad(corners, normal, color, pattern, patternColor, isUnlit)` (method, since 0.4.0): Adds one quad. (faces like block terrain and floors, four points in perimeter order, no outline)
  - `corners` Array<Array<number>>: Four points [x, y, z]
  - `normal` Array<number>: [x, y, z]
  - `color` Array<number>: [r, g, b] sRGB.
  - `pattern` number: Pattern number. (meaning decided by the shader, 0 means none)
  - `patternColor` Array<number> | null: [r, g, b] pattern color. (null if none)
  - `isUnlit` boolean: Unlit.

  Example: Two floor tiles with patterns

  ```js
  import { MeshBuilder, readColorValues } from "./vanilla.js";
  const builder = new MeshBuilder();
  const grassColor = readColorValues("#60a040");
  builder.addQuad([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]], [0, 1, 0], grassColor, 0, null, false);
  builder.addQuad([[1, 0, 0], [2, 0, 0], [2, 0, 1], [1, 0, 1]], [0, 1, 0], grassColor, 1, [0.4, 0.7, 0.3], false);
  ```

- `addQuadShell(quads, outlineWidth)` (method, since 0.4.0): Stores the outline shell of quads. (things built from faces, like ground chunks)
    - Using vertex positions rounded to 1e-4 as keys, adds each distinct face normal once per key and normalizes, then pushes the vertex that way by the width.
    - Face winding is the same as addQuad. (the drawing side culls front faces so only back faces show)
  - `quads` Array<object>: { corners four points [x, y, z], normal [x, y, z] }
  - `outlineWidth` number: Shell thickness. (Length in vertex coordinates)

  Example: Outline shells on the top and side faces of one block

  ```js
  import { MeshBuilder } from "./vanilla.js";
  const builder = new MeshBuilder();
  const quads = [
  	{ corners: [[0, 1, 0], [1, 1, 0], [1, 1, 1], [0, 1, 1]], normal: [0, 1, 0] },
  	{ corners: [[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], normal: [0, 0, 1] },
  ];
  for (const quad of quads) {
  	builder.addQuad(quad.corners, quad.normal, [0.5, 0.4, 0.3], 0, null, false);
  }
  builder.addQuadShell(quads, 0.02);
  ```

- `addTriangles(positions, normals, indices, matrix, color, glow, isUnlit, joints, weights, uvs = null, uvs1 = null)` (method, since 0.4.0): Stores triangles of a loaded model. (one GLB primitive, winding kept as is)
  - `positions` Float32Array: [x, y, z] per vertex.
  - `normals` Float32Array: [x, y, z] normal per vertex.
  - `indices` ArrayLike<number>: Three vertex indices per triangle.
  - `matrix` Matrix4: Matrix to multiply into the vertices.
  - `color` Array<number>: [r, g, b] sRGB.
  - `glow` number: Glow strength. (Multiplied by the base color and added, 0 means none)
  - `isUnlit` boolean: Unlit.
  - `joints` ArrayLike<number> | null: Four joint indices per vertex. (null if none)
  - `weights` ArrayLike<number> | null: Four weights per vertex.
  - `uvs` ArrayLike<number> | null: Two texture coordinates per vertex. (null if none)
  - `uvs1` ArrayLike<number> | null: Two second texture coordinates per vertex. (null if none)

  Example: Store one triangle made in code together with UVs

  ```js
  import { MeshBuilder, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);
  const normals = new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1]);
  const uvs = new Float32Array([0, 1, 1, 1, 0, 0]);
  builder.addTriangles(positions, normals, [0, 1, 2], Matrix4.createIdentity(), [1, 0.8, 0.6], 0, false, null, null, uvs);
  const arrays = builder.readArrays();
  ```

- `addOutlineTriangles(positions, indices, matrix, joints, weights)` (method, since 0.4.0): Stores the outline shell of a loaded model. (already inflated, winding kept as is)
  - `positions` Float32Array: [x, y, z] per vertex of an already inflated shell.
  - `indices` ArrayLike<number>: Three vertex indices per triangle. (Winding is stored as is)
  - `matrix` Matrix4: Matrix to multiply into the vertices.
  - `joints` ArrayLike<number> | null: Four joint indices per vertex. (null if none)
  - `weights` ArrayLike<number> | null: Four weights per vertex. (null if none)

  Example: Store a pre-inflated shell as is

  ```js
  import { MeshBuilder, createFacetedChunkGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  const body = createFacetedChunkGeometry(0.5, [1], [1, 1, 1]);
  const shell = createFacetedChunkGeometry(0.53, [1], [1, 1, 1]);
  builder.addTriangles(new Float32Array(body.positions), new Float32Array(body.normals), body.indices, Matrix4.createIdentity(), [0.6, 0.8, 1], 0, false, null, null);
  builder.addOutlineTriangles(new Float32Array(shell.positions), shell.indices, Matrix4.createIdentity(), null, null);
  ```

- `addTriangleShell(positions, normals, indices, matrix, outlineWidth, joints, weights)` (method, since 0.4.0): Builds an outline shell from a loaded model's body. (same computation as addPart's outline, winding kept as is, joint indices and weights taken from the body as is)
    - If several primitives are concatenated and passed at once, points at the same position across primitives are pushed together too.
  - `positions` ArrayLike<number>: [x, y, z] per body vertex.
  - `normals` ArrayLike<number>: [x, y, z] normal per body vertex.
  - `indices` ArrayLike<number>: Three vertex indices per triangle.
  - `matrix` Matrix4: Matrix to multiply into the vertices.
  - `outlineWidth` number: Shell thickness. (Length after the matrix is applied)
  - `joints` ArrayLike<number> | null: Four joint indices per vertex. (null if none)
  - `weights` ArrayLike<number> | null: Four weights per vertex. (null if none)

  Example: Build a shell of uniform thickness from the given body

  ```js
  import { MeshBuilder, createFacetedChunkGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  const body = createFacetedChunkGeometry(0.5, [1, 0.9, 1.1], [1, 0.8, 1]);
  const matrix = Matrix4.createTranslation(0, 0.4, 0);
  builder.addTriangles(new Float32Array(body.positions), new Float32Array(body.normals), body.indices, matrix, [0.6, 0.55, 0.5], 0, false, null, null);
  builder.addTriangleShell(body.positions, body.normals, body.indices, matrix, 0.02, null, null);
  ```

- `pushUvValues(uvs, vertexCount)` (method, since 0.4.0): Stores texture coordinates of the vertices just stored. (if uvs is null, fills with 0 only when UV already exists, gaps for earlier vertices are also filled with 0)
  - `uvs` ArrayLike<number> | null: Two texture coordinates per vertex. (null if none)
  - `vertexCount` number: Number of vertices just stored.
- `pushUv1Values(uvs1, vertexCount)` (method, since 0.4.0): Store the second texture coordinates of the vertices just added. (Same form as pushUvValues)
  - `uvs1` ArrayLike<number> | null: Two second texture coordinates per vertex. (null if none)
  - `vertexCount` number: Number of vertices just stored.
- `pushUvTarget(target, uvs, vertexCount)` (method, since 0.4.0): Store into a texture coordinate array. (Does nothing if the values are null and the array is empty, gaps of earlier vertices are 0)
  - `target` Array<number>: Target array.
  - `uvs` ArrayLike<number> | null: Two per vertex. (null if none)
  - `vertexCount` number: Number of vertices just stored.
- `addMaterialRange(materialRange)` (method, since 0.4.0): Stores a material range. ({ name, indexStart, indexCount, ... } span of body vertex indices, the GLB reader stores one per primitive)
  - `materialRange` object: { name, indexStart, indexCount, ... } span of body vertex indices. (Copied into materialRanges of readArrays)

  Example: Store one region as a named material range

  ```js
  import { MeshBuilder, createBoxGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createBoxGeometry(1, 1, 1), Matrix4.createIdentity(), "#909090");
  const screenStart = builder.getIndexCount();
  builder.addPart(createBoxGeometry(0.6, 0.4, 0.02), Matrix4.createTranslation(0, 0.1, 0.51), "#102030", { hasOutline: false });
  builder.addMaterialRange({ name: "screen", indexStart: screenStart, indexCount: builder.getIndexCount() - screenStart, extras: {} });
  const screenRange = builder.readArrays().materialRanges.find((materialRange) => materialRange.name === "screen");
  ```

- `pushSkinValues(target, joints, weights, vertexCount)` (method, since 0.4.0): Stores joint indices and weights. (not stored if there is no skeleton)
  - `target` Array<number>: Destination. (Skeleton values of the body or the outline)
  - `joints` ArrayLike<number> | null: Four joint indices per vertex. (Not stored if none)
  - `weights` ArrayLike<number> | null: Four weights per vertex. (Not stored if none)
  - `vertexCount` number: Number of vertices to store.
- `readArrays()` (method, since 0.4.0): The finished arrays. ({ vertexData, indexData, outlineVertexData, outlineIndexData, outlineBaseData })
  - returns object: { vertexData, indexData, outlineVertexData, outlineIndexData, outlineBaseData, skinData, outlineSkinData, skeleton, uvData, uv1Data, materialRanges }

  Example: Upload the built arrays to the toon renderer

  ```js
  import { ToonRenderer, MeshBuilder, createTorusGeometry, Matrix4 } from "./vanilla.js";
  const renderer = new ToonRenderer(document.createElement("canvas").getContext("webgl2"));
  const builder = new MeshBuilder();
  builder.addPart(createTorusGeometry(0.4, 0.1, 10, 24), Matrix4.createIdentity(), "#e08060");
  const arrays = builder.readArrays();
  const mesh = builder.isEmpty() ? null : renderer.createMesh(arrays);
  ```

- `readNormalMatrix(elements)` (method, since 0.4.0): 3x3 to multiply normals by. (transpose of the inverse of the matrix's upper-left 3x3, row order)
  - `elements` Float32Array: Elements of a Matrix4.
  - returns Array<number>: Nine numbers of the 3x3 to multiply into normals. (Row order, identity if the determinant is near 0)
- `readTriangleFlips(indices, worldPositions, worldNormals)` (method, since 0.4.0): Whether to flip the winding of each triangle. (flips if the face normal is opposite to the sum of vertex normals)
  - `indices` Array<number>: Three vertex indices per triangle.
  - `worldPositions` Array<number>: Vertex positions with the matrix applied. (3 each)
  - `worldNormals` Array<number>: Unit normals with the matrix applied. (3 each)
  - returns Array<boolean>: Whether to flip each triangle.
- `pushTriangles(target, indices, flips, startIndex)` (method, since 0.4.0): Stores triangles. (for ones to flip, swaps the second and third)
  - `target` Array<number>: Vertex index array to store into.
  - `indices` Array<number>: Three vertex indices per triangle of the shape.
  - `flips` Array<boolean>: Whether to flip each triangle. (readTriangleFlips)
  - `startIndex` number: First vertex order to add to the indices.
- `pushShellTriangles(target, indices, flips, startIndex, worldPositions, offsets)` (method, since 0.4.0): Stores outline shell triangles. (for ones to flip, swaps the second and third, and drops triangles whose face flipped after pushing)
    - In concave areas (where the body meets the base plate), surfaces curving tighter than the width fold the shell when pushed, flipping the face.
      A flipped face shows its back face toward the camera, so on shells drawn later with depth (buildings, rocks) it shows as a dark streak over the body.
  - `target` Array<number>: Shell vertex index array to store into.
  - `indices` ArrayLike<number>: Three vertex indices per triangle.
  - `flips` Array<boolean> | null: If absent, winding is kept as is.
  - `startIndex` number: First shell vertex order to add to the indices.
  - `worldPositions` Array<number>: Points before pushing.
  - `offsets` Array<number>: Push amount per point.
- `readOutlineOffsets(worldPositions, worldNormals, outlineWidth)` (method, since 0.4.0): Push amount per point of the outline shell. ([x, y, z] per point, points at the same position are pushed together)
    - Points at the same position (sharp edges, seams, poles) are pushed together toward the sum of their distinct normals, each added once, so the shell does not split open.
    - At edges, pushes further so it stays the width away from each adjoining face. (divided by the smallest cosine between that direction and the face normals)
      So sharp tips (cone apex) do not stick out too far, that cosine is not lowered below MINIMUM_OUTLINE_MITER_COSINE.
  - `worldPositions` Array<number>: Vertex positions with the matrix applied. (3 each)
  - `worldNormals` Array<number>: Unit normal.
  - `outlineWidth` number: Shell thickness.
  - returns Array<number>: [x, y, z] to push out per point. (3 each)
- `readBoundingRadius(positions)` (method, since 0.4.0): Bounding radius of the shape. (from the box center to the farthest point)
  - `positions` Array<number>: Vertex positions. (3 each)
  - returns number: Distance from the center of the bounding box to the farthest point.
- `readMaximumScale(elements)` (method, since 0.4.0): Largest scale of the matrix. (the largest of the three axis lengths)
  - `elements` Float32Array: Elements of a Matrix4.
  - returns number: The largest of the three axis lengths.
- `static combine(pieces)` (static, since 0.4.0): Translates several built arrays and joins them into one array. (static pieces into one mesh, no rotation or scaling)
  - Per piece { arrays: result of readArrays or readGlbArrays, offset: [x, y, z] (not translated if absent) }.
    Adds the offset to positions and the previous pieces' vertex count to indices. The outline shell and inner ranges (innerOutlineRanges, innerBodyRanges) are also translated and joined.
  - Skeletons are not joined. Even a piece with a skeleton becomes a static body in its rest pose.
  - `pieces` Array<object>: { arrays, offset }
  - returns object: Form of readArrays.

  Example: Move three rock pieces and join them into one mesh

  ```js
  import { ToonRenderer, MeshBuilder, createFacetedChunkGeometry, Matrix4 } from "./vanilla.js";
  const renderer = new ToonRenderer(document.createElement("canvas").getContext("webgl2"));
  const builder = new MeshBuilder();
  builder.addPart(createFacetedChunkGeometry(0.4, [1, 0.8, 1.1, 0.9], [1, 0.7, 1]), Matrix4.createIdentity(), "#8a8070");
  const rockArrays = builder.readArrays();
  const pieces = [{ arrays: rockArrays, offset: [0, 0, 0] }, { arrays: rockArrays, offset: [1.2, 0, 0.3] }, { arrays: rockArrays, offset: [-0.8, 0, 1] }];
  const combinedArrays = MeshBuilder.combine(pieces);
  const rocksMesh = renderer.createMesh(combinedArrays);
  ```

## Functions and constants

- `VERTEX_FLOAT_COUNT = 15` (field, since 0.4.0): Number of values per vertex. (position 3, normal 3, color 3, material 2, pattern 4)
  - returns number
- `SKIN_FLOAT_COUNT = 8` (field, since 0.4.0): Number of values stored separately per vertex for models with a skeleton. (joint indices 4, weights 4)
  - returns number
- `readColorValues(colorText)` (method, since 0.4.0): Color string to three sRGB numbers. ("#rrggbb" → [r, g, b], 0 ~ 1)
  - `colorText` string: "#rrggbb" color text.
  - returns Array<number>: [r, g, b] sRGB. (0 ~ 1)

  Example: Color text as the color of addQuad

  ```js
  import { readColorValues, MeshBuilder } from "./vanilla.js";
  const color = readColorValues("#80c040");
  const builder = new MeshBuilder();
  builder.addQuad([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]], [0, 1, 0], color, 0, null, false);
  const arrays = builder.readArrays();
  ```

- `createPartMatrix(parentMatrix, position, rotation, scale)` (method, since 0.4.0): Builds a transform matrix from position, rotation and scale. (multiplied after the parent matrix, rotation uses three.js default order XYZ)
  - `parentMatrix` Matrix4 | null: Parent matrix. (Identity if null)
  - `position` Array<number>: [x, y, z]
  - `rotation` Array<number> | null: [x, y, z] radians.
  - `scale` Array<number> | null: [x, y, z]
  - returns Matrix4: Parent x translation x rotation x scale matrix. (New matrix)

  Example: Place an arm attached to the body with the parent matrix

  ```js
  import { MeshBuilder, createPartMatrix, createBoxGeometry, createCapsuleGeometry } from "./vanilla.js";
  const builder = new MeshBuilder();
  const bodyMatrix = createPartMatrix(null, [0, 1, 0], [0, Math.PI / 4, 0], null);
  const armMatrix = createPartMatrix(bodyMatrix, [0.4, 0.1, 0], [0, 0, -0.6], [1, 1.5, 1]);
  builder.addPart(createBoxGeometry(0.5, 0.6, 0.3), bodyMatrix, "#4080c0");
  builder.addPart(createCapsuleGeometry(0.08, 0.3, 4, 10), armMatrix, "#f0c8a0");
  ```

- `createBoxGeometry(width, height, depth)` (method, since 0.4.0): Box. (separate vertices per face so edges are sharp)
  - `width` number: X axis length.
  - `height` number: Y axis length.
  - `depth` number: Z axis length.
  - returns object: { positions, normals, indices, uvs } shape. (UV 0 ~ 1 per face)

  Example: Floor plate without outline

  ```js
  import { MeshBuilder, createBoxGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  const geometry = createBoxGeometry(6, 0.2, 6);
  builder.addPart(geometry, Matrix4.createTranslation(0, -0.1, 0), "#607050", { hasOutline: false });
  const arrays = builder.readArrays();
  ```

- `createRoundedBoxGeometry(width, height, depth, segments, radius)` (method, since 0.4.0): Rounded box. (lays a grid on each face and pushes out from an inner box by the radius)
  - `width` number: X axis length.
  - `height` number: Y axis length.
  - `depth` number: Z axis length.
  - `segments` number: Number of segments in the rounded parts.
  - `radius` number: Corner radius. (If larger than the half length, that axis is fully rounded)
  - returns object: { positions, normals, indices } shape.

  Example: Box with rounded corners

  ```js
  import { MeshBuilder, createRoundedBoxGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createRoundedBoxGeometry(1, 0.6, 0.8, 4, 0.12), Matrix4.createIdentity(), "#e0a060");
  builder.addPart(createRoundedBoxGeometry(0.3, 0.1, 0.3, 2, 0.04), Matrix4.createTranslation(0, 0.35, 0), "#c04040");
  const arrays = builder.readArrays();
  ```

- `createCylinderGeometry(radiusTop, radiusBottom, height, radialSegments)` (method, since 0.4.0): Cylinder. (a funnel if the top and bottom radii differ, a cone if the top is 0, includes top and bottom caps)
  - `radiusTop` number: Top radius. (0 makes a cone)
  - `radiusBottom` number: Bottom radius.
  - `height` number: Height. (Center at the origin)
  - `radialSegments` number: Number of segments around.
  - returns object: { positions, normals, indices } shape.

  Example: Tree trunk and cone leaves

  ```js
  import { MeshBuilder, createCylinderGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createCylinderGeometry(0.08, 0.1, 0.6, 10), Matrix4.createTranslation(0, 0.3, 0), "#806040");
  builder.addPart(createCylinderGeometry(0, 0.4, 0.9, 12), Matrix4.createTranslation(0, 1, 0), "#40a050");
  const arrays = builder.readArrays();
  ```

- `createSphereGeometry(radius, widthSegments, heightSegments, thetaLength = System.Math.PI)` (method, since 0.4.0): Sphere. (from the top down to thetaLength, π/2 gives the upper hemisphere)
  - `radius` number: Radius.
  - `widthSegments` number: Number of longitude segments.
  - `heightSegments` number: Number of latitude segments.
  - `thetaLength` number: Angle to sweep from the top. (Radians, the default π is a full sphere)
  - returns object: { positions, normals, indices, uvs } shape. (UV 0 ~ 1 by longitude and latitude)

  Example: Build a roof from the upper hemisphere

  ```js
  import { MeshBuilder, createCylinderGeometry, createSphereGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createCylinderGeometry(0.6, 0.6, 1, 24), Matrix4.createTranslation(0, 0.5, 0), "#e0d0b0");
  builder.addPart(createSphereGeometry(0.6, 24, 8, Math.PI / 2), Matrix4.createTranslation(0, 1, 0), "#c06050");
  const arrays = builder.readArrays();
  ```

- `createLatheGeometry(points, segments)` (method, since 0.4.0): Lathe. (revolves the profile points [x, y] around the Y axis, normals from the profile slope)
  - `points` Array<Array<number>>: From bottom to top.
  - `segments` number: Number of segments around.
  - returns object: { positions, normals, indices } shape.

  Example: Build a vase by revolving a profile

  ```js
  import { MeshBuilder, createLatheGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  const profile = [[0.001, 0], [0.25, 0.05], [0.3, 0.3], [0.15, 0.6], [0.2, 0.75]];
  builder.addPart(createLatheGeometry(profile, 20), Matrix4.createIdentity(), "#5080c0");
  const arrays = builder.readArrays();
  ```

- `createCapsuleGeometry(radius, length, capSegments, radialSegments)` (method, since 0.4.0): Capsule. (hemispheres on the top and bottom of a middle cylinder of length length, total height is length + two radii)
  - `radius` number: Radius.
  - `length` number: Length of the middle cylinder. (Without the hemispheres)
  - `capSegments` number: Number of latitude segments of one hemisphere.
  - `radialSegments` number: Number of segments around.
  - returns object: { positions, normals, indices } shape.

  Example: Capsule lying down

  ```js
  import { MeshBuilder, createCapsuleGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createCapsuleGeometry(0.2, 0.8, 6, 16), Matrix4.createRotationZ(Math.PI / 2), "#a0d0f0");
  builder.addPart(createCapsuleGeometry(0.1, 0.3, 4, 12), Matrix4.createTranslation(0, 0.4, 0), "#80b0d0");
  const arrays = builder.readArrays();
  ```

- `createTorusGeometry(radius, tube, radialSegments, tubularSegments, arc = System.Math.PI * 2)` (method, since 0.4.0): Torus. (a ring lying on the XY plane, only as far as arc)
  - `radius` number: Radius of the ring.
  - `tube` number: Radius of the tube.
  - `radialSegments` number: Segments around the tube.
  - `tubularSegments` number: Segments around the ring.
  - `arc` number: Angle around the ring. (Radians, default 2π)
  - returns object: { positions, normals, indices } shape.

  Example: Half ring lying on the floor

  ```js
  import { MeshBuilder, createTorusGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createTorusGeometry(0.5, 0.08, 8, 24, Math.PI), Matrix4.createRotationX(-Math.PI / 2), "#f0d060");
  builder.addPart(createTorusGeometry(0.3, 0.05, 8, 20), Matrix4.createTranslation(0, 0.5, 0), "#e0b040");
  const arrays = builder.readArrays();
  ```

- `createOctahedronGeometry(radius)` (method, since 0.4.0): Octahedron. (separate vertices per face for an angular crystal)
  - `radius` number: From the center to a vertex.
  - returns object: { positions, normals, indices } shape.

  Example: Glowing crystal

  ```js
  import { MeshBuilder, createOctahedronGeometry, createCylinderGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  builder.addPart(createOctahedronGeometry(0.4), Matrix4.createScale(0.6, 1.4, 0.6), "#80e0ff", { glow: 0.8 });
  builder.addPart(createCylinderGeometry(0.3, 0.35, 0.15, 8), Matrix4.createTranslation(0, -0.6, 0), "#605870");
  const arrays = builder.readArrays();
  ```

- `createFacetedChunkGeometry(radius, vertexScales, axisScale)` (method, since 0.4.0): Angular rock. (stretches and shrinks each of the twelve icosahedron vertices, a faceted stone with separate vertices per face)
  - `radius` number: Base radius.
  - `vertexScales` Array<number>: Radius multipliers for the twelve vertices. (repeats from the start if too few)
  - `axisScale` Array<number>: [x, y, z] multiplier.
  - returns object: { positions, normals, indices }

  Example: Flat, bumpy stone

  ```js
  import { MeshBuilder, createFacetedChunkGeometry, Matrix4 } from "./vanilla.js";
  const builder = new MeshBuilder();
  const vertexScales = [1, 0.85, 1.1, 0.9, 1.05, 0.8, 1, 0.95, 1.15, 0.9, 1, 0.85];
  builder.addPart(createFacetedChunkGeometry(0.5, vertexScales, [1.2, 0.6, 1]), Matrix4.createIdentity(), "#908878");
  const arrays = builder.readArrays();
  ```

