diff --git a/editor_modules/gltf/gltf_document.cpp b/editor_modules/gltf/gltf_document.cpp index 4a203f890..74eed017e 100644 --- a/editor_modules/gltf/gltf_document.cpp +++ b/editor_modules/gltf/gltf_document.cpp @@ -75,6 +75,10 @@ #include "modules/regex/regex.h" #endif // MODULE_REGEX_ENABLED +#ifdef MODULE_GRIDMAP_ENABLED +#include "modules/gridmap/grid_map.h" +#endif // MODULE_GRIDMAP_ENABLED + Ref _mesh_to_array_mesh(Ref p_mesh) { Ref array_mesh = p_mesh; if (array_mesh.is_valid()) { @@ -5457,6 +5461,11 @@ void GLTFDocument::_convert_scene_node(Ref state, Node *p_current, co // We ignore the Pandemonium Engine node that is the skeleton. return; #endif +#ifdef MODULE_GRIDMAP_ENABLED + } else if (cast_to(p_current)) { + GridMap *gridmap = Object::cast_to(p_current); + _convert_grid_map_to_gltf(gridmap, p_gltf_parent, p_gltf_root, gltf_node, state); +#endif // MODULE_GRIDMAP_ENABLED } else if (cast_to(p_current)) { MultiMeshInstance *multi = cast_to(p_current); _convert_mult_mesh_instance_to_gltf(multi, p_gltf_parent, p_gltf_root, gltf_node, state); @@ -5531,6 +5540,36 @@ void GLTFDocument::_convert_light_to_gltf(Light *light, Ref state, Re } } +#ifdef MODULE_GRIDMAP_ENABLED +void GLTFDocument::_convert_grid_map_to_gltf(GridMap *p_grid_map, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_node_index, Ref gltf_node, Ref state) { + Array cells = p_grid_map->get_used_cells(); + for (int32_t k = 0; k < cells.size(); k++) { + GLTFNode *new_gltf_node = memnew(GLTFNode); + gltf_node->children.push_back(state->nodes.size()); + state->nodes.push_back(new_gltf_node); + Vector3 cell_location = cells[k]; + int32_t cell = p_grid_map->get_cell_item( + cell_location.x, cell_location.y, cell_location.z); + Transform cell_xform; + cell_xform.basis.set_orthogonal_index( + p_grid_map->get_cell_item_orientation( + cell_location.x, cell_location.y, cell_location.z)); + cell_xform.basis.scale(Vector3(p_grid_map->get_cell_scale(), + p_grid_map->get_cell_scale(), + p_grid_map->get_cell_scale())); + cell_xform.set_origin(p_grid_map->map_to_world( + cell_location.x, cell_location.y, cell_location.z)); + Ref gltf_mesh; + gltf_mesh.instance(); + 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(); + new_gltf_node->set_name(_gen_unique_name(state, p_grid_map->get_mesh_library()->get_item_name(cell))); + } +} +#endif // MODULE_GRIDMAP_ENABLED + void GLTFDocument::_convert_mult_mesh_instance_to_gltf(MultiMeshInstance *p_multi_mesh_instance, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_node_index, Ref gltf_node, Ref state) { Ref multi_mesh = p_multi_mesh_instance->get_multimesh(); if (multi_mesh.is_valid()) { diff --git a/editor_modules/gltf/gltf_document.h b/editor_modules/gltf/gltf_document.h index ba7b4e651..45ddc19dd 100644 --- a/editor_modules/gltf/gltf_document.h +++ b/editor_modules/gltf/gltf_document.h @@ -47,6 +47,10 @@ class Skeleton; class BoneAttachment; #endif +#ifdef MODULE_GRIDMAP_ENABLED +class GridMap; +#endif // MODULE_GRIDMAP_ENABLED + class GLTFState; class GLTFSkin; class GLTFNode; @@ -412,6 +416,13 @@ public: void _check_visibility(Node *p_node, bool &retflag); void _convert_camera_to_gltf(Camera *camera, Ref state, Ref gltf_node); +#ifdef MODULE_GRIDMAP_ENABLED + void _convert_grid_map_to_gltf( + GridMap *p_grid_map, + GLTFNodeIndex p_parent_node_index, + GLTFNodeIndex p_root_node_index, + Ref gltf_node, Ref state); +#endif // MODULE_GRIDMAP_ENABLED void _convert_mult_mesh_instance_to_gltf( MultiMeshInstance *p_scene_parent, GLTFNodeIndex p_parent_node_index,