Skip to content

Commit

Permalink
Add helper for 3D gizmos and unify box
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Sep 12, 2023
1 parent 3ed4497 commit 015953a
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 317 deletions.
79 changes: 16 additions & 63 deletions editor/plugins/gizmos/collision_shape_3d_gizmo_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "core/math/geometry_3d.h"
#include "editor/editor_settings.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/plugins/gizmos/gizmo_3d_helper.h"
#include "editor/plugins/node_3d_editor_plugin.h"
#include "scene/3d/collision_shape_3d.h"
#include "scene/resources/box_shape_3d.h"
Expand All @@ -47,6 +48,7 @@
#include "scene/resources/world_boundary_shape_3d.h"

CollisionShape3DGizmoPlugin::CollisionShape3DGizmoPlugin() {
helper.instantiate();
const Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
create_material("shape_material", gizmo_color);
const float gizmo_value = gizmo_color.get_v();
Expand All @@ -55,6 +57,9 @@ CollisionShape3DGizmoPlugin::CollisionShape3DGizmoPlugin() {
create_handle_material("handles");
}

CollisionShape3DGizmoPlugin::~CollisionShape3DGizmoPlugin() {
}

bool CollisionShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
return Object::cast_to<CollisionShape3D>(p_spatial) != nullptr;
}
Expand All @@ -80,7 +85,7 @@ String CollisionShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_g
}

if (Object::cast_to<BoxShape3D>(*s)) {
return "Size";
return helper->box_get_handle_name(p_id);
}

if (Object::cast_to<CapsuleShape3D>(*s)) {
Expand Down Expand Up @@ -135,8 +140,7 @@ Variant CollisionShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p
}

void CollisionShape3DGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
initial_transform = p_gizmo->get_node_3d()->get_global_transform();
initial_value = get_handle_value(p_gizmo, p_id, p_secondary);
helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
}

void CollisionShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
Expand All @@ -147,13 +151,8 @@ void CollisionShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, i
return;
}

Transform3D gt = initial_transform;
Transform3D gi = gt.affine_inverse();

Vector3 ray_from = p_camera->project_ray_origin(p_point);
Vector3 ray_dir = p_camera->project_ray_normal(p_point);

Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
Vector3 sg[2];
helper->get_segment(p_camera, p_point, sg);

if (Object::cast_to<SphereShape3D>(*s)) {
Ref<SphereShape3D> ss = s;
Expand Down Expand Up @@ -188,38 +187,12 @@ void CollisionShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, i
}

if (Object::cast_to<BoxShape3D>(*s)) {
Vector3 axis;
axis[p_id / 2] = 1.0;
Ref<BoxShape3D> bs = s;
Vector3 ra, rb;
int sign = p_id % 2 * -2 + 1;
Vector3 initial_size = initial_value;

Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096 * sign, sg[0], sg[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, sg[0], sg[1], ra, rb);
}

float d = ra[p_id / 2] * sign;

Vector3 he = bs->get_size();
he[p_id / 2] = d * 2;
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
he[p_id / 2] = Math::snapped(he[p_id / 2], Node3DEditor::get_singleton()->get_translate_snap());
}

if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
he[p_id / 2] = MAX(he[p_id / 2], 0.001);
bs->set_size(he);
cs->set_global_position(initial_transform.get_origin());
} else {
he[p_id / 2] = MAX(he[p_id / 2], -initial_size[p_id / 2] + 0.002);
bs->set_size((initial_size + (he - initial_size) * 0.5).abs());
Vector3 pos = initial_transform.affine_inverse().xform(initial_transform.get_origin());
pos += (bs->get_size() - initial_size) * 0.5 * sign;
cs->set_global_position(initial_transform.xform(pos));
}
Vector3 size = bs->get_size();
Vector3 position;
helper->box_set_handle(sg, p_id, size, position);
bs->set_size(size);
cs->set_global_position(position);
}

if (Object::cast_to<CapsuleShape3D>(*s)) {
Expand Down Expand Up @@ -291,20 +264,7 @@ void CollisionShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo
}

if (Object::cast_to<BoxShape3D>(*s)) {
Ref<BoxShape3D> ss = s;
if (p_cancel) {
cs->set_global_position(initial_transform.get_origin());
ss->set_size(p_restore);
return;
}

EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Change Box Shape Size"));
ur->add_do_method(ss.ptr(), "set_size", ss->get_size());
ur->add_do_method(cs, "set_global_position", cs->get_global_position());
ur->add_undo_method(ss.ptr(), "set_size", p_restore);
ur->add_undo_method(cs, "set_global_position", initial_transform.get_origin());
ur->commit_action();
helper->box_commit_handle(TTR("Change Box Shape Size"), p_cancel, cs, s.ptr());
}

if (Object::cast_to<CapsuleShape3D>(*s)) {
Expand Down Expand Up @@ -446,14 +406,7 @@ void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
lines.push_back(b);
}

Vector<Vector3> handles;

