Documentation / @sw-file-lib/geometry / GeometryBuilder
Class: GeometryBuilder
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
new GeometryBuilder(options?: GeometryBuilderOptions): GeometryBuilder;Defined in: builder.ts:171
Creates a new geometry builder.
Parameters
| Parameter | Type | Description |
|---|---|---|
options? | GeometryBuilderOptions | Builder options. |
Returns
GeometryBuilder
Properties
refine
readonly refine: boolean;Defined in: builder.ts:164
Whether earcut triangulation refinement is enabled.
Methods
add2dPolygon()
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
| Parameter | Type | Description |
|---|---|---|
polygon | readonly VertexList2D[] | Outer polygon followed by optional holes. |
options? | Add2dPolygonOptions | Polygon generation options. |
Returns
void
addExtrudedSides()
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
| Parameter | Type | Description |
|---|---|---|
vertices | VertexList2D | Polygon outline. |
options? | AddExtrudedSideOptions | Extrusion options. |
Returns
void
addFace()
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
| Parameter | Type | Description |
|---|---|---|
vertices | VertexList3D | Polygon vertices in winding order. |
options? | | number | ReadonlyColor | AddFaceOptions | Face generation options. |
Returns
void
merge()
merge(other: GeometryBuilder): void;Defined in: builder.ts:371
Appends all geometry from another builder.
Material groups are preserved.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | GeometryBuilder | Other GeometryBuilder to merge. |
Returns
void
toBufferGeometryAttributes()
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
{
color: Float32Array<ArrayBuffer>;
groups: {
length: number;
materialIndex: number;
start: number;
}[];
index: number[];
normal: Float32Array<ArrayBuffer>;
position: Float32Array<ArrayBuffer>;
}| Name | Type |
|---|---|
color | Float32Array<ArrayBuffer> |
groups | { length: number; materialIndex: number; start: number; }[] |
index | number[] |
normal | Float32Array<ArrayBuffer> |
position | Float32Array<ArrayBuffer> |
toMeshData()
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
Mesh data compatible with @sw-file-lib/core.
transform()
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
| Parameter | Type | Description |
|---|---|---|
mat? | ReadonlyMat3 | Rotation matrix. |
translation? | ReadonlyVec3 | Translation vector. |
Returns
void