Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grid snapping for box shape gizmos #82381

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions editor/plugins/gizmos/gizmo_3d_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,46 @@ String Gizmo3DHelper::box_get_handle_name(int p_id) const {
}

void Gizmo3DHelper::box_set_handle(const Vector3 p_segment[2], int p_id, Vector3 &r_box_size, Vector3 &r_box_position) {
Vector3 axis;
axis[p_id / 2] = 1.0;
Vector3 ra, rb;
int axis = p_id / 2;
int sign = p_id % 2 * -2 + 1;

Vector3 initial_size = initial_value;
float neg_end = initial_size[axis] * -0.5;
float pos_end = initial_size[axis] * 0.5;

Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096 * sign, p_segment[0], p_segment[1], ra, rb);
if (ra[p_id / 2] == 0) {
// Point before half of the shape. Needs to be calculated in opposite direction.
Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096 * -sign, p_segment[0], p_segment[1], ra, rb);
}
Vector3 axis_segment[2] = { Vector3(), Vector3() };
axis_segment[0][axis] = 4096.0;
axis_segment[1][axis] = -4096.0;
Vector3 ra, rb;
Geometry3D::get_closest_points_between_segments(axis_segment[0], axis_segment[1], p_segment[0], p_segment[1], ra, rb);

float d = ra[p_id / 2] * sign;
// Calculate new size.
r_box_size = initial_size;
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
r_box_size[axis] = ra[axis] * sign * 2;
} else {
r_box_size[axis] = sign > 0 ? ra[axis] - neg_end : pos_end - ra[axis];
}

Vector3 he = r_box_size;
he[p_id / 2] = d * 2;
// Snap to grid.
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
he[p_id / 2] = Math::snapped(he[p_id / 2], Node3DEditor::get_singleton()->get_translate_snap());
r_box_size[axis] = Math::snapped(r_box_size[axis], Node3DEditor::get_singleton()->get_translate_snap());
}
r_box_size[axis] = MAX(r_box_size[axis], 0.001);

// Adjust position.
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
he[p_id / 2] = MAX(he[p_id / 2], 0.001);
r_box_size = he;
r_box_position = initial_transform.get_origin();
} else {
he[p_id / 2] = MAX(he[p_id / 2], -initial_size[p_id / 2] + 0.002);
r_box_size = (initial_size + (he - initial_size) * 0.5).abs();
Vector3 pos = initial_transform.affine_inverse().xform(initial_transform.get_origin());
pos += (r_box_size - initial_size) * 0.5 * sign;
r_box_position = initial_transform.xform(pos);
if (sign > 0) {
pos_end = neg_end + r_box_size[axis];
} else {
neg_end = pos_end - r_box_size[axis];
}

Vector3 offset;
offset[axis] = (pos_end + neg_end) * 0.5;
r_box_position = initial_transform.xform(offset);
}
}

Expand Down
Loading