for (int i = 0; i < 3; i++) {
Vector3 ax;
ax[i] = bs->get_size()[i] / 2;
handles.push_back(ax);
handles.push_back(-ax);
}
const Vector<Vector3> handles = helper->box_get_handles(bs->get_size());

p_gizmo->add_lines(lines, material);
p_gizmo->add_collision_segments(lines);
Expand Down
6 changes: 4 additions & 2 deletions editor/plugins/gizmos/collision_shape_3d_gizmo_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@

#include "editor/plugins/node_3d_editor_gizmos.h"

class Gizmo3DHelper;

class CollisionShape3DGizmoPlugin : public EditorNode3DGizmoPlugin {
GDCLASS(CollisionShape3DGizmoPlugin, EditorNode3DGizmoPlugin);

Transform3D initial_transform;
Variant initial_value;
Ref<Gizmo3DHelper> helper;

public:
bool has_gizmo(Node3D *p_spatial) override;
Expand All @@ -52,6 +53,7 @@ class CollisionShape3DGizmoPlugin : public EditorNode3DGizmoPlugin {
void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false) override;

CollisionShape3DGizmoPlugin();
~CollisionShape3DGizmoPlugin();
};

#endif // COLLISION_SHAPE_3D_GIZMO_PLUGIN_H
72 changes: 17 additions & 55 deletions editor/plugins/gizmos/decal_gizmo_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@

#include "editor/editor_settings.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/plugins/gizmos/gizmo_3d_helper.h"
#include "editor/plugins/node_3d_editor_plugin.h"
#include "scene/3d/decal.h"

DecalGizmoPlugin::DecalGizmoPlugin() {
helper.instantiate();
Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/decal", Color(0.6, 0.5, 1.0));

create_material("decal_material", gizmo_color);

create_handle_material("handles");
}

DecalGizmoPlugin::~DecalGizmoPlugin() {
}

bool DecalGizmoPlugin::has_gizmo(Node3D *p_spatial) {
return Object::cast_to<Decal>(p_spatial) != nullptr;
}
Expand All @@ -56,69 +61,33 @@ int DecalGizmoPlugin::get_priority() const {
}

String DecalGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
switch (p_id) {
case 0:
return "Size X";
case 1:
return "Size Y";
case 2:
return "Size Z";
}

return "";
return helper->box_get_handle_name(p_id);
}

Variant DecalGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());
return decal->get_size();
}

void DecalGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
}

void DecalGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());
Transform3D gt = decal->get_global_transform();

Transform3D gi = gt.affine_inverse();

Vector3 size = decal->get_size();

Vector3 ray_from = p_camera->project_ray_origin(p_point);
Vector3 ray_dir = p_camera->project_ray_normal(p_point);

Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };

Vector3 axis;
axis[p_id] = 1.0;

Vector3 ra, rb;
Geometry3D::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
float d = ra[p_id] * 2;
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
}

if (d < 0.001) {
d = 0.001;
}
Vector3 sg[2];
helper->get_segment(p_camera, p_point, sg);

size[p_id] = d;
Vector3 position;
helper->box_set_handle(sg, p_id, size, position);
decal->set_size(size);
decal->set_global_position(position);
}

void DecalGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());

Vector3 restore = p_restore;

if (p_cancel) {
decal->set_size(restore);
return;
}

EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Change Decal Size"));
ur->add_do_method(decal, "set_size", decal->get_size());
ur->add_undo_method(decal, "set_size", restore);
ur->commit_action();
helper->box_commit_handle(TTR("Change Decal Size"), p_cancel, p_gizmo->get_node_3d());
}

void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Expand Down Expand Up @@ -153,14 +122,7 @@ void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
lines.push_back(Vector3(0, half_size_y, 0));
lines.push_back(Vector3(0, half_size_y * 1.2, 0));

Vector<Vector3> handles;

for (int i = 0; i < 3; i++) {
Vector3 ax;
ax[i] = aabb.position[i] + aabb.size[i];
handles.push_back(ax);
}

Vector<Vector3> handles = helper->box_get_handles(decal->get_size());
Ref<Material> material = get_material("decal_material", p_gizmo);

p_gizmo->add_lines(lines, material);
Expand Down
6 changes: 6 additions & 0 deletions editor/plugins/gizmos/decal_gizmo_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@

#include "editor/plugins/node_3d_editor_gizmos.h"

class Gizmo3DHelper;

class DecalGizmoPlugin : public EditorNode3DGizmoPlugin {
GDCLASS(DecalGizmoPlugin, EditorNode3DGizmoPlugin);

Ref<Gizmo3DHelper> helper;

public:
bool has_gizmo(Node3D *p_spatial) override;
String get_gizmo_name() const override;
Expand All @@ -44,10 +48,12 @@ class DecalGizmoPlugin : public EditorNode3DGizmoPlugin {

String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override;
Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override;
void begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) override;
void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override;
void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false) override;

DecalGizmoPlugin();
~DecalGizmoPlugin();
};

#endif // DECAL_GIZMO_PLUGIN_H
Loading

0 comments on commit 015953a

Please sign in to comment.