Class MeshBuilder

A helper class for easily creating meshes, with connected triangles.

Hierarchy

  • MeshBuilder

Constructors

Properties

engine: WonderlandEngine

The Wonderland Engine instance being used

triangles: Triangle[] = ...

The list of all triangles in this manifold. Note that this array might be detached from the builder and replaced with a new array. It is safe to use between operations, but when doing some operations such as subDivide4, a new array will be created.

Accessors

  • get isConnected(): boolean
  • Check if all triangles in the MeshBuilder are connected to each-other.

    Returns

    True if all triangles are connected.

    Returns boolean

Methods

  • Applies smooth normals to all vertices that have no normals set (0,0,0). Smooth normals are calculated for each vertex by getting all triangles connected to the vertex (the vertex star), and making smoothing groups by checking the angle between all of those triangles. Triangles that have similar angles will contribute to the average normal. Triangles are expected to have their helpers set. If not, make sure to call setTriangleHelpers (note that addTriangle already does this).

    Parameters

    • maxAngle: number

      Maximum angle, in radians, between 2 triangles for them to be considered part of the same smoothing group for a vertex

    • resetNormals: boolean = true

      If true, then all vertex normals will be reset to (0,0,0) before applying the modifier

    • mergeTangents: boolean = true

      Defaults to true. If true, then smoothed vertices will have their tangents merged.

    Returns void

  • Add triangles that make up a sub-divided quad. Adding a quad with 1 sub-division will create 2 new triangles, while adding a quad with 2 sub-divisions will create 8 triangles (4 sub-quads, with 2 triangles each).

    All edges in generated triangles will be connected, except the border edges; new triangles will not be connected to triangles already present in the builder.

    Parameters

    • tlPos: vec3

      Top-left quad position.

    • trPos: vec3

      Top-right quad position.

    • blPos: vec3

      Bottom-left quad position.

    • brPos: vec3

      Bottom-right quad position.

    • materialID: number

      The material to use for the quad, as a numeric ID.

    • addTangents: boolean = true

      True by default. If true, then vertex tangents will be generated for each triangle. Tangents point from left to right, and have a w component of 1.

    • subDivisions: number = 1

      The amount of horizontal and vertical subdivisions to use. 1 by default (only one square). If there are 2 sub-divisions there will be 4 squares, 3 sub-divisions there will be 9 squares, etc...

    • Optional tlUV: vec2

      Top-left quad texture coordinate.

    • Optional trUV: vec2

      Top-right quad texture coordinate.

    • Optional blUV: vec2

      Bottom-left quad texture coordinate.

    • Optional brUV: vec2

      Bottom-right quad texture coordinate.

    Returns void

  • Similar to addSubdivQuad, but also records edges and triangles to given lists so that they can be used with autoConnectEdges.

    Parameters

    • edgeList: EdgeList

      The list of edges that will be connected to the list of triangles. Edges will be appended to this list if the edgeMask is set to a non-zero value.

    • connectableTriangles: Triangle[]

      The list of triangles that will be connected to the list of edges. Triangles will be appended to this list if the connectableTriMask is set to a non-zero value.

    • edgeMask: number

      A 4-bit bitmask with the edges that will be added to the list of edges. The bits, from most significant to least significant, are: left edge, right edge, top edge, bottom edge.

    • connectableTriMask: number

      A 4-bit bitmask with the triangles that will be added to the list of connectable triangles. The bits, from most significant to least significant, are: left edge triangles, right edge triangles, top edge triangles, bottom edge triangles.

    • tlPos: vec3

      Top-left quad position.

    • trPos: vec3

      Top-right quad position.

    • blPos: vec3

      Bottom-left quad position.

    • brPos: vec3

      Bottom-right quad position.

    • materialID: number

      The material to use for the quad, as a numeric ID.

    • addTangents: boolean = true

      True by default. If true, then vertex tangents will be generated for each triangle. Tangents point from left to right, and have a w component of 1.

    • subDivisions: number = 1
    • Optional tlUV: vec2

      Top-left quad texture coordinate.

    • Optional trUV: vec2

      Top-right quad texture coordinate.

    • Optional blUV: vec2

      Bottom-left quad texture coordinate.

    • Optional brUV: vec2

      Bottom-right quad texture coordinate.

    Returns void

  • Create a list of Wonderland Engine meshes and a manifold from the current list of triangles. Helpers are expected to be set. If not, make sure to call setTriangleHelpers.

    Output meshes are optimised by merging vertices with the same vertex data, via indexing.

    Parameters

    • materialMap: Map<number, null | Material>

      Maps each material index to a Wonderland Engine material. Triangles with different material will be put in separate meshes, but in the same manifold. A null material is equivalent to the material being missing in the material map. Materials missing from the material map will use null as the material so they can be replaced later with a fallback material.

    • hints: HintMap

      Maps a hint to a material. The null key represents the default hint. A hint decides which mesh attribute to generate.

    • Optional failOnMissing: boolean

      If this is true and a hinted mesh attribute fails to be gotten, an error will be thrown.

    • Optional generateManifold: true

      True by default. If true, a manifold and a submesh map will also be generated, otherwise, these will be null. Note than if a manifold is generated, then the triangles must form a 2-manifold surface, but if a manifold is not generated, then even a triangle soup is supported.

    Returns [submeshes: Submesh[], mergeMap: MergeMap]

  • Parameters

    • materialMap: Map<number, null | Material>
    • hints: HintMap
    • failOnMissing: boolean
    • generateManifold: false

    Returns [submeshes: Submesh[], mergeMap: null]

  • Internal function. Creates a submesh

    Parameters

    • material: null | Material
    • hint: Hint
    • failOnMissing: boolean
    • triangles: Triangle[]
    • timOffset: number
    • triIdxMap: null | Uint32Array
    • totalVertexCount: number
    • indexRangeList: IndexRangeList

    Returns [submesh: Submesh, totalVertexCount: number]

  • Make UVs for an equirectangular projection. The mapping will always be heavily distorted near the poles as it is impossible to do equirectangular projections properly without custom shaders.

    Generally, more subdivisions lead to better mappings, but this has diminishing returns.

    Returns void

  • Rotate each triangle in the MeshBuilder by a given rotation.

    Parameters

    • rotation: quat

      The quaternion to rotate by.

    • rotateNormal: boolean = true

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

    • rotateTangent: boolean = true

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

    Returns void

  • Scale each triangle in the MeshBuilder by a given factor.

    Parameters

    • factor: vec3

      The factor to scale by.

    Returns void

  • Map triangles back to their indices in the triangles array by setting the helper variable of each triangle.

    Returns void

  • Internal method. Makes smooth vertex normals given helper values.

    Parameters

    • hardNormals: vec3[]
    • surfaceAreas: number[]
    • dotThreshold: number
    • triangle: Triangle
    • vertexIdx: number
    • mergeTangents: boolean

    Returns void

  • Sub-divide each triangle in the MeshBuilder into 4 triangles, where the midpoints of each edge are used as the corners of the new triangles. Replaces triangles with a new array instead of modifying it in-place.

    Returns void

  • Transform each triangle in the MeshBuilder by a given transformation matrix.

    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 it will be automatically created from the position transform matrix.

    • transformNormal: boolean = true

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

    • transformTangent: boolean = true

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

    Returns void

  • Translate each triangle in the MeshBuilder by a given offset.

    Parameters

    • offset: vec3

      The offset to translate by.

    Returns void

  • Uniformly scale each triangle in the MeshBuilder by a given factor.

    Parameters

    • factor: number

      The factor to scale by.

    Returns void

  • Modify all positions on the mesh by a given function. Other vertex attributes are not modified.

    Parameters

    • transformer: ((x: number, y: number, z: number) => vec3)

      The transformation function to use for warping each vertex. Should return a new position given an X, Y and Z component of the original triangle.

        • (x: number, y: number, z: number): vec3
        • Parameters

          • x: number
          • y: number
          • z: number

          Returns vec3

    Returns void

Generated using TypeDoc