| 1 | /**************************************************************************/ |
| 2 | /* lightmap_probe_gizmo_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "lightmap_probe_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_string_names.h" |
| 35 | #include "editor/settings/editor_settings.h" |
| 36 | #include "scene/3d/lightmap_probe.h" |
| 37 | |
| 38 | LightmapProbeGizmoPlugin::LightmapProbeGizmoPlugin() { |
| 39 | // NOTE: This gizmo only renders LightmapProbe nodes as wireframes. |
| 40 | // The solid sphere representation is handled in LightmapGIGizmoPlugin. |
| 41 | create_icon_material("lightmap_probe_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapProbe"), EditorStringName(EditorIcons))); |
| 42 | |
| 43 | Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/lightprobe_lines"); |
| 44 | probe_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/lightmap_gi_probe_size"); |
| 45 | |
| 46 | gizmo_color.a = 0.3; |
| 47 | create_material("lightprobe_lines", gizmo_color); |
| 48 | } |
| 49 | |
| 50 | bool LightmapProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 51 | return Object::cast_to<LightmapProbe>(p_spatial) != nullptr; |
| 52 | } |
| 53 | |
| 54 | String LightmapProbeGizmoPlugin::get_gizmo_name() const { |
| 55 | return "LightmapProbe"; |
| 56 | } |
| 57 | |
| 58 | int LightmapProbeGizmoPlugin::get_priority() const { |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | void LightmapProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 63 | Ref<Material> material_lines = get_material("lightprobe_lines", p_gizmo); |
| 64 | |
| 65 | p_gizmo->clear(); |
| 66 | |
| 67 | Vector<Vector3> lines; |
| 68 | |
| 69 | int stack_count = 8; |
| 70 | int sector_count = 16; |
| 71 | |
| 72 | float sector_step = (Math::PI * 2.0) / sector_count; |
| 73 | float stack_step = Math::PI / stack_count; |
| 74 | |
| 75 | Vector<Vector3> vertices; |
| 76 | // Make the lines' radius slightly smaller than its mesh representation to avoid Z-fighting. |
| 77 | float radius = probe_size * 0.495f; |
| 78 | |
| 79 | if (!Math::is_zero_approx(radius)) { |
| 80 | for (int i = 0; i <= stack_count; ++i) { |
| 81 | float stack_angle = Math::PI / 2 - i * stack_step; // starting from pi/2 to -pi/2 |
| 82 | float xy = radius * Math::cos(stack_angle); // r * cos(u) |
| 83 | float z = radius * Math::sin(stack_angle); // r * sin(u) |
| 84 | |
| 85 | // add (sector_count+1) vertices per stack |
| 86 | // the first and last vertices have same position and normal, but different tex coords |
| 87 | for (int j = 0; j <= sector_count; ++j) { |
| 88 | float sector_angle = j * sector_step; // starting from 0 to 2pi |
| 89 | |
| 90 | // vertex position (x, y, z) |
| 91 | float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v) |
| 92 | float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v) |
| 93 | |
| 94 | Vector3 n = Vector3(x, z, y); |
| 95 | vertices.push_back(n); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | for (int i = 0; i < stack_count; ++i) { |
| 100 | int k1 = i * (sector_count + 1); // beginning of current stack |
| 101 | int k2 = k1 + sector_count + 1; // beginning of next stack |
| 102 | |
| 103 | for (int j = 0; j < sector_count; ++j, ++k1, ++k2) { |
| 104 | // 2 triangles per sector excluding first and last stacks |
| 105 | // k1 => k2 => k1+1 |
| 106 | if (i != 0) { |
| 107 | lines.push_back(vertices[k1]); |
| 108 | lines.push_back(vertices[k2]); |
| 109 | lines.push_back(vertices[k1]); |
| 110 | lines.push_back(vertices[k1 + 1]); |
| 111 | } |
| 112 | |
| 113 | if (i != (stack_count - 1)) { |
| 114 | lines.push_back(vertices[k1 + 1]); |
| 115 | lines.push_back(vertices[k2]); |
| 116 | lines.push_back(vertices[k2]); |
| 117 | lines.push_back(vertices[k2 + 1]); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | p_gizmo->add_lines(lines, material_lines); |
| 123 | } |
| 124 | const Ref<Material> icon = get_material("lightmap_probe_icon", p_gizmo); |
| 125 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
| 126 | } |