Menu - Top - Home - Donate to Me

H3DU.MeshBuffer

Back to documentation index.

H3DU.MeshBuffer()

A geometric mesh in the form of buffer objects. A mesh buffer is made up of one or more vertex attribute objects, and an array of vertex indices. Each vertex attribute object contains the values of one attribute of the mesh, such as positions, vertex normals, and texture coordinates. A mesh buffer can store vertices that make up triangles, line segments, or points.

This constructor creates an empty mesh buffer.

Methods

H3DU.MeshBuffer#getAttribute(name, [semanticIndex])

Gets a vertex attribute included in this mesh buffer.

Parameters

Return Value

A vertex buffer accessor, or null if the attribute doesn't exist. (Type: H3DU.BufferAccessor)

Example

The following function gets the positions, normals, texture coordinates, and colors of each primitive (line, text, or point) in the mesh buffer. A point will have one vertex per primitive, a line two vertices and a triangle three. The attributes, if present, will be stored in the "position", "normal", "uv", and "color" properties of each vertex.

function getPrimitives(mesh) {
var p=mesh.getAttribute("POSITION")
var n=mesh.getAttribute("NORMAL")
var t=mesh.getAttribute("TEXCOORD_0")
var c=mesh.getAttribute("COLOR")
var ind=mesh.getIndices()
var primSize = 3;
if(mesh.primitiveType() === Mesh.LINES)primSize = 2;
if(mesh.primitiveType() === Mesh.POINTS)primSize = 1;
var ret=[]
for(var i=0;i<ind.length;i+=primSize) {
var prim=[]
var index=ind[i]
for(var j=0;j<primSize;j++) {
var info={}
if(p)info.position=p.getVec(index,[])
if(n)info.normal=n.getVec(index,[])
if(t)info.uv=t.getVec(index,[])
if(c)info.color=c.getVec(index,[])
}
ret.push(prim)
}
return ret
}

H3DU.MeshBuffer#getBounds()

Finds the tightest bounding box that holds all vertices in the mesh buffer. Only positions with attribute semantic POSITION are used in the bounding box calculation.

Return Value

An array of six numbers describing the tightest axis-aligned bounding box that fits all vertices in the mesh. The first three numbers are the smallest-valued X, Y, and Z coordinates, and the last three are the largest-valued X, Y, and Z coordinates. This calculation uses the attribute with the semantic POSITION and set index 0. If there is no such attribute, or no vertices are defined in this buffer, returns the array [Inf, Inf, Inf, -Inf, -Inf, -Inf]. (Type: Array.<number>)

H3DU.MeshBuffer#getIndices()

Gets the array of vertex indices used by this mesh buffer.

Return Value

Return value. (Type: Uint16Array | Uint32Array | Uint8Array)

H3DU.MeshBuffer#getPositions()

Gets an array of vertex positions held by this mesh buffer, arranged by primitive. Only values with the attribute semantic POSITION_0 are returned.

Return Value

An array of primitives, each of which holds the vertices that make up that primitive. If this mesh holds triangles, each primitive will contain three vertices; if lines, two; and if points, one. Each vertex is an at least 3-element array containing that vertex's X, Y, and Z coordinates, in that order. (Type: Array.<Array.<number>>)

H3DU.MeshBuffer#merge(other)

Merges the vertices from another mesh into this one. The vertices from the other mesh will be copied into this one, and the other mesh's indices copied or adapted.

Parameters

Return Value

This object. (Type: H3DU.MeshBuffer)

Example

var copiedMesh = new H3DU.MeshBuffer().merge(meshToCopy);

H3DU.MeshBuffer#normalizeNormals()

Modifies this mesh buffer by converting the normals it defines to unit vectors ("normalized" vectors with a length of 1). Has no effect if this mesh buffer doesn't define any normals. All attributes with the semantic NORMAL, regardless of semantic index, are affected.

Return Value

This object. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#primitiveCount()

Gets the number of primitives (triangles, lines, and points) composed by all shapes in this mesh.

Return Value

Return value. (Type: number)

H3DU.MeshBuffer#primitiveType()

Gets the type of primitive stored in this mesh buffer.

Return Value

Either H3DU.Mesh.TRIANGLES, H3DU.Mesh.LINES, or H3DU.Mesh.POINTS. (Type: number)

H3DU.MeshBuffer#recalcNormals(flat, inward)

