One of the most common reasons you may want to create a procedural asset is to generate a Mesh using code. Weaver allows you to easily and efficiently create procedural meshes using the Kybernetik.MeshBuilder class, both to efficiently generate and regenerate a dynamic mesh at runtime as well as to generate a procedural asset in the Unity Editor:
- The Building Example demonstrates how to generate a simple procedural mesh.
- Construct a
new MeshBuilderby specifying the initial vertex and index capacity. These values are simply the starting size for the internal lists which will expand as needed. - By default, a
MeshBuilderwill contain UVs and Normals. If you want any other channels such as UVs2 or Colors, you must specify theMeshChannelsin the constructor. - If you wish to use a
MeshTopologyother thanTrianglesyou can specify it in the constructor or set thetopologyfield at any time. - To build your mesh, you simply call
builder.Vertices.Add(...)(remembering to always add the same number of values to all mesh channels) followed by either adding values to thebuilder.Indices0list or calling any of thebuilder.Index...methods. - Once you are finished building, you can either implicitly cast your
MeshBuilderto aMeshor use any of theCompilemethod overloads. - If you wish to reuse your
MeshBuilderyou can simply callClearbetween each use. - Note that the source code of the
MeshBuilderclass is included in Weaver Lite.