Lens Scripting API

    Class MeshBuilder

    A class for generating meshes at runtime.

    // 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 },
    { 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!");
    }
    // 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 },
    { 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!");
    }
    Index

    Constructors

    • 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)

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

      Parameters

      • layout: any[]

      Returns MeshBuilder

    Properties

    indexType: MeshIndexType

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

    topology: MeshTopology

    The topology type used for the mesh.

    Methods

    • Appends indices to the index list.

      Parameters

      • indices: number[]

      Returns void

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

      Parameters

      • verts: number[][]

      Returns void

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

      Parameters

      • verts: number[]

      Returns void

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

      Parameters

      • from: number
      • to: number

      Returns void

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

      Parameters

      • from: number
      • to: number

      Returns void

    • Returns the number of indices in the index list.

      Returns number

    • 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 RenderMesh

    • Returns the number of vertices in the vertex list.

      Returns number

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

      Returns boolean

    • Add bones to the mesh.

      Parameters

      • bones: string[]
      • inverseMatrices: mat4[]

      Returns void

    • Sets data for a single vertex at vertex index index.

      Parameters

      • index: number
      • verts: number[]

      Returns void

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

      Returns void

    MMNEPVFCICPMFPCPTTAAATR