0
$\begingroup$

I need some help rewriting a piece of code that takes a mesh and iterates over its indices to store indicies in arrays for later use while limiting itself to under 2**16 vertices and keeping faces intact (unless I misinterpreted the code).

    def _split_meshes_for_vertex_limit(self, divided_meshes):
        new_meshes = []
        limit = 2**16
        for mesh in divided_meshes:
            if len(mesh.vertices) > limit:
                start_index = 0
                while start_index < len(mesh.indices):
                    vertex_index_mapping = {}
                    new_indices = []
                    for i in range(start_index, len(mesh.indices), 3):
                        start_index += 3
                        face = mesh.indices[i:i+3]
                        for face_index in face:
                            if not face_index in vertex_index_mapping:
                                new_index = len(vertex_index_mapping)
                                vertex_index_mapping[face_index] = new_index
                            new_indices.append(vertex_index_mapping[face_index])
                        if len(vertex_index_mapping) >= limit-3:
                            break
                    verts = [mesh.vertices[v] for v, index in sorted(vertex_index_mapping.items(), key=lambda k: k[1])]
                    new_meshes.append(Mesh(mesh.material_id, verts, new_indices))
            else:
                new_meshes.append(mesh)
        return new_meshes

The problem with the current code is that iterating over indices will cause the "submesh" to become a long strip, which is not efficient for culling. Instead, I would like to process the data in 2×2 chunks (with infinite height), and if one chunk exceeds the 2**16 limit, it should be divided recursively into smaller 2×2 chunks.

Iterating over indicies will cause the array to contain strip-like data

i need a way to store indicies in the array based on chunks (2x2, recursive)

This is an example image with colored faces that would belong into a chunk, just for better visualization (not using the same mesh as above)

enter image description here

My mesh represents a map, similar to what you might find in a game. The decimate tool has been heavily used to optimize the vertices, so they are not uniform.

Keep in mind that I do not want to split the actual mesh. I only want to store the data in my array (with the faces intact) so I can use it later. Since the faces need to remain intact, the chunks do not need to form a perfect grid but should closely resemble one.

I am new to Blender and am learning as I go. I have tried searching online and asking ChatGPT for help, but I haven't found a solution yet. This code is a bit overwhelming for me, to be honest.

$\endgroup$
6
  • $\begingroup$ Hi and welcome! The chances of getting a satisfactory answer increase if you give a complete example. What do you have (an in-game map?) and what do you want to do (culling? splitting into virtual sub-maps?). The example should include a screenshot or a sketch, as well as a description for a sample setup (e.g. add default plane, subdivide it 2x, ...) or a blend file, and a small, complete and executable script that tries to solve the task. Finally, a description of the result and what does not work. $\endgroup$ Commented Oct 9, 2024 at 9:59
  • $\begingroup$ I kind of thought that i explained everything well enough, especially with the given code example. I can not share what i am actually working on because it is confidential but i will update my initial question $\endgroup$ Commented Oct 9, 2024 at 10:12
  • $\begingroup$ Sorry, it was not clear at all. Thanks for the update & the images meanwhile. Unclear things: 1) What are "2×2 chunks (with infinite height)"? -- I guess those are the red sections, right? The mesh is quartered, only X and Y of the position are relevant, and Z is ignored, right? 2) " it should be divided recursively into smaller 2×2 chunks." -- It should be quartered again, right? 3) The mesh seems to be triangulated -- clear now. It is. 4) Blender does not guarantee a certain order of the verts. Can you guarantee it? If not, it's chaotic. 5) What about the faces that is in 2 sections? $\endgroup$ Commented Oct 9, 2024 at 10:57
  • $\begingroup$ 1) Take the meshes bounds and divide them by 2 (while not chunking in the height dimension) and choose everything within. So yes, you are right with your assumption. 2) yes, just like in my image. However i dont like how its worded. The mesh is not supposed to be cut, i just want to get the faces/verts within this "imaginary" boundary. 3) yes 4) i do not care about order, just about intact faces 5) I answered that in my post. Check the penultimate paragraph. If a face is within 2 chunks, it can be placed into the chunk that the average position of all verts is in (or randomly chosen). $\endgroup$ Commented Oct 9, 2024 at 11:44
  • $\begingroup$ 4) but the order is crucial here ;-) You get these strips because the indices are in natural order (1,2,3,4 ...). If you mess up the order by a modifier or DynTopo (43, 1, 99, 17, ...) you will get a few random "shreds" of the mesh and they are not even in the correct section. So I wonder why you rely on the indices at all if you actually want to divide them into sections. In this case, it's the positions that matter, not the indices. $\endgroup$ Commented Oct 10, 2024 at 12:29

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.