Mesh

View source

Represents vertex geometry as a mesh asset.

Examples

import { Mesh, MeshAssets, MeshAttributeData } from '@wimaengine/render-core'

const mesh = Mesh.quad2D(2, 1)
mesh.setAttribute('uv', new MeshAttributeData(new Float32Array([
  0, 0, 0, 1, 1, 1, 1, 0
])))

const handle = world.getResource(MeshAssets).add(mesh)
// Pass `handle` to `new Meshed(handle)` when attaching the asset to an entity.

Constructor


constructor()

Properties


indices: Uint16Array<ArrayBufferLike>

View source

Optional index buffer for the mesh.

The supplied Uint16Array is stored directly, so undefined represents a non-indexed mesh and mutations to the array are visible through the mesh.

Methods


getIndices(): Uint16Array<ArrayBufferLike>

View source

Returns the mesh’s current index buffer without copying it.

The returned value is the same Uint16Array stored in indices, or undefined for a non-indexed mesh.

Returns

The current index buffer, if present.

setIndices(indices: Uint16Array<ArrayBufferLike>): Mesh

View source

Replaces the mesh’s index buffer and returns this mesh for chaining.

Passing undefined clears the buffer and makes the mesh non-indexed. A supplied Uint16Array is stored as-is and is not copied.

Parameters

NameDescriptionDefault
indices——

Returns

This mesh.

getAttribute(name: string): MeshAttributeData

View source

Returns the MeshAttributeData stored under name.

The stored value is returned directly; undefined is returned when the name has no entry.

Parameters

NameDescriptionDefault
name——

Returns

The named attribute, if present.

setAttribute(name: string, attribute: MeshAttributeData): Mesh

View source

Adds or replaces a named MeshAttributeData.

The supplied attribute is stored directly without copying, and this mesh is returned for chaining.

Parameters

NameDescriptionDefault
name——
attribute——

Returns

This mesh.

getAttributes(): Map<string, MeshAttributeData>

View source

Returns the live map of all named MeshAttributeData values.

This is the mesh’s actual attribute collection, not a copy. Changes to the returned map therefore change the mesh.

Returns

The mesh’s attribute map.

deleteAttribute(name: string): void

View source

Removes the named attribute if it exists.

If no entry has name, the method does nothing. It always returns undefined.

Parameters

NameDescriptionDefault
name——

staticserialize(value: Mesh): MeshSerial

View source

Converts a mesh to its serializable form without mutating it.

When an index buffer is present, indices is a new number array made from it; otherwise it is undefined. attributes is a new array of [name, serializedAttribute] entries in map order, with each attribute converted by MeshAttributeData.serialize.

Parameters

NameDescriptionDefault
value——

Returns

The mesh’s plain serial representation.

staticdeserialize(value: MeshSerial, out?: Mesh): Mesh

View source

Restores a mesh from serialized indices and attribute entries.

A present indices array becomes a new Uint16Array; an absent or undefined value clears the index buffer. The attribute entries become a new map, with each value rebuilt by MeshAttributeData.deserialize. When out is supplied, its index buffer and attribute map are replaced and that same mesh is returned; otherwise a new mesh is created.

Parameters

NameDescriptionDefault
value——
out?—...

Returns

The restored mesh.

staticquad2D(width: number, height: number): Mesh

View source

Builds a new centered, non-indexed 2D quad.

The position2d attribute contains four vertices in the order (-width / 2, -height / 2), (-width / 2, height / 2), (width / 2, height / 2), (width / 2, -height / 2). The mesh has no index buffer, normal attribute, or UV attribute.

Parameters

NameDescriptionDefault
width——
height——

staticcircle2D(radius: number, resolution?: number): Mesh

View source

Builds a new non-indexed 2D circle vertex list.

The position2d attribute starts with the center, followed by resolution + 1 circumference samples for an integer resolution. These samples use angles from 0 through 2 * Math.PI in steps of 2 * Math.PI / resolution, so the final sample closes the circle for a positive integer resolution. The mesh has no index buffer or other attributes; resolution is used directly and is not floored.

Parameters

NameDescriptionDefault
radius——
resolution?—16

statictriangle2D(base: number, height: number, baseRatio?: number): Mesh

View source

Builds a new indexed 2D triangle.

The position2d attribute contains the two base vertices followed by the upper vertex. The vertices are (-base / 2, -height / 2), (base / 2, -height / 2), and (base * baseRatio / 2, height / 2). baseRatio defaults to 0, and the index buffer is [0, 1, 2]; no other attributes are added.

Parameters

NameDescriptionDefault
base——
height——
baseRatio?—0

statictriangle3D(base?: number, height?: number, baseL?: number): Mesh

View source

Builds a new indexed 3D triangle in the z = 0 plane.

The position3d attribute contains the two base vertices followed by the upper vertex at (base * baseL / 2, height / 2, 0). baseL defaults to 0, and the index buffer is [0, 1, 2]; no other attributes are added.

Parameters

NameDescriptionDefault
base?—1
height?—1
baseL?—0

staticcircle3D(radius?: number, segments?: number, arcstart?: number, arclength?: number): Mesh

View source

Builds a new indexed, planar 3D circle fan.

The mesh has position3d, normal3d, and uv attributes. The first vertex is the center; subsequent vertices are sampled in the z = 0 plane from arcstart in increments of arclength / segments, while the sample angle is below arclength with a small endpoint tolerance. Each consecutive pair is joined to the center by the index buffer. segments is used directly and is not floored or clamped; the upper-bound comparison uses arclength, not arcstart + arclength.

Parameters

NameDescriptionDefault
radius?—0.5
segments?—32
arcstart?—0
arclength?—...

