Skip to content

Commit

Permalink
Implement closed path for Curve3d
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenCrowDev committed Jul 8, 2024
1 parent 42e5b3a commit eb023a9
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 45 deletions.
9 changes: 9 additions & 0 deletions doc/classes/Curve3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,20 @@
[param tolerance_length] controls the maximal distance between two neighboring points, before the segment has to be subdivided.
</description>
</method>
<method name="toggle_closed">
<return type="void" />
<description>
Toggles [member closed].
</description>
</method>
</methods>
<members>
<member name="bake_interval" type="float" setter="set_bake_interval" getter="get_bake_interval" default="0.2">
The distance in meters between two adjacent cached points. Changing it forces the cache to be recomputed the next time the [method get_baked_points] or [method get_baked_length] function is called. The smaller the distance, the more points in the cache and the more memory it will consume, so use with care.
</member>
<member name="closed" type="bool" setter="set_closed" getter="is_closed" default="false">
If [code]true[/code], and the curve has more than 2 control points, the last point and the first one will be connected in a loop.
</member>
<member name="point_count" type="int" setter="set_point_count" getter="get_point_count" default="0">
The number of points describing the curve.
</member>
Expand Down
118 changes: 99 additions & 19 deletions editor/plugins/path_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,15 @@ void Path3DGizmo::redraw() {
Ref<StandardMaterial3D> path_tilt_material = gizmo_plugin->get_material("path_tilt_material", this);
Ref<StandardMaterial3D> path_tilt_muted_material = gizmo_plugin->get_material("path_tilt_muted_material", this);
Ref<StandardMaterial3D> handles_material = gizmo_plugin->get_material("handles");
Ref<StandardMaterial3D> first_pt_handle_material = gizmo_plugin->get_material("first_pt_handle");
Ref<StandardMaterial3D> last_pt_handle_material = gizmo_plugin->get_material("last_pt_handle");
Ref<StandardMaterial3D> closed_pt_handle_material = gizmo_plugin->get_material("closed_pt_handle");
Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_handles");

first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));

Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
return;
Expand Down Expand Up @@ -369,7 +376,7 @@ void Path3DGizmo::redraw() {
info.point_idx = idx;

// Collect in-handles except for the first point.
if (idx > 0 && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
if (idx > (c->is_closed() ? -1 : 0) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
const Vector3 in = c->get_point_in(idx);

info.type = HandleType::HANDLE_TYPE_IN;
Expand All @@ -383,7 +390,7 @@ void Path3DGizmo::redraw() {
}

// Collect out-handles except for the last point.
if (idx < c->get_point_count() - 1 && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
if (idx < (c->is_closed() ? c->get_point_count() : c->get_point_count() - 1) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
const Vector3 out = c->get_point_out(idx);

info.type = HandleType::HANDLE_TYPE_OUT;
Expand Down Expand Up @@ -441,7 +448,42 @@ void Path3DGizmo::redraw() {
}

if (!Path3DEditorPlugin::singleton->curve_edit->is_pressed() && primary_handle_points.size()) {
add_handles(primary_handle_points, handles_material);
// Need to define indices separately.
// Point count.
const int pc = primary_handle_points.size();
Vector<int> idx;
idx.resize(pc);
int *idx_ptr = idx.ptrw();
for (int j = 0; j < pc; j++) {
idx_ptr[j] = j;
}

// Initialize arrays for first point.
PackedVector3Array first_pt_handle_point;
Vector<int> first_pt_id;
first_pt_handle_point.append(primary_handle_points[0]);
first_pt_id.append(idx[0]);

// Initialize arrays and add handle for last point if needed.
if (pc > 1) {
PackedVector3Array last_pt_handle_point;
Vector<int> last_pt_id;
last_pt_handle_point.append(primary_handle_points[pc - 1]);
last_pt_id.append(idx[pc - 1]);
primary_handle_points.remove_at(pc - 1);
idx.remove_at(pc - 1);
add_handles(last_pt_handle_point, c->is_closed() ? handles_material : last_pt_handle_material, last_pt_id);
}

// Add handle for first point.
primary_handle_points.remove_at(0);
idx.remove_at(0);
add_handles(first_pt_handle_point, c->is_closed() ? closed_pt_handle_material : first_pt_handle_material, first_pt_id);

// Add handles for remaining intermediate points.
if (!primary_handle_points.is_empty()) {
add_handles(primary_handle_points, handles_material, idx);
}
}
if (secondary_handle_points.size()) {
add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);
Expand Down Expand Up @@ -469,7 +511,7 @@ Path3DGizmo::Path3DGizmo(Path3D *p_path, float p_disk_size) {
Path3DEditorPlugin::singleton->curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
Path3DEditorPlugin::singleton->curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
Path3DEditorPlugin::singleton->curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
Path3DEditorPlugin::singleton->curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
Path3DEditorPlugin::singleton->curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
}

EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
Expand Down Expand Up @@ -695,21 +737,18 @@ void Path3DEditorPlugin::_mode_changed(int p_mode) {
Node3DEditor::get_singleton()->clear_subgizmo_selection();
}

void Path3DEditorPlugin::_close_curve() {
void Path3DEditorPlugin::_toggle_closed_curve() {
Ref<Curve3D> c = path->get_curve();
if (c.is_null()) {
return;
}
if (c->get_point_count() < 2) {
return;
}
if (c->get_point_position(0) == c->get_point_position(c->get_point_count() - 1)) {
return;
}
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Close Curve"));
ur->add_do_method(c.ptr(), "add_point", c->get_point_position(0), c->get_point_in(0), c->get_point_out(0), -1);
ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
ur->create_action(TTR("Toggle Open/Closed Curve"));
ur->add_do_method(c.ptr(), "toggle_closed");
ur->add_undo_method(c.ptr(), "toggle_closed");
ur->commit_action();
}

Expand Down Expand Up @@ -781,7 +820,7 @@ void Path3DEditorPlugin::_update_theme() {
curve_edit_tilt->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveTilt"), EditorStringName(EditorIcons)));
curve_create->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveCreate"), EditorStringName(EditorIcons)));
curve_del->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveDelete"), EditorStringName(EditorIcons)));
curve_close->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveClose"), EditorStringName(EditorIcons)));
curve_closed->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveClose"), EditorStringName(EditorIcons)));
curve_clear_points->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Clear"), EditorStringName(EditorIcons)));
}

Expand All @@ -793,7 +832,7 @@ void Path3DEditorPlugin::_notification(int p_what) {
curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));
curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_close_curve));
curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_toggle_closed_curve));

_update_theme();
} break;
Expand Down Expand Up @@ -865,11 +904,11 @@ Path3DEditorPlugin::Path3DEditorPlugin() {
curve_del->set_tooltip_text(TTR("Delete Point"));
topmenu_bar->add_child(curve_del);

curve_close = memnew(Button);
curve_close->set_theme_type_variation("FlatButton");
curve_close->set_focus_mode(Control::FOCUS_NONE);
curve_close->set_tooltip_text(TTR("Close Curve"));
topmenu_bar->add_child(curve_close);
curve_closed = memnew(Button);
curve_closed->set_theme_type_variation("FlatButton");
curve_closed->set_focus_mode(Control::FOCUS_NONE);
curve_closed->set_tooltip_text(TTR("Toggle Open/Closed Curve"));
topmenu_bar->add_child(curve_closed);

curve_clear_points = memnew(Button);
curve_clear_points->set_theme_type_variation("FlatButton");
Expand Down Expand Up @@ -930,6 +969,14 @@ void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Ref<Curve3D> curve = path->get_curve();

Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);
Ref<StandardMaterial3D> first_pt_handle_material = get_material("first_pt_handle", p_gizmo);
Ref<StandardMaterial3D> last_pt_handle_material = get_material("last_pt_handle", p_gizmo);
Ref<StandardMaterial3D> closed_pt_handle_material = get_material("closed_pt_handle", p_gizmo);

