Skip to content

Documentation / @sw-file-lib/geometry / GeometryBuilder

Class: GeometryBuilder ​

ts
class GeometryBuilder {
  constructor(options?: GeometryBuilderOptions);
  readonly refine: boolean;
  add2dPolygon(polygon: readonly VertexList2D[], options?: Add2dPolygonOptions): void;
  addExtrudedSides(vertices: VertexList2D, options?: AddExtrudedSideOptions): void;
  addFace(vertices: VertexList3D, options?: number | ReadonlyColor | AddFaceOptions): void;
  merge(other: GeometryBuilder): void;
  toBufferGeometryAttributes(): { color: Float32Array<ArrayBuffer>; groups: { length: number; materialIndex: number; start: number }[]; index: number[]; normal: Float32Array<ArrayBuffer>; position: Float32Array<ArrayBuffer> };
  toMeshData(): MeshData;
  transform(mat?: ReadonlyMat3, translation?: ReadonlyVec3): void;
}

Defined in: builder.ts:157

Utility for constructing indexed triangle meshes.

GeometryBuilder provides a lightweight, renderer-independent API for generating and combining geometry. It is used internally by the Three.js integration package, but can also be used directly to create procedural meshes or export geometry in other formats.

Positions, normals, and indices are stored in Stormworks' left-handed coordinate system. Color is stored as RGBA with 0-255 integer components.

Constructors ​

Constructor ​

ts
new GeometryBuilder(options?: GeometryBuilderOptions): GeometryBuilder;

Defined in: builder.ts:171

Creates a new geometry builder.

Parameters ​

ParameterTypeDescription
options?GeometryBuilderOptionsBuilder options.

Returns ​

GeometryBuilder

Properties ​

refine ​

ts
readonly refine: boolean;

Defined in: builder.ts:164

Whether earcut triangulation refinement is enabled.

Methods ​

add2dPolygon() ​

ts
add2dPolygon(polygon: readonly VertexList2D[], options?: Add2dPolygonOptions): void;

Defined in: builder.ts:279

Add a triangulated 2D polygon on a constant Z plane.

The first vertex list is treated as the outer boundary, and any remaining vertex lists are treated as holes.

Parameters ​

ParameterTypeDescription
polygonreadonly VertexList2D[]Outer polygon followed by optional holes.
options?Add2dPolygonOptionsPolygon generation options.

Returns ​

void


addExtrudedSides() ​

ts
addExtrudedSides(vertices: VertexList2D, options?: AddExtrudedSideOptions): void;

Defined in: builder.ts:302

Generates the side faces of an extruded polygon.

Use { close: true } to close the path by connecting the final vertex back to the first.

This method creates only the side walls. Top and bottom faces can be generated separately with add2dPolygon.

Parameters ​

ParameterTypeDescription
verticesVertexList2DPolygon outline.
options?AddExtrudedSideOptionsExtrusion options.

Returns ​

void


addFace() ​

ts
addFace(vertices: VertexList3D, options?: 
  | number
  | ReadonlyColor
  | AddFaceOptions): void;

Defined in: builder.ts:248

Adds a planar face from 3D vertices.

The polygon is triangulated automatically, assuming the vertices are coplanar.

For convenience, options may be either a material index or a color.

Parameters ​

ParameterTypeDescription
verticesVertexList3DPolygon vertices in winding order.
options?| number | ReadonlyColor | AddFaceOptionsFace generation options.

Returns ​

void


merge() ​

ts
merge(other: GeometryBuilder): void;

Defined in: builder.ts:371

Appends all geometry from another builder.

Material groups are preserved.

Parameters ​

ParameterTypeDescription
otherGeometryBuilderOther GeometryBuilder to merge.

Returns ​

void


toBufferGeometryAttributes() ​

ts
toBufferGeometryAttributes(): {
  color: Float32Array<ArrayBuffer>;
  groups: {
     length: number;
     materialIndex: number;
     start: number;
  }[];
  index: number[];
  normal: Float32Array<ArrayBuffer>;
  position: Float32Array<ArrayBuffer>;
};

Defined in: builder.ts:404

Converts the geometry into raw attribute buffers.

This is primarily intended as a bridge to Three.js. Most applications should prefer the higher-level utilities provided by @sw-file-lib/three.

The returned positions and normals are in three.js coordinates.

Returns ​

ts
{
  color: Float32Array<ArrayBuffer>;
  groups: {
     length: number;
     materialIndex: number;
     start: number;
  }[];
  index: number[];
  normal: Float32Array<ArrayBuffer>;
  position: Float32Array<ArrayBuffer>;
}
NameType
colorFloat32Array<ArrayBuffer>
groups{ length: number; materialIndex: number; start: number; }[]
indexnumber[]
normalFloat32Array<ArrayBuffer>
positionFloat32Array<ArrayBuffer>

toMeshData() ​

ts
toMeshData(): MeshData;

Defined in: builder.ts:421

Converts the geometry into Stormworks mesh data.

The result can be serialized with @sw-file-lib/core.

Returns ​

MeshData

Mesh data compatible with @sw-file-lib/core.


transform() ​

ts
transform(mat?: ReadonlyMat3, translation?: ReadonlyVec3): void;

Defined in: builder.ts:334

Applies a transformation to all vertices and normals.

Either the rotation matrix or the translation may be omitted.

Parameters ​

ParameterTypeDescription
mat?ReadonlyMat3Rotation matrix.
translation?ReadonlyVec3Translation vector.

Returns ​

void