initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
env.add_source_files(env.scene_sources, "*.cpp")

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,298 @@
/**************************************************************************/
/* navigation_agent_3d.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "scene/main/node.h"
#include "servers/navigation/navigation_globals.h"
#include "servers/navigation/navigation_path_query_parameters_3d.h"
#include "servers/navigation/navigation_path_query_result_3d.h"
class Node3D;
class NavigationAgent3D : public Node {
GDCLASS(NavigationAgent3D, Node);
Node3D *agent_parent = nullptr;
RID agent;
RID map_override;
bool avoidance_enabled = false;
bool use_3d_avoidance = false;
uint32_t avoidance_layers = 1;
uint32_t avoidance_mask = 1;
real_t avoidance_priority = 1.0;
uint32_t navigation_layers = 1;
NavigationPathQueryParameters3D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
NavigationPathQueryParameters3D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
BitField<NavigationPathQueryParameters3D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
real_t path_desired_distance = 1.0;
real_t target_desired_distance = 1.0;
real_t height = NavigationDefaults3D::AVOIDANCE_AGENT_HEIGHT;
real_t radius = NavigationDefaults3D::AVOIDANCE_AGENT_RADIUS;
real_t path_height_offset = 0.0;
real_t neighbor_distance = NavigationDefaults3D::AVOIDANCE_AGENT_NEIGHBOR_DISTANCE;
int max_neighbors = NavigationDefaults3D::AVOIDANCE_AGENT_MAX_NEIGHBORS;
real_t time_horizon_agents = NavigationDefaults3D::AVOIDANCE_AGENT_TIME_HORIZON_AGENTS;
real_t time_horizon_obstacles = NavigationDefaults3D::AVOIDANCE_AGENT_TIME_HORIZON_OBSTACLES;
real_t max_speed = NavigationDefaults3D::AVOIDANCE_AGENT_MAX_SPEED;
real_t path_max_distance = 5.0;
bool simplify_path = false;
real_t simplify_epsilon = 0.0;
float path_return_max_length = 0.0;
float path_return_max_radius = 0.0;
int path_search_max_polygons = NavigationDefaults3D::path_search_max_polygons;
float path_search_max_distance = 0.0;
Vector3 target_position;
Ref<NavigationPathQueryParameters3D> navigation_query;
Ref<NavigationPathQueryResult3D> navigation_result;
int navigation_path_index = 0;
// the velocity result of the avoidance simulation step
Vector3 safe_velocity;
/// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update
// this velocity is not guaranteed, the simulation will try to fulfill it if possible
// if other agents or obstacles interfere it will be changed accordingly
Vector3 velocity;
bool velocity_submitted = false;
/// The submitted forced velocity, overrides the rvo agent velocity on the next update
// should only be used very intentionally and not every frame as it interferes with the simulation stability
Vector3 velocity_forced;
bool velocity_forced_submitted = false;
// 2D avoidance has no y-axis. This stores and reapplies the y-axis velocity to the agent before and after the avoidance step.
// While not perfect it at least looks way better than agent's that clip through everything that is not a flat surface
bool keep_y_velocity = true;
float stored_y_velocity = 0.0;
bool target_position_submitted = false;
bool target_reached = false;
bool navigation_finished = true;
bool last_waypoint_reached = false;
// Debug properties for exposed bindings
bool debug_enabled = false;
float debug_path_custom_point_size = 4.0;
bool debug_use_custom = false;
Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
#ifdef DEBUG_ENABLED
// Debug properties internal only
bool debug_path_dirty = true;
RID debug_path_instance;
Ref<ArrayMesh> debug_path_mesh;
Ref<StandardMaterial3D> debug_agent_path_line_custom_material;
Ref<StandardMaterial3D> debug_agent_path_point_custom_material;
#endif // DEBUG_ENABLED
protected:
static void _bind_methods();
void _notification(int p_what);
void _validate_property(PropertyInfo &p_property) const;
#ifndef DISABLE_DEPRECATED
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
#endif // DISABLE_DEPRECATED
public:
NavigationAgent3D();
virtual ~NavigationAgent3D();
RID get_rid() const { return agent; }
void set_avoidance_enabled(bool p_enabled);
bool get_avoidance_enabled() const;
void set_agent_parent(Node *p_agent_parent);
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;
void set_navigation_layer_value(int p_layer_number, bool p_value);
bool get_navigation_layer_value(int p_layer_number) const;
void set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm);
NavigationPathQueryParameters3D::PathfindingAlgorithm get_pathfinding_algorithm() const {
return pathfinding_algorithm;
}
void set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing);
NavigationPathQueryParameters3D::PathPostProcessing get_path_postprocessing() const {
return path_postprocessing;
}
void set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_flags);
BitField<NavigationPathQueryParameters3D::PathMetadataFlags> get_path_metadata_flags() const {
return path_metadata_flags;
}
void set_navigation_map(RID p_navigation_map);
RID get_navigation_map() const;
void set_path_desired_distance(real_t p_dd);
real_t get_path_desired_distance() const { return path_desired_distance; }
void set_target_desired_distance(real_t p_dd);
real_t get_target_desired_distance() const { return target_desired_distance; }
void set_radius(real_t p_radius);
real_t get_radius() const { return radius; }
void set_height(real_t p_height);
real_t get_height() const { return height; }
void set_path_height_offset(real_t p_path_height_offset);
real_t get_path_height_offset() const { return path_height_offset; }
void set_use_3d_avoidance(bool p_use_3d_avoidance);
bool get_use_3d_avoidance() const { return use_3d_avoidance; }
void set_keep_y_velocity(bool p_enabled);
bool get_keep_y_velocity() const;
void set_neighbor_distance(real_t p_distance);
real_t get_neighbor_distance() const { return neighbor_distance; }
void set_max_neighbors(int p_count);
int get_max_neighbors() const { return max_neighbors; }
void set_time_horizon_agents(real_t p_time_horizon);
real_t get_time_horizon_agents() const { return time_horizon_agents; }
void set_time_horizon_obstacles(real_t p_time_horizon);
real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
void set_max_speed(real_t p_max_speed);
real_t get_max_speed() const { return max_speed; }
void set_path_max_distance(real_t p_pmd);
real_t get_path_max_distance();
void set_target_position(Vector3 p_position);
Vector3 get_target_position() const;
void set_simplify_path(bool p_enabled);
bool get_simplify_path() const;
void set_simplify_epsilon(real_t p_epsilon);
real_t get_simplify_epsilon() const;
void set_path_return_max_length(float p_length);
float get_path_return_max_length() const;
void set_path_return_max_radius(float p_radius);
float get_path_return_max_radius() const;
void set_path_search_max_polygons(int p_max_polygons);
int get_path_search_max_polygons() const;
void set_path_search_max_distance(float p_distance);
float get_path_search_max_distance() const;
float get_path_length() const;
Vector3 get_next_path_position();
Ref<NavigationPathQueryResult3D> get_current_navigation_result() const { return navigation_result; }
const Vector<Vector3> &get_current_navigation_path() const { return navigation_result->get_path(); }
int get_current_navigation_path_index() const { return navigation_path_index; }
real_t distance_to_target() const;
bool is_target_reached() const;
bool is_target_reachable();
bool is_navigation_finished();
Vector3 get_final_position();
void set_velocity(const Vector3 p_velocity);
Vector3 get_velocity() { return velocity; }
void set_velocity_forced(const Vector3 p_velocity);
void _avoidance_done(Vector3 p_new_velocity);
PackedStringArray get_configuration_warnings() const override;
void set_avoidance_layers(uint32_t p_layers);
uint32_t get_avoidance_layers() const;
void set_avoidance_mask(uint32_t p_mask);
uint32_t get_avoidance_mask() const;
void set_avoidance_layer_value(int p_layer_number, bool p_value);
bool get_avoidance_layer_value(int p_layer_number) const;
void set_avoidance_mask_value(int p_mask_number, bool p_value);
bool get_avoidance_mask_value(int p_mask_number) const;
void set_avoidance_priority(real_t p_priority);
real_t get_avoidance_priority() const;
void set_debug_enabled(bool p_enabled);
bool get_debug_enabled() const;
void set_debug_use_custom(bool p_enabled);
bool get_debug_use_custom() const;
void set_debug_path_custom_color(Color p_color);
Color get_debug_path_custom_color() const;
void set_debug_path_custom_point_size(float p_point_size);
float get_debug_path_custom_point_size() const;
private:
bool _is_target_reachable() const;
Vector3 _get_final_position() const;
void _update_navigation();
void _advance_waypoints(const Vector3 &p_origin);
void _request_repath();
bool _is_last_waypoint() const;
void _move_to_next_waypoint();
bool _is_within_waypoint_distance(const Vector3 &p_origin) const;
bool _is_within_target_distance(const Vector3 &p_origin) const;
void _trigger_waypoint_reached();
void _transition_to_navigation_finished();
void _transition_to_target_reached();
#ifdef DEBUG_ENABLED
void _navigation_debug_changed();
void _update_debug_path();
#endif // DEBUG_ENABLED
};

View File

@@ -0,0 +1,552 @@
/**************************************************************************/
/* navigation_link_3d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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_link_3d.h"
#include "servers/navigation_server_3d.h"
#ifdef DEBUG_ENABLED
void NavigationLink3D::_update_debug_mesh() {
if (!is_inside_tree()) {
return;
}
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;
}
if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
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_null()) {
debug_mesh.instantiate();
}
RID nav_map = get_world_3d()->get_navigation_map();
real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
Vector3::Axis up_axis = up_vector.max_axis_index();
debug_mesh->clear_surfaces();
Vector<Vector3> lines;
// Draw line between the points.
lines.push_back(start_position);
lines.push_back(end_position);
// Draw start position search radius
for (int i = 0; i < 30; i++) {
// Create a circle
const float ra = Math::deg_to_rad((float)(i * 12));
const float rb = Math::deg_to_rad((float)((i + 1) * 12));
const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
// Draw axis-aligned circle
switch (up_axis) {
case Vector3::AXIS_X:
lines.append(start_position + Vector3(0, a.x, a.y));
lines.append(start_position + Vector3(0, b.x, b.y));
break;
case Vector3::AXIS_Y:
lines.append(start_position + Vector3(a.x, 0, a.y));
lines.append(start_position + Vector3(b.x, 0, b.y));
break;
case Vector3::AXIS_Z:
lines.append(start_position + Vector3(a.x, a.y, 0));
lines.append(start_position + Vector3(b.x, b.y, 0));
break;
}
}
// Draw end position search radius
for (int i = 0; i < 30; i++) {
// Create a circle
const float ra = Math::deg_to_rad((float)(i * 12));
const float rb = Math::deg_to_rad((float)((i + 1) * 12));
const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
// Draw axis-aligned circle
switch (up_axis) {
case Vector3::AXIS_X:
lines.append(end_position + Vector3(0, a.x, a.y));
lines.append(end_position + Vector3(0, b.x, b.y));
break;
case Vector3::AXIS_Y:
lines.append(end_position + Vector3(a.x, 0, a.y));
lines.append(end_position + Vector3(b.x, 0, b.y));
break;
case Vector3::AXIS_Z:
lines.append(end_position + Vector3(a.x, a.y, 0));
lines.append(end_position + Vector3(b.x, b.y, 0));
break;
}
}
const Vector3 link_segment = end_position - start_position;
const Vector3 up = Vector3(0.0, 1.0, 0.0);
const float arror_len = 0.5;
{
Vector3 anchor = start_position + (link_segment * 0.75);
Vector3 direction = start_position.direction_to(end_position);
Vector3 arrow_dir = direction.cross(up);
lines.push_back(anchor);
lines.push_back(anchor + (arrow_dir - direction) * arror_len);
arrow_dir = -direction.cross(up);
lines.push_back(anchor);
lines.push_back(anchor + (arrow_dir - direction) * arror_len);
}
if (is_bidirectional()) {
Vector3 anchor = start_position + (link_segment * 0.25);
Vector3 direction = end_position.direction_to(start_position);
Vector3 arrow_dir = direction.cross(up);
lines.push_back(anchor);
lines.push_back(anchor + (arrow_dir - direction) * arror_len);
arrow_dir = -direction.cross(up);
lines.push_back(anchor);
lines.push_back(anchor + (arrow_dir - direction) * arror_len);
}
Array mesh_array;
mesh_array.resize(Mesh::ARRAY_MAX);
mesh_array[Mesh::ARRAY_VERTEX] = lines;
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array);
RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
if (enabled) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
} else {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
}
RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
}
#endif // DEBUG_ENABLED
void NavigationLink3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rid"), &NavigationLink3D::get_rid);
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationLink3D::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationLink3D::is_enabled);
ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationLink3D::set_navigation_map);
ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationLink3D::get_navigation_map);
ClassDB::bind_method(D_METHOD("set_bidirectional", "bidirectional"), &NavigationLink3D::set_bidirectional);
ClassDB::bind_method(D_METHOD("is_bidirectional"), &NavigationLink3D::is_bidirectional);
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationLink3D::set_navigation_layers);
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationLink3D::get_navigation_layers);
ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationLink3D::set_navigation_layer_value);
ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationLink3D::get_navigation_layer_value);
ClassDB::bind_method(D_METHOD("set_start_position", "position"), &NavigationLink3D::set_start_position);
ClassDB::bind_method(D_METHOD("get_start_position"), &NavigationLink3D::get_start_position);
ClassDB::bind_method(D_METHOD("set_end_position", "position"), &NavigationLink3D::set_end_position);
ClassDB::bind_method(D_METHOD("get_end_position"), &NavigationLink3D::get_end_position);
ClassDB::bind_method(D_METHOD("set_global_start_position", "position"), &NavigationLink3D::set_global_start_position);
ClassDB::bind_method(D_METHOD("get_global_start_position"), &NavigationLink3D::get_global_start_position);
ClassDB::bind_method(D_METHOD("set_global_end_position", "position"), &NavigationLink3D::set_global_end_position);
ClassDB::bind_method(D_METHOD("get_global_end_position"), &NavigationLink3D::get_global_end_position);
ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationLink3D::set_enter_cost);
ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationLink3D::get_enter_cost);
ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationLink3D::set_travel_cost);
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationLink3D::get_travel_cost);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bidirectional"), "set_bidirectional", "is_bidirectional");
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "start_position"), "set_start_position", "get_start_position");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "end_position"), "set_end_position", "get_end_position");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
}
#ifndef DISABLE_DEPRECATED
bool NavigationLink3D::_set(const StringName &p_name, const Variant &p_value) {
if (p_name == "start_location") {
set_start_position(p_value);
return true;
}
if (p_name == "end_location") {
set_end_position(p_value);
return true;
}
return false;
}
bool NavigationLink3D::_get(const StringName &p_name, Variant &r_ret) const {
if (p_name == "start_location") {
r_ret = get_start_position();
return true;
}
if (p_name == "end_location") {
r_ret = get_end_position();
return true;
}
return false;
}
#endif // DISABLE_DEPRECATED
void NavigationLink3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_link_enter_navigation_map();
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
set_physics_process_internal(true);
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
set_physics_process_internal(false);
_link_update_transform();
} break;
case NOTIFICATION_EXIT_TREE: {
_link_exit_navigation_map();
} break;
#ifdef DEBUG_ENABLED
case NOTIFICATION_VISIBILITY_CHANGED: {
_update_debug_mesh();
} break;
#endif // DEBUG_ENABLED
}
}
NavigationLink3D::NavigationLink3D() {
link = NavigationServer3D::get_singleton()->link_create();
NavigationServer3D::get_singleton()->link_set_owner_id(link, get_instance_id());
NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
set_notify_transform(true);
}
NavigationLink3D::~NavigationLink3D() {
ERR_FAIL_NULL(NavigationServer3D::get_singleton());
NavigationServer3D::get_singleton()->free(link);
link = RID();
#ifdef DEBUG_ENABLED
ERR_FAIL_NULL(RenderingServer::get_singleton());
if (debug_instance.is_valid()) {
RenderingServer::get_singleton()->free(debug_instance);
}
if (debug_mesh.is_valid()) {
RenderingServer::get_singleton()->free(debug_mesh->get_rid());
}
#endif // DEBUG_ENABLED
}
RID NavigationLink3D::get_rid() const {
return link;
}
void NavigationLink3D::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;
NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid() && debug_mesh.is_valid()) {
if (enabled) {
Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
} else {
Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
}
}
#endif // DEBUG_ENABLED
update_gizmos();
}
void NavigationLink3D::set_navigation_map(RID p_navigation_map) {
if (map_override == p_navigation_map) {
return;
}
map_override = p_navigation_map;
NavigationServer3D::get_singleton()->link_set_map(link, map_override);
}
RID NavigationLink3D::get_navigation_map() const {
if (map_override.is_valid()) {
return map_override;
} else if (is_inside_tree()) {
return get_world_3d()->get_navigation_map();
}
return RID();
}
void NavigationLink3D::set_bidirectional(bool p_bidirectional) {
if (bidirectional == p_bidirectional) {
return;
}
bidirectional = p_bidirectional;
NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
#ifdef DEBUG_ENABLED
_update_debug_mesh();
#endif // DEBUG_ENABLED
update_gizmos();
}
void NavigationLink3D::set_navigation_layers(uint32_t p_navigation_layers) {
if (navigation_layers == p_navigation_layers) {
return;
}
navigation_layers = p_navigation_layers;
NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
}
void NavigationLink3D::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 NavigationLink3D::get_navigation_layer_value(int p_layer_number) const {
ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
return get_navigation_layers() & (1 << (p_layer_number - 1));
}
void NavigationLink3D::set_start_position(Vector3 p_position) {
if (start_position.is_equal_approx(p_position)) {
return;
}
start_position = p_position;
if (!is_inside_tree()) {
return;
}
NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
#ifdef DEBUG_ENABLED
_update_debug_mesh();
#endif // DEBUG_ENABLED
update_gizmos();
update_configuration_warnings();
}
void NavigationLink3D::set_end_position(Vector3 p_position) {
if (end_position.is_equal_approx(p_position)) {
return;
}
end_position = p_position;
if (!is_inside_tree()) {
return;
}
NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
#ifdef DEBUG_ENABLED
_update_debug_mesh();
#endif // DEBUG_ENABLED
update_gizmos();
update_configuration_warnings();
}
void NavigationLink3D::set_global_start_position(Vector3 p_position) {
if (is_inside_tree()) {
set_start_position(to_local(p_position));
} else {
set_start_position(p_position);
}
}
Vector3 NavigationLink3D::get_global_start_position() const {
if (is_inside_tree()) {
return to_global(start_position);
} else {
return start_position;
}
}
void NavigationLink3D::set_global_end_position(Vector3 p_position) {
if (is_inside_tree()) {
set_end_position(to_local(p_position));
} else {
set_end_position(p_position);
}
}
Vector3 NavigationLink3D::get_global_end_position() const {
if (is_inside_tree()) {
return to_global(end_position);
} else {
return end_position;
}
}
void NavigationLink3D::set_enter_cost(real_t p_enter_cost) {
ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
return;
}
enter_cost = p_enter_cost;
NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
}
void NavigationLink3D::set_travel_cost(real_t p_travel_cost) {
ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
return;
}
travel_cost = p_travel_cost;
NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
}
PackedStringArray NavigationLink3D::get_configuration_warnings() const {
PackedStringArray warnings = Node3D::get_configuration_warnings();
if (start_position.is_equal_approx(end_position)) {
warnings.push_back(RTR("NavigationLink3D start position should be different than the end position to be useful."));
}
return warnings;
}
void NavigationLink3D::_link_enter_navigation_map() {
if (!is_inside_tree()) {
return;
}
if (map_override.is_valid()) {
NavigationServer3D::get_singleton()->link_set_map(link, map_override);
} else {
NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
}
current_global_transform = get_global_transform();
NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
#ifdef DEBUG_ENABLED
if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
_update_debug_mesh();
}
#endif // DEBUG_ENABLED
}
void NavigationLink3D::_link_exit_navigation_map() {
NavigationServer3D::get_singleton()->link_set_map(link, RID());
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_instance, false);
}
#endif // DEBUG_ENABLED
}
void NavigationLink3D::_link_update_transform() {
if (!is_inside_tree()) {
return;
}
Transform3D new_global_transform = get_global_transform();
if (current_global_transform != new_global_transform) {
current_global_transform = new_global_transform;
NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
#ifdef DEBUG_ENABLED
if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
_update_debug_mesh();
}
#endif // DEBUG_ENABLED
}
}

View File

@@ -0,0 +1,111 @@
/**************************************************************************/
/* navigation_link_3d.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "scene/3d/node_3d.h"
class NavigationLink3D : public Node3D {
GDCLASS(NavigationLink3D, Node3D);
bool enabled = true;
RID link;
RID map_override;
bool bidirectional = true;
uint32_t navigation_layers = 1;
Vector3 end_position;
Vector3 start_position;
real_t enter_cost = 0.0;
real_t travel_cost = 1.0;
Transform3D current_global_transform;
#ifdef DEBUG_ENABLED
RID debug_instance;
Ref<ArrayMesh> debug_mesh;
void _update_debug_mesh();
#endif // DEBUG_ENABLED
protected:
static void _bind_methods();
void _notification(int p_what);
#ifndef DISABLE_DEPRECATED
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
#endif // DISABLE_DEPRECATED
public:
NavigationLink3D();
~NavigationLink3D();
RID get_rid() const;
void set_enabled(bool p_enabled);
bool is_enabled() const { return enabled; }
void set_navigation_map(RID p_navigation_map);
RID get_navigation_map() const;
void set_bidirectional(bool p_bidirectional);
bool is_bidirectional() const { return bidirectional; }
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const { return navigation_layers; }
void set_navigation_layer_value(int p_layer_number, bool p_value);
bool get_navigation_layer_value(int p_layer_number) const;
void set_start_position(Vector3 p_position);
Vector3 get_start_position() const { return start_position; }
void set_end_position(Vector3 p_position);
Vector3 get_end_position() const { return end_position; }
void set_global_start_position(Vector3 p_position);
Vector3 get_global_start_position() const;
void set_global_end_position(Vector3 p_position);
Vector3 get_global_end_position() const;
void set_enter_cost(real_t p_enter_cost);
real_t get_enter_cost() const { return enter_cost; }
void set_travel_cost(real_t p_travel_cost);
real_t get_travel_cost() const { return travel_cost; }
PackedStringArray get_configuration_warnings() const override;
private:
void _link_enter_navigation_map();
void _link_exit_navigation_map();
void _link_update_transform();
};

View File

@@ -0,0 +1,723 @@
/**************************************************************************/
/* navigation_obstacle_3d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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_obstacle_3d.h"
#include "core/math/geometry_2d.h"
#include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
#include "scene/resources/navigation_mesh.h"
#include "servers/navigation_server_3d.h"
Callable NavigationObstacle3D::_navmesh_source_geometry_parsing_callback;
RID NavigationObstacle3D::_navmesh_source_geometry_parser;
void NavigationObstacle3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rid"), &NavigationObstacle3D::get_rid);
ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationObstacle3D::set_avoidance_enabled);
ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationObstacle3D::get_avoidance_enabled);
ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationObstacle3D::set_navigation_map);
ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationObstacle3D::get_navigation_map);
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationObstacle3D::set_radius);
ClassDB::bind_method(D_METHOD("get_radius"), &NavigationObstacle3D::get_radius);
ClassDB::bind_method(D_METHOD("set_height", "height"), &NavigationObstacle3D::set_height);
ClassDB::bind_method(D_METHOD("get_height"), &NavigationObstacle3D::get_height);
ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationObstacle3D::set_velocity);
ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationObstacle3D::get_velocity);
ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationObstacle3D::set_vertices);
ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationObstacle3D::get_vertices);
ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationObstacle3D::set_avoidance_layers);
ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationObstacle3D::get_avoidance_layers);
ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationObstacle3D::set_avoidance_layer_value);
ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationObstacle3D::get_avoidance_layer_value);
ClassDB::bind_method(D_METHOD("set_use_3d_avoidance", "enabled"), &NavigationObstacle3D::set_use_3d_avoidance);
ClassDB::bind_method(D_METHOD("get_use_3d_avoidance"), &NavigationObstacle3D::get_use_3d_avoidance);
ClassDB::bind_method(D_METHOD("set_affect_navigation_mesh", "enabled"), &NavigationObstacle3D::set_affect_navigation_mesh);
ClassDB::bind_method(D_METHOD("get_affect_navigation_mesh"), &NavigationObstacle3D::get_affect_navigation_mesh);
ClassDB::bind_method(D_METHOD("set_carve_navigation_mesh", "enabled"), &NavigationObstacle3D::set_carve_navigation_mesh);
ClassDB::bind_method(D_METHOD("get_carve_navigation_mesh"), &NavigationObstacle3D::get_carve_navigation_mesh);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.0,100,0.01,suffix:m"), "set_radius", "get_radius");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.0,100,0.01,suffix:m"), "set_height", "get_height");
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices"), "set_vertices", "get_vertices");
ADD_GROUP("NavigationMesh", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "affect_navigation_mesh"), "set_affect_navigation_mesh", "get_affect_navigation_mesh");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "carve_navigation_mesh"), "set_carve_navigation_mesh", "get_carve_navigation_mesh");
ADD_GROUP("Avoidance", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance");
}
void NavigationObstacle3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POST_ENTER_TREE: {
if (map_override.is_valid()) {
_update_map(map_override);
} else if (is_inside_tree()) {
_update_map(get_world_3d()->get_navigation_map());
} else {
_update_map(RID());
}
// need to trigger map controlled agent assignment somehow for the fake_agent since obstacles use no callback like regular agents
NavigationServer3D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
_update_transform();
set_physics_process_internal(true);
#ifdef DEBUG_ENABLED
_update_debug();
#endif // DEBUG_ENABLED
} break;
#ifdef TOOLS_ENABLED
case NOTIFICATION_TRANSFORM_CHANGED: {
update_gizmos();
} break;
#endif // TOOLS_ENABLED
case NOTIFICATION_EXIT_TREE: {
set_physics_process_internal(false);
_update_map(RID());
#ifdef DEBUG_ENABLED
_clear_debug();
#endif // DEBUG_ENABLED
} break;
case NOTIFICATION_SUSPENDED:
case NOTIFICATION_PAUSED: {
if (!can_process()) {
map_before_pause = map_current;
_update_map(RID());
} else if (can_process() && !(map_before_pause == RID())) {
_update_map(map_before_pause);
map_before_pause = RID();
}
NavigationServer3D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
} break;
case NOTIFICATION_UNSUSPENDED: {
if (get_tree()->is_paused()) {
break;
}
[[fallthrough]];
}
case NOTIFICATION_UNPAUSED: {
if (!can_process()) {
map_before_pause = map_current;
_update_map(RID());
} else if (can_process() && !(map_before_pause == RID())) {
_update_map(map_before_pause);
map_before_pause = RID();
}
NavigationServer3D::get_singleton()->obstacle_set_paused(obstacle, !can_process());
} break;
#ifdef DEBUG_ENABLED
case NOTIFICATION_VISIBILITY_CHANGED: {
_update_debug();
} break;
#endif // DEBUG_ENABLED
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (is_inside_tree()) {
_update_transform();
if (velocity_submitted) {
velocity_submitted = false;
// only update if there is a noticeable change, else the rvo agent preferred velocity stays the same
if (!previous_velocity.is_equal_approx(velocity)) {
NavigationServer3D::get_singleton()->obstacle_set_velocity(obstacle, velocity);
}
previous_velocity = velocity;
}
#ifdef DEBUG_ENABLED
if (fake_agent_radius_debug_instance_rid.is_valid() && radius > 0.0) {
// Prevent non-positive scaling.
const Vector3 safe_scale = get_global_basis().get_scale().abs().maxf(0.001);
// Agent radius is a scalar value and does not support non-uniform scaling, choose the largest axis.
const float scaling_max_value = safe_scale[safe_scale.max_axis_index()];
const Vector3 uniform_max_scale = Vector3(scaling_max_value, scaling_max_value, scaling_max_value);
const Transform3D debug_transform = Transform3D(Basis().scaled(uniform_max_scale), get_global_position());
RS::get_singleton()->instance_set_transform(fake_agent_radius_debug_instance_rid, debug_transform);
}
if (static_obstacle_debug_instance_rid.is_valid() && get_vertices().size() > 0) {
// Prevent non-positive scaling.
const Vector3 safe_scale = get_global_basis().get_scale().abs().maxf(0.001);
// Obstacles are projected to the xz-plane, so only rotation around the y-axis can be taken into account.
const Transform3D debug_transform = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), get_global_rotation().y), get_global_position());
RS::get_singleton()->instance_set_transform(static_obstacle_debug_instance_rid, debug_transform);
}
#endif // DEBUG_ENABLED
}
} break;
}
}
NavigationObstacle3D::NavigationObstacle3D() {
NavigationServer3D *ns3d = NavigationServer3D::get_singleton();
obstacle = ns3d->obstacle_create();
ns3d->obstacle_set_height(obstacle, height);
ns3d->obstacle_set_radius(obstacle, radius);
ns3d->obstacle_set_vertices(obstacle, vertices);
ns3d->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
ns3d->obstacle_set_use_3d_avoidance(obstacle, use_3d_avoidance);
ns3d->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
#ifdef DEBUG_ENABLED
RenderingServer *rs = RenderingServer::get_singleton();
fake_agent_radius_debug_mesh_rid = rs->mesh_create();
static_obstacle_debug_mesh_rid = rs->mesh_create();
fake_agent_radius_debug_instance_rid = rs->instance_create();
static_obstacle_debug_instance_rid = rs->instance_create();
rs->instance_set_base(fake_agent_radius_debug_instance_rid, fake_agent_radius_debug_mesh_rid);
rs->instance_set_base(static_obstacle_debug_instance_rid, static_obstacle_debug_mesh_rid);
ns3d->connect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_fake_agent_radius_debug));
ns3d->connect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_static_obstacle_debug));
_update_fake_agent_radius_debug();
_update_static_obstacle_debug();
#endif // DEBUG_ENABLED
#ifdef TOOLS_ENABLED
set_notify_transform(true);
#endif // TOOLS_ENABLED
}
NavigationObstacle3D::~NavigationObstacle3D() {
NavigationServer3D *ns3d = NavigationServer3D::get_singleton();
ERR_FAIL_NULL(ns3d);
ns3d->free(obstacle);
obstacle = RID();
#ifdef DEBUG_ENABLED
ns3d->disconnect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_fake_agent_radius_debug));
ns3d->disconnect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_static_obstacle_debug));
RenderingServer *rs = RenderingServer::get_singleton();
ERR_FAIL_NULL(rs);
if (fake_agent_radius_debug_instance_rid.is_valid()) {
rs->free(fake_agent_radius_debug_instance_rid);
fake_agent_radius_debug_instance_rid = RID();
}
if (fake_agent_radius_debug_mesh_rid.is_valid()) {
rs->free(fake_agent_radius_debug_mesh_rid);
fake_agent_radius_debug_mesh_rid = RID();
}
if (static_obstacle_debug_instance_rid.is_valid()) {
rs->free(static_obstacle_debug_instance_rid);
static_obstacle_debug_instance_rid = RID();
}
if (static_obstacle_debug_mesh_rid.is_valid()) {
rs->free(static_obstacle_debug_mesh_rid);
static_obstacle_debug_mesh_rid = RID();
}
#endif // DEBUG_ENABLED
}
void NavigationObstacle3D::set_vertices(const Vector<Vector3> &p_vertices) {
vertices = p_vertices;
Vector<Vector2> vertices_2d;
vertices_2d.resize(vertices.size());
const Vector3 *vertices_ptr = vertices.ptr();
Vector2 *vertices_2d_ptrw = vertices_2d.ptrw();
for (int i = 0; i < vertices.size(); i++) {
vertices_2d_ptrw[i] = Vector2(vertices_ptr[i].x, vertices_ptr[i].z);
}
vertices_are_clockwise = !Geometry2D::is_polygon_clockwise(vertices_2d); // Geometry2D is inverted.
vertices_are_valid = !Geometry2D::triangulate_polygon(vertices_2d).is_empty();
const Basis basis = is_inside_tree() ? get_global_basis() : get_basis();
const float rotation_y = is_inside_tree() ? get_global_rotation().y : get_rotation().y;
const Vector3 safe_scale = basis.get_scale().abs().maxf(0.001);
const Transform3D safe_transform = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), rotation_y), Vector3());
NavigationServer3D::get_singleton()->obstacle_set_vertices(obstacle, safe_transform.xform(vertices));
#ifdef DEBUG_ENABLED
_update_static_obstacle_debug();
update_gizmos();
#endif // DEBUG_ENABLED
}
void NavigationObstacle3D::set_navigation_map(RID p_navigation_map) {
if (map_override == p_navigation_map) {
return;
}
map_override = p_navigation_map;
_update_map(map_override);
}
RID NavigationObstacle3D::get_navigation_map() const {
if (map_override.is_valid()) {
return map_override;
} else if (is_inside_tree()) {
return get_world_3d()->get_navigation_map();
}
return RID();
}
void NavigationObstacle3D::set_radius(real_t p_radius) {
ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
if (Math::is_equal_approx(radius, p_radius)) {
return;
}
radius = p_radius;
// Prevent non-positive or non-uniform scaling of dynamic obstacle radius.
const Vector3 safe_scale = (is_inside_tree() ? get_global_basis() : get_basis()).get_scale().abs().maxf(0.001);
NavigationServer3D::get_singleton()->obstacle_set_radius(obstacle, safe_scale[safe_scale.max_axis_index()] * radius);
#ifdef DEBUG_ENABLED
_update_fake_agent_radius_debug();
update_gizmos();
#endif // DEBUG_ENABLED
}
void NavigationObstacle3D::set_height(real_t p_height) {
ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive.");
if (Math::is_equal_approx(height, p_height)) {
return;
}
height = p_height;
const float scale_factor = MAX(Math::abs((is_inside_tree() ? get_global_basis() : get_basis()).get_scale().y), 0.001);
NavigationServer3D::get_singleton()->obstacle_set_height(obstacle, scale_factor * height);
#ifdef DEBUG_ENABLED
_update_static_obstacle_debug();
update_gizmos();
#endif // DEBUG_ENABLED
}
void NavigationObstacle3D::set_avoidance_layers(uint32_t p_layers) {
avoidance_layers = p_layers;
NavigationServer3D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers);
}
uint32_t NavigationObstacle3D::get_avoidance_layers() const {
return avoidance_layers;
}
void NavigationObstacle3D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
uint32_t avoidance_layers_new = get_avoidance_layers();
if (p_value) {
avoidance_layers_new |= 1 << (p_layer_number - 1);
} else {
avoidance_layers_new &= ~(1 << (p_layer_number - 1));
}
set_avoidance_layers(avoidance_layers_new);
}
bool NavigationObstacle3D::get_avoidance_layer_value(int p_layer_number) const {
ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
return get_avoidance_layers() & (1 << (p_layer_number - 1));
}
void NavigationObstacle3D::set_avoidance_enabled(bool p_enabled) {
if (avoidance_enabled == p_enabled) {
return;
}
avoidance_enabled = p_enabled;
NavigationServer3D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled);
}
bool NavigationObstacle3D::get_avoidance_enabled() const {
return avoidance_enabled;
}
void NavigationObstacle3D::set_use_3d_avoidance(bool p_use_3d_avoidance) {
use_3d_avoidance = p_use_3d_avoidance;
_update_use_3d_avoidance(use_3d_avoidance);
notify_property_list_changed();
}
void NavigationObstacle3D::set_velocity(const Vector3 p_velocity) {
velocity = p_velocity;
velocity_submitted = true;
}
void NavigationObstacle3D::set_affect_navigation_mesh(bool p_enabled) {
affect_navigation_mesh = p_enabled;
}
bool NavigationObstacle3D::get_affect_navigation_mesh() const {
return affect_navigation_mesh;
}
void NavigationObstacle3D::set_carve_navigation_mesh(bool p_enabled) {
carve_navigation_mesh = p_enabled;
}
bool NavigationObstacle3D::get_carve_navigation_mesh() const {
return carve_navigation_mesh;
}
PackedStringArray NavigationObstacle3D::get_configuration_warnings() const {
PackedStringArray warnings = Node3D::get_configuration_warnings();
if (get_global_rotation().x != 0.0 || get_global_rotation().z != 0.0) {
warnings.push_back(RTR("NavigationObstacle3D only takes global rotation around the y-axis into account. Rotations around the x-axis or z-axis might lead to unexpected results."));
}
const Vector3 global_scale = get_global_basis().get_scale();
if (global_scale.x < 0.001 || global_scale.y < 0.001 || global_scale.z < 0.001) {
warnings.push_back(RTR("NavigationObstacle3D does not support negative or zero scaling."));
}
if (radius > 0.0 && !get_global_basis().is_conformal()) {
warnings.push_back(RTR("The agent radius can only be scaled uniformly. The largest scale value along the three axes will be used."));
}
return warnings;
}
void NavigationObstacle3D::navmesh_parse_init() {
ERR_FAIL_NULL(NavigationServer3D::get_singleton());
if (!_navmesh_source_geometry_parser.is_valid()) {
_navmesh_source_geometry_parsing_callback = callable_mp_static(&NavigationObstacle3D::navmesh_parse_source_geometry);
_navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
}
}
void NavigationObstacle3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
NavigationObstacle3D *obstacle = Object::cast_to<NavigationObstacle3D>(p_node);
if (obstacle == nullptr) {
return;
}
if (!obstacle->get_affect_navigation_mesh()) {
return;
}
const float elevation = obstacle->get_global_position().y + p_source_geometry_data->root_node_transform.origin.y;
// Prevent non-positive scaling.
const Vector3 safe_scale = obstacle->get_global_basis().get_scale().abs().maxf(0.001);
const float obstacle_radius = obstacle->get_radius();
if (obstacle_radius > 0.0) {
// Radius defined obstacle should be uniformly scaled from obstacle basis max scale axis.
const float scaling_max_value = safe_scale[safe_scale.max_axis_index()];
const Vector3 uniform_max_scale = Vector3(scaling_max_value, scaling_max_value, scaling_max_value);
const Transform3D obstacle_circle_transform = p_source_geometry_data->root_node_transform * Transform3D(Basis().scaled(uniform_max_scale), obstacle->get_global_position());
Vector<Vector3> obstruction_circle_vertices;
// The point of this is that the moving obstacle can make a simple hole in the navigation mesh and affect the pathfinding.
// Without, navigation paths can go directly through the middle of the obstacle and conflict with the avoidance to get agents stuck.
// No place for excessive "round" detail here. Every additional edge adds a high cost for something that needs to be quick, not pretty.
static const int circle_points = 12;
obstruction_circle_vertices.resize(circle_points);
Vector3 *circle_vertices_ptrw = obstruction_circle_vertices.ptrw();
const real_t circle_point_step = Math::TAU / circle_points;
for (int i = 0; i < circle_points; i++) {
const float angle = i * circle_point_step;
circle_vertices_ptrw[i] = obstacle_circle_transform.xform(Vector3(Math::cos(angle) * obstacle_radius, 0.0, Math::sin(angle) * obstacle_radius));
}
p_source_geometry_data->add_projected_obstruction(obstruction_circle_vertices, elevation - obstacle_radius, scaling_max_value * obstacle_radius, obstacle->get_carve_navigation_mesh());
}
// Obstacles are projected to the xz-plane, so only rotation around the y-axis can be taken into account.
const Transform3D node_xform = p_source_geometry_data->root_node_transform * Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle->get_global_rotation().y), obstacle->get_global_position());
const Vector<Vector3> &obstacle_vertices = obstacle->get_vertices();
if (obstacle_vertices.is_empty()) {
return;
}
Vector<Vector3> obstruction_shape_vertices;
obstruction_shape_vertices.resize(obstacle_vertices.size());
const Vector3 *obstacle_vertices_ptr = obstacle_vertices.ptr();
Vector3 *obstruction_shape_vertices_ptrw = obstruction_shape_vertices.ptrw();
for (int i = 0; i < obstacle_vertices.size(); i++) {
obstruction_shape_vertices_ptrw[i] = node_xform.xform(obstacle_vertices_ptr[i]);
obstruction_shape_vertices_ptrw[i].y = 0.0;
}
p_source_geometry_data->add_projected_obstruction(obstruction_shape_vertices, elevation, safe_scale.y * obstacle->get_height(), obstacle->get_carve_navigation_mesh());
}
void NavigationObstacle3D::_update_map(RID p_map) {
NavigationServer3D::get_singleton()->obstacle_set_map(obstacle, p_map);
map_current = p_map;
}
void NavigationObstacle3D::_update_position(const Vector3 p_position) {
NavigationServer3D::get_singleton()->obstacle_set_position(obstacle, p_position);
}
void NavigationObstacle3D::_update_transform() {
_update_position(get_global_position());
// Prevent non-positive or non-uniform scaling of dynamic obstacle radius.
const Vector3 safe_scale = get_global_basis().get_scale().abs().maxf(0.001);
const float scaling_max_value = safe_scale[safe_scale.max_axis_index()];
NavigationServer3D::get_singleton()->obstacle_set_radius(obstacle, scaling_max_value * radius);
// Apply modified node transform which only takes y-axis rotation into account to vertices.
const Transform3D safe_transform = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), get_global_rotation().y), Vector3());
NavigationServer3D::get_singleton()->obstacle_set_vertices(obstacle, safe_transform.xform(vertices));
NavigationServer3D::get_singleton()->obstacle_set_height(obstacle, safe_scale.y * height);
}
void NavigationObstacle3D::_update_use_3d_avoidance(bool p_use_3d_avoidance) {
NavigationServer3D::get_singleton()->obstacle_set_use_3d_avoidance(obstacle, use_3d_avoidance);
_update_map(map_current);
}
#ifdef DEBUG_ENABLED
void NavigationObstacle3D::_update_debug() {
RenderingServer *rs = RenderingServer::get_singleton();
if (is_inside_tree()) {
rs->instance_set_visible(fake_agent_radius_debug_instance_rid, is_visible_in_tree());
rs->instance_set_visible(static_obstacle_debug_instance_rid, is_visible_in_tree());
rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, get_world_3d()->get_scenario());
rs->instance_set_scenario(static_obstacle_debug_instance_rid, get_world_3d()->get_scenario());
rs->instance_set_transform(fake_agent_radius_debug_instance_rid, Transform3D(Basis(), get_global_position()));
rs->instance_set_transform(static_obstacle_debug_instance_rid, Transform3D(Basis(), get_global_position()));
_update_fake_agent_radius_debug();
_update_static_obstacle_debug();
} else {
rs->mesh_clear(fake_agent_radius_debug_mesh_rid);
rs->mesh_clear(static_obstacle_debug_mesh_rid);
rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, RID());
rs->instance_set_scenario(static_obstacle_debug_instance_rid, RID());
}
}
void NavigationObstacle3D::_update_fake_agent_radius_debug() {
NavigationServer3D *ns3d = NavigationServer3D::get_singleton();
RenderingServer *rs = RenderingServer::get_singleton();
bool is_debug_enabled = false;
if (Engine::get_singleton()->is_editor_hint()) {
is_debug_enabled = true;
} else if (ns3d->get_debug_enabled() &&
ns3d->get_debug_avoidance_enabled() &&
ns3d->get_debug_navigation_avoidance_enable_obstacles_radius()) {
is_debug_enabled = true;
}
rs->mesh_clear(fake_agent_radius_debug_mesh_rid);
if (!is_debug_enabled) {
return;
}
Vector<Vector3> face_vertex_array;
Vector<int> face_indices_array;
int i, j, prevrow, thisrow, point;
float x, y, z;
int rings = 16;
int radial_segments = 32;
point = 0;
thisrow = 0;
prevrow = 0;
for (j = 0; j <= (rings + 1); j++) {
float v = j;
float w;
v /= (rings + 1);
w = std::sin(Math::PI * v);
y = (radius)*std::cos(Math::PI * v);
for (i = 0; i <= radial_segments; i++) {
float u = i;
u /= radial_segments;
x = std::sin(u * Math::TAU);
z = std::cos(u * Math::TAU);
Vector3 p = Vector3(x * radius * w, y, z * radius * w);
face_vertex_array.push_back(p);
point++;
if (i > 0 && j > 0) {
face_indices_array.push_back(prevrow + i - 1);
face_indices_array.push_back(prevrow + i);
face_indices_array.push_back(thisrow + i - 1);
face_indices_array.push_back(prevrow + i);
face_indices_array.push_back(thisrow + i);
face_indices_array.push_back(thisrow + i - 1);
};
};
prevrow = thisrow;
thisrow = point;
};
Array face_mesh_array;
face_mesh_array.resize(Mesh::ARRAY_MAX);
face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
face_mesh_array[Mesh::ARRAY_INDEX] = face_indices_array;
rs->mesh_add_surface_from_arrays(fake_agent_radius_debug_mesh_rid, RS::PRIMITIVE_TRIANGLES, face_mesh_array);
Ref<StandardMaterial3D> face_material = ns3d->get_debug_navigation_avoidance_obstacles_radius_material();
rs->instance_set_surface_override_material(fake_agent_radius_debug_instance_rid, 0, face_material->get_rid());
if (is_inside_tree()) {
rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, get_world_3d()->get_scenario());
rs->instance_set_visible(fake_agent_radius_debug_instance_rid, is_visible_in_tree());
}
}
#endif // DEBUG_ENABLED
#ifdef DEBUG_ENABLED
void NavigationObstacle3D::_update_static_obstacle_debug() {
if (Engine::get_singleton()->is_editor_hint()) {
// Don't update inside Editor as Node3D gizmo takes care of this.
return;
}
NavigationServer3D *ns3d = NavigationServer3D::get_singleton();
RenderingServer *rs = RenderingServer::get_singleton();
bool is_debug_enabled = false;
if (ns3d->get_debug_enabled() &&
ns3d->get_debug_avoidance_enabled() &&
ns3d->get_debug_navigation_avoidance_enable_obstacles_static()) {
is_debug_enabled = true;
}
rs->mesh_clear(static_obstacle_debug_mesh_rid);
if (!is_debug_enabled) {
return;
}
const int vertex_count = vertices.size();
if (vertex_count < 3) {
if (static_obstacle_debug_instance_rid.is_valid()) {
rs->instance_set_visible(static_obstacle_debug_instance_rid, false);
}
return;
}
Vector<Vector3> edge_vertex_array;
edge_vertex_array.resize(vertex_count * 8);
Vector3 *edge_vertex_array_ptrw = edge_vertex_array.ptrw();
int vertex_index = 0;
for (int i = 0; i < vertex_count; i++) {
Vector3 point = vertices[i];
Vector3 next_point = vertices[(i + 1) % vertex_count];
Vector3 direction = next_point.direction_to(point);
Vector3 arrow_dir = direction.cross(Vector3(0.0, 1.0, 0.0));
Vector3 edge_middle = point + ((next_point - point) * 0.5);
edge_vertex_array_ptrw[vertex_index++] = edge_middle;
edge_vertex_array_ptrw[vertex_index++] = edge_middle + (arrow_dir * 0.5);
edge_vertex_array_ptrw[vertex_index++] = point;
edge_vertex_array_ptrw[vertex_index++] = next_point;
edge_vertex_array_ptrw[vertex_index++] = Vector3(point.x, height, point.z);
edge_vertex_array_ptrw[vertex_index++] = Vector3(next_point.x, height, next_point.z);
edge_vertex_array_ptrw[vertex_index++] = point;
edge_vertex_array_ptrw[vertex_index++] = Vector3(point.x, height, point.z);
}
Array edge_mesh_array;
edge_mesh_array.resize(Mesh::ARRAY_MAX);
edge_mesh_array[Mesh::ARRAY_VERTEX] = edge_vertex_array;
rs->mesh_add_surface_from_arrays(static_obstacle_debug_mesh_rid, RS::PRIMITIVE_LINES, edge_mesh_array);
Ref<StandardMaterial3D> edge_material;
if (are_vertices_valid()) {
edge_material = ns3d->get_debug_navigation_avoidance_static_obstacle_pushout_edge_material();
} else {
edge_material = ns3d->get_debug_navigation_avoidance_static_obstacle_pushin_edge_material();
}
rs->instance_set_surface_override_material(static_obstacle_debug_instance_rid, 0, edge_material->get_rid());
if (is_inside_tree()) {
rs->instance_set_scenario(static_obstacle_debug_instance_rid, get_world_3d()->get_scenario());
rs->instance_set_visible(static_obstacle_debug_instance_rid, is_visible_in_tree());
}
}
#endif // DEBUG_ENABLED
#ifdef DEBUG_ENABLED
void NavigationObstacle3D::_clear_debug() {
RenderingServer *rs = RenderingServer::get_singleton();
ERR_FAIL_NULL(rs);
rs->mesh_clear(fake_agent_radius_debug_mesh_rid);
rs->mesh_clear(static_obstacle_debug_mesh_rid);
rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, RID());
rs->instance_set_scenario(static_obstacle_debug_instance_rid, RID());
}
#endif // DEBUG_ENABLED

View File

@@ -0,0 +1,142 @@
/**************************************************************************/
/* navigation_obstacle_3d.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "scene/3d/node_3d.h"
class NavigationMesh;
class NavigationMeshSourceGeometryData3D;
class NavigationObstacle3D : public Node3D {
GDCLASS(NavigationObstacle3D, Node3D);
RID obstacle;
RID map_before_pause;
RID map_override;
RID map_current;
real_t height = 1.0;
real_t radius = 0.0;
Vector<Vector3> vertices;
bool vertices_are_clockwise = true;
bool vertices_are_valid = true;
bool avoidance_enabled = true;
uint32_t avoidance_layers = 1;
bool use_3d_avoidance = false;
Vector3 velocity;
Vector3 previous_velocity;
bool velocity_submitted = false;
bool affect_navigation_mesh = false;
bool carve_navigation_mesh = false;
#ifdef DEBUG_ENABLED
RID fake_agent_radius_debug_instance_rid;
RID fake_agent_radius_debug_mesh_rid;
RID static_obstacle_debug_instance_rid;
RID static_obstacle_debug_mesh_rid;
private:
void _update_debug();
void _update_fake_agent_radius_debug();
void _update_static_obstacle_debug();
void _clear_debug();
#endif // DEBUG_ENABLED
protected:
static void _bind_methods();
void _notification(int p_what);
public:
NavigationObstacle3D();
virtual ~NavigationObstacle3D();
RID get_rid() const { return obstacle; }
void set_avoidance_enabled(bool p_enabled);
bool get_avoidance_enabled() const;
void set_navigation_map(RID p_navigation_map);
RID get_navigation_map() const;
void set_radius(real_t p_radius);
real_t get_radius() const { return radius; }
void set_height(real_t p_height);
real_t get_height() const { return height; }
void set_vertices(const Vector<Vector3> &p_vertices);
const Vector<Vector3> &get_vertices() const { return vertices; }
bool are_vertices_clockwise() const { return vertices_are_clockwise; }
bool are_vertices_valid() const { return vertices_are_valid; }
void set_avoidance_layers(uint32_t p_layers);
uint32_t get_avoidance_layers() const;
void set_avoidance_layer_value(int p_layer_number, bool p_value);
bool get_avoidance_layer_value(int p_layer_number) const;
void set_use_3d_avoidance(bool p_use_3d_avoidance);
bool get_use_3d_avoidance() const { return use_3d_avoidance; }
void set_velocity(const Vector3 p_velocity);
Vector3 get_velocity() const { return velocity; }
void _avoidance_done(Vector3 p_new_velocity); // Dummy
void set_affect_navigation_mesh(bool p_enabled);
bool get_affect_navigation_mesh() const;
void set_carve_navigation_mesh(bool p_enabled);
bool get_carve_navigation_mesh() const;
PackedStringArray get_configuration_warnings() const override;
private:
static Callable _navmesh_source_geometry_parsing_callback;
static RID _navmesh_source_geometry_parser;
public:
static void navmesh_parse_init();
static void navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
private:
void _update_map(RID p_map);
void _update_position(const Vector3 p_position);
void _update_transform();
void _update_use_3d_avoidance(bool p_use_3d_avoidance);
};

View File

@@ -0,0 +1,766 @@
/**************************************************************************/
/* navigation_region_3d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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_region_3d.h"
#include "core/math/random_pcg.h"
#include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
#include "servers/navigation_server_3d.h"
RID NavigationRegion3D::get_rid() const {
return region;
}
void NavigationRegion3D::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;
NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
if (!is_enabled()) {
if (debug_mesh.is_valid()) {
if (debug_mesh->get_surface_count() > 0) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
}
if (debug_mesh->get_surface_count() > 1) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
}
}
} else {
if (debug_mesh.is_valid()) {
if (debug_mesh->get_surface_count() > 0) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID());
}
if (debug_mesh->get_surface_count() > 1) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID());
}
}
}
}
#endif // DEBUG_ENABLED
update_gizmos();
}
bool NavigationRegion3D::is_enabled() const {
return enabled;
}
void NavigationRegion3D::set_use_edge_connections(bool p_enabled) {
if (use_edge_connections == p_enabled) {
return;
}
use_edge_connections = p_enabled;
NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
}
bool NavigationRegion3D::get_use_edge_connections() const {
return use_edge_connections;
}
void NavigationRegion3D::set_navigation_layers(uint32_t p_navigation_layers) {
if (navigation_layers == p_navigation_layers) {
return;
}
navigation_layers = p_navigation_layers;
NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
}
uint32_t NavigationRegion3D::get_navigation_layers() const {
return navigation_layers;
}
void NavigationRegion3D::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 NavigationRegion3D::get_navigation_layer_value(int p_layer_number) const {
ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
return get_navigation_layers() & (1 << (p_layer_number - 1));
}
void NavigationRegion3D::set_enter_cost(real_t p_enter_cost) {
ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
return;
}
enter_cost = p_enter_cost;
NavigationServer3D::get_singleton()->region_set_enter_cost(region, enter_cost);
}
real_t NavigationRegion3D::get_enter_cost() const {
return enter_cost;
}
void NavigationRegion3D::set_travel_cost(real_t p_travel_cost) {
ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
return;
}
travel_cost = p_travel_cost;
NavigationServer3D::get_singleton()->region_set_travel_cost(region, travel_cost);
}
real_t NavigationRegion3D::get_travel_cost() const {
return travel_cost;
}
RID NavigationRegion3D::get_region_rid() const {
return get_rid();
}
void NavigationRegion3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_region_enter_navigation_map();
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
set_physics_process_internal(true);
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
set_physics_process_internal(false);
_region_update_transform();
} break;
case NOTIFICATION_EXIT_TREE: {
_region_exit_navigation_map();
} break;
}
}
void NavigationRegion3D::set_navigation_mesh(const Ref<NavigationMesh> &p_navigation_mesh) {
if (navigation_mesh.is_valid()) {
navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
}
navigation_mesh = p_navigation_mesh;
if (navigation_mesh.is_valid()) {
navigation_mesh->connect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
}
_navigation_mesh_changed();
}
Ref<NavigationMesh> NavigationRegion3D::get_navigation_mesh() const {
return navigation_mesh;
}
void NavigationRegion3D::set_navigation_map(RID p_navigation_map) {
if (map_override == p_navigation_map) {
return;
}
map_override = p_navigation_map;
NavigationServer3D::get_singleton()->region_set_map(region, map_override);
}
RID NavigationRegion3D::get_navigation_map() const {
if (map_override.is_valid()) {
return map_override;
} else if (is_inside_tree()) {
return get_world_3d()->get_navigation_map();
}
return RID();
}
void NavigationRegion3D::bake_navigation_mesh(bool p_on_thread) {
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");
ERR_FAIL_COND_MSG(navigation_mesh.is_null(), "Baking the navigation mesh requires a valid `NavigationMesh` resource.");
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
source_geometry_data.instantiate();
NavigationServer3D::get_singleton()->parse_source_geometry_data(navigation_mesh, source_geometry_data, this);
if (p_on_thread) {
NavigationServer3D::get_singleton()->bake_from_source_geometry_data_async(navigation_mesh, source_geometry_data, callable_mp(this, &NavigationRegion3D::_bake_finished));
} else {
NavigationServer3D::get_singleton()->bake_from_source_geometry_data(navigation_mesh, source_geometry_data, callable_mp(this, &NavigationRegion3D::_bake_finished));
}
}
void NavigationRegion3D::_bake_finished() {
if (!Thread::is_main_thread()) {
callable_mp(this, &NavigationRegion3D::_bake_finished).call_deferred();
return;
}
emit_signal(SNAME("bake_finished"));
}
bool NavigationRegion3D::is_baking() const {
return NavigationServer3D::get_singleton()->is_baking_navigation_mesh(navigation_mesh);
}
PackedStringArray NavigationRegion3D::get_configuration_warnings() const {
PackedStringArray warnings = Node3D::get_configuration_warnings();
if (is_visible_in_tree() && is_inside_tree()) {
if (navigation_mesh.is_null()) {
warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work."));
}
}
return warnings;
}
void NavigationRegion3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion3D::get_rid);
ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navigation_mesh"), &NavigationRegion3D::set_navigation_mesh);
ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationRegion3D::get_navigation_mesh);
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion3D::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion3D::is_enabled);
ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion3D::set_navigation_map);
ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion3D::get_navigation_map);
ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion3D::set_use_edge_connections);
ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion3D::get_use_edge_connections);
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion3D::set_navigation_layers);
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion3D::get_navigation_layers);
ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion3D::set_navigation_layer_value);
ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion3D::get_navigation_layer_value);
ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion3D::get_region_rid);
ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion3D::set_enter_cost);
ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion3D::get_enter_cost);
ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion3D::set_travel_cost);
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion3D::get_travel_cost);
ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationRegion3D::bake_navigation_mesh, DEFVAL(true));
ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion3D::is_baking);
ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationRegion3D::get_bounds);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
ADD_SIGNAL(MethodInfo("bake_finished"));
}
#ifndef DISABLE_DEPRECATED
// Compatibility with earlier 4.0 betas.
bool NavigationRegion3D::_set(const StringName &p_name, const Variant &p_value) {
if (p_name == "navmesh") {
set_navigation_mesh(p_value);
return true;
}
return false;
}
bool NavigationRegion3D::_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
void NavigationRegion3D::_navigation_mesh_changed() {
_update_bounds();
NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
#ifdef DEBUG_ENABLED
if (is_inside_tree() && NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
if (navigation_mesh.is_valid()) {
_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);
}
}
}
_update_debug_edge_connections_mesh();
#endif // DEBUG_ENABLED
emit_signal(SNAME("navigation_mesh_changed"));
update_gizmos();
update_configuration_warnings();
}
#ifdef DEBUG_ENABLED
void NavigationRegion3D::_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
#ifdef DEBUG_ENABLED
void NavigationRegion3D::_navigation_debug_changed() {
if (is_inside_tree()) {
_update_debug_mesh();
_update_debug_edge_connections_mesh();
}
}
#endif // DEBUG_ENABLED
void NavigationRegion3D::_region_enter_navigation_map() {
if (!is_inside_tree()) {
return;
}
if (map_override.is_valid()) {
NavigationServer3D::get_singleton()->region_set_map(region, map_override);
} else {
NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
}
current_global_transform = get_global_transform();
NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
#ifdef DEBUG_ENABLED
if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
_update_debug_mesh();
}
#endif // DEBUG_ENABLED
}
void NavigationRegion3D::_region_exit_navigation_map() {
NavigationServer3D::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 NavigationRegion3D::_region_update_transform() {
if (!is_inside_tree()) {
return;
}
Transform3D new_global_transform = get_global_transform();
if (current_global_transform != new_global_transform) {
current_global_transform = new_global_transform;
NavigationServer3D::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
}
}
NavigationRegion3D::NavigationRegion3D() {
set_notify_transform(true);
region = NavigationServer3D::get_singleton()->region_create();
NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
NavigationServer3D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
NavigationServer3D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
#ifdef DEBUG_ENABLED
NavigationServer3D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_navigation_debug_changed));
#endif // DEBUG_ENABLED
}
NavigationRegion3D::~NavigationRegion3D() {
if (navigation_mesh.is_valid()) {
navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
}
ERR_FAIL_NULL(NavigationServer3D::get_singleton());
NavigationServer3D::get_singleton()->free(region);
#ifdef DEBUG_ENABLED
NavigationServer3D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_navigation_debug_changed));
ERR_FAIL_NULL(RenderingServer::get_singleton());
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
}
#ifdef DEBUG_ENABLED
void NavigationRegion3D::_update_debug_mesh() {
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;
}
if (!NavigationServer3D::get_singleton()->get_debug_enabled() || !NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
if (debug_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_instance, false);
}
return;
}
if (navigation_mesh.is_null()) {
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_null()) {
debug_mesh.instantiate();
}
debug_mesh->clear_surfaces();
Vector<Vector3> vertices = navigation_mesh->get_vertices();
if (vertices.is_empty()) {
return;
}
int polygon_count = navigation_mesh->get_polygon_count();
if (polygon_count == 0) {
return;
}
bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
int vertex_count = 0;
int line_count = 0;
for (int i = 0; i < polygon_count; i++) {
const Vector<int> &polygon = navigation_mesh->get_polygon(i);
int polygon_size = polygon.size();
if (polygon_size < 3) {
continue;
}
line_count += polygon_size * 2;
vertex_count += (polygon_size - 2) * 3;
}
Vector<Vector3> face_vertex_array;
face_vertex_array.resize(vertex_count);
Vector<Color> face_color_array;
if (enabled_geometry_face_random_color) {
face_color_array.resize(vertex_count);
}
Vector<Vector3> line_vertex_array;
if (enabled_edge_lines) {
line_vertex_array.resize(line_count);
}
Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
RandomPCG rand;
Color polygon_color = debug_navigation_geometry_face_color;
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++) {
const Vector<int> &polygon_indices = navigation_mesh->get_polygon(polygon_index);
int polygon_indices_size = polygon_indices.size();
if (polygon_indices_size < 3) {
continue;
}
if (enabled_geometry_face_random_color) {
// 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;
}
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;
}
if (enabled_edge_lines) {
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;
}
}
}
}
Ref<StandardMaterial3D> face_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_material();
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) {
Ref<StandardMaterial3D> line_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_material();
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());
RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
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) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
}
if (debug_mesh->get_surface_count() > 1) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
}
}
} else {
if (debug_mesh.is_valid()) {
if (debug_mesh->get_surface_count() > 0) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID());
}
if (debug_mesh->get_surface_count() > 1) {
RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID());
}
}
}
}
#endif // DEBUG_ENABLED
#ifdef DEBUG_ENABLED
void NavigationRegion3D::_update_debug_edge_connections_mesh() {
if (!NavigationServer3D::get_singleton()->get_debug_enabled() || !NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
if (debug_edge_connections_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
}
return;
}
if (!is_inside_tree()) {
return;
}
if (!use_edge_connections || !NavigationServer3D::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;
}
if (navigation_mesh.is_null()) {
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_null()) {
debug_edge_connections_mesh.instantiate();
}
debug_edge_connections_mesh->clear_surfaces();
float edge_connection_margin = NavigationServer3D::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 = NavigationServer3D::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;
vertex_array.resize(connections_count * 6);
Vector3 *vertex_array_ptrw = vertex_array.ptrw();
int vertex_array_index = 0;
for (int i = 0; i < connections_count; i++) {
Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(region, i);
Vector3 connection_pathway_end = NavigationServer3D::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_ptrw[vertex_array_index++] = connection_pathway_start;
vertex_array_ptrw[vertex_array_index++] = connection_pathway_end;
vertex_array_ptrw[vertex_array_index++] = left_start_pos;
vertex_array_ptrw[vertex_array_index++] = right_start_pos;
vertex_array_ptrw[vertex_array_index++] = left_end_pos;
vertex_array_ptrw[vertex_array_index++] = right_end_pos;
}
if (vertex_array.is_empty()) {
return;
}
Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material();
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_LINES, 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 = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
if (!enable_edge_connections) {
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
}
}
#endif // DEBUG_ENABLED
void NavigationRegion3D::_update_bounds() {
if (navigation_mesh.is_null()) {
bounds = AABB();
return;
}
const Vector<Vector3> &vertices = navigation_mesh->get_vertices();
if (vertices.is_empty()) {
bounds = AABB();
return;
}
const Transform3D gt = is_inside_tree() ? get_global_transform() : get_transform();
AABB new_bounds;
new_bounds.position = gt.xform(vertices[0]);
for (const Vector3 &vertex : vertices) {
new_bounds.expand_to(gt.xform(vertex));
}
bounds = new_bounds;
}

View File

@@ -0,0 +1,124 @@
/**************************************************************************/
/* navigation_region_3d.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#pragma once
#include "scene/3d/node_3d.h"
#include "scene/resources/navigation_mesh.h"
class NavigationRegion3D : public Node3D {
GDCLASS(NavigationRegion3D, Node3D);
bool enabled = true;
bool use_edge_connections = true;
RID region;
RID map_override;
uint32_t navigation_layers = 1;
real_t enter_cost = 0.0;
real_t travel_cost = 1.0;
Ref<NavigationMesh> navigation_mesh;
Transform3D current_global_transform;
void _navigation_mesh_changed();
AABB bounds;
#ifdef DEBUG_ENABLED
RID debug_instance;
RID debug_edge_connections_instance;
Ref<ArrayMesh> debug_mesh;
Ref<ArrayMesh> debug_edge_connections_mesh;
private:
void _update_debug_mesh();
void _update_debug_edge_connections_mesh();
void _navigation_map_changed(RID p_map);
void _navigation_debug_changed();
#endif // DEBUG_ENABLED
protected:
void _notification(int p_what);
static void _bind_methods();
#ifndef DISABLE_DEPRECATED
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
#endif // DISABLE_DEPRECATED
public:
RID get_rid() const;
void set_enabled(bool p_enabled);
bool is_enabled() const;
void set_navigation_map(RID p_navigation_map);
RID get_navigation_map() const;
void set_use_edge_connections(bool p_enabled);
bool get_use_edge_connections() const;
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;
void set_navigation_layer_value(int p_layer_number, bool p_value);
bool get_navigation_layer_value(int p_layer_number) const;
RID get_region_rid() const;
void set_enter_cost(real_t p_enter_cost);
real_t get_enter_cost() const;
void set_travel_cost(real_t p_travel_cost);
real_t get_travel_cost() const;
void set_navigation_mesh(const Ref<NavigationMesh> &p_navigation_mesh);
Ref<NavigationMesh> get_navigation_mesh() const;
/// Bakes the navigation mesh; once done, automatically
/// sets the new navigation mesh and emits a signal
void bake_navigation_mesh(bool p_on_thread);
void _bake_finished();
bool is_baking() const;
PackedStringArray get_configuration_warnings() const override;
AABB get_bounds() const { return bounds; }
NavigationRegion3D();
~NavigationRegion3D();
private:
void _update_bounds();
void _region_enter_navigation_map();
void _region_exit_navigation_map();
void _region_update_transform();
};