first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));

PackedVector3Array handles;

if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
Expand All @@ -942,7 +989,37 @@ void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
}

if (handles.size()) {
p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
// Point count.
const int pc = handles.size();

// Initialize arrays for first point.
PackedVector3Array first_pt;
first_pt.append(handles[0]);

// Initialize arrays and add handle for last point if needed.
if (pc > 1) {
PackedVector3Array last_pt;
last_pt.append(handles[handles.size() - 1]);
handles.remove_at(handles.size() - 1);
if (curve->is_closed()) {
p_gizmo->add_vertices(last_pt, handle_material, Mesh::PRIMITIVE_POINTS);
} else {
p_gizmo->add_vertices(last_pt, last_pt_handle_material, Mesh::PRIMITIVE_POINTS);
}
}

// Add handle for first point.
handles.remove_at(0);
if (curve->is_closed()) {
p_gizmo->add_vertices(first_pt, closed_pt_handle_material, Mesh::PRIMITIVE_POINTS);
} else {
p_gizmo->add_vertices(first_pt, first_pt_handle_material, Mesh::PRIMITIVE_POINTS);
}

// Add handles for remaining intermediate points.
if (!handles.is_empty()) {
p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
}
}
}

Expand Down Expand Up @@ -1059,5 +1136,8 @@ Path3DGizmoPlugin::Path3DGizmoPlugin(float p_disk_size) {
create_material("path_tilt_material", path_tilt_color);
create_material("path_tilt_muted_material", path_tilt_color * 0.7);
create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
create_handle_material("first_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
create_handle_material("last_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
create_handle_material("closed_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
}
4 changes: 2 additions & 2 deletions editor/plugins/path_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Path3DEditorPlugin : public EditorPlugin {
Button *curve_edit_curve = nullptr;
Button *curve_edit_tilt = nullptr;
Button *curve_del = nullptr;
Button *curve_close = nullptr;
Button *curve_closed = nullptr;
Button *curve_clear_points = nullptr;
MenuButton *handle_menu = nullptr;

Expand All @@ -140,7 +140,7 @@ class Path3DEditorPlugin : public EditorPlugin {
void _update_theme();

void _mode_changed(int p_mode);
void _close_curve();
void _toggle_closed_curve();
void _handle_option_pressed(int p_option);
bool handle_clicked = false;
bool mirror_handle_angle;
Expand Down
Loading

0 comments on commit eb023a9

Please sign in to comment.