A mesh is a collection of faces, lines, and points. Faces can contain geometry attributes including points, normals, UVs, etc. Lines consist of pairs of positions. Points consist of a position. The MeshData class is a blueprint for instances of a mesh that allow the reuse of the underlying geometry.
To add a mesh to the scene several functions must be used. First, use the createMesh() function which accepts a MeshData object and returns a MeshID. Next, use the MeshInstanceData constructor with the MeshId returned from createMesh() to create a MeshInstanceData object. Finally, use the MeshInstanceData object with the createMeshInstance() function to insert the mesh into your scene and make it visible.
Sample code demonstrating the use of createMesh() can be seen further down in this guide here, and a snippet demonstrating the use of the MeshInstanceData constructor with createMeshInstance() can be seen here.
A MeshId is a pair of numbers that is a unique identifier to a mesh object. It can be used to create a MeshInstanceData object. The replaceMesh() function can be used to change the MeshData geometry identified by the MeshId. If the geometry for any mesh changes, all instances of that mesh in the scene will update accordingly.
Face data contains an array of floating-point data describing the points in space for the faces to be added to the mesh and is defined by calling addFaces(). This function will add one face element. To add multiple faces, call addFaces() for each face. Face data can also contain normals, colors, or texture parameters for the corresponding vertex data points.
The snippet below contains createTriangleMeshData which creates a MeshData object representing a triangle with vertex coordinates (0,0,0), (0,1,0), (1,0,0). Note that the MeshData FaceWinding is set to clockwise, and the normal for each vertex is (0,0,1). If the FaceWinding was set to CounterClockwise, the normal for these coordinates would be (0,0,-1). If the normal does not match the specified FaceWinding, the face lighting will not be correct. With backface visibility off (the default setting), the triangle will only be visible when the camera direction is facing towards the front. Changing FaceWinding will change which side the triangle is visible from. To update the MeshData's FaceWinding use setFaceWinding().
Polyline data contains an array of floating-point data describing the points on the line and is defined by calling addPolyLine().
createPolylineMeshData creates a MeshData object representing a polyline containing the coordinates (0,0,0), (0,1,0), (1,0,0):
Point data contains an array of floating-point data describing the points and is defined by calling addPoints().
createPointMeshData returns a MeshData object representing the points at the coordinates (0,0,0), (0,1,0), (1,0,0):
The code snippet below creates a function which generates MeshData for a cube. We will use this function to create three cube instances.
MeshInstanceData is an object representing a mesh instance. This class allows for specifying properties of a mesh instance. Examples of instance properties that can be set are color and transformation matrix. The function createMeshInstance() takes a MeshInstanceData object and returns a NodeId that references that instance.
A NodeId is a number that references an assembly tree node. To get the type of node referenced by a NodeId, you can use the getNodeType() function. A NodeId can be used to interact with, and retrieve properties about a node. For example, the NodeId can be used to: get the parent node's NodeId, get the node's matrix, etc.
Create Mesh Instance
In the snippet below we create another mesh instance in the scene. Note that the same MeshId is used. A translation is also applied to move the cube along the X axis.
Using the replaceMesh() function, we can change the MeshData for all instances of a Mesh. Here, we replace the MeshData for the first two cubes that we instanced. The new MeshData created is a smaller cube. In the scene, there will now be one large cube and two smaller cubes. Note that replacing mesh data for dynamically created meshes works the same as replacing mesh data loaded from a SC model.
Level of Detail (LOD)
Utilizing a lower level of detail for a mesh will reduce the geometric complexity of its instance. This is available for models that have either been converted from BREP data, or authored with mesh levels. Additionally, LODs must be authored before the model is loaded. LODs do not exist for models that have been authored in the viewer. The default mesh level can be set with the parameter defaultMeshLevel when the viewer is started.
- There is no way to save meshes that are authored in the web viewer to a file.
- Meshes authored in the web viewer are generally used for geometry that cannot be created in advanced or is not meant to be part of the model file.
- When interacting with meshes once a model is loaded, there is no difference between dynamically created meshes and ones loaded from a SC model.