Skip to content

Commit 13907ec

Browse files
committedMar 11, 2025
Merge pull request #103910 from kiroxas/ImproveSphereGizmoPerformance
Improve Sphere gizmo performance
2 parents 590fa90 + 4915f91 commit 13907ec

File tree

1 file changed

+63
-28
lines changed

1 file changed

+63
-28
lines changed
 

‎editor/plugins/gizmos/collision_shape_3d_gizmo_plugin.cpp

+63-28
Original file line numberDiff line numberDiff line change
@@ -357,42 +357,77 @@ void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
357357

358358
if (Object::cast_to<SphereShape3D>(*s)) {
359359
Ref<SphereShape3D> sp = s;
360-
float r = sp->get_radius();
360+
float radius = sp->get_radius();
361+
362+
#define PUSH_QUARTER(from_x, from_y, to_x, to_y, y) \
363+
points_ptrw[index++] = Vector3(from_x, y, from_y); \
364+
points_ptrw[index++] = Vector3(to_x, y, to_y); \
365+
points_ptrw[index++] = Vector3(from_x, y, -from_y); \
366+
points_ptrw[index++] = Vector3(to_x, y, -to_y); \
367+
points_ptrw[index++] = Vector3(-from_x, y, from_y); \
368+
points_ptrw[index++] = Vector3(-to_x, y, to_y); \
369+
points_ptrw[index++] = Vector3(-from_x, y, -from_y); \
370+
points_ptrw[index++] = Vector3(-to_x, y, -to_y);
371+
372+
#define PUSH_QUARTER_XY(from_x, from_y, to_x, to_y, y) \
373+
points_ptrw[index++] = Vector3(from_x, -from_y - y, 0); \
374+
points_ptrw[index++] = Vector3(to_x, -to_y - y, 0); \
375+
points_ptrw[index++] = Vector3(from_x, from_y + y, 0); \
376+
points_ptrw[index++] = Vector3(to_x, to_y + y, 0); \
377+
points_ptrw[index++] = Vector3(-from_x, -from_y - y, 0); \
378+
points_ptrw[index++] = Vector3(-to_x, -to_y - y, 0); \
379+
points_ptrw[index++] = Vector3(-from_x, from_y + y, 0); \
380+
points_ptrw[index++] = Vector3(-to_x, to_y + y, 0);
381+
382+
#define PUSH_QUARTER_YZ(from_x, from_y, to_x, to_y, y) \
383+
points_ptrw[index++] = Vector3(0, -from_y - y, from_x); \
384+
points_ptrw[index++] = Vector3(0, -to_y - y, to_x); \
385+
points_ptrw[index++] = Vector3(0, from_y + y, from_x); \
386+
points_ptrw[index++] = Vector3(0, to_y + y, to_x); \
387+
points_ptrw[index++] = Vector3(0, -from_y - y, -from_x); \
388+
points_ptrw[index++] = Vector3(0, -to_y - y, -to_x); \
389+
points_ptrw[index++] = Vector3(0, from_y + y, -from_x); \
390+
points_ptrw[index++] = Vector3(0, to_y + y, -to_x);
391+
392+
// Number of points in an octant. So there will be 8 * points_in_octant * 2 points in total for one circle.
393+
// This Corresponds to the smoothness of the circle.
394+
const uint32_t points_in_octant = 16;
395+
const real_t inc = (Math_PI / (4 * points_in_octant));
396+
const real_t radius_squared = radius * radius;
397+
real_t r = 0;
361398

362399
Vector<Vector3> points;
400+
uint32_t index = 0;
401+
// 3 full circles.
402+
points.resize(3 * 8 * points_in_octant * 2);
403+
Vector3 *points_ptrw = points.ptrw();
363404

364-
for (int i = 0; i <= 360; i++) {
365-
float ra = Math::deg_to_rad((float)i);
366-
float rb = Math::deg_to_rad((float)i + 1);
367-
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
368-
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
369-
370-
points.push_back(Vector3(a.x, 0, a.y));
371-
points.push_back(Vector3(b.x, 0, b.y));
372-
points.push_back(Vector3(0, a.x, a.y));
373-
points.push_back(Vector3(0, b.x, b.y));
374-
points.push_back(Vector3(a.x, a.y, 0));
375-
points.push_back(Vector3(b.x, b.y, 0));
376-
}
405+
float previous_x = radius;
406+
float previous_y = 0.f;
377407

378-
Vector<Vector3> collision_segments;
408+
for (uint32_t i = 0; i < points_in_octant; ++i) {
409+
r += inc;
410+
real_t x = Math::cos(r) * radius;
411+
real_t y = Math::sqrt(radius_squared - (x * x));
379412

380-
for (int i = 0; i < 64; i++) {
381-
float ra = i * (Math_TAU / 64.0);
382-
float rb = (i + 1) * (Math_TAU / 64.0);
383-
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
384-
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
385-
386-
collision_segments.push_back(Vector3(a.x, 0, a.y));
387-
collision_segments.push_back(Vector3(b.x, 0, b.y));
388-
collision_segments.push_back(Vector3(0, a.x, a.y));
389-
collision_segments.push_back(Vector3(0, b.x, b.y));
390-
collision_segments.push_back(Vector3(a.x, a.y, 0));
391-
collision_segments.push_back(Vector3(b.x, b.y, 0));
413+
PUSH_QUARTER(previous_x, previous_y, x, y, 0);
414+
PUSH_QUARTER(previous_y, previous_x, y, x, 0);
415+
416+
PUSH_QUARTER_XY(previous_x, previous_y, x, y, 0);
417+
PUSH_QUARTER_XY(previous_y, previous_x, y, x, 0);
418+
419+
PUSH_QUARTER_YZ(previous_x, previous_y, x, y, 0);
420+
PUSH_QUARTER_YZ(previous_y, previous_x, y, x, 0)
421+
422+
previous_x = x;
423+
previous_y = y;
392424
}
425+
#undef PUSH_QUARTER
426+
#undef PUSH_QUARTER_XY
427+
#undef PUSH_QUARTER_YZ
393428

394429
p_gizmo->add_lines(points, material, false, collision_color);
395-
p_gizmo->add_collision_segments(collision_segments);
430+
p_gizmo->add_collision_segments(points);
396431
Vector<Vector3> handles;
397432
handles.push_back(Vector3(r, 0, 0));
398433
p_gizmo->add_handles(handles, handles_material);

0 commit comments

Comments
 (0)
Please sign in to comment.