I’ve been working on generating a simplified mesh in Godot from cubes placed in a GridMap, but I keep hitting a snag. My goal is to combine the cube meshes into a single `ArrayMesh`, but when I check for surfaces, it always returns 0, which is really frustrating! Here’s a snippet of the code I’m using:
“`gdscript
func create_combined_cube_mesh(gridmap: GridMap) -> ArrayMesh:
print(“Create combined cube mesh…”)
var array_mesh = ArrayMesh.new()
var box_mesh = BoxMesh.new()
box_mesh.size = gridmap.cell_size
var arrays = box_mesh.surface_get_arrays(0)
var vertices = arrays[Mesh.ARRAY_VERTEX]
var indices = arrays[Mesh.ARRAY_INDEX]
var mesh_data = []
mesh_data.resize(Mesh.ARRAY_MAX)
mesh_data[Mesh.ARRAY_VERTEX] = []
mesh_data[Mesh.ARRAY_INDEX] = []
var vertex_offset = 0
for cell in gridmap.get_used_cells():
var transform = gridmap.map_to_local(cell)
for v in vertices:
mesh_data[Mesh.ARRAY_VERTEX].append(v + transform)
for i in indices:
mesh_data[Mesh.ARRAY_INDEX].append(i + vertex_offset)
vertex_offset += vertices.size()
array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, mesh_data)
print(array_mesh.surface_get_arrays(0).size()) // always prints 0
return array_mesh if mesh_data[Mesh.ARRAY_VERTEX].size() > 0 else null
“`
I ran through the code and it looks fine at first glance, but that `surface_get_arrays(0).size()` call always returns 0 after I try to add the surface. I’ve double-checked that `mesh_data` actually collects the vertices and indices from the `BoxMesh`, and I should be getting a valid mesh. But it seems like something’s going wrong when I add the surface or maybe even before that.
Has anyone else had this issue, or does something seem off with my approach? I thought maybe it has to do with how I’m transforming the vertices or populating the `mesh_data`, but I can’t put my finger on it. Any advice or guidance would be super helpful. What could be causing the `ArrayMesh` to end up empty when it should be populated?
It appears that the issue lies in how the `mesh_data` is being populated and passed to the `add_surface_from_arrays` method. In your current implementation, you’re attempting to directly append vertices and indices from the `BoxMesh` surface but might not be utilizing the correct mesh structure. Specifically, ensure that each vertex array and index array is properly formatted before you call `add_surface_from_arrays`. For instance, the `mesh_data` should not only be initialized but also adequately filled. After your loop that populates `mesh_data`, consider adding checks to verify that the arrays actually contain data by printing their sizes before the call to add the surface.
Another aspect to consider is the nature of the indices. You are modifying them based on the `vertex_offset`, which is correct, but ensure that the base indices you’re using are also valid for the `ArrayMesh`. It’s possible that the `box_mesh` might not have the expected structure, particularly if any transformations or settings are not defined appropriately. Additionally, if the `mesh_data` is being resized frequently, it could lead to incorrect sizing in the arrays. Finally, always check for any errors returned by `add_surface_from_arrays` which could give insights into why the mesh is not being populated as expected. By confirming these points, you should be able to identify the root cause of the empty mesh issue.
It looks like you’re running into a common issue when working with meshes in Godot. From what you’ve provided, it seems like you are on the right track, but there are a few points to double-check that might be causing your `ArrayMesh` to be empty.
1. Mesh Surface Initialization: After you call `array_mesh.add_surface_from_arrays(…)`, it’s worth checking if the mesh data for vertices and indices is actually being populated correctly. Try printing `mesh_data[Mesh.ARRAY_VERTEX]` and `mesh_data[Mesh.ARRAY_INDEX]` before you add the surface to see if they contain any values.
2. Mesh Array Size: When you add the surface, you should also ensure that the arrays you’re passing in are properly initialized and filled. Verify that your `mesh_data` arrays have vertices and indices correctly. If they don’t have any vertices or indices at the time of adding the surface, it would reflect zero surfaces.
3. Transform Logic: The transformation you’re applying to the vertices might be incorrect. You are using `transform` to offset the vertices, so double-check that `gridmap.map_to_local(cell)` returns the expected transformation. It’s possible that the resulting values are not translating the vertices as you intend.
4. Appending Indices: When you’re incrementing the index values with `vertex_offset`, ensure that it’s accumulative across all cells. If the indices get reset unintentionally, this could lead to incorrect indexing.
It might also help to look at the official Godot documentation or community forums to see if others have had a similar issue. Debugging mesh creation can be tricky, but checking these aspects should help you diagnose the problem!