Skip to content

Commit

Permalink
Fix GLTF exporter crash when using GridMap
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyqiu committed Feb 20, 2022
1 parent 6071d78 commit 2e75471
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@
#include "modules/regex/regex.h"
#endif // MODULE_REGEX_ENABLED

Ref<ArrayMesh> _mesh_to_array_mesh(Ref<Mesh> p_mesh) {
Ref<ArrayMesh> 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<Material> 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<GLTFState> state, Node *p_root, const String &p_path) {
uint64_t begin_time = OS::get_singleton()->get_ticks_usec();

Expand Down Expand Up @@ -5065,11 +5087,6 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> state, MeshInst
Mesh::PrimitiveType primitive_type = godot_mesh->surface_get_primitive_type(surface_i);
Array arrays = godot_mesh->surface_get_arrays(surface_i);
Ref<Material> mat = godot_mesh->surface_get_material(surface_i);
Ref<ArrayMesh> 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);
}
Expand Down Expand Up @@ -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(
Expand All @@ -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<GLTFMesh> 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();
Expand Down

0 comments on commit 2e75471

Please sign in to comment.