Class Triangle

An easy-to-use triangle representation. Memory efficient per triangle, but not per whole mesh, as it does not share vertex data with other triangles. Represents a manifold by providing which triangle an edge is connected to. Winding order is counter-clockwise.

          0
edge 0 / \ edge 2
(AB) / \ (CA)
1-----2
edge 1
(BC)

Hierarchy

  • Triangle

Constructors

  • Create a new Triangle.

    Parameters

    • Optional vertexData: Float32Array

      If supplied, uses this vertex data for the triangle, with the same format as vertexData. If not supplied, then creates a new vertex data array where all vertices have values of 0 for all attributes.

    Returns Triangle

Properties

bitData: number = 0b111111

Generic data of this triangle, packed in a single integer.

Bits 0-5: Which edge of another triangle is each respective edge connected to?

  • bits 0-1: edge index of other triangle for this triangle's edge 0
  • bits 2-3: edge index of other triangle for this triangle's edge 1
  • bits 4-5: edge index of other triangle for this triangle's edge 2

Other triangle's edge index can be 0, 1 or 2. If 3, then the edge is not connected.

Bits 6-31: 25-bit material ID. Triangles with different material IDs will be put in different WL.Mesh instances, but the same manifold if they are connected.

edgeTriangle0: null | Triangle = null

The triangle connected to edge 0

edgeTriangle1: null | Triangle = null

The triangle connected to edge 1

edgeTriangle2: null | Triangle = null

The triangle connected to edge 2

helper: number = 0

A generic helper variable intended to be used as an index.

vertexData: Float32Array

The positions, normals and UVs of each triangle. Even though normals and UVs are optional, space will still be reserved for them.

The format is interleaved:

  • float 0-2: vertex 0's position
  • float 3-5: vertex 0's normal
  • float 6-7: vertex 0's UV (unused if no uvs)
  • float 8-11: vertex 0's tangent (unused if no tangents)
  • float 12-15: vertex 0's color (unused if no colors)
  • float 16-18: vertex 1's position
  • float 19-21: vertex 1's normal
  • float 22-23: vertex 1's UV
  • float 24-27: vertex 1's tangent
  • float 28-31: vertex 1's color
  • float 32-34: vertex 2's position
  • float 35-37: vertex 2's normal
  • float 38-39: vertex 2's UV
  • float 40-43: vertex 2's tangent
  • float 44-47: vertex 2's color

Accessors

  • get materialID(): number
  • The material ID of this triangle. This will map to a WL.Material instance once the Triangle is converted to a WL.Mesh.

    Returns number

  • set materialID(newMaterialID: number): void
  • Parameters

    • newMaterialID: number

    Returns void

