Constructor
new Mesh()
Create an empty mesh
- Source
const mesh = buildLathe([[0, -1], [1, 0], [0, 1]], 4); // octahedron
mesh.render(buildMatrix(vec3(0, 1, 0)), undefined, RED);Members
bounds :Object|undefined
- Object |
undefined
| Type | Description |
|---|---|
| Object | | Bounding box, for picking, measured with the radius |
- Source
buffer :WebGLBuffer|undefined
- WebGLBuffer |
undefined
| Type | Description |
|---|---|
| WebGLBuffer | | GPU vertex buffer, created by upload |
- Source
bufferCount
Properties| Type | Description |
|---|---|
| number | Indices in the GPU index buffer, three per triangle |
- Source
colors :Array.<Color>
- Array.<Color>
| Type | Description |
|---|---|
| Array.<Color> | Vertex colors |
- Source
dirty
Properties| Type | Description |
|---|---|
| boolean | The mesh changed and needs uploading again, set it yourself if you edit the arrays |
- Source
doubleSided
Properties| Type | Description |
|---|---|
| boolean | Draw both sides, each lit as the side that is seen; off skips the faces pointing away, which is faster and right for closed shapes, the open builders like buildGrid and buildRibbon turn it on |
- Source
dynamicDraw
Properties| Type | Description |
|---|---|
| boolean | The values change often but the shape never does, for a water surface or a cloth: set once, the mesh keeps its GPU layout and a dirty upload only rewrites the vertices into the buffer it has; the strip must keep the same points in the same order, a new point count asserts; the layout is decided by the first upload, so strip entries equal then stay one vertex and triangles with no area then stay dropped, set vertexKeys or give it distinct values at the start, not a flat grid of one color or points all in one place |
- Source
indexBuffer :WebGLBuffer|undefined
- WebGLBuffer |
undefined
| Type | Description |
|---|---|
| WebGLBuffer | | GPU index buffer, the triangles, created by upload |
- Source
indices :Array.<number>|undefined
- Array.<number> |
undefined
| Type | Description |
|---|---|
| Array.<number> | | The mesh as an indexed triangle list instead of a strip: the arrays hold each vertex once and this says how they join, three vertex numbers per triangle, counter clockwise seen from the front like a strip's first triangle; addTriangles and the loaders fill it, toIndexed turns a strip mesh into this form |
- Source
instanceData :Float32Array|undefined
- Float32Array |
undefined
- Source
instanced :boolean|undefined
- boolean |
undefined
| Type | Description |
|---|---|
| boolean | | Draw every use of this mesh in the opaque stage as one instanced call, undefined follows render3D.instancing |
- Source
normals :Array.<Vector3>
- Array.<Vector3>
| Type | Description |
|---|---|
| Array.<Vector3> | Vertex normals |
- Source
points :Array.<Vector3>
- Array.<Vector3>
| Type | Description |
|---|---|
| Array.<Vector3> | Vertex positions, in strip order or one per vertex of an indexed mesh |
- Source
radius
Properties| Type | Description |
|---|---|
| number | Bounding sphere radius around the origin, for culling and picking, computed by upload |
- Source
uvs :Array.<Vector2>
- Array.<Vector2>
| Type | Description |
|---|---|
| Array.<Vector2> | Vertex texture coords, 0-1 across the tile |
- Source
vertexCount
Number of vertices in the mesh
- Source
vertexKeys :Int32Array|undefined
- Int32Array |
undefined
| Type | Description |
|---|---|
| Int32Array | | Which strip entries are one vertex, set by a builder that knows, one whole number per entry with equal numbers meaning the same vertex; upload skips its search for them, then drops the keys, since an edit after that may tell the entries apart; adding geometry or recomputing normals drops them too |
- Source
vertexLayout :Object|undefined
- Object |
undefined
- Source
Methods
addQuad(a, b, c, d, coloropt, uvsopt) → {Mesh}
Add a flat quad from four corners in loop order, counter clockwise seen from the front, a is the top left of the texture
| Name | Type | Attributes | Description |
|---|---|---|---|
a | Vector3 | ||
b | Vector3 | ||
c | Vector3 | ||
d | Vector3 | ||
color | Color | | <optional> | One for all or one per corner |
uvs | Array.<Vector2> | <optional> | One per corner, default across the tile |
- Source
- Type:
- Mesh
addStrip(points, normalsopt, uvsopt, colorsopt) → {Mesh}
Add a triangle strip, joined to the previous one by invisible flat triangles so one mesh holds many strips
- Strip order: the first three points make a triangle, then each point makes another with the two before it
- List the first three points counter clockwise as seen from the front, or the face points away and may vanish when back faces are culled
| Name | Type | Attributes | Description |
|---|---|---|---|
points | Array.<Vector3> | Strip order | |
normals | Vector3 | | <optional> | One for all or one per point, default up |
uvs | Vector2 | | <optional> | One for all or one per point, default zero |
colors | Color | | <optional> | One for all or one per point, default white |
- Source
- Type:
- Mesh
addTriangles(points, indices, normalsopt, uvsopt, colorsopt) → {Mesh}
Add triangles over their own vertices, the indexed form a model file comes in
- The mesh becomes indexed: a strip mesh is turned into triangles first, and strips added later join as triangles
- List each triangle counter clockwise as seen from the front, like a strip's first triangle
| Name | Type | Attributes | Description |
|---|---|---|---|
points | Array.<Vector3> | Each vertex once | |
indices | Array.<number> | Three vertex numbers per triangle, into points | |
normals | Vector3 | | <optional> | One for all or one per point, default up |
uvs | Vector2 | | <optional> | One for all or one per point, default zero |
colors | Color | | <optional> | One for all or one per point, default white |
- Source
- Type:
- Mesh
center() → {Mesh}
Move the mesh so the center of its bounds is on the origin
- Source
- Type:
- Mesh
combine(mesh, matrixopt, coloropt) → {Mesh}
Append another mesh transformed by a matrix, for building one shape out of several
| Name | Type | Attributes | Description |
|---|---|---|---|
mesh | Mesh | ||
matrix | Matrix4 | | <optional> | Transform, or just a position to move it to |
color | Color | <optional> | Multiplies the appended vertex colors |
- Source
- Type:
- Mesh
computeNormals(smoothopt) → {Mesh}
Derive normals from the triangles, of the strip or of the index list
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
smooth | boolean | <optional> | false | Round the lighting across faces instead of giving each face a hard edge; flat normals on an indexed mesh give every corner its own vertex |
- Source
- Type:
- Mesh
computeRadius() → {number}
Measure the bounding sphere around the origin into radius, called by upload
- Source
- Type:
- number
dispose()
Delete the GPU buffer now, the CPU arrays stay so the mesh can be rendered again
- Optional, the buffer is freed anyway once the mesh is garbage collected, this frees it right away
- Source
fit(sizeopt) → {Mesh}
Scale the mesh evenly so its largest extent is a size, for loaded models of unknown units
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
size | number | <optional> | 1 |
- Source
- Type:
- Mesh
flipNormals() → {Mesh}
Turn the mesh inside out so it is lit and drawn from within, for rooms and domes
- Source
- Type:
- Mesh
getBounds() → {Object}
Measure the axis aligned box around the vertices
- Source
- Type:
- Object
getTriangles() → {Object}
The mesh as an indexed triangle list, what upload sends to the GPU: the strip's real triangles over its distinct vertices, the joins between its pieces dropped and every triangle facing the way it did in the strip
- Vertices are compared to a millionth, so two at one place with the same normal, uv and color are one
- Source
- vertices are strip indices, one per distinct vertex; indices are the triangles, three per triangle, into vertices
- Type:
- Object
intersect(mesh, matrixopt) → {Mesh}
Returns a new mesh of only what is in both this mesh and the other, see subtract
| Name | Type | Attributes | Description |
|---|---|---|---|
mesh | Mesh | Closed, as the builders make them apart from the open ones like buildGrid | |
matrix | Matrix4 | | <optional> | Places the other mesh, or just a position to move it to |
- Source
- Type:
- Mesh
mirror(axisopt) → {Mesh}
Returns a new mesh: this one and its mirror image across the plane through the origin facing axis
- For modeling half a shape against that plane, a part that crosses it overlaps its image
| Name | Type | Attributes | Description |
|---|---|---|---|
axis | Vector3 | <optional> | Faces the mirror plane, vec3(1,0,0) mirrors across x |
- Source
- Type:
- Mesh
render(matrixopt, tileInfoopt, coloropt)
Draw the mesh with the current draw state, batched with its other uses in the opaque stage
| Name | Type | Attributes | Description |
|---|---|---|---|
matrix | Matrix4 | | <optional> | Object transform, or just a position to draw it at |
tileInfo | TileInfo | | <optional> | Texture, mesh uvs map across the tile or the whole texture |
color | Color | <optional> | Tint |
- Source
scaleUVs(scale) → {Mesh}
Scale every uv, so a whole texture repeats across the mesh when its TextureInfo wraps
| Name | Type | Description |
|---|---|---|
scale | Vector2 | | Repeats across and up, a number for both |
- Source
- Type:
- Mesh
setColor(color) → {Mesh}
Set every vertex color
| Name | Type | Description |
|---|---|---|
color | Color |
- Source
- Type:
- Mesh
spin(count, axisopt) → {Mesh}
Returns a new mesh of count copies of this one, each turned further around an axis through the origin
| Name | Type | Attributes | Description |
|---|---|---|---|
count | number | Copies, spaced evenly around the whole turn | |
axis | Vector3 | <optional> | Up by default |
- Source
- Type:
- Mesh
subtract(mesh, matrixopt) → {Mesh}
Returns a new mesh of this one with the other cut out of it, CSG with BSP trees
- Both must be closed, every edge shared by two triangles, as the builders make them apart from the open ones like buildGrid and buildRibbon; the result is closed and indexed, and neither mesh changes
- The faces a cut makes come from the other mesh's surface, turned to face out, with its normals, uvs and colors, so a smooth cylinder drills a round hole
- Parts that overlap must be joined with union to be one solid, not with combine, mirror or spin, which leave them overlapping, and CSG then gives a wrong shape
- Cuts split the triangles near them, so the result has more: a few thousand triangles take tens to a few hundred milliseconds, so build shapes this way at load time, not every frame; joining several cutters with union and cutting once is quicker than cutting with each in turn
- Details closer than about 1e-4 are made one, so build a very small part larger and scale it after
| Name | Type | Attributes | Description |
|---|---|---|---|
mesh | Mesh | Closed, as the builders make them apart from the open ones like buildGrid | |
matrix | Matrix4 | | <optional> | Places the other mesh, or just a position to move it to |
- Source
- Type:
- Mesh
const wall = buildBox(vec3(4, 3, .5)).subtract(buildBox(vec3(1, 2, 1)), vec3(0, -.5, 0)); // a doorwaytoIndexed() → {Mesh}
Turn a strip mesh into the indexed form, each distinct vertex once and the real triangles over them, in place
- An indexed mesh is left as it is; the builders make strips and a loader makes this, and either draws the same
- Source
- Type:
- Mesh
transform(matrix) → {Mesh}
Move, turn or scale every vertex in place, normals follow along
| Name | Type | Description |
|---|---|---|
matrix | Matrix4 | | Transform, or just an offset to move by |
- Source
- Type:
- Mesh
union(mesh, matrixopt) → {Mesh}
Returns a new mesh of everything in this mesh or the other, see subtract
| Name | Type | Attributes | Description |
|---|---|---|---|
mesh | Mesh | Closed, as the builders make them apart from the open ones like buildGrid | |
matrix | Matrix4 | | <optional> | Places the other mesh, or just a position to move it to |
- Source
- Type:
- Mesh
upload() → {Mesh}
Pack the vertices and create the GPU buffer, called automatically by render
- Source
- Type:
- Mesh