Path: blob/master/editor/scene/3d/gizmos/camera_3d_gizmo_plugin.cpp
21730 views
/**************************************************************************/1/* camera_3d_gizmo_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 "camera_3d_gizmo_plugin.h"3132#include "core/math/geometry_3d.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/editor_undo_redo_manager.h"36#include "editor/scene/3d/node_3d_editor_plugin.h"37#include "editor/settings/editor_settings.h"38#include "scene/3d/camera_3d.h"3940Camera3DGizmoPlugin::Camera3DGizmoPlugin() {41Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/camera");4243create_material("camera_material", gizmo_color);44create_icon_material("camera_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoCamera3D"), EditorStringName(EditorIcons)));45create_handle_material("handles");46}4748bool Camera3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {49return Object::cast_to<Camera3D>(p_spatial) != nullptr;50}5152String Camera3DGizmoPlugin::get_gizmo_name() const {53return "Camera3D";54}5556int Camera3DGizmoPlugin::get_priority() const {57return -1;58}5960String Camera3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {61Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());6263if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {64return "FOV";65} else {66return "Size";67}68}6970Variant Camera3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {71Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());7273if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {74return camera->get_fov();75} else {76return camera->get_size();77}78}7980void Camera3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {81Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());8283Transform3D gt = camera->get_global_transform();84Transform3D gi = gt.affine_inverse();8586Vector3 ray_from = p_camera->project_ray_origin(p_point);87Vector3 ray_dir = p_camera->project_ray_normal(p_point);8889Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };9091if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {92Transform3D gt2 = camera->get_global_transform();93float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], 1.0, gt2);94camera->set("fov", CLAMP(a * 2.0, 1, 179));95} else {96Camera3D::KeepAspect aspect = camera->get_keep_aspect_mode();97Vector3 camera_far = aspect == Camera3D::KeepAspect::KEEP_WIDTH ? Vector3(4096, 0, -1) : Vector3(0, 4096, -1);9899Vector3 ra, rb;100Geometry3D::get_closest_points_between_segments(Vector3(0, 0, -1), camera_far, s[0], s[1], ra, rb);101float d = aspect == Camera3D::KeepAspect::KEEP_WIDTH ? ra.x * 2 : ra.y * 2;102if (Node3DEditor::get_singleton()->is_snap_enabled()) {103d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());104}105106d = CLAMP(d, 0.1, 16384);107108camera->set("size", d);109}110}111112void Camera3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {113Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());114115if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {116if (p_cancel) {117camera->set("fov", p_restore);118} else {119EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();120ur->create_action(TTR("Change Camera FOV"));121ur->add_do_property(camera, "fov", camera->get_fov());122ur->add_undo_property(camera, "fov", p_restore);123ur->commit_action();124}125126} else {127if (p_cancel) {128camera->set("size", p_restore);129} else {130EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();131ur->create_action(TTR("Change Camera Size"));132ur->add_do_property(camera, "size", camera->get_size());133ur->add_undo_property(camera, "size", p_restore);134ur->commit_action();135}136}137}138139void Camera3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {140Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());141142p_gizmo->clear();143144Vector<Vector3> lines;145Vector<Vector3> handles;146147Ref<Material> material = get_material("camera_material", p_gizmo);148Ref<Material> icon = get_material("camera_icon", p_gizmo);149150const Size2i viewport_size = Node3DEditor::get_camera_viewport_size(camera);151const real_t viewport_aspect = viewport_size.x > 0 && viewport_size.y > 0 ? viewport_size.aspect() : 1.0;152const Size2 size_factor = viewport_aspect > 1.0 ? Size2(1.0, 1.0 / viewport_aspect) : Size2(viewport_aspect, 1.0);153154#define ADD_TRIANGLE(m_a, m_b, m_c) \155{ \156lines.push_back(m_a); \157lines.push_back(m_b); \158lines.push_back(m_b); \159lines.push_back(m_c); \160lines.push_back(m_c); \161lines.push_back(m_a); \162}163164#define ADD_QUAD(m_a, m_b, m_c, m_d) \165{ \166lines.push_back(m_a); \167lines.push_back(m_b); \168lines.push_back(m_b); \169lines.push_back(m_c); \170lines.push_back(m_c); \171lines.push_back(m_d); \172lines.push_back(m_d); \173lines.push_back(m_a); \174}175176switch (camera->get_projection()) {177case Camera3D::PROJECTION_PERSPECTIVE: {178// The real FOV is halved for accurate representation179float fov = camera->get_fov() / 2.0;180181const float hsize = Math::sin(Math::deg_to_rad(fov));182const float depth = -Math::cos(Math::deg_to_rad(fov));183184Vector3 side;185if (camera->get_keep_aspect_mode() == Camera3D::KEEP_WIDTH) {186side = Vector3(hsize * size_factor.x, 0, depth * size_factor.x);187} else {188side = Vector3(hsize * size_factor.x, 0, depth * size_factor.y);189}190Vector3 nside = Vector3(-side.x, side.y, side.z);191Vector3 up = Vector3(0, hsize * size_factor.y, 0);192193ADD_TRIANGLE(Vector3(), side + up, side - up);194ADD_TRIANGLE(Vector3(), nside + up, nside - up);195ADD_TRIANGLE(Vector3(), side + up, nside + up);196ADD_TRIANGLE(Vector3(), side - up, nside - up);197198handles.push_back(side);199side.x = MIN(side.x, hsize * 0.25);200nside.x = -side.x;201Vector3 tup(0, up.y + hsize / 2, side.z);202ADD_TRIANGLE(tup, side + up, nside + up);203} break;204205case Camera3D::PROJECTION_ORTHOGONAL: {206Camera3D::KeepAspect aspect = camera->get_keep_aspect_mode();207208float size = camera->get_size();209float keep_size = size * 0.5;210211Vector3 right, up;212Vector3 back(0, 0, -1.0);213214if (aspect == Camera3D::KeepAspect::KEEP_WIDTH) {215right = Vector3(keep_size, 0, 0);216up = Vector3(0, keep_size / viewport_aspect, 0);217handles.push_back(right + back);218} else {219right = Vector3(keep_size * viewport_aspect, 0, 0);220up = Vector3(0, keep_size, 0);221handles.push_back(up + back);222}223224ADD_QUAD(-up - right, -up + right, up + right, up - right);225ADD_QUAD(-up - right + back, -up + right + back, up + right + back, up - right + back);226ADD_QUAD(up + right, up + right + back, up - right + back, up - right);227ADD_QUAD(-up + right, -up + right + back, -up - right + back, -up - right);228229right.x = MIN(right.x, keep_size * 0.25);230Vector3 tup(0, up.y + keep_size / 2, back.z);231ADD_TRIANGLE(tup, right + up + back, -right + up + back);232} break;233234case Camera3D::PROJECTION_FRUSTUM: {235float hsize = camera->get_size() / 2.0;236237Vector3 side = Vector3(hsize, 0, -camera->get_near()).normalized();238side.x *= size_factor.x;239Vector3 nside = Vector3(-side.x, side.y, side.z);240Vector3 up = Vector3(0, hsize * size_factor.y, 0);241Vector3 offset = Vector3(camera->get_frustum_offset().x, camera->get_frustum_offset().y, 0.0);242243ADD_TRIANGLE(Vector3(), side + up + offset, side - up + offset);244ADD_TRIANGLE(Vector3(), nside + up + offset, nside - up + offset);245ADD_TRIANGLE(Vector3(), side + up + offset, nside + up + offset);246ADD_TRIANGLE(Vector3(), side - up + offset, nside - up + offset);247248side.x = MIN(side.x, hsize * 0.25);249nside.x = -side.x;250Vector3 tup(0, up.y + hsize / 2, side.z);251ADD_TRIANGLE(tup + offset, side + up + offset, nside + up + offset);252} break;253}254255#undef ADD_TRIANGLE256#undef ADD_QUAD257258p_gizmo->add_lines(lines, material);259p_gizmo->add_unscaled_billboard(icon, 0.05);260p_gizmo->add_collision_segments(lines);261262if (!handles.is_empty()) {263p_gizmo->add_handles(handles, get_material("handles"));264}265}266267float Camera3DGizmoPlugin::_find_closest_angle_to_half_pi_arc(const Vector3 &p_from, const Vector3 &p_to, float p_arc_radius, const Transform3D &p_arc_xform) {268//bleh, discrete is simpler269static const int arc_test_points = 64;270float min_d = 1e20;271Vector3 min_p;272273for (int i = 0; i < arc_test_points; i++) {274float a = i * Math::PI * 0.5 / arc_test_points;275float an = (i + 1) * Math::PI * 0.5 / arc_test_points;276Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;277Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;278279Vector3 ra, rb;280Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);281282float d = ra.distance_to(rb);283if (d < min_d) {284min_d = d;285min_p = ra;286}287}288289//min_p = p_arc_xform.affine_inverse().xform(min_p);290float a = (Math::PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();291return Math::rad_to_deg(a);292}293294295