Methods

  • Automatically make tangents for this triangle, by using the direction of an edge of the triangle as the tangent.

    Parameters

    • edgeIndex: number

      The index of the edge to use as the direction. For example, if 0 is used, then the direction from vertex 0 to vertex 1 is used as the direction of the tangent.

    • flip: boolean = false

      If true, then the direction of the edge will be flipped. False by default.

    Returns void

  • Connect a given edge to any other triangle's edge. When connecting, the other triangle is modified. If there is already a connection on the edge, then the edge is diconnected (same for the other triangle's edge).

    Parameters

    • edgeIndex: number

      The index of the edge in counter-clockwise order. For example, edge 0 is the edge between vertex 0 and vertex 1.

    • otherEdgeIndex: number

      The index of the edge of the other triangle in counter-clockwise order. For example, edge 0 is the edge between vertex 0 and vertex 1.

    • otherTriangle: Triangle

      The other triangle to connect to.

    Returns void

  • Disconnect a given edge from any other triangle that it's connected to. When disconnecting, the other triangle is modified.

    Parameters

    • edgeIndex: number

      The index of the edge in counter-clockwise order. For example, edge 0 is the edge between vertex 0 and vertex 1.

    Returns void

  • Get a copy of the color of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns vec4

  • Get the edge that is connected to a given edge of this triangle, as a pair containing the other edge index and the other triangle.

    Returns

    If the edge is not connected, returns null, otherwise, returns a tuple containing the other edge index and the other triangle.

    Parameters

    • edgeIndex: number

      The index of the edge in counter-clockwise order. For example, edge 0 is the edge between vertex 0 and vertex 1.

    Returns null | [number, Triangle]

  • Check whether the edge between 2 given vertices matches any edge from another triangle

    Returns

    0 to 2 if an edge of another triangle matches with the input edge, where the returned number is the matching edge index, or null if no edge matches.

    Parameters

    • aVertexIndex: number

      The index of the vertex, from 0 to 2, of the first point in the edge of this triangle.

    • bVertexIndex: number

      The index of the vertex, from 0 to 2, of the second point in the edge of this triangle.

    • otherTriangle: Triangle

      The other triangle to compare edges with.

    Returns null | number

  • Calculate the midpoint of the triangle by getting the average position of each vertex.

    Returns

    A new vec3 containing the midpoint of the triangle.

    Returns vec3

  • Get a copy of the normal of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns vec3

  • Get a copy of the position of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns vec3

  • Calculate the surface area of the triangle.

    Returns

    The surface area of the triangle.

    Returns number

  • Get a copy of the tangent of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns vec4

  • Get a copy of the texture coordinates of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns vec2

  • Get a copy of the interleaved vertex data at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns Float32Array

  • Get the offset on the vertexData buffer for a specific vertex.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns number

  • Get the vertex star of a specific vertex. The star of a vertex is a list of triangles that are connected to a vertex.

    Returns

    A list of triangles and vertex indices that share the given vertex. Given as a list of pairs, where each pair contains the connected triangle, and the vertex index in that triangle.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns VertexStar

  • Check if this triangle has non-zero vertex normals.

    Returns

    True if the vertex normals at a given vertex index are not zero.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    Returns number

  • Check if a given edge is connected to another triangle.

    Parameters

    • edgeIndex: number

      The index of the edge in counter-clockwise order. For example, edge 0 is the edge between vertex 0 and vertex 1.

    Returns boolean

  • Normalize this triangle's position, in place. Normals are set to be equal to the position, and tangents are set to go around the zenith (around +y in CCW direction/west to east). Useful for spherifying a mesh.

    Returns void

  • Check if the position of a vertex matches the position of another vertex from another triangle.

    Returns

    True if the position matches, false otherwise.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • otherVertexIndex: number

      The index of the vertex in the other triangle, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • otherTriangle: Triangle

      The triangle which has the other vertex.

    Returns boolean

  • Rotate the triangle by a given rotation.

    Parameters

    • rotation: quat

      The quaternion to rotate by.

    • rotateNormal: boolean = true

      Should the normals of the triangle be rotated? Defaults to true.

    • rotateTangent: boolean = true

      Should the tangents of the triangle be rotated? Defaults to true.

    Returns void

  • Scale the triangle by a given factor.

    Parameters

    • factor: vec3

      The factor to scale by.

    Returns void

  • Set the tangent of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • newColor: Readonly<vec4>

    Returns void

  • Set the colors of all vertices in this triangle.

    Parameters

    • new0: Readonly<vec4>

      The new color for the vertex (vertex 0).

    • new1: Readonly<vec4>

      The new color for the vertex (vertex 1).

    • new2: Readonly<vec4>

      The new color for the vertex (vertex 2).

    Returns void

  • Internal method. Set a connected edge of this triangle.

    Parameters

    • edgeIndex: number
    • triangle: null | Triangle

    Returns void

  • Set the normal of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • newNormal: Readonly<vec3>

      The new normal for the vertex.

    Returns void

  • Set the normals of all vertices in this triangle.

    Parameters

    • new0: Readonly<vec3>

      The new normal for the vertex (vertex 0).

    • new1: Readonly<vec3>

      The new normal for the vertex (vertex 1).

    • new2: Readonly<vec3>

      The new normal for the vertex (vertex 2).

    Returns void

  • Set the position of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • newPosition: Readonly<vec3>

      The new position for the vertex.

    Returns void

  • Set the positions of all vertices in this triangle.

    Parameters

    • new0: Readonly<vec3>

      The new position for the vertex (vertex 0).

    • new1: Readonly<vec3>

      The new position for the vertex (vertex 1).

    • new2: Readonly<vec3>

      The new position for the vertex (vertex 2).

    Returns void

  • Set the tangent of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • newTangent: Readonly<vec4>

      The new tangent for the vertex.

    Returns void

  • Set the tangents of all vertices in this triangle.

    Parameters

    • new0: Readonly<vec4>

      The new tangent for the vertex (vertex 0).

    • new1: Readonly<vec4>

      The new tangent for the vertex (vertex 1).

    • new2: Readonly<vec4>

      The new tangent for the vertex (vertex 2).

    Returns void

  • Set the texture coordinates of a vertex at a given vertex index.

    Parameters

    • vertexIndex: number

      The index of the vertex, from 0 to 2. 0 is the first vertex of the triangle, etc...

    • newUV: Readonly<vec2>

      The new texture coordinates for the vertex.

    Returns void

  • Set the UVs of all vertices in this triangle.

    Parameters

    • new0: Readonly<vec2>

      The new UV for the vertex (vertex 0).

    • new1: Readonly<vec2>

      The new UV for the vertex (vertex 1).

    • new2: Readonly<vec2>

      The new UV for the vertex (vertex 2).

    Returns void

  • Transform vertices by a given transformation matrix. By default, normals and tangents are also transformed. If they are transformed, then a normal matrix needs to be supplied, otherwise they are not transformed.

    Parameters

    • matrix: mat4

      The transformation matrix to transform positions by.

    • Optional normalMatrix: mat3

      The transformation matrix to transform normals and tangents by. Will be ignored if normals and tangents aren't transformed. If not supplied and normals or tangents are to be transformed, then the triangle's normals and tangents will not be transformed.

    • transformNormal: boolean = true

      Should the normals of the triangle be transformed? Defaults to true.

    • transformTangent: boolean = true

      Should the tangents of the triangle be transformed? Defaults to true.

    Returns void

  • Translate the triangle by a given offset.

    Parameters

    • offset: vec3

      The offset to translate by.

    Returns void

  • Uniformly scale the triangle by a given factor.

    Parameters

    • factor: number

      The factor to scale by.

    Returns void

  • Create a new Triangle from WL.Mesh data.

    Parameters

    • idx0: number

      The index of the first vertex in the given mesh attributes

    • idx1: number

      The index of the second vertex in the given mesh attributes

    • idx2: number

      The index of the third vertex in the given mesh attributes

    • positions: MeshAttributeAccessor<Float32ArrayConstructor>

      A mesh attribute accessor for the mesh's vertex positions

    • normals: null | MeshAttributeAccessor<Float32ArrayConstructor> = null

      An optional mesh attribute accessor for the mesh's vertex normals

    • uvs: null | MeshAttributeAccessor<Float32ArrayConstructor> = null

      An optional mesh attribute accessor for the mesh's vertex texture coordinates

    • tangents: null | MeshAttributeAccessor<Float32ArrayConstructor> = null

      An optional mesh attribute accessor for the mesh's vertex tangents

    • colors: null | MeshAttributeAccessor<Float32ArrayConstructor> = null

      An optional mesh attribute accessor for the mesh's vertex colors

    Returns Triangle

  • Create a new Triangle from a list of vertices, in counter-clockwise order. Each vertex has the same interleaved format as vertexData, but only has a single vertex instead of 3.

    Parameters

    • vert0: Float32Array

      The first vertex.

    • vert1: Float32Array

      The second vertex.

    • vert2: Float32Array

      The third vertex.

    Returns Triangle

Generated using TypeDoc