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 MeshBuilder
by 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
MeshBuilder
will contain UVs and Normals. If you want any other channels such as UVs2 or Colors, you must specify theMeshChannels
in the constructor. - If you wish to use a
MeshTopology
other thanTriangles
you can specify it in the constructor or set thetopology
field 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.Indices0
list or calling any of thebuilder.Index...
methods. - Once you are finished building, you can either implicitly cast your
MeshBuilder
to aMesh
or use any of theCompile
method overloads. - If you wish to reuse your
MeshBuilder
you can simply callClear
between each use. - Note that the source code of the
MeshBuilder
class is included in Weaver Lite.