1 | /**************************************************************************/ |
2 | /* navigation_region_3d_gizmo_plugin.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "navigation_region_3d_gizmo_plugin.h" |
32 | |
33 | #include "editor/plugins/node_3d_editor_plugin.h" |
34 | #include "scene/3d/navigation_region_3d.h" |
35 | #include "servers/navigation_server_3d.h" |
36 | |
37 | NavigationRegion3DGizmoPlugin::NavigationRegion3DGizmoPlugin() { |
38 | create_material("face_material" , NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color(), false, false, true); |
39 | create_material("face_material_disabled" , NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_color(), false, false, true); |
40 | create_material("edge_material" , NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_color()); |
41 | create_material("edge_material_disabled" , NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_color()); |
42 | |
43 | Color baking_aabb_material_color = Color(0.8, 0.5, 0.7); |
44 | baking_aabb_material_color.a = 0.1; |
45 | create_material("baking_aabb_material" , baking_aabb_material_color); |
46 | } |
47 | |
48 | bool NavigationRegion3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
49 | return Object::cast_to<NavigationRegion3D>(p_spatial) != nullptr; |
50 | } |
51 | |
52 | String NavigationRegion3DGizmoPlugin::get_gizmo_name() const { |
53 | return "NavigationRegion3D" ; |
54 | } |
55 | |
56 | int NavigationRegion3DGizmoPlugin::get_priority() const { |
57 | return -1; |
58 | } |
59 | |
60 | void NavigationRegion3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
61 | NavigationRegion3D *navigationregion = Object::cast_to<NavigationRegion3D>(p_gizmo->get_node_3d()); |
62 | |
63 | p_gizmo->clear(); |
64 | Ref<NavigationMesh> navigationmesh = navigationregion->get_navigation_mesh(); |
65 | if (navigationmesh.is_null()) { |
66 | return; |
67 | } |
68 | |
69 | AABB baking_aabb = navigationmesh->get_filter_baking_aabb(); |
70 | if (baking_aabb.has_volume()) { |
71 | Vector3 baking_aabb_offset = navigationmesh->get_filter_baking_aabb_offset(); |
72 | |
73 | if (p_gizmo->is_selected()) { |
74 | Ref<Material> material = get_material("baking_aabb_material" , p_gizmo); |
75 | p_gizmo->add_solid_box(material, baking_aabb.get_size(), baking_aabb.get_center() + baking_aabb_offset); |
76 | } |
77 | } |
78 | |
79 | Vector<Vector3> vertices = navigationmesh->get_vertices(); |
80 | const Vector3 *vr = vertices.ptr(); |
81 | List<Face3> faces; |
82 | for (int i = 0; i < navigationmesh->get_polygon_count(); i++) { |
83 | Vector<int> p = navigationmesh->get_polygon(i); |
84 | |
85 | for (int j = 2; j < p.size(); j++) { |
86 | Face3 f; |
87 | f.vertex[0] = vr[p[0]]; |
88 | f.vertex[1] = vr[p[j - 1]]; |
89 | f.vertex[2] = vr[p[j]]; |
90 | |
91 | faces.push_back(f); |
92 | } |
93 | } |
94 | |
95 | if (faces.is_empty()) { |
96 | return; |
97 | } |
98 | |
99 | HashMap<_EdgeKey, bool, _EdgeKey> edge_map; |
100 | Vector<Vector3> tmeshfaces; |
101 | tmeshfaces.resize(faces.size() * 3); |
102 | |
103 | { |
104 | Vector3 *tw = tmeshfaces.ptrw(); |
105 | int tidx = 0; |
106 | |
107 | for (const Face3 &f : faces) { |
108 | for (int j = 0; j < 3; j++) { |
109 | tw[tidx++] = f.vertex[j]; |
110 | _EdgeKey ek; |
111 | ek.from = f.vertex[j].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON)); |
112 | ek.to = f.vertex[(j + 1) % 3].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON)); |
113 | if (ek.from < ek.to) { |
114 | SWAP(ek.from, ek.to); |
115 | } |
116 | |
117 | HashMap<_EdgeKey, bool, _EdgeKey>::Iterator F = edge_map.find(ek); |
118 | |
119 | if (F) { |
120 | F->value = false; |
121 | |
122 | } else { |
123 | edge_map[ek] = true; |
124 | } |
125 | } |
126 | } |
127 | } |
128 | Vector<Vector3> lines; |
129 | |
130 | for (const KeyValue<_EdgeKey, bool> &E : edge_map) { |
131 | if (E.value) { |
132 | lines.push_back(E.key.from); |
133 | lines.push_back(E.key.to); |
134 | } |
135 | } |
136 | |
137 | Ref<TriangleMesh> tmesh = memnew(TriangleMesh); |
138 | tmesh->create(tmeshfaces); |
139 | |
140 | p_gizmo->add_collision_triangles(tmesh); |
141 | p_gizmo->add_collision_segments(lines); |
142 | |
143 | Ref<ArrayMesh> debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); |
144 | int polygon_count = navigationmesh->get_polygon_count(); |
145 | |
146 | // build geometry face surface |
147 | Vector<Vector3> face_vertex_array; |
148 | face_vertex_array.resize(polygon_count * 3); |
149 | |
150 | for (int i = 0; i < polygon_count; i++) { |
151 | Vector<int> polygon = navigationmesh->get_polygon(i); |
152 | |
153 | face_vertex_array.push_back(vertices[polygon[0]]); |
154 | face_vertex_array.push_back(vertices[polygon[1]]); |
155 | face_vertex_array.push_back(vertices[polygon[2]]); |
156 | } |
157 | |
158 | Array face_mesh_array; |
159 | face_mesh_array.resize(Mesh::ARRAY_MAX); |
160 | face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array; |
161 | |
162 | // if enabled add vertex colors to colorize each face individually |
163 | RandomPCG rand; |
164 | bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color(); |
165 | if (enabled_geometry_face_random_color) { |
166 | Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color(); |
167 | Color polygon_color = debug_navigation_geometry_face_color; |
168 | |
169 | Vector<Color> face_color_array; |
170 | face_color_array.resize(polygon_count * 3); |
171 | |
172 | for (int i = 0; i < polygon_count; i++) { |
173 | // Generate the polygon color, slightly randomly modified from the settings one. |
174 | 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); |
175 | polygon_color.a = debug_navigation_geometry_face_color.a; |
176 | |
177 | Vector<int> polygon = navigationmesh->get_polygon(i); |
178 | |
179 | face_color_array.push_back(polygon_color); |
180 | face_color_array.push_back(polygon_color); |
181 | face_color_array.push_back(polygon_color); |
182 | } |
183 | face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array; |
184 | } |
185 | |
186 | debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array); |
187 | p_gizmo->add_mesh(debug_mesh, navigationregion->is_enabled() ? get_material("face_material" , p_gizmo) : get_material("face_material_disabled" , p_gizmo)); |
188 | |
189 | // if enabled build geometry edge line surface |
190 | bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines(); |
191 | if (enabled_edge_lines) { |
192 | Vector<Vector3> line_vertex_array; |
193 | line_vertex_array.resize(polygon_count * 6); |
194 | |
195 | for (int i = 0; i < polygon_count; i++) { |
196 | Vector<int> polygon = navigationmesh->get_polygon(i); |
197 | |
198 | line_vertex_array.push_back(vertices[polygon[0]]); |
199 | line_vertex_array.push_back(vertices[polygon[1]]); |
200 | line_vertex_array.push_back(vertices[polygon[1]]); |
201 | line_vertex_array.push_back(vertices[polygon[2]]); |
202 | line_vertex_array.push_back(vertices[polygon[2]]); |
203 | line_vertex_array.push_back(vertices[polygon[0]]); |
204 | } |
205 | |
206 | p_gizmo->add_lines(line_vertex_array, navigationregion->is_enabled() ? get_material("edge_material" , p_gizmo) : get_material("edge_material_disabled" , p_gizmo)); |
207 | } |
208 | } |
209 | |