Debian Codegraph @

editor/scene/3d/gizmos/physics/collision_polygon_3d_gizmo_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* collision_polygon_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 "collision_polygon_3d_gizmo_plugin.h"
32
33 #include "core/math/geometry_2d.h"
34 #include "scene/3d/physics/collision_polygon_3d.h"
35
36 CollisionPolygon3DGizmoPlugin::CollisionPolygon3DGizmoPlugin() {
37 create_collision_material("shape_material", 2.0);
38 create_collision_material("shape_material_arraymesh", 0.0625);
39
40 create_collision_material("shape_material_disabled", 0.0625);
41 create_collision_material("shape_material_arraymesh_disabled", 0.015625);
42 }
43
44 void CollisionPolygon3DGizmoPlugin::create_collision_material(const String &p_name, float p_alpha) {
45 Vector<Ref<StandardMaterial3D>> mats;
46
47 const Color collision_color(1.0, 1.0, 1.0, p_alpha);
48
49 for (int i = 0; i < 4; i++) {
50 bool instantiated = i < 2;
51
52 Ref<StandardMaterial3D> material;
53 material.instantiate();
54
55 Color color = collision_color;
56 color.a *= instantiated ? 0.25 : 1.0;
57
58 material->set_albedo(color);
59 material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
60 material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
61 material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN + 1);
62 material->set_cull_mode(StandardMaterial3D::CULL_BACK);
63 material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
64 material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
65 material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
66
67 mats.push_back(material);
68 }
69
70 materials[p_name] = mats;
71 }
72
73 bool CollisionPolygon3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
74 return Object::cast_to<CollisionPolygon3D>(p_spatial) != nullptr;
75 }
76
77 String CollisionPolygon3DGizmoPlugin::get_gizmo_name() const {
78 return "CollisionPolygon3D";
79 }
80
81 int CollisionPolygon3DGizmoPlugin::get_priority() const {
82 return -1;
83 }
84
85 void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
86 CollisionPolygon3D *polygon = Object::cast_to<CollisionPolygon3D>(p_gizmo->get_node_3d());
87
88 p_gizmo->clear();
89
90 const Ref<StandardMaterial3D> material =
91 get_material(!polygon->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
92 const Ref<StandardMaterial3D> material_arraymesh =
93 get_material(!polygon->is_disabled() ? "shape_material_arraymesh" : "shape_material_arraymesh_disabled", p_gizmo);
94
95 const Color collision_color = polygon->is_disabled() ? Color(1.0, 1.0, 1.0, 0.75) : polygon->get_debug_color();
96
97 Vector<Vector2> points = polygon->get_polygon();
98 float depth = polygon->get_depth() * 0.5;
99
100 Vector<Vector3> lines;
101 const int points_size = points.size();
102
103 for (int i = 0; i < points_size; i++) {
104 int n = (i + 1) % points_size;
105 lines.push_back(Vector3(points[i].x, points[i].y, depth));
106 lines.push_back(Vector3(points[n].x, points[n].y, depth));
107 lines.push_back(Vector3(points[i].x, points[i].y, -depth));
108 lines.push_back(Vector3(points[n].x, points[n].y, -depth));
109 lines.push_back(Vector3(points[i].x, points[i].y, depth));
110 lines.push_back(Vector3(points[i].x, points[i].y, -depth));
111 }
112
113 if (polygon->get_debug_fill_enabled()) {
114 Ref<ArrayMesh> array_mesh;
115 array_mesh.instantiate();
116
117 Vector<Vector3> verts;
118 Vector<Color> colors;
119 Vector<int> indices;
120
121 // Determine orientation of the 2D polygon's vertices to determine
122 // which direction to draw outer polygons.
123 float signed_area = 0.0f;
124 for (int i = 0; i < points_size; i++) {
125 const int j = (i + 1) % points_size;
126 signed_area += points[i].x * points[j].y - points[j].x * points[i].y;
127 }
128
129 // Generate triangles for the sides of the extruded polygon.
130 for (int i = 0; i < points_size; i++) {
131 verts.push_back(Vector3(points[i].x, points[i].y, depth));
132 verts.push_back(Vector3(points[i].x, points[i].y, -depth));
133
134 colors.push_back(collision_color);
135 colors.push_back(collision_color);
136 }
137
138 const int verts_size = verts.size();
139 for (int i = 0; i < verts_size; i += 2) {
140 const int j = (i + 1) % verts_size;
141 const int k = (i + 2) % verts_size;
142 const int l = (i + 3) % verts_size;
143
144 indices.push_back(i);
145 if (signed_area < 0) {
146 indices.push_back(j);
147 indices.push_back(k);
148 } else {
149 indices.push_back(k);
150 indices.push_back(j);
151 }
152
153 indices.push_back(j);
154 if (signed_area < 0) {
155 indices.push_back(l);
156 indices.push_back(k);
157 } else {
158 indices.push_back(k);
159 indices.push_back(l);
160 }
161 }
162
163 Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon->get_polygon());
164
165 // Generate triangles for the bottom cap of the extruded polygon.
166 for (int i = 0; i < decomp.size(); i++) {
167 Vector<Vector3> cap_verts_bottom;
168 Vector<Color> cap_colours_bottom;
169 Vector<int> cap_indices_bottom;
170
171 const int index_offset = verts.size();
172
173 const Vector<Vector2> &convex = decomp[i];
174 const int convex_size = convex.size();
175
176 for (int j = 0; j < convex_size; j++) {
177 cap_verts_bottom.push_back(Vector3(convex[j].x, convex[j].y, -depth));
178 cap_colours_bottom.push_back(collision_color);
179 }
180
181 if (convex_size >= 3) {
182 for (int j = 1; j < convex_size; j++) {
183 const int k = (j + 1) % convex_size;
184
185 cap_indices_bottom.push_back(index_offset + 0);
186 cap_indices_bottom.push_back(index_offset + j);
187 cap_indices_bottom.push_back(index_offset + k);
188 }
189 }
190 verts.append_array(cap_verts_bottom);
191 colors.append_array(cap_colours_bottom);
192 indices.append_array(cap_indices_bottom);
193 }
194
195 // Generate triangles for the top cap of the extruded polygon.
196 for (int i = 0; i < decomp.size(); i++) {
197 Vector<Vector3> cap_verts_top;
198 Vector<Color> cap_colours_top;
199 Vector<int> cap_indices_top;
200
201 const int index_offset = verts.size();
202
203 const Vector<Vector2> &convex = decomp[i];
204 const int convex_size = convex.size();
205
206 for (int j = 0; j < convex_size; j++) {
207 cap_verts_top.push_back(Vector3(convex[j].x, convex[j].y, depth));
208 cap_colours_top.push_back(collision_color);
209 }
210
211 if (convex_size >= 3) {
212 for (int j = 1; j < convex_size; j++) {
213 const int k = (j + 1) % convex_size;
214
215 cap_indices_top.push_back(index_offset + k);
216 cap_indices_top.push_back(index_offset + j);
217 cap_indices_top.push_back(index_offset + 0);
218 }
219 }
220 verts.append_array(cap_verts_top);
221 colors.append_array(cap_colours_top);
222 indices.append_array(cap_indices_top);
223 }
224
225 Array a;
226 a.resize(Mesh::ARRAY_MAX);
227 a[RS::ARRAY_VERTEX] = verts;
228 a[RS::ARRAY_COLOR] = colors;
229 a[RS::ARRAY_INDEX] = indices;
230 array_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
231
232 p_gizmo->add_mesh(array_mesh, material_arraymesh);
233 }
234
235 p_gizmo->add_lines(lines, material, false, collision_color);
236 p_gizmo->add_collision_segments(lines);
237 }