| 1 | /**************************************************************************/ |
| 2 | /* grid_map.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "grid_map.h" |
| 32 | |
| 33 | #include "core/io/marshalls.h" |
| 34 | #include "core/templates/a_hash_map.h" |
| 35 | #include "scene/resources/3d/mesh_library.h" |
| 36 | #include "scene/resources/3d/primitive_meshes.h" |
| 37 | #include "scene/resources/surface_tool.h" |
| 38 | #include "servers/rendering/rendering_server.h" |
| 39 | |
| 40 | #ifndef PHYSICS_3D_DISABLED |
| 41 | #include "core/math/convex_hull.h" |
| 42 | #include "scene/resources/3d/box_shape_3d.h" |
| 43 | #include "scene/resources/3d/capsule_shape_3d.h" |
| 44 | #include "scene/resources/3d/concave_polygon_shape_3d.h" |
| 45 | #include "scene/resources/3d/convex_polygon_shape_3d.h" |
| 46 | #include "scene/resources/3d/cylinder_shape_3d.h" |
| 47 | #include "scene/resources/3d/height_map_shape_3d.h" |
| 48 | #include "scene/resources/3d/shape_3d.h" |
| 49 | #include "scene/resources/3d/sphere_shape_3d.h" |
| 50 | #include "scene/resources/physics_material.h" |
| 51 | #endif // PHYSICS_3D_DISABLED |
| 52 | |
| 53 | #ifndef NAVIGATION_3D_DISABLED |
| 54 | #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h" |
| 55 | #include "servers/navigation_3d/navigation_server_3d.h" |
| 56 | |
| 57 | Callable GridMap::_navmesh_source_geometry_parsing_callback; |
| 58 | RID GridMap::_navmesh_source_geometry_parser; |
| 59 | #endif // NAVIGATION_3D_DISABLED |
| 60 | |
| 61 | bool GridMap::_set(const StringName &p_name, const Variant &p_value) { |
| 62 | String name = p_name; |
| 63 | |
| 64 | if (name == "data") { |
| 65 | Dictionary d = p_value; |
| 66 | |
| 67 | if (d.has("cells")) { |
| 68 | Vector<int> cells = d["cells"]; |
| 69 | int amount = cells.size(); |
| 70 | const int *r = cells.ptr(); |
| 71 | ERR_FAIL_COND_V(amount % 3, false); // not even |
| 72 | cell_map.clear(); |
| 73 | for (int i = 0; i < amount / 3; i++) { |
| 74 | IndexKey ik; |
| 75 | ik.key = decode_uint64((const uint8_t *)&r[i * 3]); |
| 76 | Cell cell; |
| 77 | cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]); |
| 78 | cell_map[ik] = cell; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | _recreate_octant_data(); |
| 83 | |
| 84 | } else if (name == "baked_meshes") { |
| 85 | clear_baked_meshes(); |
| 86 | |
| 87 | Array meshes = p_value; |
| 88 | |
| 89 | RID scenario; |
| 90 | if (is_inside_tree()) { |
| 91 | scenario = get_world_3d()->get_scenario(); |
| 92 | } |
| 93 | |
| 94 | for (int i = 0; i < meshes.size(); i++) { |
| 95 | BakedMesh bm; |
| 96 | bm.mesh = meshes[i]; |
| 97 | ERR_CONTINUE(bm.mesh.is_null()); |
| 98 | bm.instance = RS::get_singleton()->instance_create(); |
| 99 | RS::get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid()); |
| 100 | RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id()); |
| 101 | if (is_inside_tree()) { |
| 102 | RS::get_singleton()->instance_set_scenario(bm.instance, scenario); |
| 103 | RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform()); |
| 104 | } |
| 105 | baked_meshes.push_back(bm); |
| 106 | } |
| 107 | |
| 108 | _recreate_octant_data(); |
| 109 | |
| 110 | } else { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool GridMap::_get(const StringName &p_name, Variant &r_ret) const { |
| 118 | String name = p_name; |
| 119 | |
| 120 | if (name == "data") { |
| 121 | Dictionary d; |
| 122 | |
| 123 | Vector<int> cells; |
| 124 | cells.resize(cell_map.size() * 3); |
| 125 | { |
| 126 | int *w = cells.ptrw(); |
| 127 | int i = 0; |
| 128 | for (const KeyValue<IndexKey, Cell> &E : cell_map) { |
| 129 | encode_uint64(E.key.key, (uint8_t *)&w[i * 3]); |
| 130 | encode_uint32(E.value.cell, (uint8_t *)&w[i * 3 + 2]); |
| 131 | i++; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | d["cells"] = cells; |
| 136 | |
| 137 | r_ret = d; |
| 138 | } else if (name == "baked_meshes") { |
| 139 | Array ret; |
| 140 | ret.resize(baked_meshes.size()); |
| 141 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 142 | ret[i] = baked_meshes[i].mesh; |
| 143 | } |
| 144 | r_ret = ret; |
| 145 | |
| 146 | } else { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | void GridMap::_get_property_list(List<PropertyInfo> *p_list) const { |
| 154 | if (baked_meshes.size()) { |
| 155 | p_list->push_back(PropertyInfo(Variant::ARRAY, "baked_meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE)); |
| 156 | } |
| 157 | |
| 158 | p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE)); |
| 159 | } |
| 160 | |
| 161 | #ifndef PHYSICS_3D_DISABLED |
| 162 | void GridMap::set_collision_layer(uint32_t p_layer) { |
| 163 | collision_layer = p_layer; |
| 164 | _update_physics_bodies_collision_properties(); |
| 165 | } |
| 166 | |
| 167 | uint32_t GridMap::get_collision_layer() const { |
| 168 | return collision_layer; |
| 169 | } |
| 170 | |
| 171 | void GridMap::set_collision_mask(uint32_t p_mask) { |
| 172 | collision_mask = p_mask; |
| 173 | _update_physics_bodies_collision_properties(); |
| 174 | } |
| 175 | |
| 176 | uint32_t GridMap::get_collision_mask() const { |
| 177 | return collision_mask; |
| 178 | } |
| 179 | |
| 180 | void GridMap::set_collision_layer_value(int p_layer_number, bool p_value) { |
| 181 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive."); |
| 182 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive."); |
| 183 | uint32_t collision_layer_new = get_collision_layer(); |
| 184 | if (p_value) { |
| 185 | collision_layer_new |= 1 << (p_layer_number - 1); |
| 186 | } else { |
| 187 | collision_layer_new &= ~(1 << (p_layer_number - 1)); |
| 188 | } |
| 189 | set_collision_layer(collision_layer_new); |
| 190 | } |
| 191 | |
| 192 | bool GridMap::get_collision_layer_value(int p_layer_number) const { |
| 193 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 194 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 195 | return get_collision_layer() & (1 << (p_layer_number - 1)); |
| 196 | } |
| 197 | |
| 198 | void GridMap::set_collision_mask_value(int p_layer_number, bool p_value) { |
| 199 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive."); |
| 200 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive."); |
| 201 | uint32_t mask = get_collision_mask(); |
| 202 | if (p_value) { |
| 203 | mask |= 1 << (p_layer_number - 1); |
| 204 | } else { |
| 205 | mask &= ~(1 << (p_layer_number - 1)); |
| 206 | } |
| 207 | set_collision_mask(mask); |
| 208 | } |
| 209 | |
| 210 | void GridMap::set_collision_priority(real_t p_priority) { |
| 211 | collision_priority = p_priority; |
| 212 | _update_physics_bodies_collision_properties(); |
| 213 | } |
| 214 | |
| 215 | real_t GridMap::get_collision_priority() const { |
| 216 | return collision_priority; |
| 217 | } |
| 218 | |
| 219 | void GridMap::set_physics_material(Ref<PhysicsMaterial> p_material) { |
| 220 | physics_material = p_material; |
| 221 | _update_physics_bodies_characteristics(); |
| 222 | } |
| 223 | |
| 224 | Ref<PhysicsMaterial> GridMap::get_physics_material() const { |
| 225 | return physics_material; |
| 226 | } |
| 227 | |
| 228 | bool GridMap::get_collision_mask_value(int p_layer_number) const { |
| 229 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 230 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive."); |
| 231 | return get_collision_mask() & (1 << (p_layer_number - 1)); |
| 232 | } |
| 233 | |
| 234 | Array GridMap::get_collision_shapes() const { |
| 235 | Array shapes; |
| 236 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 237 | Octant *g = E.value; |
| 238 | RID body = g->static_body; |
| 239 | Transform3D body_xform = PhysicsServer3D::get_singleton()->body_get_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM); |
| 240 | int nshapes = PhysicsServer3D::get_singleton()->body_get_shape_count(body); |
| 241 | for (int i = 0; i < nshapes; i++) { |
| 242 | RID shape = PhysicsServer3D::get_singleton()->body_get_shape(body, i); |
| 243 | Transform3D xform = PhysicsServer3D::get_singleton()->body_get_shape_transform(body, i); |
| 244 | shapes.push_back(body_xform * xform); |
| 245 | shapes.push_back(shape); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return shapes; |
| 250 | } |
| 251 | #endif // PHYSICS_3D_DISABLED |
| 252 | |
| 253 | void GridMap::set_bake_navigation(bool p_bake_navigation) { |
| 254 | bake_navigation = p_bake_navigation; |
| 255 | _recreate_octant_data(); |
| 256 | } |
| 257 | |
| 258 | bool GridMap::is_baking_navigation() { |
| 259 | return bake_navigation; |
| 260 | } |
| 261 | |
| 262 | #ifndef NAVIGATION_3D_DISABLED |
| 263 | void GridMap::set_navigation_map(RID p_navigation_map) { |
| 264 | map_override = p_navigation_map; |
| 265 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 266 | Octant &g = *octant_map[E.key]; |
| 267 | for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) { |
| 268 | if (F.value.region.is_valid()) { |
| 269 | NavigationServer3D::get_singleton()->region_set_map(F.value.region, map_override); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | RID GridMap::get_navigation_map() const { |
| 276 | if (map_override.is_valid()) { |
| 277 | return map_override; |
| 278 | } else if (is_inside_tree()) { |
| 279 | return get_world_3d()->get_navigation_map(); |
| 280 | } |
| 281 | return RID(); |
| 282 | } |
| 283 | #endif // NAVIGATION_3D_DISABLED |
| 284 | |
| 285 | void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) { |
| 286 | if (mesh_library.is_valid()) { |
| 287 | mesh_library->disconnect_changed(callable_mp(this, &GridMap::_recreate_octant_data)); |
| 288 | } |
| 289 | mesh_library = p_mesh_library; |
| 290 | if (mesh_library.is_valid()) { |
| 291 | mesh_library->connect_changed(callable_mp(this, &GridMap::_recreate_octant_data)); |
| 292 | } |
| 293 | |
| 294 | _recreate_octant_data(); |
| 295 | emit_signal(CoreStringName(changed)); |
| 296 | } |
| 297 | |
| 298 | Ref<MeshLibrary> GridMap::get_mesh_library() const { |
| 299 | return mesh_library; |
| 300 | } |
| 301 | |
| 302 | void GridMap::set_cell_size(const Vector3 &p_size) { |
| 303 | ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001); |
| 304 | cell_size = p_size; |
| 305 | _recreate_octant_data(); |
| 306 | emit_signal(SNAME("cell_size_changed"), cell_size); |
| 307 | } |
| 308 | |
| 309 | Vector3 GridMap::get_cell_size() const { |
| 310 | return cell_size; |
| 311 | } |
| 312 | |
| 313 | void GridMap::set_octant_size(int p_size) { |
| 314 | ERR_FAIL_COND(p_size == 0); |
| 315 | octant_size = p_size; |
| 316 | _recreate_octant_data(); |
| 317 | } |
| 318 | |
| 319 | int GridMap::get_octant_size() const { |
| 320 | return octant_size; |
| 321 | } |
| 322 | |
| 323 | void GridMap::set_center_x(bool p_enable) { |
| 324 | center_x = p_enable; |
| 325 | _recreate_octant_data(); |
| 326 | } |
| 327 | |
| 328 | bool GridMap::get_center_x() const { |
| 329 | return center_x; |
| 330 | } |
| 331 | |
| 332 | void GridMap::set_center_y(bool p_enable) { |
| 333 | center_y = p_enable; |
| 334 | _recreate_octant_data(); |
| 335 | } |
| 336 | |
| 337 | bool GridMap::get_center_y() const { |
| 338 | return center_y; |
| 339 | } |
| 340 | |
| 341 | void GridMap::set_center_z(bool p_enable) { |
| 342 | center_z = p_enable; |
| 343 | _recreate_octant_data(); |
| 344 | } |
| 345 | |
| 346 | bool GridMap::get_center_z() const { |
| 347 | return center_z; |
| 348 | } |
| 349 | |
| 350 | void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) { |
| 351 | if (baked_meshes.size() && !recreating_octants) { |
| 352 | //if you set a cell item, baked meshes go good bye |
| 353 | clear_baked_meshes(); |
| 354 | _recreate_octant_data(); |
| 355 | } |
| 356 | |
| 357 | ERR_FAIL_INDEX(Math::abs(p_position.x), 1 << 20); |
| 358 | ERR_FAIL_INDEX(Math::abs(p_position.y), 1 << 20); |
| 359 | ERR_FAIL_INDEX(Math::abs(p_position.z), 1 << 20); |
| 360 | |
| 361 | IndexKey key; |
| 362 | key.x = p_position.x; |
| 363 | key.y = p_position.y; |
| 364 | key.z = p_position.z; |
| 365 | |
| 366 | const OctantKey ok = get_octant_key_from_cell_coords(p_position); |
| 367 | |
| 368 | if (p_item < 0) { |
| 369 | //erase |
| 370 | if (cell_map.has(key)) { |
| 371 | OctantKey octantkey = ok; |
| 372 | |
| 373 | ERR_FAIL_COND(!octant_map.has(octantkey)); |
| 374 | Octant &g = *octant_map[octantkey]; |
| 375 | g.cells.erase(key); |
| 376 | g.dirty = true; |
| 377 | cell_map.erase(key); |
| 378 | _queue_octants_dirty(); |
| 379 | } |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | OctantKey octantkey = ok; |
| 384 | |
| 385 | if (!octant_map.has(octantkey)) { |
| 386 | //create octant because it does not exist |
| 387 | Octant *g = memnew(Octant); |
| 388 | g->dirty = true; |
| 389 | #ifndef PHYSICS_3D_DISABLED |
| 390 | g->static_body = PhysicsServer3D::get_singleton()->body_create(); |
| 391 | PhysicsServer3D::get_singleton()->body_set_mode(g->static_body, PhysicsServer3D::BODY_MODE_STATIC); |
| 392 | PhysicsServer3D::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id()); |
| 393 | PhysicsServer3D::get_singleton()->body_set_collision_layer(g->static_body, collision_layer); |
| 394 | PhysicsServer3D::get_singleton()->body_set_collision_mask(g->static_body, collision_mask); |
| 395 | PhysicsServer3D::get_singleton()->body_set_collision_priority(g->static_body, collision_priority); |
| 396 | if (physics_material.is_valid()) { |
| 397 | PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, physics_material->computed_friction()); |
| 398 | PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material->computed_bounce()); |
| 399 | } |
| 400 | #endif // PHYSICS_3D_DISABLED |
| 401 | SceneTree *st = SceneTree::get_singleton(); |
| 402 | |
| 403 | if (st && st->is_debugging_collisions_hint()) { |
| 404 | g->collision_debug = RenderingServer::get_singleton()->mesh_create(); |
| 405 | g->collision_debug_instance = RenderingServer::get_singleton()->instance_create(); |
| 406 | RenderingServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug); |
| 407 | } |
| 408 | |
| 409 | octant_map[octantkey] = g; |
| 410 | |
| 411 | if (is_inside_world()) { |
| 412 | _octant_enter_world(octantkey); |
| 413 | _octant_transform(octantkey); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | Octant &g = *octant_map[octantkey]; |
| 418 | g.cells.insert(key); |
| 419 | g.dirty = true; |
| 420 | _queue_octants_dirty(); |
| 421 | |
| 422 | Cell c; |
| 423 | c.item = p_item; |
| 424 | c.rot = p_rot; |
| 425 | |
| 426 | cell_map[key] = c; |
| 427 | } |
| 428 | |
| 429 | int GridMap::get_cell_item(const Vector3i &p_position) const { |
| 430 | ERR_FAIL_INDEX_V(Math::abs(p_position.x), 1 << 20, INVALID_CELL_ITEM); |
| 431 | ERR_FAIL_INDEX_V(Math::abs(p_position.y), 1 << 20, INVALID_CELL_ITEM); |
| 432 | ERR_FAIL_INDEX_V(Math::abs(p_position.z), 1 << 20, INVALID_CELL_ITEM); |
| 433 | |
| 434 | IndexKey key; |
| 435 | key.x = p_position.x; |
| 436 | key.y = p_position.y; |
| 437 | key.z = p_position.z; |
| 438 | |
| 439 | if (!cell_map.has(key)) { |
| 440 | return INVALID_CELL_ITEM; |
| 441 | } |
| 442 | return cell_map[key].item; |
| 443 | } |
| 444 | |
| 445 | int GridMap::get_cell_item_orientation(const Vector3i &p_position) const { |
| 446 | ERR_FAIL_INDEX_V(Math::abs(p_position.x), 1 << 20, -1); |
| 447 | ERR_FAIL_INDEX_V(Math::abs(p_position.y), 1 << 20, -1); |
| 448 | ERR_FAIL_INDEX_V(Math::abs(p_position.z), 1 << 20, -1); |
| 449 | |
| 450 | IndexKey key; |
| 451 | key.x = p_position.x; |
| 452 | key.y = p_position.y; |
| 453 | key.z = p_position.z; |
| 454 | |
| 455 | if (!cell_map.has(key)) { |
| 456 | return -1; |
| 457 | } |
| 458 | return cell_map[key].rot; |
| 459 | } |
| 460 | |
| 461 | static const Basis _ortho_bases[24] = { |
| 462 | Basis(1, 0, 0, 0, 1, 0, 0, 0, 1), |
| 463 | Basis(0, -1, 0, 1, 0, 0, 0, 0, 1), |
| 464 | Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1), |
| 465 | Basis(0, 1, 0, -1, 0, 0, 0, 0, 1), |
| 466 | Basis(1, 0, 0, 0, 0, -1, 0, 1, 0), |
| 467 | Basis(0, 0, 1, 1, 0, 0, 0, 1, 0), |
| 468 | Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0), |
| 469 | Basis(0, 0, -1, -1, 0, 0, 0, 1, 0), |
| 470 | Basis(1, 0, 0, 0, -1, 0, 0, 0, -1), |
| 471 | Basis(0, 1, 0, 1, 0, 0, 0, 0, -1), |
| 472 | Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1), |
| 473 | Basis(0, -1, 0, -1, 0, 0, 0, 0, -1), |
| 474 | Basis(1, 0, 0, 0, 0, 1, 0, -1, 0), |
| 475 | Basis(0, 0, -1, 1, 0, 0, 0, -1, 0), |
| 476 | Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0), |
| 477 | Basis(0, 0, 1, -1, 0, 0, 0, -1, 0), |
| 478 | Basis(0, 0, 1, 0, 1, 0, -1, 0, 0), |
| 479 | Basis(0, -1, 0, 0, 0, 1, -1, 0, 0), |
| 480 | Basis(0, 0, -1, 0, -1, 0, -1, 0, 0), |
| 481 | Basis(0, 1, 0, 0, 0, -1, -1, 0, 0), |
| 482 | Basis(0, 0, 1, 0, -1, 0, 1, 0, 0), |
| 483 | Basis(0, 1, 0, 0, 0, 1, 1, 0, 0), |
| 484 | Basis(0, 0, -1, 0, 1, 0, 1, 0, 0), |
| 485 | Basis(0, -1, 0, 0, 0, -1, 1, 0, 0) |
| 486 | }; |
| 487 | |
| 488 | Basis GridMap::get_cell_item_basis(const Vector3i &p_position) const { |
| 489 | int orientation = get_cell_item_orientation(p_position); |
| 490 | |
| 491 | if (orientation == -1) { |
| 492 | return Basis(); |
| 493 | } |
| 494 | |
| 495 | return get_basis_with_orthogonal_index(orientation); |
| 496 | } |
| 497 | |
| 498 | Basis GridMap::get_basis_with_orthogonal_index(int p_index) const { |
| 499 | ERR_FAIL_INDEX_V(p_index, 24, Basis()); |
| 500 | |
| 501 | return _ortho_bases[p_index]; |
| 502 | } |
| 503 | |
| 504 | int GridMap::get_orthogonal_index_from_basis(const Basis &p_basis) const { |
| 505 | Basis orth = p_basis; |
| 506 | for (int i = 0; i < 3; i++) { |
| 507 | for (int j = 0; j < 3; j++) { |
| 508 | real_t v = orth[i][j]; |
| 509 | if (v > 0.5) { |
| 510 | v = 1.0; |
| 511 | } else if (v < -0.5) { |
| 512 | v = -1.0; |
| 513 | } else { |
| 514 | v = 0; |
| 515 | } |
| 516 | |
| 517 | orth[i][j] = v; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | for (int i = 0; i < 24; i++) { |
| 522 | if (_ortho_bases[i] == orth) { |
| 523 | return i; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | GridMap::OctantKey GridMap::get_octant_key_from_index_key(const IndexKey &p_index_key) const { |
| 531 | const int x = p_index_key.x > 0 ? p_index_key.x / octant_size : (p_index_key.x - (octant_size - 1)) / octant_size; |
| 532 | const int y = p_index_key.y > 0 ? p_index_key.y / octant_size : (p_index_key.y - (octant_size - 1)) / octant_size; |
| 533 | const int z = p_index_key.z > 0 ? p_index_key.z / octant_size : (p_index_key.z - (octant_size - 1)) / octant_size; |
| 534 | |
| 535 | OctantKey ok; |
| 536 | ok.key = 0; |
| 537 | ok.x = x; |
| 538 | ok.y = y; |
| 539 | ok.z = z; |
| 540 | return ok; |
| 541 | } |
| 542 | |
| 543 | GridMap::OctantKey GridMap::get_octant_key_from_cell_coords(const Vector3i &p_cell_coords) const { |
| 544 | const int x = p_cell_coords.x > 0 ? p_cell_coords.x / octant_size : (p_cell_coords.x - (octant_size - 1)) / octant_size; |
| 545 | const int y = p_cell_coords.y > 0 ? p_cell_coords.y / octant_size : (p_cell_coords.y - (octant_size - 1)) / octant_size; |
| 546 | const int z = p_cell_coords.z > 0 ? p_cell_coords.z / octant_size : (p_cell_coords.z - (octant_size - 1)) / octant_size; |
| 547 | |
| 548 | OctantKey ok; |
| 549 | ok.key = 0; |
| 550 | ok.x = x; |
| 551 | ok.y = y; |
| 552 | ok.z = z; |
| 553 | return ok; |
| 554 | } |
| 555 | |
| 556 | Vector3i GridMap::local_to_map(const Vector3 &p_world_position) const { |
| 557 | Vector3 map_position = (p_world_position / cell_size).floor(); |
| 558 | return Vector3i(map_position); |
| 559 | } |
| 560 | |
| 561 | Vector3 GridMap::map_to_local(const Vector3i &p_map_position) const { |
| 562 | Vector3 offset = _get_offset(); |
| 563 | Vector3 local_position( |
| 564 | p_map_position.x * cell_size.x + offset.x, |
| 565 | p_map_position.y * cell_size.y + offset.y, |
| 566 | p_map_position.z * cell_size.z + offset.z); |
| 567 | return local_position; |
| 568 | } |
| 569 | |
| 570 | void GridMap::_octant_transform(const OctantKey &p_key) { |
| 571 | ERR_FAIL_COND(!octant_map.has(p_key)); |
| 572 | Octant &g = *octant_map[p_key]; |
| 573 | #ifndef PHYSICS_3D_DISABLED |
| 574 | PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform()); |
| 575 | |
| 576 | if (g.collision_debug_instance.is_valid()) { |
| 577 | RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform()); |
| 578 | } |
| 579 | #endif // PHYSICS_3D_DISABLED |
| 580 | |
| 581 | #ifndef NAVIGATION_3D_DISABLED |
| 582 | // update transform for NavigationServer regions and navigation debugmesh instances |
| 583 | for (const KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) { |
| 584 | if (bake_navigation) { |
| 585 | if (E.value.region.is_valid()) { |
| 586 | NavigationServer3D::get_singleton()->region_set_transform(E.value.region, get_global_transform() * E.value.xform); |
| 587 | } |
| 588 | if (E.value.navigation_mesh_debug_instance.is_valid()) { |
| 589 | RS::get_singleton()->instance_set_transform(E.value.navigation_mesh_debug_instance, get_global_transform() * E.value.xform); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | #endif // NAVIGATION_3D_DISABLED |
| 594 | |
| 595 | for (int i = 0; i < g.multimesh_instances.size(); i++) { |
| 596 | RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform()); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | bool GridMap::_octant_update(const OctantKey &p_key) { |
| 601 | ERR_FAIL_COND_V(!octant_map.has(p_key), false); |
| 602 | Octant &g = *octant_map[p_key]; |
| 603 | if (!g.dirty) { |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | #ifndef PHYSICS_3D_DISABLED |
| 608 | //erase body shapes |
| 609 | PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body); |
| 610 | |
| 611 | //erase body shapes debug |
| 612 | if (g.collision_debug.is_valid()) { |
| 613 | RS::get_singleton()->mesh_clear(g.collision_debug); |
| 614 | } |
| 615 | #endif // PHYSICS_3D_DISABLED |
| 616 | |
| 617 | #ifndef NAVIGATION_3D_DISABLED |
| 618 | //erase navigation |
| 619 | for (KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) { |
| 620 | if (E.value.region.is_valid()) { |
| 621 | NavigationServer3D::get_singleton()->free_rid(E.value.region); |
| 622 | E.value.region = RID(); |
| 623 | } |
| 624 | if (E.value.navigation_mesh_debug_instance.is_valid()) { |
| 625 | RS::get_singleton()->free_rid(E.value.navigation_mesh_debug_instance); |
| 626 | E.value.navigation_mesh_debug_instance = RID(); |
| 627 | } |
| 628 | } |
| 629 | g.navigation_cell_ids.clear(); |
| 630 | #endif // NAVIGATION_3D_DISABLED |
| 631 | |
| 632 | //erase multimeshes |
| 633 | |
| 634 | for (int i = 0; i < g.multimesh_instances.size(); i++) { |
| 635 | RS::get_singleton()->free_rid(g.multimesh_instances[i].instance); |
| 636 | RS::get_singleton()->free_rid(g.multimesh_instances[i].multimesh); |
| 637 | } |
| 638 | g.multimesh_instances.clear(); |
| 639 | |
| 640 | if (g.cells.is_empty()) { |
| 641 | //octant no longer needed |
| 642 | _octant_clean_up(p_key); |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | Vector<Vector3> col_debug; |
| 647 | |
| 648 | /* |
| 649 | * foreach item in this octant, |
| 650 | * set item's multimesh's instance count to number of cells which have this item |
| 651 | * and set said multimesh bounding box to one containing all cells which have this item |
| 652 | */ |
| 653 | |
| 654 | struct MultiMeshItemPlacement { |
| 655 | Transform3D transform; |
| 656 | IndexKey index_key; |
| 657 | }; |
| 658 | AHashMap<int, LocalVector<MultiMeshItemPlacement>> item_id_to_multimesh_item_placements; |
| 659 | |
| 660 | RID scenario; |
| 661 | #ifndef NAVIGATION_3D_DISABLED |
| 662 | RID navigation_map; |
| 663 | #endif |
| 664 | |
| 665 | if (is_inside_tree()) { |
| 666 | scenario = get_world_3d()->get_scenario(); |
| 667 | #ifndef NAVIGATION_3D_DISABLED |
| 668 | navigation_map = get_world_3d()->get_navigation_map(); |
| 669 | #endif |
| 670 | } |
| 671 | |
| 672 | for (const IndexKey &E : g.cells) { |
| 673 | ERR_CONTINUE(!cell_map.has(E)); |
| 674 | const Cell &c = cell_map[E]; |
| 675 | |
| 676 | if (mesh_library.is_null() || !mesh_library->has_item(c.item)) { |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | Vector3 cellpos = Vector3(E.x, E.y, E.z); |
| 681 | Vector3 ofs = _get_offset(); |
| 682 | |
| 683 | Transform3D xform; |
| 684 | |
| 685 | xform.basis = _ortho_bases[c.rot]; |
| 686 | xform.set_origin(cellpos * cell_size + ofs); |
| 687 | xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale)); |
| 688 | if (baked_meshes.is_empty()) { |
| 689 | if (mesh_library->get_item_mesh(c.item).is_valid()) { |
| 690 | if (!item_id_to_multimesh_item_placements.has(c.item)) { |
| 691 | item_id_to_multimesh_item_placements[c.item] = LocalVector<MultiMeshItemPlacement>(); |
| 692 | } |
| 693 | |
| 694 | MultiMeshItemPlacement p; |
| 695 | p.transform = xform * mesh_library->get_item_mesh_transform(c.item); |
| 696 | p.index_key = E; |
| 697 | item_id_to_multimesh_item_placements[c.item].push_back(p); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | #ifndef PHYSICS_3D_DISABLED |
| 702 | Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item); |
| 703 | // add the item's shape at given xform to octant's static_body |
| 704 | for (int i = 0; i < shapes.size(); i++) { |
| 705 | // add the item's shape |
| 706 | if (shapes[i].shape.is_null()) { |
| 707 | continue; |
| 708 | } |
| 709 | PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform); |
| 710 | if (g.collision_debug.is_valid()) { |
| 711 | shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform); |
| 712 | } |
| 713 | } |
| 714 | #endif // PHYSICS_3D_DISABLED |
| 715 | |
| 716 | #ifndef NAVIGATION_3D_DISABLED |
| 717 | // add the item's navigation_mesh at given xform to GridMap's Navigation ancestor |
| 718 | Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(c.item); |
| 719 | if (navigation_mesh.is_valid()) { |
| 720 | Octant::NavigationCell nm; |
| 721 | nm.xform = xform * mesh_library->get_item_navigation_mesh_transform(c.item); |
| 722 | nm.navigation_layers = mesh_library->get_item_navigation_layers(c.item); |
| 723 | |
| 724 | if (bake_navigation) { |
| 725 | RID region = NavigationServer3D::get_singleton()->region_create(); |
| 726 | NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id()); |
| 727 | NavigationServer3D::get_singleton()->region_set_navigation_layers(region, nm.navigation_layers); |
| 728 | NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh); |
| 729 | NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform); |
| 730 | if (is_inside_tree()) { |
| 731 | if (map_override.is_valid()) { |
| 732 | NavigationServer3D::get_singleton()->region_set_map(region, map_override); |
| 733 | } else { |
| 734 | NavigationServer3D::get_singleton()->region_set_map(region, navigation_map); |
| 735 | } |
| 736 | } |
| 737 | nm.region = region; |
| 738 | |
| 739 | #ifdef DEBUG_ENABLED |
| 740 | // add navigation debugmesh visual instances if debug is enabled |
| 741 | SceneTree *st = SceneTree::get_singleton(); |
| 742 | if (st && st->is_debugging_navigation_hint()) { |
| 743 | if (!nm.navigation_mesh_debug_instance.is_valid()) { |
| 744 | RID navigation_mesh_debug_rid = navigation_mesh->get_debug_mesh()->get_rid(); |
| 745 | nm.navigation_mesh_debug_instance = RS::get_singleton()->instance_create(); |
| 746 | RS::get_singleton()->instance_set_base(nm.navigation_mesh_debug_instance, navigation_mesh_debug_rid); |
| 747 | } |
| 748 | if (is_inside_tree()) { |
| 749 | RS::get_singleton()->instance_set_scenario(nm.navigation_mesh_debug_instance, scenario); |
| 750 | RS::get_singleton()->instance_set_transform(nm.navigation_mesh_debug_instance, get_global_transform() * nm.xform); |
| 751 | } |
| 752 | } |
| 753 | #endif // DEBUG_ENABLED |
| 754 | } |
| 755 | g.navigation_cell_ids[E] = nm; |
| 756 | } |
| 757 | #endif // NAVIGATION_3D_DISABLED |
| 758 | } |
| 759 | |
| 760 | #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 761 | if (bake_navigation) { |
| 762 | _update_octant_navigation_debug_edge_connections_mesh(p_key); |
| 763 | } |
| 764 | #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 765 | |
| 766 | //update multimeshes, only if not baked |
| 767 | if (baked_meshes.is_empty()) { |
| 768 | for (const KeyValue<int, LocalVector<MultiMeshItemPlacement>> &E : item_id_to_multimesh_item_placements) { |
| 769 | Octant::MultimeshInstance mmi; |
| 770 | |
| 771 | RID mm = RS::get_singleton()->multimesh_create(); |
| 772 | RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D); |
| 773 | RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid()); |
| 774 | |
| 775 | int idx = 0; |
| 776 | const LocalVector<MultiMeshItemPlacement> &mm_item_placements = E.value; |
| 777 | for (const MultiMeshItemPlacement &mm_item_placement : mm_item_placements) { |
| 778 | RS::get_singleton()->multimesh_instance_set_transform(mm, idx, mm_item_placement.transform); |
| 779 | #ifdef TOOLS_ENABLED |
| 780 | |
| 781 | Octant::MultimeshInstance::Item it; |
| 782 | it.index = idx; |
| 783 | it.transform = mm_item_placement.transform; |
| 784 | it.key = mm_item_placement.index_key; |
| 785 | mmi.items.push_back(it); |
| 786 | #endif |
| 787 | |
| 788 | idx++; |
| 789 | } |
| 790 | |
| 791 | RID instance = RS::get_singleton()->instance_create(); |
| 792 | RS::get_singleton()->instance_set_base(instance, mm); |
| 793 | |
| 794 | if (is_inside_tree()) { |
| 795 | RS::get_singleton()->instance_set_scenario(instance, scenario); |
| 796 | RS::get_singleton()->instance_set_transform(instance, get_global_transform()); |
| 797 | } |
| 798 | |
| 799 | RS::ShadowCastingSetting cast_shadows = (RS::ShadowCastingSetting)mesh_library->get_item_mesh_cast_shadow(E.key); |
| 800 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(instance, cast_shadows); |
| 801 | |
| 802 | mmi.multimesh = mm; |
| 803 | mmi.instance = instance; |
| 804 | |
| 805 | g.multimesh_instances.push_back(mmi); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | #ifndef PHYSICS_3D_DISABLED |
| 810 | if (col_debug.size()) { |
| 811 | SceneTree *st = SceneTree::get_singleton(); |
| 812 | |
| 813 | Vector<Color> colors; |
| 814 | colors.resize(col_debug.size()); |
| 815 | if (st) { |
| 816 | colors.fill(st->get_debug_collisions_color()); |
| 817 | } |
| 818 | |
| 819 | Array arr; |
| 820 | arr.resize(RS::ARRAY_MAX); |
| 821 | arr[RS::ARRAY_VERTEX] = col_debug; |
| 822 | arr[RS::ARRAY_COLOR] = colors; |
| 823 | |
| 824 | RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr); |
| 825 | if (st) { |
| 826 | RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid()); |
| 827 | } |
| 828 | } |
| 829 | #endif // PHYSICS_3D_DISABLED |
| 830 | |
| 831 | g.dirty = false; |
| 832 | |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | #ifndef PHYSICS_3D_DISABLED |
| 837 | void GridMap::_update_physics_bodies_collision_properties() { |
| 838 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 839 | PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer); |
| 840 | PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask); |
| 841 | PhysicsServer3D::get_singleton()->body_set_collision_priority(E.value->static_body, collision_priority); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void GridMap::_update_physics_bodies_characteristics() { |
| 846 | real_t friction = 1.0; |
| 847 | real_t bounce = 0.0; |
| 848 | if (physics_material.is_valid()) { |
| 849 | friction = physics_material->computed_friction(); |
| 850 | bounce = physics_material->computed_bounce(); |
| 851 | } |
| 852 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 853 | PhysicsServer3D::get_singleton()->body_set_param(E.value->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, friction); |
| 854 | PhysicsServer3D::get_singleton()->body_set_param(E.value->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, bounce); |
| 855 | } |
| 856 | } |
| 857 | #endif // PHYSICS_3D_DISABLED |
| 858 | |
| 859 | void GridMap::_octant_enter_world(const OctantKey &p_key) { |
| 860 | ERR_FAIL_COND(!octant_map.has(p_key)); |
| 861 | Octant &g = *octant_map[p_key]; |
| 862 | |
| 863 | const RID scenario = get_world_3d()->get_scenario(); |
| 864 | |
| 865 | #ifndef PHYSICS_3D_DISABLED |
| 866 | PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform()); |
| 867 | PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space()); |
| 868 | |
| 869 | if (g.collision_debug_instance.is_valid()) { |
| 870 | RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, scenario); |
| 871 | RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform()); |
| 872 | } |
| 873 | #endif // PHYSICS_3D_DISABLED |
| 874 | |
| 875 | for (int i = 0; i < g.multimesh_instances.size(); i++) { |
| 876 | RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, scenario); |
| 877 | RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform()); |
| 878 | } |
| 879 | |
| 880 | #ifndef NAVIGATION_3D_DISABLED |
| 881 | const RID navigation_map = get_world_3d()->get_navigation_map(); |
| 882 | |
| 883 | if (bake_navigation && mesh_library.is_valid()) { |
| 884 | for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) { |
| 885 | if (cell_map.has(F.key) && F.value.region.is_valid() == false) { |
| 886 | Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(cell_map[F.key].item); |
| 887 | if (navigation_mesh.is_valid()) { |
| 888 | RID region = NavigationServer3D::get_singleton()->region_create(); |
| 889 | NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id()); |
| 890 | NavigationServer3D::get_singleton()->region_set_navigation_layers(region, F.value.navigation_layers); |
| 891 | NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh); |
| 892 | NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform); |
| 893 | if (map_override.is_valid()) { |
| 894 | NavigationServer3D::get_singleton()->region_set_map(region, map_override); |
| 895 | } else { |
| 896 | NavigationServer3D::get_singleton()->region_set_map(region, navigation_map); |
| 897 | } |
| 898 | |
| 899 | F.value.region = region; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | #ifdef DEBUG_ENABLED |
| 905 | if (bake_navigation) { |
| 906 | if (!g.navigation_debug_edge_connections_instance.is_valid()) { |
| 907 | g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create(); |
| 908 | } |
| 909 | if (g.navigation_debug_edge_connections_mesh.is_null()) { |
| 910 | g.navigation_debug_edge_connections_mesh.instantiate(); |
| 911 | } |
| 912 | |
| 913 | _update_octant_navigation_debug_edge_connections_mesh(p_key); |
| 914 | } |
| 915 | #endif // DEBUG_ENABLED |
| 916 | } |
| 917 | #endif // NAVIGATION_3D_DISABLED |
| 918 | } |
| 919 | |
| 920 | void GridMap::_octant_exit_world(const OctantKey &p_key) { |
| 921 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 922 | #ifndef PHYSICS_3D_DISABLED |
| 923 | ERR_FAIL_NULL(PhysicsServer3D::get_singleton()); |
| 924 | #endif // PHYSICS_3D_DISABLED |
| 925 | #ifndef NAVIGATION_3D_DISABLED |
| 926 | ERR_FAIL_NULL(NavigationServer3D::get_singleton()); |
| 927 | #endif // NAVIGATION_3D_DISABLED |
| 928 | |
| 929 | ERR_FAIL_COND(!octant_map.has(p_key)); |
| 930 | Octant &g = *octant_map[p_key]; |
| 931 | |
| 932 | #ifndef PHYSICS_3D_DISABLED |
| 933 | PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform()); |
| 934 | PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID()); |
| 935 | |
| 936 | if (g.collision_debug_instance.is_valid()) { |
| 937 | RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID()); |
| 938 | } |
| 939 | #endif // PHYSICS_3D_DISABLED |
| 940 | |
| 941 | for (int i = 0; i < g.multimesh_instances.size(); i++) { |
| 942 | RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID()); |
| 943 | } |
| 944 | |
| 945 | #ifndef NAVIGATION_3D_DISABLED |
| 946 | for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) { |
| 947 | if (F.value.region.is_valid()) { |
| 948 | NavigationServer3D::get_singleton()->free_rid(F.value.region); |
| 949 | F.value.region = RID(); |
| 950 | } |
| 951 | if (F.value.navigation_mesh_debug_instance.is_valid()) { |
| 952 | RS::get_singleton()->free_rid(F.value.navigation_mesh_debug_instance); |
| 953 | F.value.navigation_mesh_debug_instance = RID(); |
| 954 | } |
| 955 | } |
| 956 | #endif // NAVIGATION_3D_DISABLED |
| 957 | |
| 958 | #ifdef DEBUG_ENABLED |
| 959 | if (bake_navigation) { |
| 960 | if (g.navigation_debug_edge_connections_instance.is_valid()) { |
| 961 | RenderingServer::get_singleton()->free_rid(g.navigation_debug_edge_connections_instance); |
| 962 | g.navigation_debug_edge_connections_instance = RID(); |
| 963 | } |
| 964 | if (g.navigation_debug_edge_connections_mesh.is_valid()) { |
| 965 | g.navigation_debug_edge_connections_mesh.unref(); |
| 966 | } |
| 967 | } |
| 968 | #endif // DEBUG_ENABLED |
| 969 | } |
| 970 | |
| 971 | void GridMap::_octant_clean_up(const OctantKey &p_key) { |
| 972 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 973 | #ifndef PHYSICS_3D_DISABLED |
| 974 | ERR_FAIL_NULL(PhysicsServer3D::get_singleton()); |
| 975 | #endif // PHYSICS_3D_DISABLED |
| 976 | #ifndef NAVIGATION_3D_DISABLED |
| 977 | ERR_FAIL_NULL(NavigationServer3D::get_singleton()); |
| 978 | #endif // NAVIGATION_3D_DISABLED |
| 979 | |
| 980 | ERR_FAIL_COND(!octant_map.has(p_key)); |
| 981 | Octant &g = *octant_map[p_key]; |
| 982 | |
| 983 | #ifndef PHYSICS_3D_DISABLED |
| 984 | if (g.collision_debug.is_valid()) { |
| 985 | RS::get_singleton()->free_rid(g.collision_debug); |
| 986 | } |
| 987 | if (g.collision_debug_instance.is_valid()) { |
| 988 | RS::get_singleton()->free_rid(g.collision_debug_instance); |
| 989 | } |
| 990 | |
| 991 | PhysicsServer3D::get_singleton()->free_rid(g.static_body); |
| 992 | #endif // PHYSICS_3D_DISABLED |
| 993 | |
| 994 | #ifndef NAVIGATION_3D_DISABLED |
| 995 | // Erase navigation |
| 996 | for (const KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) { |
| 997 | if (E.value.region.is_valid()) { |
| 998 | NavigationServer3D::get_singleton()->free_rid(E.value.region); |
| 999 | } |
| 1000 | if (E.value.navigation_mesh_debug_instance.is_valid()) { |
| 1001 | RS::get_singleton()->free_rid(E.value.navigation_mesh_debug_instance); |
| 1002 | } |
| 1003 | } |
| 1004 | g.navigation_cell_ids.clear(); |
| 1005 | #endif // NAVIGATION_3D_DISABLED |
| 1006 | |
| 1007 | #ifdef DEBUG_ENABLED |
| 1008 | if (bake_navigation) { |
| 1009 | if (g.navigation_debug_edge_connections_instance.is_valid()) { |
| 1010 | RenderingServer::get_singleton()->free_rid(g.navigation_debug_edge_connections_instance); |
| 1011 | g.navigation_debug_edge_connections_instance = RID(); |
| 1012 | } |
| 1013 | if (g.navigation_debug_edge_connections_mesh.is_valid()) { |
| 1014 | g.navigation_debug_edge_connections_mesh.unref(); |
| 1015 | } |
| 1016 | } |
| 1017 | #endif // DEBUG_ENABLED |
| 1018 | |
| 1019 | //erase multimeshes |
| 1020 | |
| 1021 | for (int i = 0; i < g.multimesh_instances.size(); i++) { |
| 1022 | RS::get_singleton()->free_rid(g.multimesh_instances[i].instance); |
| 1023 | RS::get_singleton()->free_rid(g.multimesh_instances[i].multimesh); |
| 1024 | } |
| 1025 | g.multimesh_instances.clear(); |
| 1026 | } |
| 1027 | |
| 1028 | void GridMap::_notification(int p_what) { |
| 1029 | switch (p_what) { |
| 1030 | case NOTIFICATION_ENTER_WORLD: { |
| 1031 | last_transform = get_global_transform(); |
| 1032 | |
| 1033 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 1034 | _octant_enter_world(E.key); |
| 1035 | } |
| 1036 | |
| 1037 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 1038 | RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario()); |
| 1039 | RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform()); |
| 1040 | } |
| 1041 | } break; |
| 1042 | |
| 1043 | case NOTIFICATION_ENTER_TREE: { |
| 1044 | #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1045 | if (bake_navigation && NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) { |
| 1046 | _update_navigation_debug_edge_connections(); |
| 1047 | } |
| 1048 | #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1049 | _update_visibility(); |
| 1050 | } break; |
| 1051 | |
| 1052 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 1053 | Transform3D new_xform = get_global_transform(); |
| 1054 | if (new_xform == last_transform) { |
| 1055 | break; |
| 1056 | } |
| 1057 | //update run |
| 1058 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 1059 | _octant_transform(E.key); |
| 1060 | } |
| 1061 | |
| 1062 | last_transform = new_xform; |
| 1063 | |
| 1064 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 1065 | RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform()); |
| 1066 | } |
| 1067 | } break; |
| 1068 | |
| 1069 | case NOTIFICATION_EXIT_WORLD: { |
| 1070 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 1071 | _octant_exit_world(E.key); |
| 1072 | } |
| 1073 | |
| 1074 | //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS); |
| 1075 | //_update_octants_callback(); |
| 1076 | //_update_area_instances(); |
| 1077 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 1078 | RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID()); |
| 1079 | } |
| 1080 | } break; |
| 1081 | |
| 1082 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 1083 | _update_visibility(); |
| 1084 | } break; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | void GridMap::_update_visibility() { |
| 1089 | if (!is_inside_tree()) { |
| 1090 | return; |
| 1091 | } |
| 1092 | |
| 1093 | for (KeyValue<OctantKey, Octant *> &e : octant_map) { |
| 1094 | Octant *octant = e.value; |
| 1095 | for (int i = 0; i < octant->multimesh_instances.size(); i++) { |
| 1096 | const Octant::MultimeshInstance &mi = octant->multimesh_instances[i]; |
| 1097 | RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree()); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 1102 | RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree()); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | void GridMap::_queue_octants_dirty() { |
| 1107 | if (awaiting_update) { |
| 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | callable_mp(this, &GridMap::_update_octants_callback).call_deferred(); |
| 1112 | awaiting_update = true; |
| 1113 | } |
| 1114 | |
| 1115 | void GridMap::_recreate_octant_data() { |
| 1116 | recreating_octants = true; |
| 1117 | HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map; |
| 1118 | _clear_internal(); |
| 1119 | for (const KeyValue<IndexKey, Cell> &E : cell_copy) { |
| 1120 | set_cell_item(Vector3i(E.key), E.value.item, E.value.rot); |
| 1121 | } |
| 1122 | recreating_octants = false; |
| 1123 | } |
| 1124 | |
| 1125 | void GridMap::_clear_internal() { |
| 1126 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 1127 | if (is_inside_world()) { |
| 1128 | _octant_exit_world(E.key); |
| 1129 | } |
| 1130 | |
| 1131 | _octant_clean_up(E.key); |
| 1132 | memdelete(E.value); |
| 1133 | } |
| 1134 | |
| 1135 | octant_map.clear(); |
| 1136 | cell_map.clear(); |
| 1137 | } |
| 1138 | |
| 1139 | void GridMap::clear() { |
| 1140 | _clear_internal(); |
| 1141 | clear_baked_meshes(); |
| 1142 | } |
| 1143 | |
| 1144 | #ifndef DISABLE_DEPRECATED |
| 1145 | void GridMap::resource_changed(const Ref<Resource> &p_res) { |
| 1146 | } |
| 1147 | #endif |
| 1148 | |
| 1149 | void GridMap::_update_octants_callback() { |
| 1150 | if (!awaiting_update) { |
| 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | LocalVector<OctantKey> to_delete; |
| 1155 | to_delete.reserve(octant_map.size()); |
| 1156 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 1157 | if (_octant_update(E.key)) { |
| 1158 | to_delete.push_back(E.key); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | while (!to_delete.is_empty()) { |
| 1163 | const OctantKey &octantkey = to_delete[0]; |
| 1164 | memdelete(octant_map[octantkey]); |
| 1165 | octant_map.erase(octantkey); |
| 1166 | to_delete.remove_at_unordered(0); |
| 1167 | } |
| 1168 | |
| 1169 | _update_visibility(); |
| 1170 | awaiting_update = false; |
| 1171 | } |
| 1172 | |
| 1173 | void GridMap::_bind_methods() { |
| 1174 | #ifndef PHYSICS_3D_DISABLED |
| 1175 | ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer); |
| 1176 | ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer); |
| 1177 | |
| 1178 | ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask); |
| 1179 | ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask); |
| 1180 | |
| 1181 | ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value); |
| 1182 | ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value); |
| 1183 | |
| 1184 | ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value); |
| 1185 | ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value); |
| 1186 | |
| 1187 | ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &GridMap::set_collision_priority); |
| 1188 | ClassDB::bind_method(D_METHOD("get_collision_priority"), &GridMap::get_collision_priority); |
| 1189 | |
| 1190 | ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material); |
| 1191 | ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material); |
| 1192 | #endif // PHYSICS_3D_DISABLED |
| 1193 | |
| 1194 | ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation); |
| 1195 | ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation); |
| 1196 | |
| 1197 | #ifndef NAVIGATION_3D_DISABLED |
| 1198 | ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &GridMap::set_navigation_map); |
| 1199 | ClassDB::bind_method(D_METHOD("get_navigation_map"), &GridMap::get_navigation_map); |
| 1200 | #endif // NAVIGATION_3D_DISABLED |
| 1201 | |
| 1202 | ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library); |
| 1203 | ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library); |
| 1204 | |
| 1205 | ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size); |
| 1206 | ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size); |
| 1207 | |
| 1208 | ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale); |
| 1209 | ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale); |
| 1210 | |
| 1211 | ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size); |
| 1212 | ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size); |
| 1213 | |
| 1214 | ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0)); |
| 1215 | ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item); |
| 1216 | ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation); |
| 1217 | ClassDB::bind_method(D_METHOD("get_cell_item_basis", "position"), &GridMap::get_cell_item_basis); |
| 1218 | ClassDB::bind_method(D_METHOD("get_basis_with_orthogonal_index", "index"), &GridMap::get_basis_with_orthogonal_index); |
| 1219 | ClassDB::bind_method(D_METHOD("get_orthogonal_index_from_basis", "basis"), &GridMap::get_orthogonal_index_from_basis); |
| 1220 | |
| 1221 | ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &GridMap::local_to_map); |
| 1222 | ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &GridMap::map_to_local); |
| 1223 | |
| 1224 | #ifndef DISABLE_DEPRECATED |
| 1225 | ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed); |
| 1226 | #endif |
| 1227 | |
| 1228 | ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x); |
| 1229 | ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x); |
| 1230 | ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y); |
| 1231 | ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y); |
| 1232 | ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z); |
| 1233 | ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z); |
| 1234 | |
| 1235 | ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear); |
| 1236 | |
| 1237 | ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells); |
| 1238 | ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item); |
| 1239 | |
| 1240 | ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes); |
| 1241 | ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes); |
| 1242 | ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance); |
| 1243 | |
| 1244 | ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes); |
| 1245 | ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1)); |
| 1246 | |
| 1247 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library"); |
| 1248 | #ifndef PHYSICS_3D_DISABLED |
| 1249 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material"); |
| 1250 | #endif // PHYSICS_3D_DISABLED |
| 1251 | ADD_GROUP("Cell", "cell_"); |
| 1252 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size"); |
| 1253 | ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size"); |
| 1254 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x"); |
| 1255 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y"); |
| 1256 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z"); |
| 1257 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale"); |
| 1258 | #ifndef PHYSICS_3D_DISABLED |
| 1259 | ADD_GROUP("Collision", "collision_"); |
| 1260 | ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer"); |
| 1261 | ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask"); |
| 1262 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority"); |
| 1263 | #endif // PHYSICS_3D_DISABLED |
| 1264 | ADD_GROUP("Navigation", ""); |
| 1265 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation"); |
| 1266 | |
| 1267 | BIND_CONSTANT(INVALID_CELL_ITEM); |
| 1268 | |
| 1269 | ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size"))); |
| 1270 | ADD_SIGNAL(MethodInfo(CoreStringName(changed))); |
| 1271 | } |
| 1272 | |
| 1273 | void GridMap::set_cell_scale(float p_scale) { |
| 1274 | cell_scale = p_scale; |
| 1275 | _recreate_octant_data(); |
| 1276 | } |
| 1277 | |
| 1278 | float GridMap::get_cell_scale() const { |
| 1279 | return cell_scale; |
| 1280 | } |
| 1281 | |
| 1282 | TypedArray<Vector3i> GridMap::get_used_cells() const { |
| 1283 | TypedArray<Vector3i> a; |
| 1284 | a.resize(cell_map.size()); |
| 1285 | int i = 0; |
| 1286 | for (const KeyValue<IndexKey, Cell> &E : cell_map) { |
| 1287 | Vector3i p(E.key.x, E.key.y, E.key.z); |
| 1288 | a[i++] = p; |
| 1289 | } |
| 1290 | |
| 1291 | return a; |
| 1292 | } |
| 1293 | |
| 1294 | TypedArray<Vector3i> GridMap::get_used_cells_by_item(int p_item) const { |
| 1295 | TypedArray<Vector3i> a; |
| 1296 | for (const KeyValue<IndexKey, Cell> &E : cell_map) { |
| 1297 | if ((int)E.value.item == p_item) { |
| 1298 | Vector3i p(E.key.x, E.key.y, E.key.z); |
| 1299 | a.push_back(p); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | return a; |
| 1304 | } |
| 1305 | |
| 1306 | Array GridMap::get_meshes() const { |
| 1307 | if (mesh_library.is_null()) { |
| 1308 | return Array(); |
| 1309 | } |
| 1310 | |
| 1311 | Vector3 ofs = _get_offset(); |
| 1312 | Array meshes; |
| 1313 | |
| 1314 | for (const KeyValue<IndexKey, Cell> &E : cell_map) { |
| 1315 | int id = E.value.item; |
| 1316 | if (!mesh_library->has_item(id)) { |
| 1317 | continue; |
| 1318 | } |
| 1319 | Ref<Mesh> mesh = mesh_library->get_item_mesh(id); |
| 1320 | if (mesh.is_null()) { |
| 1321 | continue; |
| 1322 | } |
| 1323 | |
| 1324 | IndexKey ik = E.key; |
| 1325 | |
| 1326 | Vector3 cellpos = Vector3(ik.x, ik.y, ik.z); |
| 1327 | |
| 1328 | Transform3D xform; |
| 1329 | |
| 1330 | xform.basis = _ortho_bases[E.value.rot]; |
| 1331 | |
| 1332 | xform.set_origin(cellpos * cell_size + ofs); |
| 1333 | xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale)); |
| 1334 | |
| 1335 | meshes.push_back(xform * mesh_library->get_item_mesh_transform(id)); |
| 1336 | meshes.push_back(mesh); |
| 1337 | } |
| 1338 | |
| 1339 | return meshes; |
| 1340 | } |
| 1341 | |
| 1342 | Vector3 GridMap::_get_offset() const { |
| 1343 | return Vector3( |
| 1344 | cell_size.x * 0.5 * int(center_x), |
| 1345 | cell_size.y * 0.5 * int(center_y), |
| 1346 | cell_size.z * 0.5 * int(center_z)); |
| 1347 | } |
| 1348 | |
| 1349 | void GridMap::clear_baked_meshes() { |
| 1350 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 1351 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 1352 | RS::get_singleton()->free_rid(baked_meshes[i].instance); |
| 1353 | } |
| 1354 | baked_meshes.clear(); |
| 1355 | |
| 1356 | _recreate_octant_data(); |
| 1357 | } |
| 1358 | |
| 1359 | void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) { |
| 1360 | if (mesh_library.is_null()) { |
| 1361 | return; |
| 1362 | } |
| 1363 | |
| 1364 | //generate |
| 1365 | HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map; |
| 1366 | |
| 1367 | for (KeyValue<IndexKey, Cell> &E : cell_map) { |
| 1368 | IndexKey key = E.key; |
| 1369 | |
| 1370 | int item = E.value.item; |
| 1371 | if (!mesh_library->has_item(item)) { |
| 1372 | continue; |
| 1373 | } |
| 1374 | |
| 1375 | Ref<Mesh> mesh = mesh_library->get_item_mesh(item); |
| 1376 | if (mesh.is_null()) { |
| 1377 | continue; |
| 1378 | } |
| 1379 | |
| 1380 | Vector3 cellpos = Vector3(key.x, key.y, key.z); |
| 1381 | Vector3 ofs = _get_offset(); |
| 1382 | |
| 1383 | Transform3D xform; |
| 1384 | |
| 1385 | xform.basis = _ortho_bases[E.value.rot]; |
| 1386 | xform.set_origin(cellpos * cell_size + ofs); |
| 1387 | xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale)); |
| 1388 | |
| 1389 | const OctantKey ok = get_octant_key_from_index_key(key); |
| 1390 | |
| 1391 | if (!surface_map.has(ok)) { |
| 1392 | surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>(); |
| 1393 | } |
| 1394 | |
| 1395 | HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok]; |
| 1396 | |
| 1397 | for (int i = 0; i < mesh->get_surface_count(); i++) { |
| 1398 | if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) { |
| 1399 | continue; |
| 1400 | } |
| 1401 | |
| 1402 | Ref<Material> surf_mat = mesh->surface_get_material(i); |
| 1403 | if (!mat_map.has(surf_mat)) { |
| 1404 | Ref<SurfaceTool> st; |
| 1405 | st.instantiate(); |
| 1406 | st->begin(Mesh::PRIMITIVE_TRIANGLES); |
| 1407 | st->set_material(surf_mat); |
| 1408 | mat_map[surf_mat] = st; |
| 1409 | } |
| 1410 | |
| 1411 | mat_map[surf_mat]->append_from(mesh, i, xform); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) { |
| 1416 | Ref<ArrayMesh> mesh; |
| 1417 | mesh.instantiate(); |
| 1418 | for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) { |
| 1419 | F.value->commit(mesh); |
| 1420 | } |
| 1421 | |
| 1422 | BakedMesh bm; |
| 1423 | bm.mesh = mesh; |
| 1424 | bm.instance = RS::get_singleton()->instance_create(); |
| 1425 | RS::get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid()); |
| 1426 | RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id()); |
| 1427 | if (is_inside_tree()) { |
| 1428 | RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario()); |
| 1429 | RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform()); |
| 1430 | } |
| 1431 | |
| 1432 | if (p_gen_lightmap_uv) { |
| 1433 | mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size); |
| 1434 | } |
| 1435 | baked_meshes.push_back(bm); |
| 1436 | } |
| 1437 | |
| 1438 | _recreate_octant_data(); |
| 1439 | } |
| 1440 | |
| 1441 | Array GridMap::get_bake_meshes() { |
| 1442 | if (!baked_meshes.size()) { |
| 1443 | make_baked_meshes(true); |
| 1444 | } |
| 1445 | |
| 1446 | Array arr; |
| 1447 | for (int i = 0; i < baked_meshes.size(); i++) { |
| 1448 | arr.push_back(baked_meshes[i].mesh); |
| 1449 | arr.push_back(Transform3D()); |
| 1450 | } |
| 1451 | |
| 1452 | return arr; |
| 1453 | } |
| 1454 | |
| 1455 | RID GridMap::get_bake_mesh_instance(int p_idx) { |
| 1456 | ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID()); |
| 1457 | return baked_meshes[p_idx].instance; |
| 1458 | } |
| 1459 | |
| 1460 | GridMap::GridMap() { |
| 1461 | set_notify_transform(true); |
| 1462 | #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1463 | NavigationServer3D::get_singleton()->connect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed)); |
| 1464 | NavigationServer3D::get_singleton()->connect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections)); |
| 1465 | #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1466 | } |
| 1467 | |
| 1468 | #ifndef NAVIGATION_3D_DISABLED |
| 1469 | void GridMap::navmesh_parse_init() { |
| 1470 | ERR_FAIL_NULL(NavigationServer3D::get_singleton()); |
| 1471 | if (!_navmesh_source_geometry_parser.is_valid()) { |
| 1472 | _navmesh_source_geometry_parsing_callback = callable_mp_static(&GridMap::navmesh_parse_source_geometry); |
| 1473 | _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create(); |
| 1474 | NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback); |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | void GridMap::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) { |
| 1479 | GridMap *gridmap = Object::cast_to<GridMap>(p_node); |
| 1480 | |
| 1481 | if (gridmap == nullptr) { |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type(); |
| 1486 | #ifndef PHYSICS_3D_DISABLED |
| 1487 | uint32_t parsed_collision_mask = p_navigation_mesh->get_collision_mask(); |
| 1488 | #endif // PHYSICS_3D_DISABLED |
| 1489 | |
| 1490 | if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) { |
| 1491 | Array meshes = gridmap->get_meshes(); |
| 1492 | Transform3D xform = gridmap->get_global_transform(); |
| 1493 | for (int i = 0; i < meshes.size(); i += 2) { |
| 1494 | Ref<Mesh> mesh = meshes[i + 1]; |
| 1495 | if (mesh.is_valid()) { |
| 1496 | p_source_geometry_data->add_mesh(mesh, xform * (Transform3D)meshes[i]); |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | #ifndef PHYSICS_3D_DISABLED |
| 1501 | else if ((parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) && (gridmap->get_collision_layer() & parsed_collision_mask)) { |
| 1502 | Array shapes = gridmap->get_collision_shapes(); |
| 1503 | for (int i = 0; i < shapes.size(); i += 2) { |
| 1504 | RID shape = shapes[i + 1]; |
| 1505 | PhysicsServer3D::ShapeType type = PhysicsServer3D::get_singleton()->shape_get_type(shape); |
| 1506 | Variant data = PhysicsServer3D::get_singleton()->shape_get_data(shape); |
| 1507 | |
| 1508 | switch (type) { |
| 1509 | case PhysicsServer3D::SHAPE_SPHERE: { |
| 1510 | real_t radius = data; |
| 1511 | Array arr; |
| 1512 | arr.resize(RS::ARRAY_MAX); |
| 1513 | SphereMesh::create_mesh_array(arr, radius, radius * 2.0); |
| 1514 | p_source_geometry_data->add_mesh_array(arr, shapes[i]); |
| 1515 | } break; |
| 1516 | case PhysicsServer3D::SHAPE_BOX: { |
| 1517 | Vector3 extents = data; |
| 1518 | Array arr; |
| 1519 | arr.resize(RS::ARRAY_MAX); |
| 1520 | BoxMesh::create_mesh_array(arr, extents * 2.0); |
| 1521 | p_source_geometry_data->add_mesh_array(arr, shapes[i]); |
| 1522 | } break; |
| 1523 | case PhysicsServer3D::SHAPE_CAPSULE: { |
| 1524 | Dictionary dict = data; |
| 1525 | real_t radius = dict["radius"]; |
| 1526 | real_t height = dict["height"]; |
| 1527 | Array arr; |
| 1528 | arr.resize(RS::ARRAY_MAX); |
| 1529 | CapsuleMesh::create_mesh_array(arr, radius, height); |
| 1530 | p_source_geometry_data->add_mesh_array(arr, shapes[i]); |
| 1531 | } break; |
| 1532 | case PhysicsServer3D::SHAPE_CYLINDER: { |
| 1533 | Dictionary dict = data; |
| 1534 | real_t radius = dict["radius"]; |
| 1535 | real_t height = dict["height"]; |
| 1536 | Array arr; |
| 1537 | arr.resize(RS::ARRAY_MAX); |
| 1538 | CylinderMesh::create_mesh_array(arr, radius, radius, height); |
| 1539 | p_source_geometry_data->add_mesh_array(arr, shapes[i]); |
| 1540 | } break; |
| 1541 | case PhysicsServer3D::SHAPE_CONVEX_POLYGON: { |
| 1542 | PackedVector3Array vertices = data; |
| 1543 | Geometry3D::MeshData md; |
| 1544 | |
| 1545 | Error err = ConvexHullComputer::convex_hull(vertices, md); |
| 1546 | |
| 1547 | if (err == OK) { |
| 1548 | PackedVector3Array faces; |
| 1549 | |
| 1550 | for (const Geometry3D::MeshData::Face &face : md.faces) { |
| 1551 | for (uint32_t k = 2; k < face.indices.size(); ++k) { |
| 1552 | faces.push_back(md.vertices[face.indices[0]]); |
| 1553 | faces.push_back(md.vertices[face.indices[k - 1]]); |
| 1554 | faces.push_back(md.vertices[face.indices[k]]); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | p_source_geometry_data->add_faces(faces, shapes[i]); |
| 1559 | } |
| 1560 | } break; |
| 1561 | case PhysicsServer3D::SHAPE_CONCAVE_POLYGON: { |
| 1562 | Dictionary dict = data; |
| 1563 | PackedVector3Array faces = Variant(dict["faces"]); |
| 1564 | p_source_geometry_data->add_faces(faces, shapes[i]); |
| 1565 | } break; |
| 1566 | case PhysicsServer3D::SHAPE_HEIGHTMAP: { |
| 1567 | Dictionary dict = data; |
| 1568 | ///< dict( int:"width", int:"depth",float:"cell_size", float_array:"heights" |
| 1569 | int heightmap_depth = dict["depth"]; |
| 1570 | int heightmap_width = dict["width"]; |
| 1571 | |
| 1572 | if (heightmap_depth >= 2 && heightmap_width >= 2) { |
| 1573 | const Vector<real_t> &map_data = dict["heights"]; |
| 1574 | |
| 1575 | Vector2 heightmap_gridsize(heightmap_width - 1, heightmap_depth - 1); |
| 1576 | Vector3 start = Vector3(heightmap_gridsize.x, 0, heightmap_gridsize.y) * -0.5; |
| 1577 | |
| 1578 | Vector<Vector3> vertex_array; |
| 1579 | vertex_array.resize((heightmap_depth - 1) * (heightmap_width - 1) * 6); |
| 1580 | Vector3 *vertex_array_ptrw = vertex_array.ptrw(); |
| 1581 | const real_t *map_data_ptr = map_data.ptr(); |
| 1582 | int vertex_index = 0; |
| 1583 | |
| 1584 | for (int d = 0; d < heightmap_depth - 1; d++) { |
| 1585 | for (int w = 0; w < heightmap_width - 1; w++) { |
| 1586 | vertex_array_ptrw[vertex_index] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + w], d); |
| 1587 | vertex_array_ptrw[vertex_index + 1] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + w + 1], d); |
| 1588 | vertex_array_ptrw[vertex_index + 2] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + heightmap_width + w], d + 1); |
| 1589 | vertex_array_ptrw[vertex_index + 3] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + w + 1], d); |
| 1590 | vertex_array_ptrw[vertex_index + 4] = start + Vector3(w + 1, map_data_ptr[(heightmap_width * d) + heightmap_width + w + 1], d + 1); |
| 1591 | vertex_array_ptrw[vertex_index + 5] = start + Vector3(w, map_data_ptr[(heightmap_width * d) + heightmap_width + w], d + 1); |
| 1592 | vertex_index += 6; |
| 1593 | } |
| 1594 | } |
| 1595 | if (vertex_array.size() > 0) { |
| 1596 | p_source_geometry_data->add_faces(vertex_array, shapes[i]); |
| 1597 | } |
| 1598 | } |
| 1599 | } break; |
| 1600 | default: { |
| 1601 | WARN_PRINT("Unsupported collision shape type."); |
| 1602 | } break; |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | #endif // PHYSICS_3D_DISABLED |
| 1607 | } |
| 1608 | #endif // NAVIGATION_3D_DISABLED |
| 1609 | |
| 1610 | #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1611 | void GridMap::_update_navigation_debug_edge_connections() { |
| 1612 | if (bake_navigation) { |
| 1613 | for (const KeyValue<OctantKey, Octant *> &E : octant_map) { |
| 1614 | _update_octant_navigation_debug_edge_connections_mesh(E.key); |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | void GridMap::_navigation_map_changed(RID p_map) { |
| 1620 | if (bake_navigation && is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) { |
| 1621 | _update_navigation_debug_edge_connections(); |
| 1622 | } |
| 1623 | } |
| 1624 | #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1625 | |
| 1626 | GridMap::~GridMap() { |
| 1627 | clear(); |
| 1628 | #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1629 | NavigationServer3D::get_singleton()->disconnect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed)); |
| 1630 | NavigationServer3D::get_singleton()->disconnect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections)); |
| 1631 | #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1632 | } |
| 1633 | |
| 1634 | #if defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |
| 1635 | void GridMap::_update_octant_navigation_debug_edge_connections_mesh(const OctantKey &p_key) { |
| 1636 | ERR_FAIL_COND(!octant_map.has(p_key)); |
| 1637 | Octant &g = *octant_map[p_key]; |
| 1638 | |
| 1639 | if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) { |
| 1640 | if (g.navigation_debug_edge_connections_instance.is_valid()) { |
| 1641 | RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false); |
| 1642 | } |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | if (!is_inside_tree()) { |
| 1647 | return; |
| 1648 | } |
| 1649 | |
| 1650 | if (!bake_navigation) { |
| 1651 | if (g.navigation_debug_edge_connections_instance.is_valid()) { |
| 1652 | RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false); |
| 1653 | } |
| 1654 | return; |
| 1655 | } |
| 1656 | |
| 1657 | if (!g.navigation_debug_edge_connections_instance.is_valid()) { |
| 1658 | g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create(); |
| 1659 | } |
| 1660 | |
| 1661 | if (g.navigation_debug_edge_connections_mesh.is_null()) { |
| 1662 | g.navigation_debug_edge_connections_mesh.instantiate(); |
| 1663 | } |
| 1664 | |
| 1665 | g.navigation_debug_edge_connections_mesh->clear_surfaces(); |
| 1666 | |
| 1667 | float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map()); |
| 1668 | float half_edge_connection_margin = edge_connection_margin * 0.5; |
| 1669 | |
| 1670 | Vector<Vector3> vertex_array; |
| 1671 | |
| 1672 | for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) { |
| 1673 | if (cell_map.has(F.key) && F.value.region.is_valid()) { |
| 1674 | int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(F.value.region); |
| 1675 | if (connections_count == 0) { |
| 1676 | continue; |
| 1677 | } |
| 1678 | |
| 1679 | for (int i = 0; i < connections_count; i++) { |
| 1680 | Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(F.value.region, i); |
| 1681 | Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(F.value.region, i); |
| 1682 | |
| 1683 | Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end); |
| 1684 | Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start); |
| 1685 | |
| 1686 | Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0)); |
| 1687 | Vector3 start_left_dir = -start_right_dir; |
| 1688 | |
| 1689 | Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0)); |
| 1690 | Vector3 end_left_dir = -end_right_dir; |
| 1691 | |
| 1692 | Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin); |
| 1693 | Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin); |
| 1694 | Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin); |
| 1695 | Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin); |
| 1696 | |
| 1697 | vertex_array.push_back(right_end_pos); |
| 1698 | vertex_array.push_back(left_start_pos); |
| 1699 | vertex_array.push_back(right_start_pos); |
| 1700 | |
| 1701 | vertex_array.push_back(left_end_pos); |
| 1702 | vertex_array.push_back(right_end_pos); |
| 1703 | vertex_array.push_back(right_start_pos); |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | if (vertex_array.is_empty()) { |
| 1709 | return; |
| 1710 | } |
| 1711 | |
| 1712 | Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material(); |
| 1713 | |
| 1714 | Array mesh_array; |
| 1715 | mesh_array.resize(Mesh::ARRAY_MAX); |
| 1716 | mesh_array[Mesh::ARRAY_VERTEX] = vertex_array; |
| 1717 | |
| 1718 | g.navigation_debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array); |
| 1719 | g.navigation_debug_edge_connections_mesh->surface_set_material(0, edge_connections_material); |
| 1720 | |
| 1721 | RS::get_singleton()->instance_set_base(g.navigation_debug_edge_connections_instance, g.navigation_debug_edge_connections_mesh->get_rid()); |
| 1722 | RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, is_visible_in_tree()); |
| 1723 | if (is_inside_tree()) { |
| 1724 | RS::get_singleton()->instance_set_scenario(g.navigation_debug_edge_connections_instance, get_world_3d()->get_scenario()); |
| 1725 | } |
| 1726 | |
| 1727 | bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections(); |
| 1728 | if (!enable_edge_connections) { |
| 1729 | RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false); |
| 1730 | } |
| 1731 | } |
| 1732 | #endif // defined(DEBUG_ENABLED) && !defined(NAVIGATION_3D_DISABLED) |