Path: blob/master/editor/scene/3d/gizmos/gizmo_3d_helper.cpp
21807 views
/**************************************************************************/1/* gizmo_3d_helper.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 "gizmo_3d_helper.h"3132#include "core/input/input.h"33#include "core/math/geometry_3d.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/3d/node_3d_editor_plugin.h"36#include "scene/3d/camera_3d.h"3738void Gizmo3DHelper::initialize_handle_action(const Variant &p_initial_value, const Transform3D &p_initial_transform) {39initial_value = p_initial_value;40initial_transform = p_initial_transform;41}4243void Gizmo3DHelper::get_segment(Camera3D *p_camera, const Point2 &p_point, Vector3 *r_segment) {44Transform3D gt = initial_transform;45Transform3D gi = gt.affine_inverse();4647Vector3 ray_from = p_camera->project_ray_origin(p_point);48Vector3 ray_dir = p_camera->project_ray_normal(p_point);4950r_segment[0] = gi.xform(ray_from);51r_segment[1] = gi.xform(ray_from + ray_dir * 4096);52}5354Vector<Vector3> Gizmo3DHelper::box_get_handles(const Vector3 &p_box_size) {55Vector<Vector3> handles;56for (int i = 0; i < 3; i++) {57Vector3 ax;58ax[i] = p_box_size[i] / 2;59handles.push_back(ax);60handles.push_back(-ax);61}62return handles;63}6465String Gizmo3DHelper::box_get_handle_name(int p_id) const {66switch (p_id) {67case 0:68case 1:69return "Size X";70case 2:71case 3:72return "Size Y";73case 4:74case 5:75return "Size Z";76}77return "";78}7980void Gizmo3DHelper::box_set_handle(const Vector3 p_segment[2], int p_id, Vector3 &r_box_size, Vector3 &r_box_position) {81int axis = p_id / 2;82int sign = p_id % 2 * -2 + 1;8384Vector3 initial_size = initial_value;85float neg_end = initial_size[axis] * -0.5;86float pos_end = initial_size[axis] * 0.5;8788Vector3 axis_segment[2] = { Vector3(), Vector3() };89axis_segment[0][axis] = 4096.0;90axis_segment[1][axis] = -4096.0;91Vector3 ra, rb;92Geometry3D::get_closest_points_between_segments(axis_segment[0], axis_segment[1], p_segment[0], p_segment[1], ra, rb);9394// Calculate new size.95r_box_size = initial_size;96if (Input::get_singleton()->is_key_pressed(Key::ALT)) {97r_box_size[axis] = ra[axis] * sign * 2;98} else {99r_box_size[axis] = sign > 0 ? ra[axis] - neg_end : pos_end - ra[axis];100}101102// Snap to grid.103if (Node3DEditor::get_singleton()->is_snap_enabled()) {104r_box_size[axis] = Math::snapped(r_box_size[axis], Node3DEditor::get_singleton()->get_translate_snap());105}106r_box_size[axis] = MAX(r_box_size[axis], 0.001);107108// Adjust position.109if (Input::get_singleton()->is_key_pressed(Key::ALT)) {110r_box_position = initial_transform.get_origin();111} else {112if (sign > 0) {113pos_end = neg_end + r_box_size[axis];114} else {115neg_end = pos_end - r_box_size[axis];116}117118Vector3 offset;119offset[axis] = (pos_end + neg_end) * 0.5;120r_box_position = initial_transform.xform(offset);121}122}123124void Gizmo3DHelper::box_commit_handle(const String &p_action_name, bool p_cancel, Object *p_position_object, Object *p_size_object, const StringName &p_position_property, const StringName &p_size_property) {125if (!p_size_object) {126p_size_object = p_position_object;127}128129if (p_cancel) {130p_size_object->set(p_size_property, initial_value);131p_position_object->set(p_position_property, initial_transform.get_origin());132return;133}134135EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();136ur->create_action(p_action_name);137ur->add_do_property(p_size_object, p_size_property, p_size_object->get(p_size_property));138ur->add_do_property(p_position_object, p_position_property, p_position_object->get(p_position_property));139ur->add_undo_property(p_size_object, p_size_property, initial_value);140ur->add_undo_property(p_position_object, p_position_property, initial_transform.get_origin());141ur->commit_action();142}143144Vector<Vector3> Gizmo3DHelper::cylinder_get_handles(real_t p_height, real_t p_radius) {145Vector<Vector3> handles;146handles.push_back(Vector3(0, p_height * 0.5, 0));147handles.push_back(Vector3(0, p_height * -0.5, 0));148handles.push_back(Vector3(p_radius, 0, 0));149return handles;150}151152String Gizmo3DHelper::_cylinder_or_capsule_or_cone_frustum_get_handle_name(int p_id) const {153if (p_id < 2) {154return "Height";155} else {156return "Radius";157}158}159160void Gizmo3DHelper::_cylinder_or_capsule_or_cone_frustum_set_handle(const Vector3 p_segment[2], int p_id, real_t &r_height, real_t &r_radius_top, real_t &r_radius_bottom, Vector3 &r_position, bool p_is_capsule, bool p_is_frustum) {161ERR_FAIL_COND(p_is_capsule && p_is_frustum);162163real_t initial_radius_top, initial_radius_bottom, initial_height;164if (p_is_frustum) {165initial_radius_top = initial_value.operator Vector3().x;166initial_radius_bottom = initial_value.operator Vector3().y;167initial_height = initial_value.operator Vector3().z;168} else {169initial_radius_top = initial_value.operator Vector2().x;170initial_radius_bottom = initial_radius_top;171initial_height = initial_value.operator Vector2().y;172}173174int sign = p_id == 1 ? -1 : 1;175int axis = p_id >= 2 ? 0 : 1;176177Vector3 axis_vector;178axis_vector[axis] = sign;179Vector3 ra, rb;180Geometry3D::get_closest_points_between_segments(axis_vector * -4096, axis_vector * 4096, p_segment[0], p_segment[1], ra, rb);181float d = axis_vector.dot(ra);182183// Snap to grid.184if (Node3DEditor::get_singleton()->is_snap_enabled()) {185d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());186}187188if (p_id < 2) {189// Adjust height.190if (Input::get_singleton()->is_key_pressed(Key::ALT)) {191r_height = d * 2.0;192} else {193r_height = (initial_height * 0.5) + d;194}195196if (r_height < 0.001) {197r_height = 0.001;198}199200// Adjust position.201if (Input::get_singleton()->is_key_pressed(Key::ALT)) {202r_position = initial_transform.get_origin();203} else {204Vector3 offset;205offset[axis] = (r_height - initial_height) * 0.5 * sign;206r_position = initial_transform.xform(offset);207}208209if (p_is_capsule) {210r_radius_top = MIN(initial_radius_top, r_height / 2.0);211r_radius_bottom = r_radius_top;212} else {213r_radius_top = initial_radius_top;214r_radius_bottom = initial_radius_bottom;215}216} else {217// Adjust radius.218if (d < 0.001) {219d = 0.001;220}221if (!p_is_frustum) {222r_radius_top = d;223r_radius_bottom = d;224} else {225real_t diff = d - (initial_radius_bottom + initial_radius_top) / 2.0;226diff = MAX(MAX(-initial_radius_bottom + 0.001, -initial_radius_top + 0.001), diff);227r_radius_top = initial_radius_top + diff;228r_radius_bottom = initial_radius_bottom + diff;229}230231r_position = initial_transform.get_origin();232233if (p_is_capsule) {234r_height = MAX(initial_height, r_radius_top * 2.0);235} else {236r_height = initial_height;237}238}239}240241void Gizmo3DHelper::cylinder_commit_handle(int p_id, const String &p_radius_action_name, const String &p_height_action_name, bool p_cancel, Object *p_position_object, Object *p_height_object, Object *p_radius_object, const StringName &p_position_property, const StringName &p_height_property, const StringName &p_radius_property) {242if (!p_height_object) {243p_height_object = p_position_object;244}245if (!p_radius_object) {246p_radius_object = p_position_object;247}248249if (p_cancel) {250p_radius_object->set(p_radius_property, initial_value.operator Vector2().x);251p_height_object->set(p_height_property, initial_value.operator Vector2().y);252p_position_object->set(p_position_property, initial_transform.get_origin());253return;254}255256EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();257ur->create_action(p_id < 2 ? p_height_action_name : p_radius_action_name);258ur->add_do_property(p_radius_object, p_radius_property, p_radius_object->get(p_radius_property));259ur->add_undo_property(p_radius_object, p_radius_property, initial_value.operator Vector2().x);260ur->add_do_property(p_height_object, p_height_property, p_height_object->get(p_height_property));261ur->add_undo_property(p_height_object, p_height_property, initial_value.operator Vector2().y);262ur->add_do_property(p_position_object, p_position_property, p_position_object->get(p_position_property));263ur->add_undo_property(p_position_object, p_position_property, initial_transform.get_origin());264ur->commit_action();265}266267Vector<Vector3> Gizmo3DHelper::cone_frustum_get_handles(real_t p_height, real_t p_radius_top, real_t p_radius_bottom) {268Vector<Vector3> handles;269handles.push_back(Vector3(0, p_height * 0.5, 0));270handles.push_back(Vector3(0, p_height * -0.5, 0));271handles.push_back(Vector3((p_radius_top + p_radius_bottom) / 2.0, 0, 0));272return handles;273}274275void Gizmo3DHelper::cone_frustum_commit_handle(int p_id, const String &p_radius_action_name, const String &p_height_action_name, bool p_cancel, Object *p_position_object, Object *p_height_object, Object *p_radius_top_object, Object *p_radius_bottom_object, const StringName &p_position_property, const StringName &p_height_property, const StringName &p_radius_top_property, const StringName &p_radius_bottom_property) {276if (!p_height_object) {277p_height_object = p_position_object;278}279if (!p_radius_top_object) {280p_radius_top_object = p_position_object;281}282if (!p_radius_bottom_object) {283p_radius_bottom_object = p_position_object;284}285286if (p_cancel) {287p_radius_top_object->set(p_radius_top_property, initial_value.operator Vector3().x);288p_radius_bottom_object->set(p_radius_bottom_property, initial_value.operator Vector3().y);289p_height_object->set(p_height_property, initial_value.operator Vector3().z);290p_position_object->set(p_position_property, initial_transform.get_origin());291return;292}293294EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();295ur->create_action(p_id < 2 ? p_height_action_name : p_radius_action_name);296ur->add_do_property(p_radius_top_object, p_radius_top_property, p_radius_top_object->get(p_radius_top_property));297ur->add_do_property(p_radius_bottom_object, p_radius_bottom_property, p_radius_bottom_object->get(p_radius_bottom_property));298ur->add_undo_property(p_radius_top_object, p_radius_top_property, initial_value.operator Vector3().x);299ur->add_undo_property(p_radius_bottom_object, p_radius_bottom_property, initial_value.operator Vector3().y);300ur->add_do_property(p_height_object, p_height_property, p_height_object->get(p_height_property));301ur->add_undo_property(p_height_object, p_height_property, initial_value.operator Vector3().z);302ur->add_do_property(p_position_object, p_position_property, p_position_object->get(p_position_property));303ur->add_undo_property(p_position_object, p_position_property, initial_transform.get_origin());304ur->commit_action();305}306307308