Path: blob/master/editor/scene/3d/gizmos/camera_3d_gizmo_plugin.cpp
9903 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 "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/3d/node_3d_editor_plugin.h"36#include "editor/settings/editor_settings.h"37#include "scene/3d/camera_3d.h"3839Camera3DGizmoPlugin::Camera3DGizmoPlugin() {40Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/camera");4142create_material("camera_material", gizmo_color);43create_icon_material("camera_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoCamera3D"), EditorStringName(EditorIcons)));44create_handle_material("handles");45}4647bool Camera3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {48return Object::cast_to<Camera3D>(p_spatial) != nullptr;49}5051String Camera3DGizmoPlugin::get_gizmo_name() const {52return "Camera3D";53}5455int Camera3DGizmoPlugin::get_priority() const {56return -1;57}5859String Camera3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {60Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());6162if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {63return "FOV";64} else {65return "Size";66}67}6869Variant Camera3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {70Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());7172if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {73return camera->get_fov();74} else {75return camera->get_size();76}77}7879void Camera3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {80Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());8182Transform3D gt = camera->get_global_transform();83Transform3D gi = gt.affine_inverse();8485Vector3 ray_from = p_camera->project_ray_origin(p_point);86Vector3 ray_dir = p_camera->project_ray_normal(p_point);8788Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };8990if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {91Transform3D gt2 = camera->get_global_transform();92float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], 1.0, gt2);93camera->set("fov", CLAMP(a * 2.0, 1, 179));94} else {95Camera3D::KeepAspect aspect = camera->get_keep_aspect_mode();96Vector3 camera_far = aspect == Camera3D::KeepAspect::KEEP_WIDTH ? Vector3(4096, 0, -1) : Vector3(0, 4096, -1);9798Vector3 ra, rb;99Geometry3D::get_closest_points_between_segments(Vector3(0, 0, -1), camera_far, s[0], s[1], ra, rb);100float d = aspect == Camera3D::KeepAspect::KEEP_WIDTH ? ra.x * 2 : ra.y * 2;101if (Node3DEditor::get_singleton()->is_snap_enabled()) {102d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());103}104105d = CLAMP(d, 0.1, 16384);106107camera->set("size", d);108}109}110111void Camera3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {112Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());113114if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {115if (p_cancel) {116camera->set("fov", p_restore);117} else {118EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();119ur->create_action(TTR("Change Camera FOV"));120ur->add_do_property(camera, "fov", camera->get_fov());121ur->add_undo_property(camera, "fov", p_restore);122ur->commit_action();123}124125} else {126if (p_cancel) {127camera->set("size", p_restore);128} else {129EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();130ur->create_action(TTR("Change Camera Size"));131ur->add_do_property(camera, "size", camera->get_size());132ur->add_undo_property(camera, "size", p_restore);133ur->commit_action();134}135}136}137138void Camera3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {139Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d());140141p_gizmo->clear();142143Vector<Vector3> lines;144Vector<Vector3> handles;145146Ref<Material> material = get_material("camera_material", p_gizmo);147Ref<Material> icon = get_material("camera_icon", p_gizmo);148149const Size2i viewport_size = Node3DEditor::get_camera_viewport_size(camera);150const real_t viewport_aspect = viewport_size.x > 0 && viewport_size.y > 0 ? viewport_size.aspect() : 1.0;151const Size2 size_factor = viewport_aspect > 1.0 ? Size2(1.0, 1.0 / viewport_aspect) : Size2(viewport_aspect, 1.0);152153#define ADD_TRIANGLE(m_a, m_b, m_c) \154{ \155lines.push_back(m_a); \156lines.push_back(m_b); \157lines.push_back(m_b); \158lines.push_back(m_c); \159lines.push_back(m_c); \160lines.push_back(m_a); \161}162163#define ADD_QUAD(m_a, m_b, m_c, m_d) \164{ \165lines.push_back(m_a); \166lines.push_back(m_b); \167lines.push_back(m_b); \168lines.push_back(m_c); \169lines.push_back(m_c); \170lines.push_back(m_d); \171lines.push_back(m_d); \172lines.push_back(m_a); \173}174175switch (camera->get_projection()) {176case Camera3D::PROJECTION_PERSPECTIVE: {177// The real FOV is halved for accurate representation178float fov = camera->get_fov() / 2.0;179180const float hsize = Math::sin(Math::deg_to_rad(fov));181const float depth = -Math::cos(Math::deg_to_rad(fov));182183Vector3 side;184if (camera->get_keep_aspect_mode() == Camera3D::KEEP_WIDTH) {185side = Vector3(hsize * size_factor.x, 0, depth * size_factor.x);186} else {187side = Vector3(hsize * size_factor.x, 0, depth * size_factor.y);188}189Vector3 nside = Vector3(-side.x, side.y, side.z);190Vector3 up = Vector3(0, hsize * size_factor.y, 0);191192ADD_TRIANGLE(Vector3(), side + up, side - up);193ADD_TRIANGLE(Vector3(), nside + up, nside - up);194ADD_TRIANGLE(Vector3(), side + up, nside + up);195ADD_TRIANGLE(Vector3(), side - up, nside - up);196197handles.push_back(side);198side.x = MIN(side.x, hsize * 0.25);199nside.x = -side.x;200Vector3 tup(0, up.y + hsize / 2, side.z);201ADD_TRIANGLE(tup, side + up, nside + up);202} break;203204case Camera3D::PROJECTION_ORTHOGONAL: {205Camera3D::KeepAspect aspect = camera->get_keep_aspect_mode();206207float size = camera->get_size();208float keep_size = size * 0.5;209210Vector3 right, up;211Vector3 back(0, 0, -1.0);212213if (aspect == Camera3D::KeepAspect::KEEP_WIDTH) {214right = Vector3(keep_size, 0, 0);215up = Vector3(0, keep_size / viewport_aspect, 0);216handles.push_back(right + back);217} else {218right = Vector3(keep_size * viewport_aspect, 0, 0);219up = Vector3(0, keep_size, 0);220handles.push_back(up + back);221}222223ADD_QUAD(-up - right, -up + right, up + right, up - right);224ADD_QUAD(-up - right + back, -up + right + back, up + right + back, up - right + back);225ADD_QUAD(up + right, up + right + back, up - right + back, up - right);226ADD_QUAD(-up + right, -up + right + back, -up - right + back, -up - right);227228right.x = MIN(right.x, keep_size * 0.25);229Vector3 tup(0, up.y + keep_size / 2, back.z);230ADD_TRIANGLE(tup, right + up + back, -right + up + back);231} break;232233case Camera3D::PROJECTION_FRUSTUM: {234float hsize = camera->get_size() / 2.0;235236Vector3 side = Vector3(hsize, 0, -camera->get_near()).normalized();237side.x *= size_factor.x;238Vector3 nside = Vector3(-side.x, side.y, side.z);239Vector3 up = Vector3(0, hsize * size_factor.y, 0);240Vector3 offset = Vector3(camera->get_frustum_offset().x, camera->get_frustum_offset().y, 0.0);241242ADD_TRIANGLE(Vector3(), side + up + offset, side - up + offset);243ADD_TRIANGLE(Vector3(), nside + up + offset, nside - up + offset);244ADD_TRIANGLE(Vector3(), side + up + offset, nside + up + offset);245ADD_TRIANGLE(Vector3(), side - up + offset, nside - up + offset);246247side.x = MIN(side.x, hsize * 0.25);248nside.x = -side.x;249Vector3 tup(0, up.y + hsize / 2, side.z);250ADD_TRIANGLE(tup + offset, side + up + offset, nside + up + offset);251} break;252}253254#undef ADD_TRIANGLE255#undef ADD_QUAD256257p_gizmo->add_lines(lines, material);258p_gizmo->add_unscaled_billboard(icon, 0.05);259p_gizmo->add_collision_segments(lines);260261if (!handles.is_empty()) {262p_gizmo->add_handles(handles, get_material("handles"));263}264}265266float 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) {267//bleh, discrete is simpler268static const int arc_test_points = 64;269float min_d = 1e20;270Vector3 min_p;271272for (int i = 0; i < arc_test_points; i++) {273float a = i * Math::PI * 0.5 / arc_test_points;274float an = (i + 1) * Math::PI * 0.5 / arc_test_points;275Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;276Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;277278Vector3 ra, rb;279Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);280281float d = ra.distance_to(rb);282if (d < min_d) {283min_d = d;284min_p = ra;285}286}287288//min_p = p_arc_xform.affine_inverse().xform(min_p);289float a = (Math::PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();290return Math::rad_to_deg(a);291}292293294