Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************/
2 /* collision_shape_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_shape_3d_gizmo_plugin.h"
32
33 #include "core/math/convex_hull.h"
34 #include "core/math/geometry_3d.h"
35 #include "editor/editor_undo_redo_manager.h"
36 #include "editor/scene/3d/gizmos/gizmo_3d_helper.h"
37 #include "editor/scene/3d/node_3d_editor_plugin.h"
38 #include "scene/3d/physics/collision_shape_3d.h"
39 #include "scene/resources/3d/box_shape_3d.h"
40 #include "scene/resources/3d/capsule_shape_3d.h"
41 #include "scene/resources/3d/concave_polygon_shape_3d.h"
42 #include "scene/resources/3d/convex_polygon_shape_3d.h"
43 #include "scene/resources/3d/cylinder_shape_3d.h"
44 #include "scene/resources/3d/height_map_shape_3d.h"
45 #include "scene/resources/3d/separation_ray_shape_3d.h"
46 #include "scene/resources/3d/sphere_shape_3d.h"
47 #include "scene/resources/3d/world_boundary_shape_3d.h"
48
49 CollisionShape3DGizmoPlugin::CollisionShape3DGizmoPlugin() {
50 helper.instantiate();
51
52 create_collision_material("shape_material", 2.0);
53 create_collision_material("shape_material_arraymesh", 0.0625);
54
55 create_collision_material("shape_material_disabled", 0.0625);
56 create_collision_material("shape_material_arraymesh_disabled", 0.015625);
57
58 create_handle_material("handles");
59 }
60
61 void CollisionShape3DGizmoPlugin::create_collision_material(const String &p_name, float p_alpha) {
62 Vector<Ref<StandardMaterial3D>> mats;
63
64 const Color collision_color(1.0, 1.0, 1.0, p_alpha);
65
66 for (int i = 0; i < 4; i++) {
67 bool instantiated = i < 2;
68
69 Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
70
71 Color color = collision_color;
72 color.a *= instantiated ? 0.25 : 1.0;
73
74 material->set_albedo(color);
75 material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
76 material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
77 material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN + 1);
78 material->set_cull_mode(StandardMaterial3D::CULL_BACK);
79 material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
80 material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
81 material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
82
83 mats.push_back(material);
84 }
85
86 materials[p_name] = mats;
87 }
88
89 bool CollisionShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
90 return Object::cast_to<CollisionShape3D>(p_spatial) != nullptr;
91 }
92
93 String CollisionShape3DGizmoPlugin::get_gizmo_name() const {
94 return "CollisionShape3D";
95 }
96
97 int CollisionShape3DGizmoPlugin::get_priority() const {
98 return -1;
99 }
100
101 String CollisionShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
102 const CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_node_3d());
103
104 Ref<Shape3D> s = cs->get_shape();
105 if (s.is_null()) {
106 return "";
107 }
108
109 if (Object::cast_to<SphereShape3D>(*s)) {
110 return "Radius";
111 }
112
113 if (Object::cast_to<BoxShape3D>(*s)) {
114 return helper->box_get_handle_name(p_id);
115 }
116
117 if (Object::cast_to<CapsuleShape3D>(*s)) {
118 return helper->capsule_get_handle_name(p_id);
119 }
120
121 if (Object::cast_to<CylinderShape3D>(*s)) {
122 return helper->cylinder_get_handle_name(p_id);
123 }
124
125 if (Object::cast_to<SeparationRayShape3D>(*s)) {
126 return "Length";
127 }
128
129 return "";
130 }
131
132 Variant CollisionShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
133 CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_node_3d());
134
135 Ref<Shape3D> s = cs->get_shape();
136 if (s.is_null()) {
137 return Variant();
138 }
139
140 if (Object::cast_to<SphereShape3D>(*s)) {
141 Ref<SphereShape3D> ss = s;
142 return ss->get_radius();
143 }
144
145 if (Object::cast_to<BoxShape3D>(*s)) {
146 Ref<BoxShape3D> bs = s;
147 return bs->get_size();
148 }
149
150 if (Object::cast_to<CapsuleShape3D>(*s)) {
151 Ref<CapsuleShape3D> cs2 = s;
152 return Vector2(cs2->get_radius(), cs2->get_height());
153 }
154
155 if (Object::cast_to<CylinderShape3D>(*s)) {
156 Ref<CylinderShape3D> cs2 = s;
157 return Vector2(cs2->get_radius(), cs2->get_height());
158 }
159
160 if (Object::cast_to<SeparationRayShape3D>(*s)) {
161 Ref<SeparationRayShape3D> cs2 = s;
162 return cs2->get_length();
163 }
164
165 return Variant();
166 }
167
168 void CollisionShape3DGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
169 helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
170 }
171
172 void CollisionShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
173 CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_node_3d());
174
175 Ref<Shape3D> s = cs->get_shape();
176 if (s.is_null()) {
177 return;
178 }
179
180 Vector3 sg[2];
181 helper->get_segment(p_camera, p_point, sg);
182
183 if (Object::cast_to<SphereShape3D>(*s)) {
184 Ref<SphereShape3D> ss = s;
185 Vector3 ra, rb;
186 Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
187 float d = ra.x;
188 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
189 d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
190 }
191
192 if (d < 0.001) {
193 d = 0.001;
194 }
195
196 ss->set_radius(d);
197 }
198
199 if (Object::cast_to<SeparationRayShape3D>(*s)) {
200 Ref<SeparationRayShape3D> rs = s;
201 Vector3 ra, rb;
202 Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(0, 0, 4096), sg[0], sg[1], ra, rb);
203 float d = ra.z;
204 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
205 d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
206 }
207
208 if (d < 0.001) {
209 d = 0.001;
210 }
211
212 rs->set_length(d);
213 }
214
215 if (Object::cast_to<BoxShape3D>(*s)) {
216 Ref<BoxShape3D> bs = s;
217 Vector3 size = bs->get_size();
218 Vector3 position;
219 helper->box_set_handle(sg, p_id, size, position);
220 bs->set_size(size);
221 cs->set_global_position(position);
222 }
223
224 if (Object::cast_to<CapsuleShape3D>(*s)) {
225 Ref<CapsuleShape3D> cs2 = s;
226
227 real_t height = cs2->get_height();
228 real_t radius = cs2->get_radius();
229 Vector3 position;
230 helper->capsule_set_handle(sg, p_id, height, radius, position);
231 cs2->set_height(height);
232 cs2->set_radius(radius);
233 cs->set_global_position(position);
234 }
235
236 if (Object::cast_to<CylinderShape3D>(*s)) {
237 Ref<CylinderShape3D> cs2 = s;
238
239 real_t height = cs2->get_height();
240 real_t radius = cs2->get_radius();
241 Vector3 position;
242 helper->cylinder_set_handle(sg, p_id, height, radius, position);
243 cs2->set_height(height);
244 cs2->set_radius(radius);
245 cs->set_global_position(position);
246 }
247 }
248
249 void CollisionShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
250 CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_node_3d());
251
252 Ref<Shape3D> s = cs->get_shape();
253 if (s.is_null()) {
254 return;
255 }
256
257 if (Object::cast_to<SphereShape3D>(*s)) {
258 Ref<SphereShape3D> ss = s;
259 if (p_cancel) {
260 ss->set_radius(p_restore);
261 return;
262 }
263
264 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
265 ur->create_action(TTR("Change Sphere Shape Radius"));
266 ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
267 ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
268 ur->commit_action();
269 }
270
271 if (Object::cast_to<BoxShape3D>(*s)) {
272 helper->box_commit_handle(TTR("Change Box Shape Size"), p_cancel, cs, s.ptr());
273 }
274
275 if (Object::cast_to<CapsuleShape3D>(*s)) {
276 Ref<CapsuleShape3D> ss = s;
277 helper->cylinder_commit_handle(p_id, TTR("Change Capsule Shape Radius"), TTR("Change Capsule Shape Height"), p_cancel, cs, *ss, *ss);
278 }
279
280 if (Object::cast_to<CylinderShape3D>(*s)) {
281 Ref<CylinderShape3D> ss = s;
282 helper->cylinder_commit_handle(p_id, TTR("Change Cylinder Shape Radius"), TTR("Change Cylinder Shape Height"), p_cancel, cs, *ss, *ss);
283 }
284
285 if (Object::cast_to<SeparationRayShape3D>(*s)) {
286 Ref<SeparationRayShape3D> ss = s;
287 if (p_cancel) {
288 ss->set_length(p_restore);
289 return;
290 }
291
292 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
293 ur->create_action(TTR("Change Separation Ray Shape Length"));
294 ur->add_do_method(ss.ptr(), "set_length", ss->get_length());
295 ur->add_undo_method(ss.ptr(), "set_length", p_restore);
296 ur->commit_action();
297 }
298 }
299
300 void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
301 CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_node_3d());
302
303 p_gizmo->clear();
304
305 Ref<Shape3D> s = cs->get_shape();
306 if (s.is_null()) {
307 return;
308 }
309
310 const Ref<StandardMaterial3D> material =
311 get_material(!cs->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
312 const Ref<StandardMaterial3D> material_arraymesh =
313 get_material(!cs->is_disabled() ? "shape_material_arraymesh" : "shape_material_arraymesh_disabled", p_gizmo);
314 const Ref<Material> handles_material = get_material("handles");
315
316 const Color collision_color = cs->is_disabled() ? Color(1.0, 1.0, 1.0, 0.75) : cs->get_debug_color();
317
318 if (cs->get_debug_fill_enabled()) {
319 Ref<ArrayMesh> array_mesh = s->get_debug_arraymesh_faces(collision_color);
320 if (array_mesh.is_valid() && array_mesh->get_surface_count() > 0) {
321 p_gizmo->add_mesh(array_mesh, material_arraymesh);
322 }
323 }
324
325 if (Object::cast_to<SphereShape3D>(*s)) {
326 Ref<SphereShape3D> sp = s;
327 float radius = sp->get_radius();
328
329 #define PUSH_QUARTER(from_x, from_y, to_x, to_y, y) \
330 points_ptrw[index++] = Vector3(from_x, y, from_y); \
331 points_ptrw[index++] = Vector3(to_x, y, to_y); \
332 points_ptrw[index++] = Vector3(from_x, y, -from_y); \
333 points_ptrw[index++] = Vector3(to_x, y, -to_y); \
334 points_ptrw[index++] = Vector3(-from_x, y, from_y); \
335 points_ptrw[index++] = Vector3(-to_x, y, to_y); \
336 points_ptrw[index++] = Vector3(-from_x, y, -from_y); \
337 points_ptrw[index++] = Vector3(-to_x, y, -to_y);
338
339 #define PUSH_QUARTER_XY(from_x, from_y, to_x, to_y, y) \
340 points_ptrw[index++] = Vector3(from_x, -from_y - y, 0); \
341 points_ptrw[index++] = Vector3(to_x, -to_y - y, 0); \
342 points_ptrw[index++] = Vector3(from_x, from_y + y, 0); \
343 points_ptrw[index++] = Vector3(to_x, to_y + y, 0); \
344 points_ptrw[index++] = Vector3(-from_x, -from_y - y, 0); \
345 points_ptrw[index++] = Vector3(-to_x, -to_y - y, 0); \
346 points_ptrw[index++] = Vector3(-from_x, from_y + y, 0); \
347 points_ptrw[index++] = Vector3(-to_x, to_y + y, 0);
348
349 #define PUSH_QUARTER_YZ(from_x, from_y, to_x, to_y, y) \
350 points_ptrw[index++] = Vector3(0, -from_y - y, from_x); \
351 points_ptrw[index++] = Vector3(0, -to_y - y, to_x); \
352 points_ptrw[index++] = Vector3(0, from_y + y, from_x); \
353 points_ptrw[index++] = Vector3(0, to_y + y, to_x); \
354 points_ptrw[index++] = Vector3(0, -from_y - y, -from_x); \
355 points_ptrw[index++] = Vector3(0, -to_y - y, -to_x); \
356 points_ptrw[index++] = Vector3(0, from_y + y, -from_x); \
357 points_ptrw[index++] = Vector3(0, to_y + y, -to_x);
358
359 // Number of points in an octant. So there will be 8 * points_in_octant * 2 points in total for one circle.
360 // This Corresponds to the smoothness of the circle.
361 const uint32_t points_in_octant = 16;
362 const real_t inc = (Math::PI / (4 * points_in_octant));
363 const real_t radius_squared = radius * radius;
364 real_t r = 0;
365
366 Vector<Vector3> points;
367 uint32_t index = 0;
368 // 3 full circles.
369 points.resize(3 * 8 * points_in_octant * 2);
370 Vector3 *points_ptrw = points.ptrw();
371
372 float previous_x = radius;
373 float previous_y = 0.f;
374
375 for (uint32_t i = 0; i < points_in_octant; ++i) {
376 r += inc;
377 real_t x = Math::cos(r) * radius;
378 real_t y = Math::sqrt(radius_squared - (x * x));
379
380 PUSH_QUARTER(previous_x, previous_y, x, y, 0);
381 PUSH_QUARTER(previous_y, previous_x, y, x, 0);
382
383 PUSH_QUARTER_XY(previous_x, previous_y, x, y, 0);
384 PUSH_QUARTER_XY(previous_y, previous_x, y, x, 0);
385
386 PUSH_QUARTER_YZ(previous_x, previous_y, x, y, 0);
387 PUSH_QUARTER_YZ(previous_y, previous_x, y, x, 0)
388
389 previous_x = x;
390 previous_y = y;
391 }
392 #undef PUSH_QUARTER
393 #undef PUSH_QUARTER_XY
394 #undef PUSH_QUARTER_YZ
395
396 p_gizmo->add_lines(points, material, false, collision_color);
397 p_gizmo->add_collision_segments(points);
398 Vector<Vector3> handles;
399 handles.push_back(Vector3(radius, 0, 0));
400 p_gizmo->add_handles(handles, handles_material);
401 }
402
403 if (Object::cast_to<BoxShape3D>(*s)) {
404 Ref<BoxShape3D> bs = s;
405 Vector<Vector3> lines;
406 AABB aabb;
407 aabb.position = -bs->get_size() / 2;
408 aabb.size = bs->get_size();
409
410 for (int i = 0; i < 12; i++) {
411 Vector3 a, b;
412 aabb.get_edge(i, a, b);
413 lines.push_back(a);
414 lines.push_back(b);
415 }
416
417 const Vector<Vector3> handles = helper->box_get_handles(bs->get_size());
418
419 p_gizmo->add_lines(lines, material, false, collision_color);
420 p_gizmo->add_collision_segments(lines);
421 p_gizmo->add_handles(handles, handles_material);
422 }
423
424 if (Object::cast_to<CapsuleShape3D>(*s)) {
425 Ref<CapsuleShape3D> cs2 = s;
426 float radius = cs2->get_radius();
427 float height = cs2->get_height();
428
429 // Number of points in an octant. So there will be 8 * points_in_octant points in total.
430 // This corresponds to the smoothness of the circle.
431 const uint32_t points_in_octant = 16;
432 const real_t octant_angle = Math::PI / 4;
433 const real_t inc = (Math::PI / (4 * points_in_octant));
434 const real_t radius_squared = radius * radius;
435 real_t r = 0;
436
437 Vector<Vector3> points;
438 // 4 vertical lines and 4 full circles.
439 points.resize(4 * 2 + 4 * 8 * points_in_octant * 2);
440 Vector3 *points_ptrw = points.ptrw();
441
442 uint32_t index = 0;
443 float y_value = height * 0.5 - radius;
444
445 // Vertical Lines.
446 points_ptrw[index++] = Vector3(0.f, y_value, radius);
447 points_ptrw[index++] = Vector3(0.f, -y_value, radius);
448 points_ptrw[index++] = Vector3(0.f, y_value, -radius);
449 points_ptrw[index++] = Vector3(0.f, -y_value, -radius);
450 points_ptrw[index++] = Vector3(radius, y_value, 0.f);
451 points_ptrw[index++] = Vector3(radius, -y_value, 0.f);
452 points_ptrw[index++] = Vector3(-radius, y_value, 0.f);
453 points_ptrw[index++] = Vector3(-radius, -y_value, 0.f);
454
455 #define PUSH_QUARTER(from_x, from_y, to_x, to_y, y) \
456 points_ptrw[index++] = Vector3(from_x, y, from_y); \
457 points_ptrw[index++] = Vector3(to_x, y, to_y); \
458 points_ptrw[index++] = Vector3(from_x, y, -from_y); \
459 points_ptrw[index++] = Vector3(to_x, y, -to_y); \
460 points_ptrw[index++] = Vector3(-from_x, y, from_y); \
461 points_ptrw[index++] = Vector3(-to_x, y, to_y); \
462 points_ptrw[index++] = Vector3(-from_x, y, -from_y); \
463 points_ptrw[index++] = Vector3(-to_x, y, -to_y);
464
465 #define PUSH_QUARTER_XY(from_x, from_y, to_x, to_y, y) \
466 points_ptrw[index++] = Vector3(from_x, -from_y - y, 0); \
467 points_ptrw[index++] = Vector3(to_x, -to_y - y, 0); \
468 points_ptrw[index++] = Vector3(from_x, from_y + y, 0); \
469 points_ptrw[index++] = Vector3(to_x, to_y + y, 0); \
470 points_ptrw[index++] = Vector3(-from_x, -from_y - y, 0); \
471 points_ptrw[index++] = Vector3(-to_x, -to_y - y, 0); \
472 points_ptrw[index++] = Vector3(-from_x, from_y + y, 0); \
473 points_ptrw[index++] = Vector3(-to_x, to_y + y, 0);
474
475 #define PUSH_QUARTER_YZ(from_x, from_y, to_x, to_y, y) \
476 points_ptrw[index++] = Vector3(0, -from_y - y, from_x); \
477 points_ptrw[index++] = Vector3(0, -to_y - y, to_x); \
478 points_ptrw[index++] = Vector3(0, from_y + y, from_x); \
479 points_ptrw[index++] = Vector3(0, to_y + y, to_x); \
480 points_ptrw[index++] = Vector3(0, -from_y - y, -from_x); \
481 points_ptrw[index++] = Vector3(0, -to_y - y, -to_x); \
482 points_ptrw[index++] = Vector3(0, from_y + y, -from_x); \
483 points_ptrw[index++] = Vector3(0, to_y + y, -to_x);
484
485 float previous_x = radius;
486 float previous_y = 0.f;
487
488 for (uint32_t i = 0; i < points_in_octant; ++i) {
489 r += inc;
490 real_t x = Math::cos((i == points_in_octant - 1) ? octant_angle : r) * radius;
491 real_t y = Math::sqrt(radius_squared - (x * x));
492
493 // High circle ring.
494 PUSH_QUARTER(previous_x, previous_y, x, y, y_value);
495 PUSH_QUARTER(previous_y, previous_x, y, x, y_value);
496
497 // Low circle ring.
498 PUSH_QUARTER(previous_x, previous_y, x, y, -y_value);
499 PUSH_QUARTER(previous_y, previous_x, y, x, -y_value);
500
501 // Up and Low circle in X-Y plane.
502 PUSH_QUARTER_XY(previous_x, previous_y, x, y, y_value);
503 PUSH_QUARTER_XY(previous_y, previous_x, y, x, y_value);
504
505 // Up and Low circle in Y-Z plane.
506 PUSH_QUARTER_YZ(previous_x, previous_y, x, y, y_value);
507 PUSH_QUARTER_YZ(previous_y, previous_x, y, x, y_value)
508
509 previous_x = x;
510 previous_y = y;
511 }
512
513 #undef PUSH_QUARTER
514 #undef PUSH_QUARTER_XY
515 #undef PUSH_QUARTER_YZ
516
517 p_gizmo->add_lines(points, material, false, collision_color);
518 p_gizmo->add_collision_segments(points);
519
520 Vector<Vector3> handles = helper->capsule_get_handles(cs2->get_height(), cs2->get_radius());
521 p_gizmo->add_handles(handles, handles_material);
522 }
523
524 if (Object::cast_to<CylinderShape3D>(*s)) {
525 Ref<CylinderShape3D> cs2 = s;
526 float radius = cs2->get_radius();
527 float height = cs2->get_height();
528
529 #define PUSH_QUARTER(from_x, from_y, to_x, to_y, y) \
530 points_ptrw[index++] = Vector3(from_x, y, from_y); \
531 points_ptrw[index++] = Vector3(to_x, y, to_y); \
532 points_ptrw[index++] = Vector3(from_x, y, -from_y); \
533 points_ptrw[index++] = Vector3(to_x, y, -to_y); \
534 points_ptrw[index++] = Vector3(-from_x, y, from_y); \
535 points_ptrw[index++] = Vector3(-to_x, y, to_y); \
536 points_ptrw[index++] = Vector3(-from_x, y, -from_y); \
537 points_ptrw[index++] = Vector3(-to_x, y, -to_y);
538
539 // Number of points in an octant. So there will be 8 * points_in_octant * 2 points in total for one circle.
540 // This corresponds to the smoothness of the circle.
541 const uint32_t points_in_octant = 16;
542 const real_t inc = (Math::PI / (4 * points_in_octant));
543 const real_t radius_squared = radius * radius;
544 real_t r = 0;
545
546 Vector<Vector3> points;
547 uint32_t index = 0;
548 // 4 vertical lines and 2 full circles.
549 points.resize(4 * 2 + 2 * 8 * points_in_octant * 2);
550 Vector3 *points_ptrw = points.ptrw();
551 float y_value = height * 0.5;
552
553 // Vertical lines.
554 points_ptrw[index++] = Vector3(0.f, y_value, radius);
555 points_ptrw[index++] = Vector3(0.f, -y_value, radius);
556 points_ptrw[index++] = Vector3(0.f, y_value, -radius);
557 points_ptrw[index++] = Vector3(0.f, -y_value, -radius);
558 points_ptrw[index++] = Vector3(radius, y_value, 0.f);
559 points_ptrw[index++] = Vector3(radius, -y_value, 0.f);
560 points_ptrw[index++] = Vector3(-radius, y_value, 0.f);
561 points_ptrw[index++] = Vector3(-radius, -y_value, 0.f);
562
563 float previous_x = radius;
564 float previous_y = 0.f;
565
566 for (uint32_t i = 0; i < points_in_octant; ++i) {
567 r += inc;
568 real_t x = Math::cos(r) * radius;
569 real_t y = Math::sqrt(radius_squared - (x * x));
570
571 // High circle ring.
572 PUSH_QUARTER(previous_x, previous_y, x, y, y_value);
573 PUSH_QUARTER(previous_y, previous_x, y, x, y_value);
574
575 // Low circle ring.
576 PUSH_QUARTER(previous_x, previous_y, x, y, -y_value);
577 PUSH_QUARTER(previous_y, previous_x, y, x, -y_value);
578
579 previous_x = x;
580 previous_y = y;
581 }
582 #undef PUSH_QUARTER
583
584 p_gizmo->add_lines(points, material, false, collision_color);
585 p_gizmo->add_collision_segments(points);
586
587 Vector<Vector3> handles = helper->cylinder_get_handles(cs2->get_height(), cs2->get_radius());
588 p_gizmo->add_handles(handles, handles_material);
589 }
590
591 if (Object::cast_to<WorldBoundaryShape3D>(*s)) {
592 Ref<WorldBoundaryShape3D> wbs = s;
593 const Plane &p = wbs->get_plane();
594
595 Vector3 n1 = p.get_any_perpendicular_normal();
596 Vector3 n2 = p.normal.cross(n1).normalized();
597
598 Vector3 pface[4] = {
599 p.normal * p.d + n1 * 10.0 + n2 * 10.0,
600 p.normal * p.d + n1 * 10.0 + n2 * -10.0,
601 p.normal * p.d + n1 * -10.0 + n2 * -10.0,
602 p.normal * p.d + n1 * -10.0 + n2 * 10.0,
603 };
604
605 Vector<Vector3> points = {
606 pface[0],
607 pface[1],
608 pface[1],
609 pface[2],
610 pface[2],
611 pface[3],
612 pface[3],
613 pface[0],
614 p.normal * p.d,
615 p.normal * p.d + p.normal * 3
616 };
617
618 p_gizmo->add_lines(points, material, false, collision_color);
619 p_gizmo->add_collision_segments(points);
620 }
621
622 if (Object::cast_to<ConvexPolygonShape3D>(*s)) {
623 Vector<Vector3> points = Object::cast_to<ConvexPolygonShape3D>(*s)->get_points();
624
625 if (points.size() > 1) { // Need at least 2 points for a line.
626 Vector<Vector3> varr = Variant(points);
627 Geometry3D::MeshData md;
628 Error err = ConvexHullComputer::convex_hull(varr, md);
629 if (err == OK) {
630 Vector<Vector3> lines;
631 lines.resize(md.edges.size() * 2);
632 for (uint32_t i = 0; i < md.edges.size(); i++) {
633 lines.write[i * 2 + 0] = md.vertices[md.edges[i].vertex_a];
634 lines.write[i * 2 + 1] = md.vertices[md.edges[i].vertex_b];
635 }
636 p_gizmo->add_lines(lines, material, false, collision_color);
637 p_gizmo->add_collision_segments(lines);
638 }
639 }
640 }
641
642 if (Object::cast_to<ConcavePolygonShape3D>(*s)) {
643 Ref<ConcavePolygonShape3D> cs2 = s;
644 Ref<ArrayMesh> mesh = cs2->get_debug_mesh();
645 p_gizmo->add_lines(cs2->get_debug_mesh_lines(), material, false, collision_color);
646 p_gizmo->add_collision_segments(cs2->get_debug_mesh_lines());
647 }
648
649 if (Object::cast_to<SeparationRayShape3D>(*s)) {
650 Ref<SeparationRayShape3D> rs = s;
651
652 Vector<Vector3> points = {
653 Vector3(),
654 Vector3(0, 0, rs->get_length())
655 };
656 p_gizmo->add_lines(points, material, false, collision_color);
657 p_gizmo->add_collision_segments(points);
658 Vector<Vector3> handles;
659 handles.push_back(Vector3(0, 0, rs->get_length()));
660 p_gizmo->add_handles(handles, handles_material);
661 }
662
663 if (Object::cast_to<HeightMapShape3D>(*s)) {
664 Ref<HeightMapShape3D> hms = s;
665
666 Vector<Vector3> lines = hms->get_debug_mesh_lines();
667 p_gizmo->add_lines(lines, material, false, collision_color);
668 }
669 }