Class
Mesh
View sourceRepresents 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
Properties
indices: Uint16Array<ArrayBufferLike>
View sourceOptional 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.
attributes: Map<string, MeshAttributeData>
View sourceNamed mesh attributes held by the mesh.
The map is initialized empty. Its values are MeshAttributeData
instances, and both the map and its values are stored directly rather
than copied.
Default: ...
Methods
getIndices(): Uint16Array<ArrayBufferLike>
View sourceReturns 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.
setIndices(indices: Uint16Array<ArrayBufferLike>): Mesh
View sourceReplaces 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.
getAttribute(name: string): MeshAttributeData
View sourceReturns the MeshAttributeData stored under name.
The stored value is returned directly; undefined is returned when the
name has no entry.
setAttribute(name: string, attribute: MeshAttributeData): Mesh
View sourceAdds or replaces a named MeshAttributeData.
The supplied attribute is stored directly without copying, and this mesh is returned for chaining.
getAttributes(): Map<string, MeshAttributeData>
View sourceReturns 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.
deleteAttribute(name: string): void
View sourceRemoves the named attribute if it exists.
If no entry has name, the method does nothing. It always returns
undefined.
staticserialize(value: Mesh): MeshSerial
View sourceConverts 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.
staticdeserialize(value: MeshSerial, out?: Mesh): Mesh
View sourceRestores 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.
staticquad2D(width: number, height: number): Mesh
View sourceBuilds 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.
staticcircle2D(radius: number, resolution?: number): Mesh
View sourceBuilds 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.
statictriangle2D(base: number, height: number, baseRatio?: number): Mesh
View sourceBuilds 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.
statictriangle3D(base?: number, height?: number, baseL?: number): Mesh
View sourceBuilds 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.
staticcircle3D(radius?: number, segments?: number, arcstart?: number, arclength?: number): Mesh
View sourceBuilds 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.
staticplane3D(width?: number, height?: number, widthSegments?: number, heightSegments?: number): Mesh
View sourceBuilds 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.
staticring3D(innerRadius?: number, outerRadius?: number, thetaSegments?: number, phiSegments?: number, thetaStart?: number, thetaLength?: number): Mesh
View sourceBuilds 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.
staticlathe3D(points?: Vector2[], segments?: number, phiStart?: number, phiLength?: number): Mesh
View sourceBuilds 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.
staticcube(width?: number, height?: number, depth?: number, widthSegments?: number, heightSegments?: number, depthSegments?: number): Mesh
View sourceBuilds 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.
staticuvSphere(radius?: number, widthSegments?: number, heightSegments?: number, phiStart?: number, phiLength?: number, thetaStart?: number, thetaLength?: number): Mesh
View sourceBuilds 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.
statictorus(radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, arc?: number): Mesh
View sourceBuilds 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.
staticcylinder(radiusTop?: number, radiusBottom?: number, height?: number, radialSegments?: number, heightSegments?: number, openEnded?: boolean, thetaStart?: number, thetaLength?: number): Mesh
View sourceBuilds 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.
staticcone(radius?: number, height?: number, radialSegments?: number, heightSegments?: number, openEnded?: boolean, thetaStart?: number, thetaLength?: number): Mesh
View sourceBuilds 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.
staticdefault(): Mesh
View sourceCreates 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.
