2022-03-15 13:29:32 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* navigation_mesh_instance.cpp */
|
|
|
|
/*************************************************************************/
|
2023-12-18 00:25:33 +01:00
|
|
|
/* This file is part of: */
|
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
2022-03-15 13:29:32 +01:00
|
|
|
/*************************************************************************/
|
2023-12-18 00:25:33 +01:00
|
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
2022-03-15 13:29:32 +01:00
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2023-12-18 00:25:33 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
2022-03-15 13:29:32 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "navigation_mesh_instance.h"
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
#include "core/core_string_names.h"
|
2022-07-27 18:51:18 +02:00
|
|
|
#include "core/os/os.h"
|
2022-03-15 13:29:32 +01:00
|
|
|
#include "mesh_instance.h"
|
|
|
|
#include "navigation.h"
|
2023-12-18 00:25:33 +01:00
|
|
|
#include "scene/resources/material/spatial_material.h"
|
2023-12-10 15:22:09 +01:00
|
|
|
#include "scene/resources/mesh/mesh.h"
|
2023-12-10 14:23:24 +01:00
|
|
|
#include "scene/resources/navigation/navigation_mesh.h"
|
|
|
|
#include "scene/resources/navigation/navigation_mesh_source_geometry_data_3d.h"
|
2022-08-29 17:33:10 +02:00
|
|
|
#include "scene/resources/world_3d.h"
|
2023-06-06 09:35:59 +02:00
|
|
|
#include "servers/navigation/navigation_mesh_generator.h"
|
2023-06-05 18:05:15 +02:00
|
|
|
#include "servers/navigation_server.h"
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
void NavigationMeshInstance::set_enabled(bool p_enabled) {
|
|
|
|
if (enabled == p_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
enabled = p_enabled;
|
|
|
|
|
2023-09-04 20:22:28 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_enabled(region, enabled);
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
if (!is_enabled()) {
|
|
|
|
if (debug_mesh.is_valid()) {
|
|
|
|
if (debug_mesh->get_surface_count() > 0) {
|
2023-06-09 22:03:42 +02:00
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 0, NavigationServer::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
if (debug_mesh->get_surface_count() > 1) {
|
2023-06-09 22:03:42 +02:00
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 1, NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
} else {
|
2023-06-05 18:05:15 +02:00
|
|
|
if (debug_mesh.is_valid()) {
|
|
|
|
if (debug_mesh->get_surface_count() > 0) {
|
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 0, RID());
|
|
|
|
}
|
|
|
|
if (debug_mesh->get_surface_count() > 1) {
|
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 1, RID());
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-05 18:05:15 +02:00
|
|
|
#endif // DEBUG_ENABLED
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2022-08-11 01:24:29 +02:00
|
|
|
update_gizmos();
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigationMeshInstance::is_enabled() const {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:54:05 +02:00
|
|
|
void NavigationMeshInstance::set_use_edge_connections(bool p_enabled) {
|
|
|
|
if (use_edge_connections == p_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
use_edge_connections = p_enabled;
|
|
|
|
|
2023-06-09 22:03:42 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
|
2023-06-09 20:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigationMeshInstance::get_use_edge_connections() const {
|
|
|
|
return use_edge_connections;
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
void NavigationMeshInstance::set_navigation_layers(uint32_t p_navigation_layers) {
|
|
|
|
navigation_layers = p_navigation_layers;
|
|
|
|
NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t NavigationMeshInstance::get_navigation_layers() const {
|
|
|
|
return navigation_layers;
|
|
|
|
}
|
|
|
|
|
2023-06-09 15:44:12 +02:00
|
|
|
void NavigationMeshInstance::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 NavigationMeshInstance::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));
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
void NavigationMeshInstance::set_enter_cost(real_t p_enter_cost) {
|
|
|
|
ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
|
2023-06-06 10:14:15 +02:00
|
|
|
if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
enter_cost = p_enter_cost;
|
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_enter_cost(region, p_enter_cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t NavigationMeshInstance::get_enter_cost() const {
|
|
|
|
return enter_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationMeshInstance::set_travel_cost(real_t p_travel_cost) {
|
|
|
|
ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
|
2023-06-06 10:14:15 +02:00
|
|
|
if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
travel_cost = p_travel_cost;
|
|
|
|
NavigationServer::get_singleton()->region_set_travel_cost(region, travel_cost);
|
2022-07-28 22:34:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t NavigationMeshInstance::get_travel_cost() const {
|
|
|
|
return travel_cost;
|
|
|
|
}
|
|
|
|
|
2022-04-30 15:54:12 +02:00
|
|
|
RID NavigationMeshInstance::get_region_rid() const {
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
/////////////////////////////
|
|
|
|
|
|
|
|
void NavigationMeshInstance::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
Spatial *c = this;
|
|
|
|
while (c) {
|
|
|
|
navigation = Object::cast_to<Navigation>(c);
|
|
|
|
if (navigation) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = c->get_parent_spatial();
|
|
|
|
}
|
|
|
|
|
2023-09-04 21:17:49 +02:00
|
|
|
_region_enter_navigation_map();
|
2022-03-15 13:29:32 +01:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
2023-06-09 16:43:14 +02:00
|
|
|
set_physics_process_internal(true);
|
|
|
|
} break;
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-06-09 16:43:14 +02:00
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
|
|
|
set_physics_process_internal(false);
|
2023-09-04 21:17:49 +02:00
|
|
|
|
|
|
|
_region_update_transform();
|
2022-03-15 13:29:32 +01:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2023-09-04 21:17:49 +02:00
|
|
|
_region_exit_navigation_map();
|
2023-06-05 18:05:15 +02:00
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
void NavigationMeshInstance::set_navigation_mesh(const Ref<NavigationMesh> &p_navigation_mesh) {
|
2023-09-04 17:09:39 +02:00
|
|
|
if (navigation_mesh.is_valid()) {
|
|
|
|
navigation_mesh->disconnect(CoreStringNames::get_singleton()->changed, this, "_navigation_mesh_changed");
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
navigation_mesh = p_navigation_mesh;
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
if (navigation_mesh.is_valid()) {
|
|
|
|
navigation_mesh->connect(CoreStringNames::get_singleton()->changed, this, "_navigation_mesh_changed");
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
_navigation_mesh_changed();
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<NavigationMesh> NavigationMeshInstance::get_navigation_mesh() const {
|
2023-09-04 17:09:39 +02:00
|
|
|
return navigation_mesh;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
void NavigationMeshInstance::set_navigation_map(RID p_navigation_map) {
|
|
|
|
if (map_override == p_navigation_map) {
|
|
|
|
return;
|
2022-07-28 22:34:28 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
map_override = p_navigation_map;
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_map(region, map_override);
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
RID NavigationMeshInstance::get_navigation_map() const {
|
|
|
|
if (map_override.is_valid()) {
|
|
|
|
return map_override;
|
2023-09-04 21:17:49 +02:00
|
|
|
} else if (navigation) {
|
|
|
|
return navigation->get_rid();
|
2023-06-06 09:35:59 +02:00
|
|
|
} else if (is_inside_tree()) {
|
|
|
|
return get_world_3d()->get_navigation_map();
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
2023-06-06 09:35:59 +02:00
|
|
|
return RID();
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2022-04-03 23:12:43 +02:00
|
|
|
void NavigationMeshInstance::bake_navigation_mesh(bool p_on_thread) {
|
2023-06-06 09:35:59 +02:00
|
|
|
ERR_FAIL_COND_MSG(!get_navigation_mesh().is_valid(), "Can't bake the navigation mesh if the `NavigationMesh` resource doesn't exist");
|
|
|
|
if (baking_started) {
|
|
|
|
WARN_PRINT("NavigationMesh baking already started. Wait for it to finish.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
baking_started = true;
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
navigation_mesh->clear();
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 14:36:21 +02:00
|
|
|
if (p_on_thread && OS::get_singleton()->can_use_threads()) {
|
2023-06-06 09:35:59 +02:00
|
|
|
Ref<FuncRef> f;
|
|
|
|
f.instance();
|
|
|
|
f->set_instance(this);
|
|
|
|
f->set_function("_bake_finished");
|
2022-07-27 18:51:18 +02:00
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
NavigationMeshGenerator::get_singleton()->parse_and_bake_3d(navigation_mesh, this, f);
|
2022-04-03 23:12:43 +02:00
|
|
|
} else {
|
2023-09-04 17:09:39 +02:00
|
|
|
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data = NavigationMeshGenerator::get_singleton()->parse_3d_source_geometry_data(navigation_mesh, this);
|
|
|
|
NavigationMeshGenerator::get_singleton()->bake_3d_from_source_geometry_data(navigation_mesh, source_geometry_data);
|
|
|
|
_bake_finished(navigation_mesh);
|
2022-04-03 23:12:43 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
void NavigationMeshInstance::_bake_finished(Ref<NavigationMesh> p_navigation_mesh) {
|
2023-06-06 09:35:59 +02:00
|
|
|
baking_started = false;
|
2023-09-04 17:09:39 +02:00
|
|
|
set_navigation_mesh(p_navigation_mesh);
|
2022-03-15 13:29:32 +01:00
|
|
|
emit_signal("bake_finished");
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
void NavigationMeshInstance::_navigation_mesh_changed() {
|
2023-09-04 17:09:39 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
|
2023-06-06 09:35:59 +02:00
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
update_gizmos();
|
|
|
|
update_configuration_warning();
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
emit_signal("navigation_mesh_changed");
|
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2023-06-10 20:58:49 +02:00
|
|
|
if (is_inside_tree() && NavigationServer::get_singleton()->get_debug_navigation_enabled()) {
|
2023-09-04 17:09:39 +02:00
|
|
|
if (navigation_mesh.is_valid()) {
|
2023-06-06 09:35:59 +02:00
|
|
|
_update_debug_mesh();
|
|
|
|
_update_debug_edge_connections_mesh();
|
|
|
|
} else {
|
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_instance, false);
|
|
|
|
}
|
|
|
|
if (debug_edge_connections_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 18:05:15 +02:00
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
void NavigationMeshInstance::_navigation_map_changed(RID p_map) {
|
|
|
|
if (is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) {
|
|
|
|
_update_debug_edge_connections_mesh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
String NavigationMeshInstance::get_configuration_warning() const {
|
|
|
|
if (!is_visible_in_tree() || !is_inside_tree()) {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
if (!navigation_mesh.is_valid()) {
|
2022-03-15 13:29:32 +01:00
|
|
|
return TTR("A NavigationMesh resource must be set or created for this node to work.");
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
return String();
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationMeshInstance::_bind_methods() {
|
2023-06-06 09:35:59 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationMeshInstance::set_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationMeshInstance::is_enabled);
|
|
|
|
|
2023-06-09 20:54:05 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationMeshInstance::set_use_edge_connections);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationMeshInstance::get_use_edge_connections);
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navigation_mesh"), &NavigationMeshInstance::set_navigation_mesh);
|
2022-03-15 13:29:32 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationMeshInstance::get_navigation_mesh);
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationMeshInstance::set_navigation_map);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationMeshInstance::get_navigation_map);
|
2022-04-30 15:54:12 +02:00
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationMeshInstance::set_navigation_layers);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationMeshInstance::get_navigation_layers);
|
|
|
|
|
2023-06-09 15:44:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationMeshInstance::set_navigation_layer_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationMeshInstance::get_navigation_layer_value);
|
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationMeshInstance::set_enter_cost);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationMeshInstance::get_enter_cost);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationMeshInstance::set_travel_cost);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationMeshInstance::get_travel_cost);
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationMeshInstance::get_region_rid);
|
|
|
|
|
2022-04-03 23:12:43 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationMeshInstance::bake_navigation_mesh, DEFVAL(true));
|
2023-06-06 09:35:59 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_bake_finished"), &NavigationMeshInstance::_bake_finished);
|
|
|
|
ClassDB::bind_method(D_METHOD("_navigation_mesh_changed"), &NavigationMeshInstance::_navigation_mesh_changed);
|
2023-06-05 18:05:15 +02:00
|
|
|
|
2023-06-05 18:21:45 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2023-06-05 18:05:15 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_update_debug_mesh"), &NavigationMeshInstance::_update_debug_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("_update_debug_edge_connections_mesh"), &NavigationMeshInstance::_update_debug_edge_connections_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("_navigation_map_changed"), &NavigationMeshInstance::_navigation_map_changed);
|
2023-06-05 18:21:45 +02:00
|
|
|
#endif
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
|
2022-03-15 13:29:32 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
|
2023-06-09 20:54:05 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
|
2022-07-28 22:34:28 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "enter_cost"), "set_enter_cost", "get_enter_cost");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "travel_cost"), "set_travel_cost", "get_travel_cost");
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
|
|
|
|
ADD_SIGNAL(MethodInfo("bake_finished"));
|
|
|
|
}
|
|
|
|
|
2023-09-05 09:13:54 +02:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
|
|
|
//Renamed after 4.0
|
|
|
|
bool NavigationMeshInstance::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
if (p_name == "navmesh") {
|
|
|
|
set_navigation_mesh(p_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigationMeshInstance::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
if (p_name == "navmesh") {
|
|
|
|
r_ret = get_navigation_mesh();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif // DISABLE_DEPRECATED
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
void NavigationMeshInstance::_changed_callback(Object *p_changed, const char *p_prop) {
|
2022-08-11 01:24:29 +02:00
|
|
|
update_gizmos();
|
2022-03-15 13:29:32 +01:00
|
|
|
update_configuration_warning();
|
|
|
|
}
|
|
|
|
|
|
|
|
NavigationMeshInstance::NavigationMeshInstance() {
|
|
|
|
set_notify_transform(true);
|
2022-07-28 22:34:28 +02:00
|
|
|
|
2023-06-09 16:43:14 +02:00
|
|
|
navigation = nullptr;
|
2023-06-09 20:54:05 +02:00
|
|
|
use_edge_connections = true;
|
2023-06-09 16:43:14 +02:00
|
|
|
|
2022-07-28 22:34:28 +02:00
|
|
|
enter_cost = 0.0;
|
|
|
|
travel_cost = 1.0;
|
|
|
|
|
|
|
|
navigation_layers = 1;
|
|
|
|
|
2023-06-06 09:35:59 +02:00
|
|
|
baking_started = false;
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
region = NavigationServer::get_singleton()->region_create();
|
2023-06-09 19:46:44 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_owner_id(region, get_instance_id());
|
2022-07-28 22:34:28 +02:00
|
|
|
NavigationServer::get_singleton()->region_set_enter_cost(region, get_enter_cost());
|
|
|
|
NavigationServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());
|
2022-03-15 13:29:32 +01:00
|
|
|
enabled = true;
|
2023-06-05 18:05:15 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2023-06-09 22:03:42 +02:00
|
|
|
NavigationServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed");
|
|
|
|
NavigationServer::get_singleton()->connect("navigation_debug_changed", this, "_update_debug_mesh");
|
|
|
|
NavigationServer::get_singleton()->connect("navigation_debug_changed", this, "_update_debug_edge_connections_mesh");
|
2023-06-05 18:05:15 +02:00
|
|
|
#endif // DEBUG_ENABLED
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NavigationMeshInstance::~NavigationMeshInstance() {
|
2023-09-04 17:09:39 +02:00
|
|
|
if (navigation_mesh.is_valid()) {
|
|
|
|
navigation_mesh->disconnect(CoreStringNames::get_singleton()->changed, this, "_navigation_mesh_changed");
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
2023-06-06 10:14:15 +02:00
|
|
|
|
|
|
|
ERR_FAIL_NULL(NavigationServer::get_singleton());
|
2022-03-15 13:29:32 +01:00
|
|
|
NavigationServer::get_singleton()->free(region);
|
2023-06-05 18:05:15 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2023-06-09 22:03:42 +02:00
|
|
|
NavigationServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed");
|
|
|
|
NavigationServer::get_singleton()->disconnect("navigation_debug_changed", this, "_update_debug_mesh");
|
|
|
|
NavigationServer::get_singleton()->disconnect("navigation_debug_changed", this, "_update_debug_edge_connections_mesh");
|
2023-06-05 18:05:15 +02:00
|
|
|
|
2023-06-06 10:14:15 +02:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->free(debug_instance);
|
|
|
|
}
|
|
|
|
if (debug_mesh.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->free(debug_mesh->get_rid());
|
|
|
|
}
|
|
|
|
if (debug_edge_connections_instance.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->free(debug_edge_connections_instance);
|
|
|
|
}
|
|
|
|
if (debug_edge_connections_mesh.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->free(debug_edge_connections_mesh->get_rid());
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
}
|
|
|
|
|
2023-09-04 21:17:49 +02:00
|
|
|
void NavigationMeshInstance::_region_enter_navigation_map() {
|
|
|
|
if (!is_inside_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
RID map = get_navigation_map();
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_map(region, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
current_global_transform = get_global_transform();
|
|
|
|
NavigationServer::get_singleton()->region_set_transform(region, current_global_transform);
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (NavigationServer::get_singleton()->get_debug_navigation_enabled()) {
|
|
|
|
_update_debug_mesh();
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationMeshInstance::_region_exit_navigation_map() {
|
|
|
|
NavigationServer::get_singleton()->region_set_map(region, RID());
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_instance, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_edge_connections_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationMeshInstance::_region_update_transform() {
|
|
|
|
if (!is_inside_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform new_global_transform = get_global_transform();
|
|
|
|
|
|
|
|
if (current_global_transform != new_global_transform) {
|
|
|
|
current_global_transform = new_global_transform;
|
|
|
|
|
|
|
|
NavigationServer::get_singleton()->region_set_transform(region, current_global_transform);
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
void NavigationMeshInstance::_update_debug_mesh() {
|
2023-09-03 08:45:25 +02:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
// don't update inside Editor as node 3d gizmo takes care of this
|
|
|
|
// as collisions and selections for Editor Viewport need to be updated
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-10 20:58:49 +02:00
|
|
|
if (!NavigationServer::get_singleton()->get_debug_navigation_enabled()) {
|
2023-06-05 18:05:15 +02:00
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_instance, false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
if (!navigation_mesh.is_valid()) {
|
2023-06-05 18:05:15 +02:00
|
|
|
if (debug_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_instance, false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug_instance.is_valid()) {
|
|
|
|
debug_instance = RenderingServer::get_singleton()->instance_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug_mesh.is_valid()) {
|
|
|
|
debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_mesh->clear_surfaces();
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
PoolVector<Vector3> vertices = navigation_mesh->get_vertices();
|
2023-06-05 18:05:15 +02:00
|
|
|
if (vertices.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
int polygon_count = navigation_mesh->get_polygon_count();
|
2023-06-05 18:05:15 +02:00
|
|
|
if (polygon_count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:03:30 +02:00
|
|
|
bool enabled_geometry_face_random_color = NavigationServer::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
|
|
|
|
bool enabled_edge_lines = NavigationServer::get_singleton()->get_debug_navigation_enable_edge_lines();
|
|
|
|
|
|
|
|
int vertex_count = 0;
|
|
|
|
int line_count = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < polygon_count; i++) {
|
2023-09-04 17:09:39 +02:00
|
|
|
const Vector<int> &polygon = navigation_mesh->get_polygon(i);
|
2023-06-09 13:03:30 +02:00
|
|
|
int polygon_size = polygon.size();
|
|
|
|
if (polygon_size < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
line_count += polygon_size * 2;
|
|
|
|
vertex_count += (polygon_size - 2) * 3;
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
Vector<Vector3> face_vertex_array;
|
2023-06-09 13:03:30 +02:00
|
|
|
face_vertex_array.resize(vertex_count);
|
2023-06-05 18:05:15 +02:00
|
|
|
|
|
|
|
Vector<Color> face_color_array;
|
|
|
|
if (enabled_geometry_face_random_color) {
|
2023-06-09 13:03:30 +02:00
|
|
|
face_color_array.resize(vertex_count);
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Vector3> line_vertex_array;
|
|
|
|
if (enabled_edge_lines) {
|
2023-06-09 13:03:30 +02:00
|
|
|
line_vertex_array.resize(line_count);
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Color debug_navigation_geometry_face_color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color();
|
|
|
|
|
2023-06-05 19:02:24 +02:00
|
|
|
RandomPCG rand;
|
2023-06-05 18:05:15 +02:00
|
|
|
Color polygon_color = debug_navigation_geometry_face_color;
|
|
|
|
|
2023-06-09 13:03:30 +02:00
|
|
|
int face_vertex_index = 0;
|
|
|
|
int line_vertex_index = 0;
|
|
|
|
|
|
|
|
Vector3 *face_vertex_array_ptrw = face_vertex_array.ptrw();
|
|
|
|
Color *face_color_array_ptrw = face_color_array.ptrw();
|
|
|
|
Vector3 *line_vertex_array_ptrw = line_vertex_array.ptrw();
|
|
|
|
|
|
|
|
for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
|
2023-09-04 17:09:39 +02:00
|
|
|
const Vector<int> &polygon_indices = navigation_mesh->get_polygon(polygon_index);
|
2023-06-09 13:03:30 +02:00
|
|
|
int polygon_indices_size = polygon_indices.size();
|
|
|
|
if (polygon_indices_size < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
if (enabled_geometry_face_random_color) {
|
2023-06-05 19:02:24 +02:00
|
|
|
// Generate the polygon color, slightly randomly modified from the settings one.
|
|
|
|
polygon_color.set_hsv(debug_navigation_geometry_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_navigation_geometry_face_color.get_s(), debug_navigation_geometry_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
|
|
|
|
polygon_color.a = debug_navigation_geometry_face_color.a;
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 13:03:30 +02:00
|
|
|
for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
|
|
|
|
face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
|
|
|
|
face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
|
|
|
|
face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
|
|
|
|
if (enabled_geometry_face_random_color) {
|
|
|
|
face_color_array_ptrw[face_vertex_index] = polygon_color;
|
|
|
|
face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
|
|
|
|
face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
|
|
|
|
}
|
|
|
|
face_vertex_index += 3;
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled_edge_lines) {
|
2023-06-09 13:03:30 +02:00
|
|
|
for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
|
|
|
|
line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
|
|
|
|
line_vertex_index += 1;
|
|
|
|
if (polygon_indices_index + 1 == polygon_indices_size) {
|
|
|
|
line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
|
|
|
|
line_vertex_index += 1;
|
|
|
|
} else {
|
|
|
|
line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
|
|
|
|
line_vertex_index += 1;
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 22:03:42 +02:00
|
|
|
Ref<SpatialMaterial> face_material = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_material();
|
2023-06-09 13:03:30 +02:00
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
Array face_mesh_array;
|
|
|
|
face_mesh_array.resize(Mesh::ARRAY_MAX);
|
|
|
|
face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
|
|
|
|
if (enabled_geometry_face_random_color) {
|
|
|
|
face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
|
|
|
|
}
|
|
|
|
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
|
|
|
|
debug_mesh->surface_set_material(0, face_material);
|
|
|
|
|
|
|
|
if (enabled_edge_lines) {
|
2023-06-09 22:03:42 +02:00
|
|
|
Ref<SpatialMaterial> line_material = NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_material();
|
2023-06-09 13:03:30 +02:00
|
|
|
|
2023-06-05 18:05:15 +02:00
|
|
|
Array line_mesh_array;
|
|
|
|
line_mesh_array.resize(Mesh::ARRAY_MAX);
|
|
|
|
line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
|
|
|
|
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, line_mesh_array);
|
|
|
|
debug_mesh->surface_set_material(1, line_material);
|
|
|
|
}
|
|
|
|
|
|
|
|
RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
|
2023-06-09 16:43:14 +02:00
|
|
|
RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
|
2023-06-05 18:05:15 +02:00
|
|
|
RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
|
|
|
|
}
|
|
|
|
if (!is_enabled()) {
|
|
|
|
if (debug_mesh.is_valid()) {
|
|
|
|
if (debug_mesh->get_surface_count() > 0) {
|
2023-06-09 22:03:42 +02:00
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 0, NavigationServer::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
if (debug_mesh->get_surface_count() > 1) {
|
2023-06-09 22:03:42 +02:00
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 1, NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
|
2023-06-05 18:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (debug_mesh.is_valid()) {
|
|
|
|
if (debug_mesh->get_surface_count() > 0) {
|
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 0, RID());
|
|
|
|
}
|
|
|
|
if (debug_mesh->get_surface_count() > 1) {
|
|
|
|
RS::get_singleton()->instance_set_surface_material(debug_instance, 1, RID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
void NavigationMeshInstance::_update_debug_edge_connections_mesh() {
|
2023-06-10 20:58:49 +02:00
|
|
|
if (!NavigationServer::get_singleton()->get_debug_navigation_enabled()) {
|
2023-06-05 18:05:15 +02:00
|
|
|
if (debug_edge_connections_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_inside_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:54:05 +02:00
|
|
|
if (!use_edge_connections || !NavigationServer::get_singleton()->map_get_use_edge_connections(get_world_3d()->get_navigation_map())) {
|
|
|
|
if (debug_edge_connections_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:09:39 +02:00
|
|
|
if (!navigation_mesh.is_valid()) {
|
2023-06-05 18:05:15 +02:00
|
|
|
if (debug_edge_connections_instance.is_valid()) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug_edge_connections_instance.is_valid()) {
|
|
|
|
debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug_edge_connections_mesh.is_valid()) {
|
|
|
|
debug_edge_connections_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_edge_connections_mesh->clear_surfaces();
|
|
|
|
|
|
|
|
float edge_connection_margin = NavigationServer::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
|
|
|
|
float half_edge_connection_margin = edge_connection_margin * 0.5;
|
|
|
|
int connections_count = NavigationServer::get_singleton()->region_get_connections_count(region);
|
|
|
|
|
|
|
|
if (connections_count == 0) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Vector3> vertex_array;
|
2023-06-05 18:56:41 +02:00
|
|
|
//vertex_array.resize(connections_count * 6);
|
2023-06-05 18:05:15 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < connections_count; i++) {
|
|
|
|
Vector3 connection_pathway_start = NavigationServer::get_singleton()->region_get_connection_pathway_start(region, i);
|
|
|
|
Vector3 connection_pathway_end = NavigationServer::get_singleton()->region_get_connection_pathway_end(region, i);
|
|
|
|
|
|
|
|
Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
|
|
|
|
Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
|
|
|
|
|
|
|
|
Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
|
|
|
|
Vector3 start_left_dir = -start_right_dir;
|
|
|
|
|
|
|
|
Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
|
|
|
|
Vector3 end_left_dir = -end_right_dir;
|
|
|
|
|
|
|
|
Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
|
|
|
|
Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
|
|
|
|
Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
|
|
|
|
Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
|
|
|
|
|
|
|
|
vertex_array.push_back(right_end_pos);
|
|
|
|
vertex_array.push_back(left_start_pos);
|
|
|
|
vertex_array.push_back(right_start_pos);
|
|
|
|
|
|
|
|
vertex_array.push_back(left_end_pos);
|
|
|
|
vertex_array.push_back(right_end_pos);
|
|
|
|
vertex_array.push_back(right_start_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vertex_array.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-09 22:03:42 +02:00
|
|
|
Ref<SpatialMaterial> edge_connections_material = NavigationServer::get_singleton()->get_debug_navigation_edge_connections_material();
|
2023-06-05 18:05:15 +02:00
|
|
|
|
|
|
|
Array mesh_array;
|
|
|
|
mesh_array.resize(Mesh::ARRAY_MAX);
|
|
|
|
mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
|
|
|
|
|
|
|
|
debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
|
|
|
|
debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
|
|
|
|
|
|
|
|
RS::get_singleton()->instance_set_base(debug_edge_connections_instance, debug_edge_connections_mesh->get_rid());
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, is_visible_in_tree());
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
RS::get_singleton()->instance_set_scenario(debug_edge_connections_instance, get_world_3d()->get_scenario());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool enable_edge_connections = NavigationServer::get_singleton()->get_debug_navigation_enable_edge_connections();
|
|
|
|
if (!enable_edge_connections) {
|
|
|
|
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
2023-06-05 18:05:15 +02:00
|
|
|
#endif // DEBUG_ENABLED
|