diff options
Diffstat (limited to 'modules/gridmap')
-rw-r--r-- | modules/gridmap/doc_classes/GridMap.xml | 17 | ||||
-rw-r--r-- | modules/gridmap/editor/grid_map_editor_plugin.cpp | 7 | ||||
-rw-r--r-- | modules/gridmap/grid_map.cpp | 111 | ||||
-rw-r--r-- | modules/gridmap/grid_map.h | 31 | ||||
-rw-r--r-- | modules/gridmap/register_types.cpp | 15 | ||||
-rw-r--r-- | modules/gridmap/register_types.h | 6 |
6 files changed, 143 insertions, 44 deletions
diff --git a/modules/gridmap/doc_classes/GridMap.xml b/modules/gridmap/doc_classes/GridMap.xml index 407ce961c8..499f54e3ba 100644 --- a/modules/gridmap/doc_classes/GridMap.xml +++ b/modules/gridmap/doc_classes/GridMap.xml @@ -73,6 +73,13 @@ Returns an array of [Transform3D] and [Mesh] references corresponding to the non-empty cells in the grid. The transforms are specified in world space. </description> </method> + <method name="get_navigation_layer_value" qualifiers="const"> + <return type="bool" /> + <argument index="0" name="layer_number" type="int" /> + <description> + Returns whether or not the specified layer of the [member navigation_layers] bitmask is enabled, given a [code]layer_number[/code] between 1 and 32. + </description> + </method> <method name="get_used_cells" qualifiers="const"> <return type="Array" /> <description> @@ -133,6 +140,14 @@ Based on [code]value[/code], enables or disables the specified layer in the [member collision_mask], given a [code]layer_number[/code] between 1 and 32. </description> </method> + <method name="set_navigation_layer_value"> + <return type="void" /> + <argument index="0" name="layer_number" type="int" /> + <argument index="1" name="value" type="bool" /> + <description> + Based on [code]value[/code], enables or disables the specified layer in the [member navigation_layers] bitmask, given a [code]layer_number[/code] between 1 and 32. + </description> + </method> <method name="world_to_map" qualifiers="const"> <return type="Vector3i" /> <argument index="0" name="world_position" type="Vector3" /> @@ -177,7 +192,7 @@ The assigned [MeshLibrary]. </member> <member name="navigation_layers" type="int" setter="set_navigation_layers" getter="get_navigation_layers" default="1"> - The navigation layers the GridMap generates its navigable regions in. + A bitmask determining all navigation layers the GridMap generated navigation regions belong to. These navigation layers can be checked upon when requesting a path with [method NavigationServer3D.map_get_path]. </member> <member name="physics_material" type="PhysicsMaterial" setter="set_physics_material" getter="get_physics_material"> Overrides the default friction and bounce physics properties for the whole [GridMap]. diff --git a/modules/gridmap/editor/grid_map_editor_plugin.cpp b/modules/gridmap/editor/grid_map_editor_plugin.cpp index 68968325dd..b694c109e1 100644 --- a/modules/gridmap/editor/grid_map_editor_plugin.cpp +++ b/modules/gridmap/editor/grid_map_editor_plugin.cpp @@ -930,6 +930,7 @@ void GridMapEditor::edit(GridMap *p_gridmap) { } update_palette(); + _update_cursor_instance(); set_process(true); @@ -957,7 +958,7 @@ void GridMapEditor::update_grid() { } void GridMapEditor::_draw_grids(const Vector3 &cell_size) { - Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant(); + Vector3 edited_floor = node->get_meta("_editor_floor_", Vector3()); for (int i = 0; i < 3; i++) { RS::get_singleton()->mesh_clear(grid[i]); @@ -1220,7 +1221,7 @@ GridMapEditor::GridMapEditor() { search_box = memnew(LineEdit); search_box->set_h_size_flags(SIZE_EXPAND_FILL); - search_box->set_placeholder(TTR("Filter meshes")); + search_box->set_placeholder(TTR("Filter Meshes")); hb->add_child(search_box); search_box->connect("text_changed", callable_mp(this, &GridMapEditor::_text_changed)); search_box->connect("gui_input", callable_mp(this, &GridMapEditor::_sbox_input)); @@ -1259,7 +1260,7 @@ GridMapEditor::GridMapEditor() { info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes.")); info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); - info_message->set_autowrap_mode(Label::AUTOWRAP_WORD_SMART); + info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); info_message->set_anchors_and_offsets_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE); mesh_library_palette->add_child(info_message); diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp index 02fe4d93de..7d80cbef7c 100644 --- a/modules/gridmap/grid_map.cpp +++ b/modules/gridmap/grid_map.cpp @@ -103,9 +103,10 @@ bool GridMap::_get(const StringName &p_name, Variant &r_ret) const { { int *w = cells.ptrw(); int i = 0; - for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next(), i++) { - encode_uint64(E->key().key, (uint8_t *)&w[i * 3]); - encode_uint32(E->get().cell, (uint8_t *)&w[i * 3 + 2]); + for (const KeyValue<IndexKey, Cell> &E : cell_map) { + encode_uint64(E.key.key, (uint8_t *)&w[i * 3]); + encode_uint32(E.value.cell, (uint8_t *)&w[i * 3 + 2]); + i++; } } @@ -225,15 +226,33 @@ bool GridMap::is_baking_navigation() { return bake_navigation; } -void GridMap::set_navigation_layers(uint32_t p_layers) { - navigation_layers = p_layers; +void GridMap::set_navigation_layers(uint32_t p_navigation_layers) { + navigation_layers = p_navigation_layers; _recreate_octant_data(); } -uint32_t GridMap::get_navigation_layers() { +uint32_t GridMap::get_navigation_layers() const { return navigation_layers; } +void GridMap::set_navigation_layer_value(int p_layer_number, bool p_value) { + ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive."); + ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive."); + uint32_t _navigation_layers = get_navigation_layers(); + if (p_value) { + _navigation_layers |= 1 << (p_layer_number - 1); + } else { + _navigation_layers &= ~(1 << (p_layer_number - 1)); + } + set_navigation_layers(_navigation_layers); +} + +bool GridMap::get_navigation_layer_value(int p_layer_number) const { + ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive."); + ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive."); + return get_navigation_layers() & (1 << (p_layer_number - 1)); +} + void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) { if (!mesh_library.is_null()) { mesh_library->unregister_owner(this); @@ -432,6 +451,18 @@ void GridMap::_octant_transform(const OctantKey &p_key) { RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform()); } + // update transform for NavigationServer regions and navigation debugmesh instances + for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) { + if (bake_navigation) { + if (E.value.region.is_valid()) { + NavigationServer3D::get_singleton()->region_set_transform(E.value.region, get_global_transform() * E.value.xform); + } + if (E.value.navmesh_debug_instance.is_valid()) { + RS::get_singleton()->instance_set_transform(E.value.navmesh_debug_instance, get_global_transform() * E.value.xform); + } + } + } + for (int i = 0; i < g.multimesh_instances.size(); i++) { RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform()); } @@ -455,6 +486,9 @@ bool GridMap::_octant_update(const OctantKey &p_key) { //erase navigation for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) { NavigationServer3D::get_singleton()->free(E.value.region); + if (E.value.navmesh_debug_instance.is_valid()) { + RS::get_singleton()->free(E.value.navmesh_debug_instance); + } } g.navmesh_ids.clear(); @@ -480,17 +514,17 @@ bool GridMap::_octant_update(const OctantKey &p_key) { * and set said multimesh bounding box to one containing all cells which have this item */ - Map<int, List<Pair<Transform3D, IndexKey>>> multimesh_items; + HashMap<int, List<Pair<Transform3D, IndexKey>>> multimesh_items; - for (Set<IndexKey>::Element *E = g.cells.front(); E; E = E->next()) { - ERR_CONTINUE(!cell_map.has(E->get())); - const Cell &c = cell_map[E->get()]; + for (const IndexKey &E : g.cells) { + ERR_CONTINUE(!cell_map.has(E)); + const Cell &c = cell_map[E]; if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) { continue; } - Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z); + Vector3 cellpos = Vector3(E.x, E.y, E.z); Vector3 ofs = _get_offset(); Transform3D xform; @@ -506,7 +540,7 @@ bool GridMap::_octant_update(const OctantKey &p_key) { Pair<Transform3D, IndexKey> p; p.first = xform * mesh_library->get_item_mesh_transform(c.item); - p.second = E->get(); + p.second = E; multimesh_items[c.item].push_back(p); } } @@ -532,14 +566,29 @@ bool GridMap::_octant_update(const OctantKey &p_key) { if (bake_navigation) { RID region = NavigationServer3D::get_singleton()->region_create(); - NavigationServer3D::get_singleton()->region_set_layers(region, navigation_layers); + NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers); NavigationServer3D::get_singleton()->region_set_navmesh(region, navmesh); - NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * mesh_library->get_item_navmesh_transform(c.item)); + NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform); NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map()); nm.region = region; + + // add navigation debugmesh visual instances if debug is enabled + SceneTree *st = SceneTree::get_singleton(); + if (st && st->is_debugging_navigation_hint()) { + if (!nm.navmesh_debug_instance.is_valid()) { + RID navmesh_debug_rid = navmesh->get_debug_mesh()->get_rid(); + nm.navmesh_debug_instance = RS::get_singleton()->instance_create(); + RS::get_singleton()->instance_set_base(nm.navmesh_debug_instance, navmesh_debug_rid); + RS::get_singleton()->mesh_surface_set_material(navmesh_debug_rid, 0, st->get_debug_navigation_material()->get_rid()); + } + if (is_inside_tree()) { + RS::get_singleton()->instance_set_scenario(nm.navmesh_debug_instance, get_world_3d()->get_scenario()); + RS::get_singleton()->instance_set_transform(nm.navmesh_debug_instance, get_global_transform() * nm.xform); + } + } } - g.navmesh_ids[E->get()] = nm; + g.navmesh_ids[E] = nm; } } @@ -628,7 +677,7 @@ void GridMap::_octant_enter_world(const OctantKey &p_key) { Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F.key].item); if (nm.is_valid()) { RID region = NavigationServer3D::get_singleton()->region_create(); - NavigationServer3D::get_singleton()->region_set_layers(region, navigation_layers); + NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers); NavigationServer3D::get_singleton()->region_set_navmesh(region, nm); NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform); NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map()); @@ -659,6 +708,10 @@ void GridMap::_octant_exit_world(const OctantKey &p_key) { NavigationServer3D::get_singleton()->free(F.value.region); F.value.region = RID(); } + if (F.value.navmesh_debug_instance.is_valid()) { + RS::get_singleton()->free(F.value.navmesh_debug_instance); + F.value.navmesh_debug_instance = RID(); + } } } @@ -677,7 +730,12 @@ void GridMap::_octant_clean_up(const OctantKey &p_key) { // Erase navigation for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) { - NavigationServer3D::get_singleton()->free(E.value.region); + if (E.value.region.is_valid()) { + NavigationServer3D::get_singleton()->free(E.value.region); + } + if (E.value.navmesh_debug_instance.is_valid()) { + RS::get_singleton()->free(E.value.navmesh_debug_instance); + } } g.navmesh_ids.clear(); @@ -770,7 +828,7 @@ void GridMap::_queue_octants_dirty() { void GridMap::_recreate_octant_data() { recreating_octants = true; - Map<IndexKey, Cell> cell_copy = cell_map; + HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map; _clear_internal(); for (const KeyValue<IndexKey, Cell> &E : cell_copy) { set_cell_item(Vector3i(E.key), E.value.item, E.value.rot); @@ -797,7 +855,7 @@ void GridMap::clear() { clear_baked_meshes(); } -void GridMap::resource_changed(const RES &p_res) { +void GridMap::resource_changed(const Ref<Resource> &p_res) { _recreate_octant_data(); } @@ -845,6 +903,9 @@ void GridMap::_bind_methods() { ClassDB::bind_method(D_METHOD("set_navigation_layers", "layers"), &GridMap::set_navigation_layers); ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers); + ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &GridMap::set_navigation_layer_value); + ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &GridMap::get_navigation_layer_value); + ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library); ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library); @@ -889,7 +950,7 @@ void GridMap::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material"); ADD_GROUP("Cell", "cell_"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size"), "set_cell_size", "get_cell_size"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size"); ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y"); @@ -969,7 +1030,7 @@ Array GridMap::get_meshes() const { xform.set_origin(cellpos * cell_size + ofs); xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale)); - meshes.push_back(xform); + meshes.push_back(xform * mesh_library->get_item_mesh_transform(id)); meshes.push_back(mesh); } @@ -998,7 +1059,7 @@ void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texe } //generate - Map<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>> surface_map; + HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map; for (KeyValue<IndexKey, Cell> &E : cell_map) { IndexKey key = E.key; @@ -1028,10 +1089,10 @@ void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texe ok.z = key.z / octant_size; if (!surface_map.has(ok)) { - surface_map[ok] = Map<Ref<Material>, Ref<SurfaceTool>>(); + surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>(); } - Map<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok]; + HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok]; for (int i = 0; i < mesh->get_surface_count(); i++) { if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) { @@ -1051,7 +1112,7 @@ void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texe } } - for (KeyValue<OctantKey, Map<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) { + for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) { Ref<ArrayMesh> mesh; mesh.instantiate(); for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) { diff --git a/modules/gridmap/grid_map.h b/modules/gridmap/grid_map.h index b09cabfe25..078a1d9de5 100644 --- a/modules/gridmap/grid_map.h +++ b/modules/gridmap/grid_map.h @@ -56,9 +56,15 @@ class GridMap : public Node3D { }; uint64_t key = 0; + static uint32_t hash(const IndexKey &p_key) { + return hash_one_uint64(p_key.key); + } _FORCE_INLINE_ bool operator<(const IndexKey &p_key) const { return key < p_key.key; } + _FORCE_INLINE_ bool operator==(const IndexKey &p_key) const { + return key == p_key.key; + } _FORCE_INLINE_ operator Vector3i() const { return Vector3i(x, y, z); @@ -92,6 +98,7 @@ class GridMap : public Node3D { struct NavMesh { RID region; Transform3D xform; + RID navmesh_debug_instance; }; struct MultimeshInstance { @@ -107,13 +114,13 @@ class GridMap : public Node3D { }; Vector<MultimeshInstance> multimesh_instances; - Set<IndexKey> cells; + HashSet<IndexKey> cells; RID collision_debug; RID collision_debug_instance; bool dirty = false; RID static_body; - Map<IndexKey, NavMesh> navmesh_ids; + HashMap<IndexKey, NavMesh> navmesh_ids; }; union OctantKey { @@ -126,8 +133,11 @@ class GridMap : public Node3D { uint64_t key = 0; - _FORCE_INLINE_ bool operator<(const OctantKey &p_key) const { - return key < p_key.key; + static uint32_t hash(const OctantKey &p_key) { + return hash_one_uint64(p_key.key); + } + _FORCE_INLINE_ bool operator==(const OctantKey &p_key) const { + return key == p_key.key; } //OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; } @@ -154,8 +164,8 @@ class GridMap : public Node3D { Ref<MeshLibrary> mesh_library; - Map<OctantKey, Octant *> octant_map; - Map<IndexKey, Cell> cell_map; + HashMap<OctantKey, Octant *, OctantKey> octant_map; + HashMap<IndexKey, Cell, IndexKey> cell_map; void _recreate_octant_data(); @@ -181,7 +191,7 @@ class GridMap : public Node3D { void _queue_octants_dirty(); void _update_octants_callback(); - void resource_changed(const RES &p_res); + void resource_changed(const Ref<Resource> &p_res); void _clear_internal(); @@ -228,8 +238,11 @@ public: void set_bake_navigation(bool p_bake_navigation); bool is_baking_navigation(); - void set_navigation_layers(uint32_t p_layers); - uint32_t get_navigation_layers(); + void set_navigation_layers(uint32_t p_navigation_layers); + uint32_t get_navigation_layers() const; + + void set_navigation_layer_value(int p_layer_number, bool p_value); + bool get_navigation_layer_value(int p_layer_number) const; void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library); Ref<MeshLibrary> get_mesh_library() const; diff --git a/modules/gridmap/register_types.cpp b/modules/gridmap/register_types.cpp index d7c9f5c92e..9efd18a265 100644 --- a/modules/gridmap/register_types.cpp +++ b/modules/gridmap/register_types.cpp @@ -39,14 +39,21 @@ #include "editor/grid_map_editor_plugin.h" #endif -void register_gridmap_types() { - GDREGISTER_CLASS(GridMap); +void initialize_gridmap_module(ModuleInitializationLevel p_level) { + if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { + GDREGISTER_CLASS(GridMap); + } #ifdef TOOLS_ENABLED - EditorPlugins::add_by_type<GridMapEditorPlugin>(); + if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { + EditorPlugins::add_by_type<GridMapEditorPlugin>(); + } #endif } -void unregister_gridmap_types() { +void uninitialize_gridmap_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } } #endif // _3D_DISABLED diff --git a/modules/gridmap/register_types.h b/modules/gridmap/register_types.h index fa3511c5d1..28f14cd398 100644 --- a/modules/gridmap/register_types.h +++ b/modules/gridmap/register_types.h @@ -31,7 +31,9 @@ #ifndef GRIDMAP_REGISTER_TYPES_H #define GRIDMAP_REGISTER_TYPES_H -void register_gridmap_types(); -void unregister_gridmap_types(); +#include "modules/register_module_types.h" + +void initialize_gridmap_module(ModuleInitializationLevel p_level); +void uninitialize_gridmap_module(ModuleInitializationLevel p_level); #endif // GRIDMAP_REGISTER_TYPES_H |