staticplane3D(width?: number, height?: number, widthSegments?: number, heightSegments?: number): Mesh

View source

Builds a new indexed 3D plane grid in the z = 0 plane.

The mesh contains position3d, normal3d, and uv attributes, with normals pointing along positive z. widthSegments and heightSegments are floored before generation, and each resulting grid cell contributes two triangles to the index buffer.

Parameters

NameDescriptionDefault
width?—1
height?—1
widthSegments?—1
heightSegments?—1

staticring3D(innerRadius?: number, outerRadius?: number, thetaSegments?: number, phiSegments?: number, thetaStart?: number, thetaLength?: number): Mesh

View source

Builds a new indexed annular 3D surface in the z = 0 plane.

The mesh includes position3d, normal3d, and uv attributes, with normals pointing along positive z. phiSegments subdivides the radius from innerRadius to outerRadius; each angular row contains samples from thetaStart through thetaStart + thetaLength. The supplied segment counts are used directly and are not floored. Adjacent radial and angular samples form cells of two indexed triangles.

Parameters

NameDescriptionDefault
innerRadius?—0.25
outerRadius?—0.5
thetaSegments?—32
phiSegments?—1
thetaStart?—0
thetaLength?—...

staticlathe3D(points?: Vector2[], segments?: number, phiStart?: number, phiLength?: number): Mesh

View source

Builds a new indexed surface by revolving a 2D profile around the y-axis.

Each profile point supplies a radial coordinate from its x value and a y coordinate. The default profile is (0, -0.5), (0.5, 0), and (0, 0.5). The mesh includes position3d, normal3d, and uv attributes, and samples angles from phiStart through phiStart + phiLength. segments is used directly for the revolution rows and is not floored; adjacent profile and revolution rows form two indexed triangles per cell.

Parameters

NameDescriptionDefault
points?—...
segments?—12
phiStart?—0
phiLength?—...

staticcube(width?: number, height?: number, depth?: number, widthSegments?: number, heightSegments?: number, depthSegments?: number): Mesh

View source

Builds a new indexed six-sided box centered at the origin.

The mesh has position3d, normal3d, and uv attributes. The width, height, and depth segment arguments control subdivisions along their corresponding dimensions on the box faces and are used directly. The subdivided faces are combined into the index buffer.

Parameters

NameDescriptionDefault
width?—1
height?—1
depth?—1
widthSegments?—1
heightSegments?—1
depthSegments?—1

staticuvSphere(radius?: number, widthSegments?: number, heightSegments?: number, phiStart?: number, phiLength?: number, thetaStart?: number, thetaLength?: number): Mesh

View source

Builds a new indexed UV sphere, optionally limited to angular ranges.

The mesh contains position3d, normal3d, and uv attributes. widthSegments and heightSegments control the subdivisions around the phi and theta directions. Vertices use phi angles from phiStart through phiStart + phiLength and theta angles from thetaStart through thetaStart + thetaLength. For index generation, the effective theta end is Math.min(thetaStart + thetaLength, Math.PI); pole-row triangles are omitted when the corresponding pole is reached. Segment counts are used directly and are not floored; the remaining grid cells are connected by the index buffer.

Parameters

NameDescriptionDefault
radius?—0.5
widthSegments?—32
heightSegments?—16
phiStart?—0
phiLength?—...
thetaStart?—0
thetaLength?—Math.PI

statictorus(radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, arc?: number): Mesh

View source

Builds a new indexed torus.

The mesh contains position3d, normal3d, and uv attributes. radius is the distance from the origin to the tube centerline, tube is the tube radius, and arc is the sweep around the central ring from angle 0 through arc. The radial and tubular segment counts are used directly and are not floored; each grid cell contributes two indexed triangles.

Parameters

NameDescriptionDefault
radius?—0.375
tube?—0.125
radialSegments?—12
tubularSegments?—48
arc?—...

staticcylinder(radiusTop?: number, radiusBottom?: number, height?: number, radialSegments?: number, heightSegments?: number, openEnded?: boolean, thetaStart?: number, thetaLength?: number): Mesh

View source

Builds a new indexed cylinder or tapered cylinder with optional end caps.

The mesh contains position3d, normal3d, and uv attributes. The side linearly changes from radiusTop at y = height / 2 to radiusBottom at y = -height / 2. radialSegments and heightSegments control the angular and vertical subdivisions and are used directly. Side and cap vertices use angles from thetaStart through thetaStart + thetaLength. When openEnded is exactly false, a top cap is added only when radiusTop > 0, and a bottom cap is added only when radiusBottom > 0; side cells and cap sectors are added to the index buffer.

Parameters

NameDescriptionDefault
radiusTop?—0.5
radiusBottom?—0.5
height?—1
radialSegments?—32
heightSegments?—1
openEnded?—false
thetaStart?—0
thetaLength?—...

staticcone(radius?: number, height?: number, radialSegments?: number, heightSegments?: number, openEnded?: boolean, thetaStart?: number, thetaLength?: number): Mesh

View source

Builds a new indexed cone using Mesh.cylinder with a zero top radius and the supplied radius as its bottom radius.

The cylinder’s attributes, segment handling, angular range, and cap behavior are preserved. With openEnded exactly false, only the bottom cap can be generated, and only when radius > 0; the zero top radius cannot produce a top cap.

Parameters

NameDescriptionDefault
radius?—0.5
height?—1
radialSegments?—32
heightSegments?—1
openEnded?—false
thetaStart?—0
thetaLength?—...

staticdefault(): Mesh

View source

Creates and returns a new empty mesh.

The returned mesh has indices set to undefined and a new empty attribute map. Each call returns a distinct mesh.