Skip to content

Documentation / @sw-file-lib/three / VehicleBodyAssembler

Class: VehicleBodyAssembler ​

ts
class VehicleBodyAssembler {
  constructor(assets: VehicleAssetResolver);
  appendComponent(componentInstance: ComponentImmutable): Promise<void>;
  appendMicrocontroller(componentInstance: ComponentImmutable): Promise<void>;
  appendPaintableSign(componentInstance: ComponentImmutable, definition: ComponentDefinitionImmutable): Promise<void>;
  appendRawComponent(componentInstance: ComponentImmutable, surfaceDefinitions?: readonly SurfaceImmutable[], objects?: readonly Object3D<Object3DEventMap>[]): void;
  appendStandardComponent(componentInstance: ComponentImmutable, definition?: ComponentDefinitionImmutable, meshesToExclude?: readonly ("mesh_data_name" | "mesh_0_name" | "mesh_1_name" | "mesh_2_name")[]): Promise<void>;
  build(surfaceOptions?: VehicleBodyBuildOptions): Group;
  buildSurfaceMesh(options?: BuildSurfaceGeometryOptions): GeometryBuilder;
}

Defined in: vehicleAssembler/bodyAssembler.ts:111

Assembles a single Stormworks vehicle body into Three.js objects.

The assembler collects component geometry and meshes, then produces one THREE.Group containing a culled surface mesh and any additional meshes.

A single instance should be used for one vehicle body.

The primary methods most applications should call are appendComponent and build.

Example ​

ts
import * as THREE from "three";
import { parseVehicleXml } from "@sw-file-lib/xml";
import { createVehicleAssetResolver, VehicleBodyAssembler } from "@sw-file-lib/three";

const assetResolver = createVehicleAssetResolver(
  (componentId) =>
    fetch(`/rom/data/definitions/${componentId}.xml`)
    .then((res) => res.ok ? res.text() : undefined),
  (meshPath) =>
    fetch(`/rom/${meshPath}`)
    .then((res) => res.ok ? res.arrayBuffer() : undefined)
);

const vehicle = parseVehicleXml(text);
const vehicleGroup = new THREE.Group();

for (const body of vehicle.bodies ?? []) {
  const assembler = new VehicleBodyAssembler(assetResolver);
  for (const component of body.components ?? []) {
    await assembler.appendComponent(component);
  }

  const obj = assembler.build();
  vehicleGroup.add(obj);
}

scene.add(vehicleGroup);

Constructors ​

Constructor ​

ts
new VehicleBodyAssembler(assets: VehicleAssetResolver): VehicleBodyAssembler;

Defined in: vehicleAssembler/bodyAssembler.ts:123

Creates a new body assembler.

Parameters ​

ParameterTypeDescription
assetsVehicleAssetResolverCallbacks used to resolve component definitions and mesh assets. Create a resolver with createVehicleAssetResolver, or make your own. Wrapping them with a cache is recommended when loading from disk or over the network.

Returns ​

VehicleBodyAssembler

Methods ​

appendComponent() ​

ts
appendComponent(componentInstance: ComponentImmutable): Promise<void>;

Defined in: vehicleAssembler/bodyAssembler.ts:136

Appends a component to the body.

The component type is detected automatically, and the appropriate builder is used internally. This is the primary method most applications should call.

Parameters ​

ParameterTypeDescription
componentInstanceComponentImmutableThe component instance from the vehicle body. Represented by a <c> element in the vehicle XML.

Returns ​

Promise<void>


appendMicrocontroller() ​

ts
appendMicrocontroller(componentInstance: ComponentImmutable): Promise<void>;

Defined in: vehicleAssembler/bodyAssembler.ts:289

Appends a microcontroller component.

Microcontrollers have variable dimensions and therefore generate their surfaces and meshes procedurally instead of using a fixed definition.

Parameters ​

ParameterTypeDescription
componentInstanceComponentImmutableThe component instance from the vehicle body. Represented by a <c> element in the vehicle XML.

Returns ​

Promise<void>


appendPaintableSign() ​

ts
appendPaintableSign(componentInstance: ComponentImmutable, definition: ComponentDefinitionImmutable): Promise<void>;

Defined in: vehicleAssembler/bodyAssembler.ts:271

Appends a paintable sign component.

Paintable signs generate their geometry dynamically from the component properties and do not use a predefined mesh asset.

Parameters ​

ParameterTypeDescription
componentInstanceComponentImmutableThe component instance from the vehicle body. Represented by a <c> element in the vehicle XML.
definitionComponentDefinitionImmutableThe component definition for the component instance.

Returns ​

Promise<void>


appendRawComponent() ​

ts
appendRawComponent(
   componentInstance: ComponentImmutable, 
   surfaceDefinitions?: readonly SurfaceImmutable[], 
   objects?: readonly Object3D<Object3DEventMap>[]): void;

Defined in: vehicleAssembler/bodyAssembler.ts:162

Appends surfaces and prebuilt objects to the body.

This is a low-level API intended for custom or unsupported components. All surfaces and objects must be provided in the component's local coordinate system; they will be transformed into body space automatically.

Parameters ​

ParameterTypeDescription
componentInstanceComponentImmutableThe component instance from the vehicle body. Represented by a <c> element in the vehicle XML.
surfaceDefinitions?readonly SurfaceImmutable[]Surface definitions in local space.
objects?readonly Object3D<Object3DEventMap>[]Additional Three.js objects in local space.

Returns ​

void


appendStandardComponent() ​

ts
appendStandardComponent(
   componentInstance: ComponentImmutable, 
   definition?: ComponentDefinitionImmutable, 
meshesToExclude?: readonly ("mesh_data_name" | "mesh_0_name" | "mesh_1_name" | "mesh_2_name")[]): Promise<void>;

Defined in: vehicleAssembler/bodyAssembler.ts:240

Appends a standard component using its component definition.

This resolves surface definitions and any required mesh assets, transforms them into world space, and adds them to the current body.

Most users should prefer appendComponent, which performs type detection automatically.

Parameters ​

ParameterTypeDescription
componentInstanceComponentImmutableThe component instance from the vehicle body. Represented by a <c> element in the vehicle XML.
definition?ComponentDefinitionImmutableThe component definition for the component instance.
meshesToExclude?readonly ("mesh_data_name" | "mesh_0_name" | "mesh_1_name" | "mesh_2_name")[]Optional list of mesh attributes to ignore.

Returns ​

Promise<void>


build() ​

ts
build(surfaceOptions?: VehicleBodyBuildOptions): Group;

Defined in: vehicleAssembler/bodyAssembler.ts:315

Builds the final Three.js object representing this vehicle body.

The returned group contains the merged surface mesh together with every additional mesh appended during assembly.

Parameters ​

ParameterType
surfaceOptions?VehicleBodyBuildOptions

Returns ​

Group

A complete Three.js group for the body.


buildSurfaceMesh() ​

ts
buildSurfaceMesh(options?: BuildSurfaceGeometryOptions): GeometryBuilder;

Defined in: vehicleAssembler/bodyAssembler.ts:303

Builds the merged surface mesh for this body using GeometryBuilder.

Surface culling is performed before constructing geometry unless explicitly disabled by the options. The returned geometry does not include any additional meshes.

Parameters ​

ParameterType
options?BuildSurfaceGeometryOptions

Returns ​

GeometryBuilder

See ​

build for the complete object including additional meshes.