Menu - Top - Home - Donate to Me

H3DU.Mesh

Back to documentation index.

H3DU.Mesh([vertices], [indices], [format])

Specifies the triangles, lines, or points that make up a geometric shape. Each vertex, that is, each point, each end of a line, and each corner of a triangle, can also specify the following attributes:

Notes:

Parameters

Members

Methods

H3DU.Mesh.BITANGENTS_BIT (constant)

Deprecated: Deprecated because the default shader no longer uses tangent and bitangent attributes for normal mapping. To define bitangent vectors for a mesh, use the H3DU.MeshBuffer class and create a buffer attribute with the H3DU.Semantics.BITANGENT semantic.

The mesh contains bitangent vectors for each vertex.

Default Value: 16

H3DU.Mesh.COLORS_BIT (constant)

The mesh contains colors for each vertex.

Default Value: 2

H3DU.Mesh.LINES (constant)

Primitive mode for rendering line segments, made up of 2 vertices each.

H3DU.Mesh.LINES_BIT (constant)

The mesh consists of lines (2 vertices per line) instead of triangles (3 vertices per line).

Default Value: 256

H3DU.Mesh.LINE_STRIP (constant)

Primitive mode for rendering connected line segments. The first 2 vertices make up the first line, and each additional line is made up of the last vertex and 1 new vertex.

Default Value: 3

H3DU.Mesh.NORMALS_BIT (constant)

The mesh contains normals for each vertex.

Default Value: 1

H3DU.Mesh.POINTS (constant)

Primitive mode for rendering points, made up of 1 vertex each.

Default Value: 0

H3DU.Mesh.POINTS_BIT (constant)

The mesh consists of points (1 vertex per line).

Default Value: 512

H3DU.Mesh.QUADS (constant)

Primitive mode for rendering quadrilaterals, made up of 4 vertices each. Each quadrilateral is broken into two triangles: the first triangle consists of the first, second, and third vertices, in that order, and the second triangle consists of the first, third, and fourth vertices, in that order.

Default Value: 7

H3DU.Mesh.QUAD_STRIP (constant)

Primitive mode for rendering a strip of quadrilaterals (quads). The first 4 vertices make up the first quad, and each additional quad is made up of the last 2 vertices of the previous quad and 2 new vertices. Each quad is broken into two triangles: the first triangle consists of the first, second, and third vertices, in that order, and the second triangle consists of the third, second, and fourth vertices, in that order.

Default Value: 8

H3DU.Mesh.TANGENTS_BIT (constant)

Deprecated: Deprecated because the default shader no longer uses tangent and bitangent attributes for normal mapping. To define tangent vectors for a mesh, use the H3DU.MeshBuffer class and create a buffer attribute with the H3DU.Semantics.TANGENT semantic.

The mesh contains tangent vectors for each vertex.

Default Value: 8

H3DU.Mesh.TEXCOORDS_BIT (constant)

The mesh contains texture coordinates for each vertex.

Default Value: 4

H3DU.Mesh.TRIANGLES (constant)

Primitive mode for rendering triangles, made up of 3 vertices each.

Default Value: 4

H3DU.Mesh.TRIANGLE_FAN (constant)

Primitive mode for rendering a triangle fan. The first 3 vertices make up the first triangle, and each additional triangle is made up of the first vertex of the first triangle, the previous vertex, and 1 new vertex.

Default Value: 6

H3DU.Mesh.TRIANGLE_STRIP (constant)

Primitive mode for rendering a triangle strip. The first 3 vertices make up the first triangle, and each additional triangle is made up of the last 2 vertices and 1 new vertex. For the second triangle in the strip, and every other triangle after that, the first and second vertices are swapped when generating that triangle.

Default Value: 5

H3DU.Mesh#bitangent3(x, y, z)

Deprecated: Deprecated because the default shader no longer uses tangent and bitangent attributes for normal mapping. To define bitangent vectors for a mesh, use the H3DU.MeshBuffer class and create a buffer attribute with the H3DU.Semantics.BITANGENT semantic.

Sets the current bitangent vector for this mesh. Future vertex positions defined (with vertex3()) will have this bitangent. The new current bitangent will apply to future vertices even if the current mode is TRIANGLE_FAN and some vertices were already given for that mode. The bitangent passed to this method will not automatically be normalized to unit length.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#color3(r, g, b)

Sets the current color for this mesh. Future vertex positions defined (with vertex3()) will have this color. The new current color will apply to future vertices even if the current mode is TRIANGLE_FAN and some vertices were already given for that mode. Only the red, green, and blue components will be used.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#enumPrimitives(func)

Enumerates the primitives (lines, triangles, and points) included in this mesh.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#getBoundingBox()

Finds the tightest axis-aligned bounding box that holds all vertices in the mesh.

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. If the mesh is empty, returns the array [Inf, Inf, Inf, -Inf, -Inf, -Inf]. (Type: Array.<number>)

H3DU.Mesh#getVertex(index)

Gets the position of the vertex with the given index in this mesh.

Parameters

Return Value

A 3-element array giving the X, Y, and Z coordinates, respectively, of the vertex position, or null if the index is less than 0 or equals the number of vertices in this mesh or greater. (Type: Array.<number>)

H3DU.Mesh#getVertexNormal(index)

Gets the normal of the vertex with the given index in this mesh.

Parameters