Recalculates the normal vectors for triangles in this mesh. For this to properly affect shading, each triangle in the mesh must have its vertices defined in counterclockwise order (if the triangle is being rendered in a right-handed coordinate system). Each normal calculated will be normalized to have a length of 1 (unless the normal is (0,0,0)), and will be stored in an attribute with semantic NORMAL_0. Will have an effect only if the buffer includes an attribute with semantic POSITION_0 and each of that attribute's values is at least 3 elements long. If the buffer already includes an attribute with semantic NORMAL_0, ensures its values are each at least 3 elements long.

Parameters

Return Value

This object. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#reverseNormals()

Modifies this mesh buffer by reversing the sign of normals it defines. Has no effect if this mesh buffer doesn't define any normals. All attributes with the semantic NORMAL, regardless of semantic index, are affected.

Return Value

This object. (Type: H3DU.MeshBuffer)

Example

The following code generates a two-sided mesh, where the normals on each side face in the opposite direction. This is only useful when drawing open geometric shapes, such as open cylinders and two-dimensional planar shapes. Due to the z-fighting effect, drawing a two-sided mesh is recommended only if face culling is enabled.

var twoSidedMesh = originalMesh.merge(
new H3DU.MeshBuffer().merge(originalMesh).reverseWinding().reverseNormals()
);

H3DU.MeshBuffer#reverseWinding()

Reverses the winding order of the triangles in this mesh buffer by swapping the second and third vertex indices of each one. Has an effect only if this mesh buffer consists of triangles.

Return Value

This object. (Type: H3DU.MeshBuffer)

Example

The following code generates a mesh that survives face culling, since the same triangles occur on each side of the mesh, but with different winding orders. This is useful when enabling This is only useful when drawing open geometric shapes, such as open cylinders and two-dimensional planar shapes. Due to the z-fighting effect, drawing this kind of mesh is recommended only if face culling is enabled.

var frontBackMesh = originalMesh.merge(
new H3DU.MeshBuffer().merge(originalMesh).reverseWinding()
);

H3DU.MeshBuffer#setAttribute(name, buffer, countPerValue, [offset], [stride])

Adds information about a buffer attribute to this mesh buffer (or sets an existing attribute's information). An attribute gives information about the per-vertex data used and stored in a vertex buffer.

Parameters

Return Value

This object.Throws an error if the given semantic is unsupported. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#setAttributeEx(name, index, buffer, [countPerValue], [offset], [stride])

Adds information about a buffer attribute to this mesh buffer (or sets an existing attribute's information), taking a semantic index as an additional parameter. An attribute gives information about the per-vertex data used and stored in a vertex buffer.

Parameters

Return Value

This object.Throws an error if the given semantic is unsupported. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#setColor(color)

Sets all the vertices in this mesh to the given color, by assigning each value with the attribute semantic COLOR to the given color. (If the attribute's count per value is less than 4, each such value will be set to as many elements of the converted 4-element color as possible.) If an attribute with the semantic COLOR doesn't exist, an attribute with the semantic COLOR_0 and a count per value of 3 is created.

All attributes with the semantic COLOR, regardless of semantic index, are affected by this method.

Parameters

Return Value

This object. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#setColor3()

Deprecated: Use H3DU.MeshBuffer#setColor instead.

Alias for the H3DU.MeshBuffer#setColor method for compatibility.

H3DU.MeshBuffer#setIndices(indices)

Sets the vertex indices used by this mesh buffer.

Parameters

Return Value

This object. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#setPrimitiveType(primType)

Sets the type of graphics primitives stored in this mesh buffer.

Parameters

Return Value

This object. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#toWireFrame()

Deprecated: Included here for compatibility with H3DU.Mesh. Use H3DU.MeshBuffer.wireFrame instead.

Creates a new mesh with triangles converted to line segments.

Return Value

A new mesh with triangles converted to lines. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#transform(matrix)

Transforms the positions and normals of all the vertices currently in this mesh. Only values with the attribute semantic POSITION_0 or NORMAL_0 will be affected by this method; values of other attributes will be unaffected.

Parameters

Return Value

This object. (Type: H3DU.MeshBuffer)

H3DU.MeshBuffer#vertexCount()

Gets the number of vertices in this mesh buffer, that is, the number of vertex indices in its index buffer (some of which may be duplicates).

Return Value

Return value. (Type: number)

H3DU.MeshBuffer#vertexIndices(primitiveIndex, ret)

Gets the vertex indices of a given primitive (triangle, line, or point) in this mesh buffer.

Parameters

Return Value

The parameter "ret". (Type: Array.<number>)

H3DU.MeshBuffer#wireFrame()

Converts the triangles in this mesh to line segments. Has no effect if this mesh doesn't use triangles as primitives.

Return Value

This object. (Type: H3DU.MeshBuffer)

Back to documentation index.