Ported from godot4: Fix NavigationRegion3D gizmo's odd visual behavior .

- DarkKilauea
92c40bcf32
This commit is contained in:
Relintai 2023-06-05 19:02:24 +02:00
parent 25078b1437
commit 3c9d9bdad4
3 changed files with 17 additions and 25 deletions

View File

@ -3542,6 +3542,11 @@ void CollisionPolygonSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
//// ////
NavigationMeshSpatialGizmoPlugin::NavigationMeshSpatialGizmoPlugin() { NavigationMeshSpatialGizmoPlugin::NavigationMeshSpatialGizmoPlugin() {
create_material("face_material", NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color(), false, false, true);
create_material("face_material_disabled", NavigationServer::get_singleton()->get_debug_navigation_geometry_face_disabled_color(), false, false, true);
create_material("edge_material", NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_color());
create_material("edge_material_disabled", NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_disabled_color());
Color baking_aabb_material_color = Color(0.8, 0.5, 0.7); Color baking_aabb_material_color = Color(0.8, 0.5, 0.7);
baking_aabb_material_color.a = 0.1; baking_aabb_material_color.a = 0.1;
create_material("baking_aabb_material", baking_aabb_material_color); create_material("baking_aabb_material", baking_aabb_material_color);
@ -3641,6 +3646,7 @@ void NavigationMeshSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
tmesh->create(tmeshfaces); tmesh->create(tmeshfaces);
p_gizmo->add_collision_triangles(tmesh); p_gizmo->add_collision_triangles(tmesh);
p_gizmo->add_collision_segments(lines);
Ref<ArrayMesh> debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); Ref<ArrayMesh> debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
int polygon_count = navigationmesh->get_polygon_count(); int polygon_count = navigationmesh->get_polygon_count();
@ -3662,6 +3668,7 @@ void NavigationMeshSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array; face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
// if enabled add vertex colors to colorize each face individually // if enabled add vertex colors to colorize each face individually
RandomPCG rand;
bool enabled_geometry_face_random_color = NavigationServer::get_singleton()->get_debug_navigation_enable_geometry_face_random_color(); bool enabled_geometry_face_random_color = NavigationServer::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
if (enabled_geometry_face_random_color) { if (enabled_geometry_face_random_color) {
Color debug_navigation_geometry_face_color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color(); Color debug_navigation_geometry_face_color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color();
@ -3671,7 +3678,9 @@ void NavigationMeshSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
face_color_array.resize(polygon_count * 3); face_color_array.resize(polygon_count * 3);
for (int i = 0; i < polygon_count; i++) { for (int i = 0; i < polygon_count; i++) {
polygon_color = debug_navigation_geometry_face_color * (Color(Math::randf(), Math::randf(), Math::randf())); // 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;
Vector<int> polygon = navigationmesh->get_polygon(i); Vector<int> polygon = navigationmesh->get_polygon(i);
@ -3683,8 +3692,7 @@ void NavigationMeshSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
} }
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array); debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
Ref<SpatialMaterial> debug_geometry_face_material = NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_face_material(); p_gizmo->add_mesh(debug_mesh, navmesh_instance->is_enabled() ? get_material("face_material", p_gizmo) : get_material("face_material_disabled", p_gizmo));
debug_mesh->surface_set_material(0, debug_geometry_face_material);
// if enabled build geometry edge line surface // if enabled build geometry edge line surface
bool enabled_edge_lines = NavigationServer::get_singleton()->get_debug_navigation_enable_edge_lines(); bool enabled_edge_lines = NavigationServer::get_singleton()->get_debug_navigation_enable_edge_lines();
@ -3704,27 +3712,8 @@ void NavigationMeshSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
line_vertex_array.push_back(vertices[polygon[0]]); line_vertex_array.push_back(vertices[polygon[0]]);
} }
Array line_mesh_array; p_gizmo->add_lines(line_vertex_array, navmesh_instance->is_enabled() ? get_material("edge_material", p_gizmo) : get_material("edge_material_disabled", p_gizmo));
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);
Ref<SpatialMaterial> debug_geometry_edge_material = NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_edge_material();
debug_mesh->surface_set_material(1, debug_geometry_edge_material);
} }
if (!navmesh_instance->is_enabled()) {
if (debug_mesh.is_valid()) {
if (debug_mesh->get_surface_count() > 0) {
debug_mesh->surface_set_material(0, NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_face_disabled_material());
}
if (debug_mesh->get_surface_count() > 1) {
debug_mesh->surface_set_material(1, NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_edge_disabled_material());
}
}
}
p_gizmo->add_mesh(debug_mesh);
p_gizmo->add_collision_segments(lines);
} }
////// //////

View File

@ -188,7 +188,7 @@ void NavigationPolygonInstance::_notification(int p_what) {
} }
// Generate the polygon color, slightly randomly modified from the settings one. // Generate the polygon color, slightly randomly modified from the settings one.
Color random_variation_color; Color random_variation_color;
random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.05, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.1); random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.1, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.2);
random_variation_color.a = color.a; random_variation_color.a = color.a;
Vector<Color> colors; Vector<Color> colors;
colors.push_back(random_variation_color); colors.push_back(random_variation_color);

View File

@ -448,11 +448,14 @@ void NavigationMeshInstance::_update_debug_mesh() {
Ref<SpatialMaterial> face_material = NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_face_material(); Ref<SpatialMaterial> face_material = NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_face_material();
Ref<SpatialMaterial> line_material = NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_edge_material(); Ref<SpatialMaterial> line_material = NavigationServer::get_singleton_mut()->get_debug_navigation_geometry_edge_material();
RandomPCG rand;
Color polygon_color = debug_navigation_geometry_face_color; Color polygon_color = debug_navigation_geometry_face_color;
for (int i = 0; i < polygon_count; i++) { for (int i = 0; i < polygon_count; i++) {
if (enabled_geometry_face_random_color) { if (enabled_geometry_face_random_color) {
polygon_color = debug_navigation_geometry_face_color * (Color(Math::randf(), Math::randf(), Math::randf())); // 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;
} }
Vector<int> polygon = navmesh->get_polygon(i); Vector<int> polygon = navmesh->get_polygon(i);