Return Value

A 3-element array giving the X, Y, and Z coordinates, respectively, of the vertex normal, or null if the index is less than 0 or equals the number of vertices in this mesh or greater. Returns (0,0,0) if the given vertex exists but doesn't define a normal. (Type: Array.<number>)

H3DU.Mesh#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. Also, resets the primitive mode (see H3DU.Mesh#mode) so that future vertices given will not build upon previous vertices.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

Example

// Use the following idiom to make a copy of a geometric mesh:
var copiedMesh = new H3DU.Mesh().merge(meshToCopy);

H3DU.Mesh#mode(m)

Changes the primitive mode for this mesh. Future vertices will be drawn as primitives of the new type. The primitive type can be set to the same mode, in which case future vertices given will not build upon previous vertices.

An H3DU.Mesh object can contain primitives of different types, such as triangles and lines. For example, it's allowed to have a mesh with triangles, then call this method, say, with Mesh.LINE_STRIP to add line segments to that mesh. However, this functionality may be deprecated in future versions.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#normal3(x, y, z)

Sets the current normal for this mesh. Future vertex positions defined (with vertex3()) will have this normal. The new current normal will apply to future vertices even if the current mode is TRIANGLE_FAN and some vertices were already given for that mode. The normal passed to this method will not automatically be normalized to unit length.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#normalizeNormals()

Modifies this mesh by converting the normals it defines to "unit vectors" ("normalized" vectors with a length of 1).

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#primitiveCount()

Gets the number of primitives (triangles, lines, or points) that this mesh contains.

Return Value

Return value. (Type: number)

H3DU.Mesh#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)).

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#recalcTangents()

Deprecated: Deprecated because the default shader no longer uses tangent and bitangent attributes for normal mapping. This method may be reimplemented in the H3DU.MeshBuffer class in the future.

Recalculates the tangent and bitangent vectors for triangles in this mesh. This method only has an effect if this mesh includes normals and texture coordinates.

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#reverseNormals()

Modifies this mesh by reversing the sign of normals it defines.

Return Value

This object. (Type: H3DU.Mesh)

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 those generated by H3DU.Meshes.createCylinder or H3DU.Meshes.createDisk. 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.Mesh().merge(originalMesh).reverseWinding().reverseNormals()
);

H3DU.Mesh#reverseWinding()

Reverses the winding order of the triangles in this mesh by swapping the second and third vertex indices of each one.

Return Value

This object. (Type: H3DU.Mesh)

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 back-face culling and drawing open geometric shapes such as those generated by H3DU.Meshes.createCylinder or H3DU.Meshes.createDisk. 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.Mesh().merge(originalMesh).reverseWinding()
);

H3DU.Mesh#setColor3(r, g, b)

Sets all the vertices in this mesh to the given color. This method doesn't change this mesh's current color. Only the color's red, green, and blue components will be used.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#setVertex(index, x, y, z)

Sets the X, Y, and Z coordinates of the vertex with the given index. Has no effect if the index is less than 0 or equals the number of vertices in this mesh or greater.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#setVertexNormal(index, x, y, z)

Sets the normal associated with the vertex with the given index. Has no effect if the index is less than 0 or equals the number of vertices in this mesh or greater.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#tangent3(x, y, z)

Deprecated: Deprecated because the default shader no longer uses tangent and bitangent attributes for normal mapping. To define tangent vectors for a mesh, use the H3DU.MeshBuffer class and create a buffer attribute with the H3DU.Semantics.TANGENT semantic.

Sets the current tangent vector for this mesh. Future vertex positions defined (with vertex3()) will have this normal. The new current tangent will apply to future vertices even if the current mode is TRIANGLE_FAN and some vertices were already given for that mode. The tangent passed to this method will not automatically be normalized to unit length.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#texCoord2(u, v)

Sets the current texture coordinates for this mesh. Future vertex positions defined (with vertex3()) will have these texture coordinates. The new current texture coordinates will apply to future vertices even if the current mode is TRIANGLE_FAN and some vertices were already given for that mode.

H3DU.Texture coordinates are a set of 2 values each ranging from 0 to 1, where (0, 0) is the lower right corner of the texture (by default), and (1, 1) is the upper right corner (by default).

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#toWireFrame()

Converts this mesh to a new mesh with triangles converted to line segments. The new mesh will reuse the vertices contained in this one without copying the vertices. If the mesh consists of points or line segments, it will remain unchanged.

Return Value

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

H3DU.Mesh#transform(matrix)

Transforms the positions and normals of all the vertices currently in this mesh. The matrix won't affect vertices added afterwards, and won't affect other attributes, including tangents and bitangents. Also, resets the primitive mode (see H3DU.Mesh#mode) so that future vertices given will not build upon previous vertices. Future vertices should not be added after calling this method without calling mode() first.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#vertex2(x, y)

Adds a new vertex to this mesh. The Z coordinate will be treated as 0.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#vertex3(x, y, z)

Adds a new vertex to this mesh. If appropriate, adds an additional face index according to this mesh's current mode. The vertex will adopt this mesh's current normal, color, and texture coordinates if they have been defined.

Parameters

Return Value

This object. (Type: H3DU.Mesh)

H3DU.Mesh#vertexCount()

Gets the number of vertices included in this mesh.

Return Value

Return value. (Type: number)

Back to documentation index.