Description

A class for generating meshes at runtime.

Example

Quad Mesh Example

// Builds a quad mesh and applies it to meshVisual
//@input Component.MeshVisual meshVisual

var builder = new MeshBuilder([
{ name: "position", components: 3 },
{ name: "normal", components: 3, normalized: true },
{ name: "texture0", components: 2 },
]);

builder.topology = MeshTopology.Triangles;
builder.indexType = MeshIndexType.UInt16;

var left = -.5;
var right = .5;
var top = .5;
var bottom = -.5;

builder.appendVerticesInterleaved([
// Position Normal UV Index
left, top, 0, 0, 0, 1, 0, 1, // 0
left, bottom, 0, 0, 0, 1, 0, 0, // 1
right, bottom, 0, 0, 0, 1, 1, 0, // 2
right, top, 0, 0, 0, 1, 1, 1, // 3
]);

builder.appendIndices([
0,1,2, // First Triangle
2,3,0, // Second Triangle
]);

if(builder.isValid()){
script.meshVisual.mesh = builder.getMesh();
builder.updateMesh();
}
else{
print("Mesh data invalid!");
}

Cube Mesh Example

// Builds a cube mesh and applies it to meshVisual
//@input Component.MeshVisual meshVisual
//@input vec3 center = {0,0,0}
//@input vec3 size = {4,4,4}

var builder = new MeshBuilder([
{ name: "position", components: 3 },
{ name: "normal", components: 3, normalized: true },
{ name: "texture0", components: 2 },
]);

builder.topology = MeshTopology.Triangles;
builder.indexType = MeshIndexType.UInt16;

var halfSize = script.size.uniformScale(.5);
var right = script.center.x + halfSize.x;
var left = script.center.x - halfSize.x;
var top = script.center.y + halfSize.y;
var bottom = script.center.y - halfSize.y;
var front = script.center.z + halfSize.z;
var back = script.center.z - halfSize.z;

// UVs of quad in order: Top left, Bottom left, Bottom right, Top right
const QUAD_UVS = [[0,1], [0,0], [1,0], [1,1]];

// Append data for 4 vertices in a quad shape
function addQuadVerts(meshBuilder, normal, positions){
for(var i=0; i<positions.length; i++){
meshBuilder.appendVertices([positions[i], normal, QUAD_UVS[i]]);
}
}

// Append the indices for two triangles, forming a quad
function addQuadIndices(meshBuilder, topLeft, bottomLeft, bottomRight, topRight){
meshBuilder.appendIndices([
topLeft, bottomLeft, bottomRight, // First Triangle
bottomRight, topRight, topLeft // Second Triangle
]);
}

// Define the normal direction and vertex positions for each side of the cube
var sides = [
{ normal: [0,0,1], // Front
positions: [[left,top,front], [left,bottom,front], [right,bottom,front], [right,top,front]] },
{ normal: [0,0,-1], // Back
positions: [[right,top,back], [right,bottom,back], [left,bottom,back], [left,top,back]] },
{ normal: [1,0,0], // Right
positions: [[right,top,front], [right,bottom,front], [right,bottom,back], [right,top,back]] },
{ normal: [-1,0,0], // Left
positions: [[left,top,back], [left,bottom,back], [left,bottom,front], [left,top,front]] },
{ normal: [0,1,0], // Top
positions: [[left,top,back], [left,top,front], [right,top,front], [right,top,back]] },
{ normal: [0,-1,0], // Bottom
positions: [[left,bottom,front], [left,bottom,back], [right,bottom,back], [right,bottom,front]] },
];

// For each side, append the vertex data and indices
for(var i=0; i<sides.length; i++){
var index = i * 4;
addQuadVerts(builder, sides[i].normal, sides[i].positions);
addQuadIndices(builder, index, index+1, index+2, index+3);
}

// Make sure the mesh is valid, then apply and update
if(builder.isValid()){
script.meshVisual.mesh = builder.getMesh();
builder.updateMesh();
}
else{
print("Mesh data invalid!");
}

Constructors

  • Parameters

    • layout: unknown[]

    Returns MeshBuilder

    Description

    Creates a new MeshBuilder with the specified vertex layout.

    Layout is given as a list of "attribute" objects with the following properties:

    name - Attribute name components - Size of the attribute (how many float values it uses) normalized (optional) - Optional property declaring that the attribute should be normalized

    var builder = new MeshBuilder([
    // vertex position (x,y,z)
    { name: "position", components: 3 },
    // normal vector (x,y,z)
    { name: "normal", components: 3, normalized: true },
    // texture UV (u,v)
    { name: "texture0", components: 2 },
    ]);

Properties

indexType: MeshIndexType

Description

The index data type used by this MeshBuilder. MeshIndexType.UInt16 is the value normally used for this.

topology: MeshTopology

Description

The topology type used for the mesh.

Methods

  • Parameters

    • indices: number[]

    Returns void

    Description

    Appends indices to the index list.

  • Parameters

    • verts: number[][]

    Returns void

    Description

    Takes a list of list of vertex values according to the layout.

  • Parameters

    • verts: number[]

    Returns void

    Description

    Similar to appendVertices, but takes all values in one large array.

  • Parameters

    • from: number
    • to: number

    Returns void

    Description

    Removes all indices starting at index from and ending before index to.

  • Parameters

    • from: number
    • to: number

    Returns void

    Description

    Removes all vertex data starting at vertex index from and ending before vertex index to.

  • Returns number

    Description

    Returns the number of indices in the index list.

  • Returns RenderMesh

    Description

    Returns a RenderMesh asset that can be applied to a MeshVisual's mesh property. This asset stays linked to the MeshBuilder that provided it, so making changes to the mesh data and calling updateMesh() will update the RenderMesh as well.

  • Returns number

    Description

    Returns the number of vertices in the vertex list.

  • Returns boolean

    Description

    Checks whether the current data entered will create a valid mesh.

  • Parameters

    • index: number
    • verts: number[]

    Returns void

    Description

    Sets data for a single vertex at vertex index index.

  • Returns void

    Description

    Rebuilds the MeshAsset controlled by this MeshBuilder using the current mesh data.

Generated using TypeDoc