Skip to content

Commit

Permalink
Merge pull request #30283 from Calinou/add-3d-gizmo-snapping
Browse files Browse the repository at this point in the history
Implement snapping for most 3D gizmos
  • Loading branch information
akien-mga authored Jul 3, 2019
2 parents 36d8936 + 0ce59bf commit 2293c61
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
8 changes: 7 additions & 1 deletion editor/plugins/path_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_p
Vector3 ray_from = p_camera->project_ray_origin(p_point);
Vector3 ray_dir = p_camera->project_ray_normal(p_point);

// Setting curve point positions
if (p_idx < c->get_point_count()) {

Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
Expand Down Expand Up @@ -126,6 +127,7 @@ void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_p

Vector3 inters;

// Setting curve in/out positions
if (p.intersects_ray(ray_from, ray_dir, &inters)) {

if (!PathEditorPlugin::singleton->is_handle_clicked()) {
Expand All @@ -135,9 +137,13 @@ void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_p
}

Vector3 local = gi.xform(inters) - base;
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
float snap = SpatialEditor::get_singleton()->get_translate_snap();
local.snap(Vector3(snap, snap, snap));
}

if (t == 0) {
c->set_point_in(idx, local);

if (PathEditorPlugin::singleton->mirror_angle_enabled())
c->set_point_out(idx, PathEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));
} else {
Expand Down
68 changes: 63 additions & 5 deletions editor/spatial_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ void LightSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx,
Geometry::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);

float d = -ra.z;
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0)
d = 0;

Expand All @@ -885,6 +889,10 @@ void LightSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx,
if (cp.intersects_ray(ray_from, ray_dir, &inters)) {

float r = inters.distance_to(gt.origin);
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
r = Math::stepify(r, SpatialEditor::get_singleton()->get_translate_snap());
}

light->set_param(Light::PARAM_RANGE, r);
}
}
Expand Down Expand Up @@ -1257,6 +1265,10 @@ void CameraSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(0, 0, -1), Vector3(4096, 0, -1), s[0], s[1], ra, rb);
float d = ra.x * 2.0;
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0)
d = 0;

Expand Down Expand Up @@ -2310,6 +2322,9 @@ void VisibilityNotifierGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int
Geometry::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb);

float d = ra[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

aabb.position[p_idx] = d - 1.0 - aabb.size[p_idx] * 0.5;
notifier->set_aabb(aabb);
Expand All @@ -2319,6 +2334,10 @@ void VisibilityNotifierGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int
Geometry::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);

float d = ra[p_idx] - ofs[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;
//resize
Expand Down Expand Up @@ -2444,7 +2463,6 @@ void ParticlesGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Ca
Particles *particles = Object::cast_to<Particles>(p_gizmo->get_spatial_node());

Transform gt = particles->get_global_transform();
//gt.orthonormalize();
Transform gi = gt.affine_inverse();

bool move = p_idx >= 3;
Expand All @@ -2467,6 +2485,9 @@ void ParticlesGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Ca
Geometry::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb);

float d = ra[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

aabb.position[p_idx] = d - 1.0 - aabb.size[p_idx] * 0.5;
particles->set_visibility_aabb(aabb);
Expand All @@ -2476,6 +2497,10 @@ void ParticlesGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Ca
Geometry::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);

float d = ra[p_idx] - ofs[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;
//resize
Expand Down Expand Up @@ -2619,6 +2644,10 @@ void ReflectionProbeGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_i
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
float d = ra[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand All @@ -2641,8 +2670,11 @@ void ReflectionProbeGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_i

Vector3 ra, rb;
Geometry::get_closest_points_between_segments(origin - axis * 16384, origin + axis * 16384, sg[0], sg[1], ra, rb);
float d = ra[p_idx];
d += 0.25;
// Adjust the actual position to account for the gizmo handle position
float d = ra[p_idx] + 0.25;
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

origin[p_idx] = d;
probe->set_origin_offset(origin);
Expand Down Expand Up @@ -2780,7 +2812,6 @@ void GIProbeGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Came
GIProbe *probe = Object::cast_to<GIProbe>(p_gizmo->get_spatial_node());

Transform gt = probe->get_global_transform();
//gt.orthonormalize();
Transform gi = gt.affine_inverse();

Vector3 extents = probe->get_extents();
Expand All @@ -2796,6 +2827,10 @@ void GIProbeGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Came
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
float d = ra[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand Down Expand Up @@ -2945,7 +2980,6 @@ void BakedIndirectLightGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int
BakedLightmap *baker = Object::cast_to<BakedLightmap>(p_gizmo->get_spatial_node());

Transform gt = baker->get_global_transform();
//gt.orthonormalize();
Transform gi = gt.affine_inverse();

Vector3 extents = baker->get_extents();
Expand All @@ -2961,6 +2995,10 @@ void BakedIndirectLightGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
float d = ra[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand Down Expand Up @@ -3158,6 +3196,10 @@ void CollisionShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, i
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
float d = ra.x;
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand All @@ -3170,6 +3212,10 @@ void CollisionShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, i
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), Vector3(0, 0, 4096), sg[0], sg[1], ra, rb);
float d = ra.z;
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand All @@ -3184,6 +3230,10 @@ void CollisionShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, i
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
float d = ra[p_idx];
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand All @@ -3202,6 +3252,11 @@ void CollisionShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, i
float d = axis.dot(ra);
if (p_idx == 1)
d -= cs2->get_radius();

if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;

Expand All @@ -3219,6 +3274,9 @@ void CollisionShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, i
Vector3 ra, rb;
Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
float d = axis.dot(ra);
if (SpatialEditor::get_singleton()->is_snap_enabled()) {
d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001)
d = 0.001;
Expand Down

0 comments on commit 2293c61

Please sign in to comment.