Render3D. Mesh

Mesh - A triangle strip with positions, normals, uvs and colors, uploaded once and drawn by matrix

  • Build with addStrip, addQuad, combine or the shape builders, then render each frame
  • The GPU buffer is created lazily on first render and dropped by dispose

Constructor

new Mesh()

Create an empty mesh

Example
const mesh = buildLathe([[0, -1], [1, 0], [0, 1]], 4); // octahedron
mesh.render(buildMatrix(vec3(0, 1, 0)), undefined, RED);

Members

buffer :WebGLBuffer|undefined

Type:
  • WebGLBuffer | undefined
Properties
TypeDescription
WebGLBuffer | undefined

GPU buffer, created by upload

bufferCount

Properties
TypeDescription
number

Vertices in the GPU buffer

colors :Array.<Color>

Type:
  • Array.<Color>
Properties
TypeDescription
Array.<Color>

Vertex colors

dirty

Properties
TypeDescription
boolean

The mesh changed and needs uploading again, set it yourself if you edit the arrays

instanced :boolean|undefined

Type:
  • boolean | undefined
Properties
TypeDescription
boolean | undefined

Draw every use of this mesh in the opaque stage as one instanced call, undefined follows render3D.instancing

normals :Array.<Vector3>

Type:
  • Array.<Vector3>
Properties
TypeDescription
Array.<Vector3>

Vertex normals

points :Array.<Vector3>

Type:
  • Array.<Vector3>
Properties
TypeDescription
Array.<Vector3>

Vertex positions in strip order

radius

Properties
TypeDescription
number

Bounding sphere radius around the origin, for culling and picking, computed by upload

uvs :Array.<Vector2>

Type:
  • Array.<Vector2>
Properties
TypeDescription
Array.<Vector2>

Vertex texture coords, 0-1 across the tile

vertexCount

Number of vertices in the mesh

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

Parameters:
NameTypeAttributesDescription
aVector3
bVector3
cVector3
dVector3
colorColor | Array.<Color><optional>

One for all or one per corner

uvsArray.<Vector2><optional>

One per corner, default across the tile

Returns:
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
Parameters:
NameTypeAttributesDescription
pointsArray.<Vector3>

Strip order

normalsVector3 | Array.<Vector3><optional>

One for all or one per point, default up

uvsVector2 | Array.<Vector2><optional>

One for all or one per point, default zero

colorsColor | Array.<Color><optional>

One for all or one per point, default white

Returns:
Type: 
Mesh

center() → {Mesh}

Move the mesh so the center of its bounds is on the origin

Returns:
Type: 
Mesh

combine(mesh, matrixopt, coloropt) → {Mesh}

Append another mesh transformed by a matrix, for building one shape out of several

Parameters:
NameTypeAttributesDescription
meshMesh
matrixMatrix4 | Vector3<optional>

Transform, or just a position to move it to

colorColor<optional>

Multiplies the appended vertex colors

Returns:
Type: 
Mesh

computeNormals(smoothopt) → {Mesh}

Derive normals from the strip's triangles

Parameters:
NameTypeAttributesDefaultDescription
smoothboolean<optional>
false

Round the lighting across faces instead of giving each face a hard edge

Returns:
Type: 
Mesh

computeRadius() → {number}

Measure the bounding sphere around the origin into radius, called by upload

Returns:
Type: 
number

dispose()

Delete the GPU buffer, the CPU arrays stay so the mesh can be rendered again

fit(sizeopt) → {Mesh}

Scale the mesh evenly so its largest extent is a size, for loaded models of unknown units

Parameters:
NameTypeAttributesDefaultDescription
sizenumber<optional>
1
Returns:
Type: 
Mesh

flipNormals() → {Mesh}

Turn the mesh inside out so it is lit and drawn from within, for rooms and domes

Returns:
Type: 
Mesh

getBounds() → {Object}

Measure the axis aligned box around the vertices

Returns:
Type: 
Object

render(matrixopt, tileInfoopt, coloropt)

Draw the mesh with the current draw state, batched with its other uses in the opaque stage

Parameters:
NameTypeAttributesDescription
matrixMatrix4 | Vector3<optional>

Object transform, or just a position to draw it at

tileInfoTileInfo | TextureInfo<optional>

Texture, mesh uvs map across the tile or the whole texture

colorColor<optional>

Tint

scaleUVs(scale) → {Mesh}

Scale every uv, so a whole texture repeats across the mesh when its TextureInfo wraps

Parameters:
NameTypeDescription
scaleVector2 | number

Repeats across and up, a number for both

Returns:
Type: 
Mesh

setColor(color) → {Mesh}

Set every vertex color

Parameters:
NameTypeDescription
colorColor
Returns:
Type: 
Mesh

transform(matrix) → {Mesh}

Move, turn or scale every vertex in place, normals follow along

Parameters:
NameTypeDescription
matrixMatrix4 | Vector3

Transform, or just an offset to move by

Returns:
Type: 
Mesh

upload() → {Mesh}

Pack the vertices and create the GPU buffer, called automatically by render

Returns:
Type: 
Mesh