Procedural Meshes

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 the MeshChannels in the constructor.
  • If you wish to use a MeshTopology other than Triangles you can specify it in the constructor or set the topology 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 the builder.Indices0 list or calling any of the builder.Index... methods.
  • Once you are finished building, you can either implicitly cast your MeshBuilder to a Mesh or use any of the Compile method overloads.
  • If you wish to reuse your MeshBuilder you can simply call Clear between each use.
  • Note that the source code of the MeshBuilder class is included in Weaver Lite.