Splitting mesh into chunks

I am looking into a way of splitting a mesh, which will be very big into 3d chunks(x, y, z based). I would imagine that i would go for a 16x16x16 size but this is besides the point. I will probably make the final meshes in blender and export them as a obj. However i do not want to create the final model until i know a way to ‘split’ it into chunks. I can handle all the code apart from the actual Splitting of the mesh into other meshes. I am guessing i would need to go through the entire list of vertices and place them into smaller lists but i am not sure how possible this would be. Also what if a triangle is right in the centre of the split? I have seen some commercial assets that work on object, like this one how ever i am not currently in the position where i can just start buying assets and seeing if i can modify them for my need. The end goal is for me to have a relay massive sphere with caves and buildings but into the mesh and to optimise it i would need to chunk it and lock the player to position 0,0,0(or something) and just load the correct chunks around him. I know i haven’t shown anything i have done but i have tried looking for this for a few days know. I know how to create meshes from code and stuff but not the way to split a mesh. Any help would be greatly liked.
(Not sure if it would be better to use an external program that could do this? And just load them from code?)

Since you say this will be a massive sphere with caves and buildings, make sure you know this… Unity has a vertex limit of 64,000 per mesh. That’s because Unity uses unsigned shorts instead of unsigned ints for the vertex indices to save memory. 64,000 isn’t a whole lot. If your mesh will be bigger than this, you can’t even load it as a mesh in Unity, so you wouldn’t be able to split it up in Unity either.

I’d suggest writing your own program to split the mesh. It won’t be hard to write a brute-force program to do this, though it will be slow. OBJ files are very easy to write a loader/saver for. Export a cube in Blender as an OBJ file, then open the OBJ in a text editor. You’ll see that OBJ files are just text files, with all of the vertex coordinates and indices right there. You say you know how to create a mesh from code, so I’ll assume you know how mesh indices work, so I won’t explain that.

Parse the file, read it into vertex, index, normals, texcoord arrays. OBJ indices are stored as i+1. The first vertex has an index of 1, not 0. Also make sure you check the “triangulate faces” check box in Blender when you export to OBJ. This will make loading/saving the OBJ files MUCH easier.

Here’s the pseudo code for what your splitting algorithm will have to do…

 - Loop through all submesh volumes you want to have

    - Loop through all triangles in the main mesh

        - If any of the triangle's 3 vertices are in the volume, and triangle hasn't been added to a new mesh yet 
       
            - Add triangle index to a list called subMeshTriangleIndices.

    - Now subMeshTriangleIndices contains the indices of all the triangles the sub mesh will have

    - Loop through subMeshTriangleIndices

        - Loop through the 3 vertices of each triangle

            - Keep a dictionary of Vector3's to ints, which maps the sub mesh's vertices to their indices

            - You need this because you'll be looping over the same vertices more than once

            - If vertex isn't in the dictionary

                - Add it to the dictionary, and give it an index

                - Increase the index by one

            - Add the vertex's index to a final subMeshTriangle array

    - Write sub mesh to file

This algorithm is pretty much the bare bones minimum of what you’ll need. It will handle triangles that are partly in two volumes. It won’t split those triangles, it will just add them to either one of the sub meshes. The algorithm will also correctly handle the remapping of vertex indices. That’s what the dictionary is for. This is brute force. Since there’s lots of looping, this could be slow though I doubt that’s much of a concern.

Good luck!