How can I add complex mesh data directly to a list through code?

I’m attempting to create a voxel system that uses a variety of custom block shapes.

Each chunk has lists for vertices, UVs, triangles and suchlike that are easily populated through code when creating cubes, as the components for each face are very short, simple and logical. However, when implementing more complex meshes for each face, although it’s still possible, it soon becomes prohibitively complex.

Therefore, is there a way I can import mesh data from a 3D modelling application and add those data to my vertex/uv/triangle lists directly through code. I know I could instantiate a lot of prefabs and then combine their meshes, but this seems very inefficient.

    // Mesh prefabs
    public Mesh testMesh;
    public Mesh testCollisionMesh;

    // Lists to hold mesh data, including for collision mesh
    public List<Vector3> vertexList = new List<Vector3>();
    public List<int> triangleList = new List<int>();
    public List<Vector2> uvList = new List<Vector2>();
    public List<Vector3> collisionVertexList = new List<Vector3>();
    public List<int> collisionTriangleList = new List<int>();

The lists here are where I’m storing the data for creating my procedural meshes. The Mesh variables at the top have the mesh component of an imported model dragged onto them in the inspector.

How can I add data to my lists from the arrays in the mesh variables? I swear this can’t be too difficult, but nothing I try seems to work. Going from lists to arrays isn’t so hard, so I’ve no idea why I can’t find a way to do the reverse without the console lighting up like a christmas tree.

Well, if you want to integrate / combine an existing mesh into a larger mesh chunk you simply have to add all of it’s vertices and remember the index of the first vertex that you add. When you add the triangle indices you just have to add the offset where the first vertex is located to all the indices.

The vertex positions has to be in a unique space. So the matrices localToWorld and worldToLocal of the Transform component are quite useful if you want to integrate an existing mesh object into another mesh. If you don’t have an existing object, creating the desired transformation matrix is quite easy with the Matrix4x4.TRS

Of course a vertex can consist of a position, color, normal, uv coordinates and other vertex properties. So when adding a vertex you want to copy all of it’s properties. Keep in mind that the normals / tangents need to be transformed as well.

Combining meshes into a single mesh of course requires that all meshes use the same material. A bit more complicated than combining the meshes is to integrate seperate textures into an atlas system and adjust the UV coordinates accordingly. Though your question is way too unspecific to go into more detail as it highly depends on your system.

There are already many games out there that use such a system. Even some minecraft mods use imported meshes. Games like SpaceEngineers(custom engine) and Robocraft(Unity) also use high complexity meshes in their voxel systems.