Path: blob/master/editor/scene/3d/gizmos/light_3d_gizmo_plugin.cpp
9906 views
/**************************************************************************/1/* light_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 "light_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 "scene/3d/light_3d.h"3738Light3DGizmoPlugin::Light3DGizmoPlugin() {39// Enable vertex colors for the materials below as the gizmo color depends on the light color.40create_material("lines_primary", Color(1, 1, 1), false, false, true);41create_material("lines_secondary", Color(1, 1, 1, 0.35), false, false, true);42create_material("lines_billboard", Color(1, 1, 1), true, false, true);4344create_icon_material("light_directional_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoDirectionalLight"), EditorStringName(EditorIcons)));45create_icon_material("light_omni_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLight"), EditorStringName(EditorIcons)));46create_icon_material("light_spot_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoSpotLight"), EditorStringName(EditorIcons)));4748create_handle_material("handles");49create_handle_material("handles_billboard", true);50}5152bool Light3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {53return Object::cast_to<Light3D>(p_spatial) != nullptr;54}5556String Light3DGizmoPlugin::get_gizmo_name() const {57return "Light3D";58}5960int Light3DGizmoPlugin::get_priority() const {61return -1;62}6364String Light3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {65if (p_id == 0) {66return "Radius";67} else {68return "Aperture";69}70}7172Variant Light3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {73Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());74if (p_id == 0) {75return light->get_param(Light3D::PARAM_RANGE);76}77if (p_id == 1) {78return light->get_param(Light3D::PARAM_SPOT_ANGLE);79}8081return Variant();82}8384void Light3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {85Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());86Transform3D gt = light->get_global_transform();87Transform3D gi = gt.affine_inverse();8889Vector3 ray_from = p_camera->project_ray_origin(p_point);90Vector3 ray_dir = p_camera->project_ray_normal(p_point);9192Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };93if (p_id == 0) {94if (Object::cast_to<SpotLight3D>(light)) {95Vector3 ra, rb;96Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);9798float d = -ra.z;99if (Node3DEditor::get_singleton()->is_snap_enabled()) {100d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());101}102103if (d <= 0) { // Equal is here for negative zero.104d = 0;105}106107light->set_param(Light3D::PARAM_RANGE, d);108} else if (Object::cast_to<OmniLight3D>(light)) {109Plane cp = Plane(p_camera->get_transform().basis.get_column(2), gt.origin);110111Vector3 inters;112if (cp.intersects_ray(ray_from, ray_dir, &inters)) {113float r = inters.distance_to(gt.origin);114if (Node3DEditor::get_singleton()->is_snap_enabled()) {115r = Math::snapped(r, Node3DEditor::get_singleton()->get_translate_snap());116}117118light->set_param(Light3D::PARAM_RANGE, r);119}120}121122} else if (p_id == 1) {123float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], light->get_param(Light3D::PARAM_RANGE), gt);124light->set_param(Light3D::PARAM_SPOT_ANGLE, CLAMP(a, 0.01, 89.99));125}126}127128void Light3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {129Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());130if (p_cancel) {131light->set_param(p_id == 0 ? Light3D::PARAM_RANGE : Light3D::PARAM_SPOT_ANGLE, p_restore);132133} else if (p_id == 0) {134EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();135ur->create_action(TTR("Change Light Radius"));136ur->add_do_method(light, "set_param", Light3D::PARAM_RANGE, light->get_param(Light3D::PARAM_RANGE));137ur->add_undo_method(light, "set_param", Light3D::PARAM_RANGE, p_restore);138ur->commit_action();139} else if (p_id == 1) {140EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();141ur->create_action(TTR("Change Light Radius"));142ur->add_do_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, light->get_param(Light3D::PARAM_SPOT_ANGLE));143ur->add_undo_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, p_restore);144ur->commit_action();145}146}147148void Light3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {149Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());150151Color color = light->get_color().srgb_to_linear() * light->get_correlated_color().srgb_to_linear();152color = color.linear_to_srgb();153// Make the gizmo color as bright as possible for better visibility154color.set_hsv(color.get_h(), color.get_s(), 1);155156p_gizmo->clear();157158if (Object::cast_to<DirectionalLight3D>(light)) {159if (p_gizmo->is_selected()) {160Ref<Material> material = get_material("lines_primary", p_gizmo);161162const int arrow_points = 7;163const float arrow_length = 1.5;164165Vector3 arrow[arrow_points] = {166Vector3(0, 0, -1),167Vector3(0, 0.8, 0),168Vector3(0, 0.3, 0),169Vector3(0, 0.3, arrow_length),170Vector3(0, -0.3, arrow_length),171Vector3(0, -0.3, 0),172Vector3(0, -0.8, 0)173};174175int arrow_sides = 2;176177Vector<Vector3> lines;178179for (int i = 0; i < arrow_sides; i++) {180for (int j = 0; j < arrow_points; j++) {181Basis ma(Vector3(0, 0, 1), Math::PI * i / arrow_sides);182183Vector3 v1 = arrow[j] - Vector3(0, 0, arrow_length);184Vector3 v2 = arrow[(j + 1) % arrow_points] - Vector3(0, 0, arrow_length);185186lines.push_back(ma.xform(v1));187lines.push_back(ma.xform(v2));188}189}190191p_gizmo->add_lines(lines, material, false, color);192}193194Ref<Material> icon = get_material("light_directional_icon", p_gizmo);195p_gizmo->add_unscaled_billboard(icon, 0.05, color);196}197198if (Object::cast_to<OmniLight3D>(light)) {199if (p_gizmo->is_selected()) {200// Use both a billboard circle and 3 non-billboard circles for a better sphere-like representation201const Ref<Material> lines_material = get_material("lines_secondary", p_gizmo);202const Ref<Material> lines_billboard_material = get_material("lines_billboard", p_gizmo);203204OmniLight3D *on = Object::cast_to<OmniLight3D>(light);205const float r = on->get_param(Light3D::PARAM_RANGE);206Vector<Vector3> points;207Vector<Vector3> points_billboard;208209for (int i = 0; i < 120; i++) {210// Create a circle211const float ra = Math::deg_to_rad((float)(i * 3));212const float rb = Math::deg_to_rad((float)((i + 1) * 3));213const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;214const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;215216// Draw axis-aligned circles217points.push_back(Vector3(a.x, 0, a.y));218points.push_back(Vector3(b.x, 0, b.y));219points.push_back(Vector3(0, a.x, a.y));220points.push_back(Vector3(0, b.x, b.y));221points.push_back(Vector3(a.x, a.y, 0));222points.push_back(Vector3(b.x, b.y, 0));223224// Draw a billboarded circle225points_billboard.push_back(Vector3(a.x, a.y, 0));226points_billboard.push_back(Vector3(b.x, b.y, 0));227}228229p_gizmo->add_lines(points, lines_material, true, color);230p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color);231232Vector<Vector3> handles;233handles.push_back(Vector3(r, 0, 0));234p_gizmo->add_handles(handles, get_material("handles_billboard"), Vector<int>(), true);235}236237const Ref<Material> icon = get_material("light_omni_icon", p_gizmo);238p_gizmo->add_unscaled_billboard(icon, 0.05, color);239}240241if (Object::cast_to<SpotLight3D>(light)) {242if (p_gizmo->is_selected()) {243const Ref<Material> material_primary = get_material("lines_primary", p_gizmo);244const Ref<Material> material_secondary = get_material("lines_secondary", p_gizmo);245246Vector<Vector3> points_primary;247Vector<Vector3> points_secondary;248SpotLight3D *sl = Object::cast_to<SpotLight3D>(light);249250float r = sl->get_param(Light3D::PARAM_RANGE);251float w = r * Math::sin(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));252float d = r * Math::cos(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));253254for (int i = 0; i < 120; i++) {255// Draw a circle256const float ra = Math::deg_to_rad((float)(i * 3));257const float rb = Math::deg_to_rad((float)((i + 1) * 3));258const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;259const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;260261points_primary.push_back(Vector3(a.x, a.y, -d));262points_primary.push_back(Vector3(b.x, b.y, -d));263264if (i % 15 == 0) {265// Draw 8 lines from the cone origin to the sides of the circle266points_secondary.push_back(Vector3(a.x, a.y, -d));267points_secondary.push_back(Vector3());268}269}270271points_primary.push_back(Vector3(0, 0, -r));272points_primary.push_back(Vector3());273274p_gizmo->add_lines(points_primary, material_primary, false, color);275p_gizmo->add_lines(points_secondary, material_secondary, false, color);276277Vector<Vector3> handles = {278Vector3(0, 0, -r),279Vector3(w, 0, -d)280};281282p_gizmo->add_handles(handles, get_material("handles"));283}284285const Ref<Material> icon = get_material("light_spot_icon", p_gizmo);286p_gizmo->add_unscaled_billboard(icon, 0.05, color);287}288}289290float Light3DGizmoPlugin::_find_closest_angle_to_half_pi_arc(const Vector3 &p_from, const Vector3 &p_to, float p_arc_radius, const Transform3D &p_arc_xform) {291//bleh, discrete is simpler292static const int arc_test_points = 64;293float min_d = 1e20;294Vector3 min_p;295296for (int i = 0; i < arc_test_points; i++) {297float a = i * Math::PI * 0.5 / arc_test_points;298float an = (i + 1) * Math::PI * 0.5 / arc_test_points;299Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;300Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;301302Vector3 ra, rb;303Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);304305float d = ra.distance_to(rb);306if (d < min_d) {307min_d = d;308min_p = ra;309}310}311312//min_p = p_arc_xform.affine_inverse().xform(min_p);313float a = (Math::PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();314return Math::rad_to_deg(a);315}316317318