Path: blob/master/editor/scene/3d/path_3d_editor_plugin.cpp
20837 views
/**************************************************************************/1/* path_3d_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "path_3d_editor_plugin.h"3132#include "core/math/geometry_2d.h"33#include "core/math/geometry_3d.h"34#include "core/os/keyboard.h"35#include "editor/editor_node.h"36#include "editor/editor_string_names.h"37#include "editor/editor_undo_redo_manager.h"38#include "editor/scene/3d/node_3d_editor_plugin.h"39#include "editor/settings/editor_settings.h"40#include "scene/gui/dialogs.h"41#include "scene/gui/menu_button.h"42#include "scene/resources/curve.h"4344String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {45Ref<Curve3D> c = path->get_curve();46if (c.is_null()) {47return "";48}4950// Primary handles: position.51if (!p_secondary) {52return TTR("Curve Point #") + itos(p_id);53}5455// Secondary handles: in, out, tilt.56const HandleInfo info = _secondary_handles_info[p_id];57switch (info.type) {58case HandleType::HANDLE_TYPE_IN:59return TTR("Handle In #") + itos(info.point_idx);60case HandleType::HANDLE_TYPE_OUT:61return TTR("Handle Out #") + itos(info.point_idx);62case HandleType::HANDLE_TYPE_TILT:63return TTR("Handle Tilt #") + itos(info.point_idx);64}6566return "";67}6869Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {70Ref<Curve3D> c = path->get_curve();71if (c.is_null()) {72return Variant();73}7475// Primary handles: position.76if (!p_secondary) {77original = c->get_point_position(p_id);78return original;79}8081// Secondary handles: in, out, tilt.82const HandleInfo info = _secondary_handles_info[p_id];83Vector3 ofs;84switch (info.type) {85case HandleType::HANDLE_TYPE_TILT:86return c->get_point_tilt(info.point_idx);87case HandleType::HANDLE_TYPE_IN:88ofs = c->get_point_in(info.point_idx);89break;90case HandleType::HANDLE_TYPE_OUT:91ofs = c->get_point_out(info.point_idx);92break;93}9495original = ofs + c->get_point_position(info.point_idx);96return ofs;97}9899void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {100Ref<Curve3D> c = path->get_curve();101if (c.is_null()) {102return;103}104105const Transform3D gt = path->get_global_transform();106const Transform3D gi = gt.affine_inverse();107const Vector3 ray_from = p_camera->project_ray_origin(p_point);108const Vector3 ray_dir = p_camera->project_ray_normal(p_point);109const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));110111// Primary handles: position.112if (!p_secondary) {113Vector3 inters;114// Special case for primary handle, the handle id equals control point id.115const int idx = p_id;116if (!Path3DEditorPlugin::singleton->_edit.waiting_handle_physics || !Path3DEditorPlugin::singleton->_edit.in_physics_frame) {117Path3DEditorPlugin::singleton->_edit.waiting_handle_physics = true;118Path3DEditorPlugin::singleton->_edit.gizmo_handle = p_id;119Path3DEditorPlugin::singleton->_edit.gizmo_handle_secondary = p_secondary;120Path3DEditorPlugin::singleton->_edit.gizmo_camera = p_camera;121Path3DEditorPlugin::singleton->_edit.mouse_pos = p_point;122return;123// Only continue if inside physics frame and waiting for physics.124}125if (Path3DEditorPlugin::singleton->snap_to_collider) {126PhysicsDirectSpaceState3D *ss = p_camera->get_world_3d()->get_direct_space_state();127128PhysicsDirectSpaceState3D::RayParameters ray_params;129ray_params.from = ray_from;130ray_params.to = ray_from + ray_dir * p_camera->get_far();131PhysicsDirectSpaceState3D::RayResult result;132if (ss->intersect_ray(ray_params, result)) {133Vector3 local = gi.xform(result.position);134c->set_point_position(idx, local);135return;136}137// Will continue and do the plane intersect_ray if doesn't hit anything.138}139if (p.intersects_ray(ray_from, ray_dir, &inters)) {140if (Node3DEditor::get_singleton()->is_snap_enabled()) {141float snap = Node3DEditor::get_singleton()->get_translate_snap();142inters.snapf(snap);143}144145Vector3 local = gi.xform(inters);146c->set_point_position(idx, local);147}148149return;150}151152// Secondary handles: in, out, tilt.153const HandleInfo info = _secondary_handles_info[p_id];154switch (info.type) {155case HandleType::HANDLE_TYPE_OUT:156case HandleType::HANDLE_TYPE_IN: {157const int idx = info.point_idx;158const Vector3 base = c->get_point_position(idx);159160Vector3 inters;161if (p.intersects_ray(ray_from, ray_dir, &inters)) {162if (!Path3DEditorPlugin::singleton->is_handle_clicked()) {163orig_in_length = c->get_point_in(idx).length();164orig_out_length = c->get_point_out(idx).length();165Path3DEditorPlugin::singleton->set_handle_clicked(true);166}167168Vector3 local = gi.xform(inters) - base;169if (Node3DEditor::get_singleton()->is_snap_enabled()) {170float snap = Node3DEditor::get_singleton()->get_translate_snap();171local.snapf(snap);172}173174// Determine if control points should be swapped based on delta movement.175// Only run on the next update after an overlap is detected, to get proper delta movement.176if (control_points_overlapped) {177control_points_overlapped = false;178Vector3 delta = local - (info.type == HANDLE_TYPE_IN ? c->get_point_in(idx) : c->get_point_out(idx));179Vector3 p0 = c->get_point_position(idx - 1) - base;180Vector3 p1 = c->get_point_position(idx + 1) - base;181HandleType new_type = Math::abs(delta.angle_to(p0)) < Math::abs(delta.angle_to(p1)) ? HANDLE_TYPE_IN : HANDLE_TYPE_OUT;182if (info.type != new_type) {183swapped_control_points_idx = idx;184}185}186187// Detect control points overlap.188bool control_points_equal = c->get_point_in(idx).is_equal_approx(c->get_point_out(idx));189if (idx > 0 && idx < (c->get_point_count() - 1) && control_points_equal) {190control_points_overlapped = true;191}192193HandleType control_type = info.type;194if (swapped_control_points_idx == idx) {195control_type = info.type == HANDLE_TYPE_IN ? HANDLE_TYPE_OUT : HANDLE_TYPE_IN;196}197198if (control_type == HandleType::HANDLE_TYPE_IN) {199c->set_point_in(idx, local);200if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {201c->set_point_out(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));202}203} else {204c->set_point_out(idx, local);205if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {206c->set_point_in(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_in_length));207}208}209}210break;211}212case HandleType::HANDLE_TYPE_TILT: {213const int idx = info.point_idx;214const Vector3 position = c->get_point_position(idx);215const Basis posture = c->get_point_baked_posture(idx);216const Vector3 tangent = -posture.get_column(2);217const Vector3 up = posture.get_column(1);218const Plane tilt_plane_global = gt.xform(Plane(tangent, position));219220Vector3 intersection;221222if (tilt_plane_global.intersects_ray(ray_from, ray_dir, &intersection)) {223Vector3 direction = gi.xform(intersection) - position;224real_t tilt_angle = up.signed_angle_to(direction, tangent);225226if (Node3DEditor::get_singleton()->is_snap_enabled()) {227real_t snap_degrees = Node3DEditor::get_singleton()->get_rotate_snap();228tilt_angle = Math::deg_to_rad(Math::snapped(Math::rad_to_deg(tilt_angle), snap_degrees));229}230231c->set_point_tilt(idx, tilt_angle);232}233break;234}235}236}237238void Path3DGizmo::commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {239swapped_control_points_idx = -1;240control_points_overlapped = false;241242Ref<Curve3D> c = path->get_curve();243if (c.is_null()) {244return;245}246247EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();248249// Primary handles: position.250if (!p_secondary && !Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {251// Special case for primary handle, the handle id equals control point id.252const int idx = p_id;253if (p_cancel) {254c->set_point_position(idx, p_restore);255return;256}257ur->create_action(TTR("Set Curve Point Position"));258ur->add_do_method(c.ptr(), "set_point_position", idx, c->get_point_position(idx));259ur->add_undo_method(c.ptr(), "set_point_position", idx, p_restore);260ur->commit_action();261262return;263}264265// Secondary handles: in, out, tilt.266const HandleInfo info = _secondary_handles_info[p_id];267const int idx = info.point_idx;268switch (info.type) {269case HandleType::HANDLE_TYPE_OUT: {270if (p_cancel) {271c->set_point_out(idx, p_restore);272273return;274}275276ur->create_action(TTR("Set Curve Out Position"));277ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));278ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);279280if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {281ur->add_do_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_out(idx) : (-c->get_point_out(idx).normalized() * orig_in_length));282ur->add_undo_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_in_length));283}284285ur->commit_action();286break;287}288case HandleType::HANDLE_TYPE_IN: {289if (p_cancel) {290c->set_point_in(idx, p_restore);291return;292}293294ur->create_action(TTR("Set Curve In Position"));295ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));296ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);297298if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {299ur->add_do_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_in(idx) : (-c->get_point_in(idx).normalized() * orig_out_length));300ur->add_undo_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_out_length));301}302303ur->commit_action();304break;305}306case HandleType::HANDLE_TYPE_TILT: {307if (p_cancel) {308c->set_point_tilt(idx, p_restore);309return;310}311ur->create_action(TTR("Set Curve Point Tilt"));312ur->add_do_method(c.ptr(), "set_point_tilt", idx, c->get_point_tilt(idx));313ur->add_undo_method(c.ptr(), "set_point_tilt", idx, p_restore);314315ur->commit_action();316break;317}318}319}320321void Path3DGizmo::redraw() {322clear();323324Ref<StandardMaterial3D> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);325Ref<StandardMaterial3D> path_tilt_material = gizmo_plugin->get_material("path_tilt_material", this);326Ref<StandardMaterial3D> path_tilt_muted_material = gizmo_plugin->get_material("path_tilt_muted_material", this);327Ref<StandardMaterial3D> handles_material = gizmo_plugin->get_material("handles");328Ref<StandardMaterial3D> first_pt_handle_material = gizmo_plugin->get_material("first_pt_handle");329Ref<StandardMaterial3D> last_pt_handle_material = gizmo_plugin->get_material("last_pt_handle");330Ref<StandardMaterial3D> closed_pt_handle_material = gizmo_plugin->get_material("closed_pt_handle");331Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_handles");332333first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));334last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));335closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));336337Ref<Curve3D> c = path->get_curve();338if (c.is_null()) {339return;340}341342debug_material = gizmo_plugin->get_material("path_material", this);343344Color path_color = path->get_debug_custom_color();345if (path_color != Color(0.0, 0.0, 0.0)) {346debug_material.instantiate();347debug_material->set_albedo(path_color);348debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);349debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);350debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);351debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);352debug_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);353}354355real_t interval = 0.1;356const real_t length = c->get_baked_length();357358// 1. Draw curve and bones if it is visible (alpha > 0.0).359if (length > CMP_EPSILON && path_color.a > 0.0) {360const int sample_count = int(length / interval) + 2;361interval = length / (sample_count - 1); // Recalculate real interval length.362363Vector<Transform3D> frames;364frames.resize(sample_count);365366{367Transform3D *w = frames.ptrw();368369for (int i = 0; i < sample_count; i++) {370w[i] = c->sample_baked_with_rotation(i * interval, true, true);371}372}373374const Transform3D *r = frames.ptr();375376Vector<Vector3> _collision_segments;377_collision_segments.resize((sample_count - 1) * 2);378Vector3 *_collisions_ptr = _collision_segments.ptrw();379380Vector<Vector3> bones;381bones.resize(sample_count * 4);382Vector3 *bones_ptr = bones.ptrw();383384Vector<Vector3> ribbon;385ribbon.resize(sample_count);386Vector3 *ribbon_ptr = ribbon.ptrw();387388for (int i = 0; i < sample_count; i++) {389const Vector3 p1 = r[i].origin;390const Vector3 side = r[i].basis.get_column(0);391const Vector3 up = r[i].basis.get_column(1);392const Vector3 forward = r[i].basis.get_column(2);393394// Collision segments.395if (i != sample_count - 1) {396const Vector3 p2 = r[i + 1].origin;397_collisions_ptr[(i * 2)] = p1;398_collisions_ptr[(i * 2) + 1] = p2;399}400401// Path3D as a ribbon.402ribbon_ptr[i] = p1;403404if (i % 4 == 0) {405// Draw fish bone every 4 points to reduce visual noise and performance impact406// (compared to drawing it for every point).407const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;408const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;409410const int bone_idx = i * 4;411412bones_ptr[bone_idx] = p1;413bones_ptr[bone_idx + 1] = p_left;414bones_ptr[bone_idx + 2] = p1;415bones_ptr[bone_idx + 3] = p_right;416}417}418419add_collision_segments(_collision_segments);420add_lines(bones, debug_material);421add_vertices(ribbon, debug_material, Mesh::PRIMITIVE_LINE_STRIP);422}423424// 2. Draw handles when selected.425if (Path3DEditorPlugin::singleton->get_edited_path() == path) {426PackedVector3Array handle_lines;427PackedVector3Array tilt_handle_lines;428PackedVector3Array primary_handle_points;429PackedVector3Array secondary_handle_points;430PackedInt32Array collected_secondary_handle_ids; // Avoid shadowing member on Node3DEditorGizmo.431432_secondary_handles_info.resize(c->get_point_count() * 3);433434const float disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");435436for (int idx = 0; idx < c->get_point_count(); idx++) {437// Collect primary-handles.438const Vector3 pos = c->get_point_position(idx);439primary_handle_points.append(pos);440441HandleInfo info;442info.point_idx = idx;443444// Collect in-handles except for the first point.445if (idx > (c->is_closed() ? -1 : 0) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {446const Vector3 in = c->get_point_in(idx);447448info.type = HandleType::HANDLE_TYPE_IN;449const int handle_idx = idx * 3 + 0;450collected_secondary_handle_ids.append(handle_idx);451_secondary_handles_info.write[handle_idx] = info;452453secondary_handle_points.append(pos + in);454handle_lines.append(pos);455handle_lines.append(pos + in);456}457458// Collect out-handles except for the last point.459if (idx < (c->is_closed() ? c->get_point_count() : c->get_point_count() - 1) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {460const Vector3 out = c->get_point_out(idx);461462info.type = HandleType::HANDLE_TYPE_OUT;463const int handle_idx = idx * 3 + 1;464collected_secondary_handle_ids.append(handle_idx);465_secondary_handles_info.write[handle_idx] = info;466467secondary_handle_points.append(pos + out);468handle_lines.append(pos);469handle_lines.append(pos + out);470}471472// Collect tilt-handles.473if (Path3DEditorPlugin::singleton->curve_edit_tilt->is_pressed()) {474// Tilt handle.475{476info.type = HandleType::HANDLE_TYPE_TILT;477const int handle_idx = idx * 3 + 2;478collected_secondary_handle_ids.append(handle_idx);479_secondary_handles_info.write[handle_idx] = info;480481const Basis posture = c->get_point_baked_posture(idx, true);482const Vector3 up = posture.get_column(1);483secondary_handle_points.append(pos + up * disk_size);484tilt_handle_lines.append(pos);485tilt_handle_lines.append(pos + up * disk_size);486}487488// Tilt disk.489{490const Basis posture = c->get_point_baked_posture(idx, false);491const Vector3 up = posture.get_column(1);492const Vector3 side = posture.get_column(0);493494PackedVector3Array disk;495disk.append(pos);496497const int n = 36;498for (int i = 0; i <= n; i++) {499const float a = Math::TAU * i / n;500const Vector3 edge = std::sin(a) * side + std::cos(a) * up;501disk.append(pos + edge * disk_size);502}503add_vertices(disk, debug_material, Mesh::PRIMITIVE_LINE_STRIP);504}505}506}507508if (handle_lines.size() > 1) {509add_lines(handle_lines, path_thin_material);510}511512if (tilt_handle_lines.size() > 1) {513add_lines(tilt_handle_lines, path_tilt_material);514}515516if (!Path3DEditorPlugin::singleton->curve_edit->is_pressed() && primary_handle_points.size()) {517// Need to define indices separately.518// Point count.519const int pc = primary_handle_points.size();520Vector<int> idx;521idx.resize(pc);522int *idx_ptr = idx.ptrw();523for (int j = 0; j < pc; j++) {524idx_ptr[j] = j;525}526527// Initialize arrays for first point.528PackedVector3Array first_pt_handle_point;529Vector<int> first_pt_id;530first_pt_handle_point.append(primary_handle_points[0]);531first_pt_id.append(idx[0]);532533// Initialize arrays and add handle for last point if needed.534if (pc > 1) {535PackedVector3Array last_pt_handle_point;536Vector<int> last_pt_id;537last_pt_handle_point.append(primary_handle_points[pc - 1]);538last_pt_id.append(idx[pc - 1]);539primary_handle_points.remove_at(pc - 1);540idx.remove_at(pc - 1);541add_handles(last_pt_handle_point, c->is_closed() ? handles_material : last_pt_handle_material, last_pt_id);542}543544// Add handle for first point.545primary_handle_points.remove_at(0);546idx.remove_at(0);547add_handles(first_pt_handle_point, c->is_closed() ? closed_pt_handle_material : first_pt_handle_material, first_pt_id);548549// Add handles for remaining intermediate points.550if (!primary_handle_points.is_empty()) {551add_handles(primary_handle_points, handles_material, idx);552}553}554if (secondary_handle_points.size()) {555add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);556}557// Draw the gizmo plugin manually, because handles are registered. In which case, the caller code skips drawing the gizmo plugin.558gizmo_plugin->redraw(this);559}560}561562void Path3DGizmo::_update_transform_gizmo() {563Node3DEditor::get_singleton()->update_transform_gizmo();564}565566Path3DGizmo::Path3DGizmo(Path3D *p_path) {567path = p_path;568set_node_3d(p_path);569orig_in_length = 0;570orig_out_length = 0;571572// Connecting to a signal once, rather than plaguing the implementation with calls to `Node3DEditor::update_transform_gizmo`.573path->connect("curve_changed", callable_mp(this, &Path3DGizmo::_update_transform_gizmo));574path->connect("debug_color_changed", callable_mp(this, &Path3DGizmo::redraw));575576Path3DEditorPlugin::singleton->curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));577Path3DEditorPlugin::singleton->curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));578Path3DEditorPlugin::singleton->curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));579Path3DEditorPlugin::singleton->curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));580Path3DEditorPlugin::singleton->curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));581}582583EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {584if (!path) {585return EditorPlugin::AFTER_GUI_INPUT_PASS;586}587Ref<Curve3D> c = path->get_curve();588if (c.is_null()) {589return EditorPlugin::AFTER_GUI_INPUT_PASS;590}591Transform3D gt = path->get_global_transform();592Transform3D it = gt.affine_inverse();593594static const int click_dist = 10; //should make global595596Ref<InputEventMouseButton> mb = p_event;597598if (mb.is_valid()) {599Point2 mbpos(mb->get_position().x, mb->get_position().y);600601Node3DEditorViewport *viewport = nullptr;602for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {603Node3DEditorViewport *vp = Node3DEditor::get_singleton()->get_editor_viewport(i);604if (vp->get_camera_3d() == p_camera) {605viewport = vp;606break;607}608}609610ERR_FAIL_NULL_V(viewport, EditorPlugin::AFTER_GUI_INPUT_PASS);611612if (!mb->is_pressed()) {613set_handle_clicked(false);614}615616if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_command_or_control_pressed()))) {617//click into curve, break it down618Vector<Vector3> v3a = c->tessellate();619int rc = v3a.size();620int closest_seg = -1;621Vector3 closest_seg_point;622623if (rc >= 2) {624int idx = 0;625const Vector3 *r = v3a.ptr();626float closest_d = 1e20;627628if (viewport->point_to_screen(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {629return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing630}631632for (int i = 0; i < c->get_point_count() - 1; i++) {633//find the offset and point index of the place to break up634int j = idx;635if (viewport->point_to_screen(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {636return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing637}638639while (j < rc && c->get_point_position(i + 1) != r[j]) {640Vector3 from = r[j];641Vector3 to = r[j + 1];642real_t cdist = from.distance_to(to);643from = gt.xform(from);644to = gt.xform(to);645if (cdist > 0) {646const Vector2 segment_a = viewport->point_to_screen(from);647const Vector2 segment_b = viewport->point_to_screen(to);648Vector2 inters = Geometry2D::get_closest_point_to_segment(mbpos, segment_a, segment_b);649float d = inters.distance_to(mbpos);650651if (d < 10 && d < closest_d) {652closest_d = d;653closest_seg = i;654Vector3 ray_from = viewport->get_ray_pos(mbpos);655Vector3 ray_dir = viewport->get_ray(mbpos);656657Vector3 ra, rb;658Geometry3D::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);659660closest_seg_point = it.xform(rb);661}662}663j++;664}665if (idx == j) {666idx++; //force next667} else {668idx = j; //swap669}670671if (j == rc) {672break;673}674}675}676677EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();678if (closest_seg != -1) {679//subdivide680681ur->create_action(TTR("Split Path"));682ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);683ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);684ur->commit_action();685return EditorPlugin::AFTER_GUI_INPUT_STOP;686687} else {688Vector3 origin;689if (c->get_point_count() == 0) {690origin = path->get_transform().get_origin();691} else {692origin = gt.xform(c->get_point_position(c->get_point_count() - 1));693}694695Vector3 ray_from = viewport->get_ray_pos(mbpos);696Vector3 ray_dir = viewport->get_ray(mbpos);697698if (snap_to_collider) {699_edit.click_ray_pos = ray_from;700_edit.click_ray_dir = ray_dir * p_camera->get_far();701_edit.gizmo_camera = p_camera;702_edit.origin = origin;703_edit.waiting_point_physics = true;704return EditorPlugin::AFTER_GUI_INPUT_STOP;705}706707Plane p(p_camera->get_transform().basis.get_column(2), origin);708709Vector3 inters;710if (p.intersects_ray(ray_from, ray_dir, &inters)) {711ur->create_action(TTR("Add Point to Curve"));712ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);713ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());714ur->commit_action();715return EditorPlugin::AFTER_GUI_INPUT_STOP;716}717718//add new at pos719}720721} else if (mb->is_pressed() && ((mb->get_button_index() == MouseButton::LEFT && curve_del->is_pressed()) || (mb->get_button_index() == MouseButton::RIGHT && curve_edit->is_pressed()))) {722const float disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");723for (int i = 0; i < c->get_point_count(); i++) {724real_t dist_to_p = viewport->point_to_screen(gt.xform(c->get_point_position(i))).distance_to(mbpos);725real_t dist_to_p_out = viewport->point_to_screen(gt.xform(c->get_point_position(i) + c->get_point_out(i))).distance_to(mbpos);726real_t dist_to_p_in = viewport->point_to_screen(gt.xform(c->get_point_position(i) + c->get_point_in(i))).distance_to(mbpos);727real_t dist_to_p_up = viewport->point_to_screen(gt.xform(c->get_point_position(i) + c->get_point_baked_posture(i, true).get_column(1) * disk_size)).distance_to(mbpos);728729// Find the offset and point index of the place to break up.730// Also check for the control points.731if (dist_to_p < click_dist) {732EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();733ur->create_action(TTR("Remove Path Point"));734ur->add_do_method(c.ptr(), "remove_point", i);735ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);736ur->commit_action();737return EditorPlugin::AFTER_GUI_INPUT_STOP;738} else if (dist_to_p_out < click_dist) {739EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();740ur->create_action(TTR("Reset Out-Control Point"));741ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());742ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));743ur->commit_action();744return EditorPlugin::AFTER_GUI_INPUT_STOP;745} else if (dist_to_p_in < click_dist) {746EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();747ur->create_action(TTR("Reset In-Control Point"));748ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());749ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));750ur->commit_action();751return EditorPlugin::AFTER_GUI_INPUT_STOP;752} else if (dist_to_p_up < click_dist) {753EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();754ur->create_action(TTR("Reset Point Tilt"));755ur->add_do_method(c.ptr(), "set_point_tilt", i, 0.0f);756ur->add_undo_method(c.ptr(), "set_point_tilt", i, c->get_point_tilt(i));757ur->commit_action();758return EditorPlugin::AFTER_GUI_INPUT_STOP;759}760}761}762}763764return EditorPlugin::AFTER_GUI_INPUT_PASS;765}766767void Path3DEditorPlugin::edit(Object *p_object) {768path = Object::cast_to<Path3D>(p_object);769_update_toolbar();770update_overlays();771}772773bool Path3DEditorPlugin::handles(Object *p_object) const {774return p_object->is_class("Path3D");775}776777void Path3DEditorPlugin::make_visible(bool p_visible) {778if (p_visible) {779topmenu_bar->show();780} else {781topmenu_bar->hide();782path = nullptr;783}784set_physics_process(p_visible);785}786787void Path3DEditorPlugin::_mode_changed(int p_mode) {788curve_create->set_pressed_no_signal(p_mode == MODE_CREATE);789curve_edit_curve->set_pressed_no_signal(p_mode == MODE_EDIT_CURVE);790curve_edit_tilt->set_pressed_no_signal(p_mode == MODE_EDIT_TILT);791curve_edit->set_pressed_no_signal(p_mode == MODE_EDIT);792curve_del->set_pressed_no_signal(p_mode == MODE_DELETE);793794Node3DEditor::get_singleton()->clear_subgizmo_selection();795}796797void Path3DEditorPlugin::_toggle_closed_curve() {798Ref<Curve3D> c = path->get_curve();799if (c.is_null()) {800return;801}802if (c->get_point_count() < 2) {803return;804}805EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();806ur->create_action(TTR("Toggle Open/Closed Curve"));807ur->add_do_method(c.ptr(), "set_closed", !c.ptr()->is_closed());808ur->add_undo_method(c.ptr(), "set_closed", c.ptr()->is_closed());809ur->commit_action();810}811812void Path3DEditorPlugin::_handle_option_pressed(int p_option) {813PopupMenu *pm;814pm = handle_menu->get_popup();815816switch (p_option) {817case HANDLE_OPTION_ANGLE: {818bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);819mirror_handle_angle = !is_checked;820pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);821pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);822} break;823case HANDLE_OPTION_LENGTH: {824bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);825mirror_handle_length = !is_checked;826pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);827} break;828case HANDLE_OPTION_SNAP_COLLIDER: {829bool is_checked = pm->is_item_checked(HANDLE_OPTION_SNAP_COLLIDER);830snap_to_collider = !is_checked;831pm->set_item_checked(HANDLE_OPTION_SNAP_COLLIDER, snap_to_collider);832} break;833}834}835836void Path3DEditorPlugin::_create_curve() {837ERR_FAIL_NULL(path);838839Ref<Curve3D> new_curve;840new_curve.instantiate();841842EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();843undo_redo->create_action(TTR("Create Curve in Path3D"));844undo_redo->add_do_property(path, "curve", new_curve);845undo_redo->add_undo_property(path, "curve", Ref<Curve3D>());846undo_redo->add_do_method(this, "_update_toolbar");847undo_redo->add_undo_method(this, "_update_toolbar");848undo_redo->commit_action();849}850851void Path3DEditorPlugin::_confirm_clear_points() {852if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {853return;854}855clear_points_dialog->reset_size();856clear_points_dialog->popup_centered();857}858859void Path3DEditorPlugin::_clear_points() {860EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();861PackedVector3Array points = path->get_curve()->get_points().duplicate();862863undo_redo->create_action(TTR("Clear Curve Points"));864undo_redo->add_do_method(this, "_clear_curve_points");865undo_redo->add_undo_method(this, "_restore_curve_points", points);866undo_redo->commit_action();867}868869void Path3DEditorPlugin::_clear_curve_points() {870if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {871return;872}873Ref<Curve3D> curve = path->get_curve();874curve->set_closed(false);875curve->clear_points();876}877878void Path3DEditorPlugin::_restore_curve_points(const PackedVector3Array &p_points) {879if (!path || path->get_curve().is_null()) {880return;881}882Ref<Curve3D> curve = path->get_curve();883884if (curve->get_point_count() > 0) {885curve->clear_points();886}887888for (int i = 0; i < p_points.size(); i += 3) {889curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]);890}891}892893void Path3DEditorPlugin::_update_theme() {894curve_edit->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveEdit")));895curve_edit_curve->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCurve")));896curve_edit_tilt->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveTilt")));897curve_create->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCreate")));898curve_del->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveDelete")));899curve_closed->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveClose")));900curve_clear_points->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Clear")));901create_curve_button->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Curve3D")));902}903904void Path3DEditorPlugin::_update_toolbar() {905if (!path) {906return;907}908bool has_curve = path->get_curve().is_valid();909toolbar->set_visible(has_curve);910create_curve_button->set_visible(!has_curve);911}912913void Path3DEditorPlugin::_notification(int p_what) {914switch (p_what) {915case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {916if (!path) {917return;918}919920if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/3d_gizmos/gizmo_settings")) {921return;922}923924path->update_gizmos();925} break;926case NOTIFICATION_PHYSICS_PROCESS: {927if (_edit.waiting_point_physics) {928_edit.waiting_point_physics = false;929const Transform3D gt = path->get_global_transform();930const Transform3D it = gt.affine_inverse();931Ref<Curve3D> c = path->get_curve();932EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();933PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state();934if (ss) {935PhysicsDirectSpaceState3D::RayParameters ray_params;936PhysicsDirectSpaceState3D::RayResult result;937ray_params.from = _edit.click_ray_pos;938ray_params.to = ray_params.from + _edit.click_ray_dir;939bool hit_something = false;940Vector3 inters;941if (ss->intersect_ray(ray_params, result)) {942inters = result.position;943hit_something = true;944} else {945Plane p(_edit.gizmo_camera->get_transform().basis.get_column(2), _edit.origin);946if (p.intersects_ray(ray_params.from, _edit.click_ray_dir, &inters)) {947hit_something = true;948}949}950if (hit_something) {951ur->create_action(TTR("Add Point to Curve"));952ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);953ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());954ur->commit_action();955}956}957}958if (_edit.waiting_handle_physics) {959_edit.in_physics_frame = true;960961// Find gizmo reference.962Vector<Ref<Node3DGizmo>> gizmos = path->get_gizmos();963for (Ref<EditorNode3DGizmo> seg : gizmos) {964if (seg.is_valid()) {965_edit.gizmo = seg;966break;967}968}969970_edit.gizmo->set_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_camera, _edit.mouse_pos);971_edit.in_physics_frame = false;972_edit.waiting_handle_physics = false;973}974}975}976}977978void Path3DEditorPlugin::_bind_methods() {979ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path3DEditorPlugin::_update_toolbar);980ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points);981ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points);982}983984Path3DEditorPlugin::Path3DEditorPlugin() {985singleton = this;986mirror_handle_angle = true;987mirror_handle_length = true;988989Ref<Path3DGizmoPlugin> gizmo_plugin;990gizmo_plugin.instantiate();991Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);992path_3d_gizmo_plugin = gizmo_plugin;993994topmenu_bar = memnew(HBoxContainer);995topmenu_bar->hide();996997toolbar = memnew(HBoxContainer);998topmenu_bar->add_child(toolbar);9991000curve_edit = memnew(Button);1001curve_edit->set_theme_type_variation(SceneStringName(FlatButton));1002curve_edit->set_toggle_mode(true);1003curve_edit->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1004curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Click: Select multiple Points") + "\n" + vformat(TTR("%s+Click: Add Point"), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL)) + "\n" + TTR("Right Click: Delete Point"));1005curve_edit->set_accessibility_name(TTRC("Select Points"));1006toolbar->add_child(curve_edit);1007curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));10081009curve_edit_curve = memnew(Button);1010curve_edit_curve->set_theme_type_variation(SceneStringName(FlatButton));1011curve_edit_curve->set_toggle_mode(true);1012curve_edit_curve->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1013curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points"));1014curve_edit_curve->set_accessibility_name(TTRC("Select Control Points"));1015toolbar->add_child(curve_edit_curve);1016curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));10171018curve_edit_tilt = memnew(Button);1019curve_edit_tilt->set_theme_type_variation(SceneStringName(FlatButton));1020curve_edit_tilt->set_toggle_mode(true);1021curve_edit_tilt->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1022curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles"));1023toolbar->add_child(curve_edit_tilt);1024curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));10251026curve_create = memnew(Button);1027curve_create->set_theme_type_variation(SceneStringName(FlatButton));1028curve_create->set_toggle_mode(true);1029curve_create->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1030curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));1031curve_create->set_accessibility_name(TTRC("Add Point (in empty space)"));1032toolbar->add_child(curve_create);1033curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));10341035curve_del = memnew(Button);1036curve_del->set_theme_type_variation(SceneStringName(FlatButton));1037curve_del->set_toggle_mode(true);1038curve_del->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1039curve_del->set_tooltip_text(TTR("Delete Point"));1040toolbar->add_child(curve_del);1041curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));10421043curve_closed = memnew(Button);1044curve_closed->set_theme_type_variation(SceneStringName(FlatButton));1045curve_closed->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1046curve_closed->set_tooltip_text(TTR("Close Curve"));1047toolbar->add_child(curve_closed);1048curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_toggle_closed_curve));10491050curve_clear_points = memnew(Button);1051curve_clear_points->set_theme_type_variation(SceneStringName(FlatButton));1052curve_clear_points->set_focus_mode(Control::FOCUS_ACCESSIBILITY);1053curve_clear_points->set_tooltip_text(TTR("Clear Points"));1054curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points));1055toolbar->add_child(curve_clear_points);10561057clear_points_dialog = memnew(ConfirmationDialog);1058clear_points_dialog->set_title(TTR("Please Confirm..."));1059clear_points_dialog->set_text(TTR("Remove all curve points?"));1060clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path3DEditorPlugin::_clear_points));1061toolbar->add_child(clear_points_dialog);10621063handle_menu = memnew(MenuButton);1064handle_menu->set_flat(false);1065handle_menu->set_theme_type_variation("FlatMenuButton");1066handle_menu->set_text(TTR("Options"));1067toolbar->add_child(handle_menu);10681069create_curve_button = memnew(Button);1070create_curve_button->set_text(TTR("Create Curve"));1071create_curve_button->hide();1072topmenu_bar->add_child(create_curve_button);1073create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_create_curve));10741075PopupMenu *menu = handle_menu->get_popup();1076menu->add_check_item(TTR("Mirror Handle Angles"));1077menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);1078menu->add_check_item(TTR("Mirror Handle Lengths"));1079menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);1080menu->add_check_item(TTR("Snap to Colliders"));1081menu->set_item_checked(HANDLE_OPTION_SNAP_COLLIDER, snap_to_collider);1082menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));10831084curve_edit->set_pressed_no_signal(true);10851086topmenu_bar->connect(SceneStringName(theme_changed), callable_mp(this, &Path3DEditorPlugin::_update_theme));1087Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);1088}10891090Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {1091Ref<Path3DGizmo> ref;10921093Path3D *path = Object::cast_to<Path3D>(p_spatial);1094if (path) {1095ref.instantiate(path);1096}10971098return ref;1099}11001101bool Path3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {1102return Object::cast_to<Path3D>(p_spatial) != nullptr;1103}11041105String Path3DGizmoPlugin::get_gizmo_name() const {1106return "Path3D";1107}11081109void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {1110Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());1111ERR_FAIL_NULL(path);11121113Ref<Curve3D> curve = path->get_curve();11141115Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);1116Ref<StandardMaterial3D> first_pt_handle_material = get_material("first_pt_handle", p_gizmo);1117Ref<StandardMaterial3D> last_pt_handle_material = get_material("last_pt_handle", p_gizmo);1118Ref<StandardMaterial3D> closed_pt_handle_material = get_material("closed_pt_handle", p_gizmo);11191120first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));1121last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));1122closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));11231124PackedVector3Array handles;11251126if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {1127for (int idx = 0; idx < curve->get_point_count(); ++idx) {1128// Collect handles.1129const Vector3 pos = curve->get_point_position(idx);11301131handles.append(pos);1132}1133}11341135if (handles.size()) {1136// Point count.1137const int pc = handles.size();11381139// Initialize arrays for first point.1140PackedVector3Array first_pt;1141first_pt.append(handles[0]);11421143// Initialize arrays and add handle for last point if needed.1144if (pc > 1) {1145PackedVector3Array last_pt;1146last_pt.append(handles[handles.size() - 1]);1147handles.remove_at(handles.size() - 1);1148if (curve->is_closed()) {1149p_gizmo->add_vertices(last_pt, handle_material, Mesh::PRIMITIVE_POINTS);1150} else {1151p_gizmo->add_vertices(last_pt, last_pt_handle_material, Mesh::PRIMITIVE_POINTS);1152}1153}11541155// Add handle for first point.1156handles.remove_at(0);1157if (curve->is_closed()) {1158p_gizmo->add_vertices(first_pt, closed_pt_handle_material, Mesh::PRIMITIVE_POINTS);1159} else {1160p_gizmo->add_vertices(first_pt, first_pt_handle_material, Mesh::PRIMITIVE_POINTS);1161}11621163// Add handles for remaining intermediate points.1164if (!handles.is_empty()) {1165p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);1166}1167}1168}11691170int Path3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {1171Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());1172ERR_FAIL_NULL_V(path, -1);1173Ref<Curve3D> curve = path->get_curve();1174ERR_FAIL_COND_V(curve.is_null(), -1);11751176if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {1177for (int idx = 0; idx < curve->get_point_count(); ++idx) {1178Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));1179if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {1180return idx;1181}1182}1183}1184return -1;1185}11861187Vector<int> Path3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {1188Vector<int> contained_points;11891190Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());1191ERR_FAIL_NULL_V(path, contained_points);1192Ref<Curve3D> curve = path->get_curve();1193ERR_FAIL_COND_V(curve.is_null(), contained_points);11941195if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {1196for (int idx = 0; idx < curve->get_point_count(); ++idx) {1197Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));1198bool is_contained_in_frustum = true;1199for (int i = 0; i < p_frustum.size(); ++i) {1200if (p_frustum[i].distance_to(pos) > 0) {1201is_contained_in_frustum = false;1202break;1203}1204}12051206if (is_contained_in_frustum) {1207contained_points.push_back(idx);1208}1209}1210}12111212return contained_points;1213}12141215Transform3D Path3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {1216Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());1217ERR_FAIL_NULL_V(path, Transform3D());1218Ref<Curve3D> curve = path->get_curve();1219ERR_FAIL_COND_V(curve.is_null(), Transform3D());1220ERR_FAIL_INDEX_V(p_id, curve->get_point_count(), Transform3D());12211222Basis basis = transformation_locked_basis.has(p_id) ? transformation_locked_basis[p_id] : curve->get_point_baked_posture(p_id, true);1223Vector3 pos = curve->get_point_position(p_id);12241225Transform3D t = Transform3D(basis, pos);1226return t;1227}12281229void Path3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {1230Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());1231ERR_FAIL_NULL(path);1232Ref<Curve3D> curve = path->get_curve();1233ERR_FAIL_COND(curve.is_null());1234ERR_FAIL_INDEX(p_id, curve->get_point_count());12351236if (!transformation_locked_basis.has(p_id)) {1237transformation_locked_basis[p_id] = Basis(curve->get_point_baked_posture(p_id, true));1238}1239curve->set_point_position(p_id, p_transform.origin);1240}12411242void Path3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {1243Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());1244ERR_FAIL_NULL(path);1245Ref<Curve3D> curve = path->get_curve();1246ERR_FAIL_COND(curve.is_null());12471248transformation_locked_basis.clear();12491250if (p_cancel) {1251for (int i = 0; i < p_ids.size(); ++i) {1252curve->set_point_position(p_ids[i], p_restore[i].origin);1253}1254return;1255}12561257EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();12581259undo_redo->create_action(TTR("Set Curve Point Position"));12601261for (int i = 0; i < p_ids.size(); ++i) {1262const int idx = p_ids[i];1263undo_redo->add_do_method(curve.ptr(), "set_point_position", idx, curve->get_point_position(idx));1264undo_redo->add_undo_method(curve.ptr(), "set_point_position", idx, p_restore[i].origin);1265}1266undo_redo->commit_action();1267}12681269int Path3DGizmoPlugin::get_priority() const {1270return -1;1271}12721273Path3DGizmoPlugin::Path3DGizmoPlugin() {1274Color path_color = SceneTree::get_singleton()->get_debug_paths_color();1275Color path_tilt_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/path_tilt");12761277create_material("path_material", path_color);1278create_material("path_thin_material", Color(0.6, 0.6, 0.6));1279create_material("path_tilt_material", path_tilt_color);1280create_material("path_tilt_muted_material", path_tilt_color * 0.7);1281create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));1282create_handle_material("first_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));1283create_handle_material("last_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));1284create_handle_material("closed_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));1285create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));1286}128712881289