From 2e75471a48f093c61bd3317d374ca5ed150eb817 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Sun, 20 Feb 2022 22:02:23 +0800 Subject: [PATCH] Fix GLTF exporter crash when using GridMap --- modules/gltf/gltf_document.cpp | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index def7d1956ce..90423e77f65 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -71,6 +71,28 @@ #include "modules/regex/regex.h" #endif // MODULE_REGEX_ENABLED +Ref _mesh_to_array_mesh(Ref p_mesh) { + Ref array_mesh = p_mesh; + if (array_mesh.is_valid()) { + return array_mesh; + } + array_mesh.instance(); + if (p_mesh.is_null()) { + return array_mesh; + } + + for (int32_t surface_i = 0; surface_i < p_mesh->get_surface_count(); surface_i++) { + Mesh::PrimitiveType primitive_type = p_mesh->surface_get_primitive_type(surface_i); + Array arrays = p_mesh->surface_get_arrays(surface_i); + Ref mat = p_mesh->surface_get_material(surface_i); + int32_t mat_idx = array_mesh->get_surface_count(); + array_mesh->add_surface_from_arrays(primitive_type, arrays); + array_mesh->surface_set_material(mat_idx, mat); + } + + return array_mesh; +} + Error GLTFDocument::serialize(Ref state, Node *p_root, const String &p_path) { uint64_t begin_time = OS::get_singleton()->get_ticks_usec(); @@ -5065,11 +5087,6 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref state, MeshInst Mesh::PrimitiveType primitive_type = godot_mesh->surface_get_primitive_type(surface_i); Array arrays = godot_mesh->surface_get_arrays(surface_i); Ref mat = godot_mesh->surface_get_material(surface_i); - Ref godot_array_mesh = godot_mesh; - String surface_name; - if (godot_array_mesh.is_valid()) { - surface_name = godot_array_mesh->surface_get_name(surface_i); - } if (p_mesh_instance->get_surface_material(surface_i).is_valid()) { mat = p_mesh_instance->get_surface_material(surface_i); } @@ -5420,8 +5437,6 @@ void GLTFDocument::_convert_grid_map_to_gltf(GridMap *p_grid_map, GLTFNodeIndex Vector3 cell_location = cells[k]; int32_t cell = p_grid_map->get_cell_item( cell_location.x, cell_location.y, cell_location.z); - MeshInstance *import_mesh_node = memnew(MeshInstance); - import_mesh_node->set_mesh(p_grid_map->get_mesh_library()->get_item_mesh(cell)); Transform cell_xform; cell_xform.basis.set_orthogonal_index( p_grid_map->get_cell_item_orientation( @@ -5433,7 +5448,7 @@ void GLTFDocument::_convert_grid_map_to_gltf(GridMap *p_grid_map, GLTFNodeIndex cell_location.x, cell_location.y, cell_location.z)); Ref gltf_mesh; gltf_mesh.instance(); - gltf_mesh = import_mesh_node; + gltf_mesh->set_mesh(_mesh_to_array_mesh(p_grid_map->get_mesh_library()->get_item_mesh(cell))); new_gltf_node->mesh = state->meshes.size(); state->meshes.push_back(gltf_mesh); new_gltf_node->xform = cell_xform * p_grid_map->get_transform();