Path: blob/master/editor/scene/3d/gizmos/light_3d_gizmo_plugin.cpp
21635 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 "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 "scene/3d/light_3d.h"3839Light3DGizmoPlugin::Light3DGizmoPlugin() {40// Enable vertex colors for the materials below as the gizmo color depends on the light color.41create_material("lines_primary", Color(1, 1, 1), false, false, true);42create_material("lines_secondary", Color(1, 1, 1, 0.35), false, false, true);43create_material("lines_billboard", Color(1, 1, 1), true, false, true);4445create_icon_material("light_directional_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoDirectionalLight"), EditorStringName(EditorIcons)));46create_icon_material("light_omni_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLight"), EditorStringName(EditorIcons)));47create_icon_material("light_spot_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoSpotLight"), EditorStringName(EditorIcons)));4849create_handle_material("handles");50create_handle_material("handles_billboard", true);51}5253bool Light3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {54return Object::cast_to<Light3D>(p_spatial) != nullptr;55}5657String Light3DGizmoPlugin::get_gizmo_name() const {58return "Light3D";59}6061int Light3DGizmoPlugin::get_priority() const {62return -1;63}6465String Light3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {66if (p_id == 0) {67return "Radius";68} else {69return "Aperture";70}71}7273Variant Light3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {74Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());75if (p_id == 0) {76return light->get_param(Light3D::PARAM_RANGE);77}78if (p_id == 1) {79return light->get_param(Light3D::PARAM_SPOT_ANGLE);80}8182return Variant();83}8485void Light3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {86Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());87Transform3D gt = light->get_global_transform();88Transform3D gi = gt.affine_inverse();8990Vector3 ray_from = p_camera->project_ray_origin(p_point);91Vector3 ray_dir = p_camera->project_ray_normal(p_point);9293Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };94if (p_id == 0) {95if (Object::cast_to<SpotLight3D>(light)) {96Vector3 ra, rb;97Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);9899float d = -ra.z;100if (Node3DEditor::get_singleton()->is_snap_enabled()) {101d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());102}103104if (d <= 0) { // Equal is here for negative zero.105d = 0;106}107108light->set_param(Light3D::PARAM_RANGE, d);109} else if (Object::cast_to<OmniLight3D>(light)) {110Plane cp = Plane(p_camera->get_transform().basis.get_column(2), gt.origin);111112Vector3 inters;113if (cp.intersects_ray(ray_from, ray_dir, &inters)) {114float r = inters.distance_to(gt.origin);115if (Node3DEditor::get_singleton()->is_snap_enabled()) {116r = Math::snapped(r, Node3DEditor::get_singleton()->get_translate_snap());117}118119light->set_param(Light3D::PARAM_RANGE, r);120}121}122123} else if (p_id == 1) {124float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], light->get_param(Light3D::PARAM_RANGE), gt);125light->set_param(Light3D::PARAM_SPOT_ANGLE, CLAMP(a, 0.01, 89.99));126}127}128129void Light3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {130Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());131if (p_cancel) {132light->set_param(p_id == 0 ? Light3D::PARAM_RANGE : Light3D::PARAM_SPOT_ANGLE, p_restore);133134} else if (p_id == 0) {135EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();136ur->create_action(TTR("Change Light Radius"));137ur->add_do_method(light, "set_param", Light3D::PARAM_RANGE, light->get_param(Light3D::PARAM_RANGE));138ur->add_undo_method(light, "set_param", Light3D::PARAM_RANGE, p_restore);139ur->commit_action();140} else if (p_id == 1) {141EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();142ur->create_action(TTR("Change Light Radius"));143ur->add_do_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, light->get_param(Light3D::PARAM_SPOT_ANGLE));144ur->add_undo_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, p_restore);145ur->commit_action();146}147}148149void Light3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {150Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());151152Color color = light->get_color().srgb_to_linear() * light->get_correlated_color().srgb_to_linear();153color = color.linear_to_srgb();154// Make the gizmo color as bright as possible for better visibility155color.set_hsv(color.get_h(), color.get_s(), 1);156157p_gizmo->clear();158159if (Object::cast_to<DirectionalLight3D>(light)) {160if (p_gizmo->is_selected()) {161Ref<Material> material = get_material("lines_primary", p_gizmo);162163const int arrow_points = 7;164const float arrow_length = 1.5;165166Vector3 arrow[arrow_points] = {167Vector3(0, 0, -1),168Vector3(0, 0.8, 0),169Vector3(0, 0.3, 0),170Vector3(0, 0.3, arrow_length),171Vector3(0, -0.3, arrow_length),172Vector3(0, -0.3, 0),173Vector3(0, -0.8, 0)174};175176int arrow_sides = 2;177178Vector<Vector3> lines;179180for (int i = 0; i < arrow_sides; i++) {181for (int j = 0; j < arrow_points; j++) {182Basis ma(Vector3(0, 0, 1), Math::PI * i / arrow_sides);183184Vector3 v1 = arrow[j] - Vector3(0, 0, arrow_length);185Vector3 v2 = arrow[(j + 1) % arrow_points] - Vector3(0, 0, arrow_length);186187lines.push_back(ma.xform(v1));188lines.push_back(ma.xform(v2));189}190}191192p_gizmo->add_lines(lines, material, false, color);193}194195Ref<Material> icon = get_material("light_directional_icon", p_gizmo);196p_gizmo->add_unscaled_billboard(icon, 0.05, color);197}198199if (Object::cast_to<OmniLight3D>(light)) {200if (p_gizmo->is_selected()) {201// Use both a billboard circle and 3 non-billboard circles for a better sphere-like representation202const Ref<Material> lines_material = get_material("lines_secondary", p_gizmo);203const Ref<Material> lines_billboard_material = get_material("lines_billboard", p_gizmo);204205OmniLight3D *on = Object::cast_to<OmniLight3D>(light);206const float r = on->get_param(Light3D::PARAM_RANGE);207Vector<Vector3> points;208Vector<Vector3> points_billboard;209210for (int i = 0; i < 120; i++) {211// Create a circle212const float ra = Math::deg_to_rad((float)(i * 3));213const float rb = Math::deg_to_rad((float)((i + 1) * 3));214const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;215const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;216217// Draw axis-aligned circles218points.push_back(Vector3(a.x, 0, a.y));219points.push_back(Vector3(b.x, 0, b.y));220points.push_back(Vector3(0, a.x, a.y));221points.push_back(Vector3(0, b.x, b.y));222points.push_back(Vector3(a.x, a.y, 0));223points.push_back(Vector3(b.x, b.y, 0));224225// Draw a billboarded circle226points_billboard.push_back(Vector3(a.x, a.y, 0));227points_billboard.push_back(Vector3(b.x, b.y, 0));228}229230p_gizmo->add_lines(points, lines_material, true, color);231p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color);232233Vector<Vector3> handles;234handles.push_back(Vector3(r, 0, 0));235p_gizmo->add_handles(handles, get_material("handles_billboard"), Vector<int>(), true);236}237238const Ref<Material> icon = get_material("light_omni_icon", p_gizmo);239p_gizmo->add_unscaled_billboard(icon, 0.05, color);240}241242if (Object::cast_to<SpotLight3D>(light)) {243if (p_gizmo->is_selected()) {244const Ref<Material> material_primary = get_material("lines_primary", p_gizmo);245const Ref<Material> material_secondary = get_material("lines_secondary", p_gizmo);246247Vector<Vector3> points_primary;248Vector<Vector3> points_secondary;249SpotLight3D *sl = Object::cast_to<SpotLight3D>(light);250251float r = sl->get_param(Light3D::PARAM_RANGE);252float w = r * Math::sin(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));253float d = r * Math::cos(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));254255for (int i = 0; i < 120; i++) {256// Draw a circle257const float ra = Math::deg_to_rad((float)(i * 3));258const float rb = Math::deg_to_rad((float)((i + 1) * 3));259const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;260const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;261262points_primary.push_back(Vector3(a.x, a.y, -d));263points_primary.push_back(Vector3(b.x, b.y, -d));264265if (i % 15 == 0) {266// Draw 8 lines from the cone origin to the sides of the circle267points_secondary.push_back(Vector3(a.x, a.y, -d));268points_secondary.push_back(Vector3());269}270}271272points_primary.push_back(Vector3(0, 0, -r));273points_primary.push_back(Vector3());274275p_gizmo->add_lines(points_primary, material_primary, false, color);276p_gizmo->add_lines(points_secondary, material_secondary, false, color);277278Vector<Vector3> handles = {279Vector3(0, 0, -r),280Vector3(w, 0, -d)281};282283p_gizmo->add_handles(handles, get_material("handles"));284}285286const Ref<Material> icon = get_material("light_spot_icon", p_gizmo);287p_gizmo->add_unscaled_billboard(icon, 0.05, color);288}289}290291float 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) {292//bleh, discrete is simpler293static const int arc_test_points = 64;294float min_d = 1e20;295Vector3 min_p;296297for (int i = 0; i < arc_test_points; i++) {298float a = i * Math::PI * 0.5 / arc_test_points;299float an = (i + 1) * Math::PI * 0.5 / arc_test_points;300Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;301Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;302303Vector3 ra, rb;304Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);305306float d = ra.distance_to(rb);307if (d < min_d) {308min_d = d;309min_p = ra;310}311}312313//min_p = p_arc_xform.affine_inverse().xform(min_p);314float a = (Math::PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();315return Math::rad_to_deg(a);316}317318319