MeshBuilder
ClassA class for generating meshes at runtime.
- Available on all platforms
Index
Properties
Methods
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 },
{ 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 },
{ 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
Constructor
new MeshBuilder(
layout):MeshBuilder
Parameters
| Parameter | Type |
|---|---|
layout | any[] |
Returns
MeshBuilder
Properties
indexType
indexType:
MeshIndexType
The index data type used by this MeshBuilder. MeshIndexType.UInt16 is the value normally used for this.
Default Value
MeshIndexType.None
topology
topology:
MeshTopology
The topology type used for the mesh.
Default Value
MeshTopology.Triangles
Methods
appendIndices()
appendIndices(
indices):void
Appends indices to the index list.
Parameters
| Parameter | Type |
|---|---|
indices | number[] |
Returns
void
appendVertices()
appendVertices(
verts):void
Takes a list of list of vertex values according to the layout.
Parameters
| Parameter | Type |
|---|---|
verts | number[][] |
Returns
void
appendVerticesInterleaved()
appendVerticesInterleaved(
verts):void
Similar to appendVertices, but takes all values in one large array.
Parameters
| Parameter | Type |
|---|---|
verts | number[] |
Returns
void
createFromMesh()
staticcreateFromMesh(mesh):MeshBuilder
Create a MeshBuilder from a RenderMesh.
Parameters
| Parameter | Type |
|---|---|
mesh | RenderMesh |
Returns
MeshBuilder
Since
Lens Scripting Version 278
eraseIndices()
eraseIndices(
from,to):void
Removes all indices starting at index from and ending before index to.
Parameters
| Parameter | Type |
|---|---|
from | number |
to | number |
Returns
void
eraseVertices()
eraseVertices(
from,to):void
Removes all vertex data starting at vertex index from and ending before vertex index to.
Parameters
| Parameter | Type |
|---|---|
from | number |
to | number |
Returns
void
getIndicesCount()
getIndicesCount():
number
Returns the number of indices in the index list.
Returns
number
getMesh()
getMesh():
RenderMesh
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
getTypeName()
staticgetTypeName():TypeName<MeshBuilder>
Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().
Returns
TypeName<MeshBuilder>
getVerticesCount()
getVerticesCount():
number
Returns the number of vertices in the vertex list.
Returns
number
isValid()
isValid():
boolean
Checks whether the current data entered will create a valid mesh.
Returns
boolean
setBones()
setBones(
bones,inverseMatrices):void
Add bones to the mesh.
Parameters
| Parameter | Type |
|---|---|
bones | string[] |
inverseMatrices | mat4[] |
Returns
void
Since
Lens Scripting Version 278
setVertexInterleaved()
setVertexInterleaved(
index,verts):void
Sets data for a single vertex at vertex index index.
Parameters
| Parameter | Type |
|---|---|
index | number |
verts | number[] |
Returns
void
updateMesh()
updateMesh():
void
Rebuilds the MeshAsset controlled by this MeshBuilder using the current mesh data.
Returns
void