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_settings.h" |
35 | #include "editor/editor_string_names.h" |
36 | #include "editor/plugins/node_3d_editor_plugin.h" |
37 | #include "scene/3d/lightmap_probe.h" |
38 | |
39 | LightmapProbeGizmoPlugin::LightmapProbeGizmoPlugin() { |
40 | create_icon_material("lightmap_probe_icon" , EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapProbe" ), EditorStringName(EditorIcons))); |
41 | |
42 | Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/lightprobe_lines" , Color(0.5, 0.6, 1)); |
43 | |
44 | gizmo_color.a = 0.3; |
45 | create_material("lightprobe_lines" , gizmo_color); |
46 | } |
47 | |
48 | bool LightmapProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
49 | return Object::cast_to<LightmapProbe>(p_spatial) != nullptr; |
50 | } |
51 | |
52 | String LightmapProbeGizmoPlugin::get_gizmo_name() const { |
53 | return "LightmapProbe" ; |
54 | } |
55 | |
56 | int LightmapProbeGizmoPlugin::get_priority() const { |
57 | return -1; |
58 | } |
59 | |
60 | void LightmapProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
61 | Ref<Material> material_lines = get_material("lightprobe_lines" , p_gizmo); |
62 | |
63 | p_gizmo->clear(); |
64 | |
65 | Vector<Vector3> lines; |
66 | |
67 | int stack_count = 8; |
68 | int sector_count = 16; |
69 | |
70 | float sector_step = (Math_PI * 2.0) / sector_count; |
71 | float stack_step = Math_PI / stack_count; |
72 | |
73 | Vector<Vector3> vertices; |
74 | float radius = 0.2; |
75 | |
76 | for (int i = 0; i <= stack_count; ++i) { |
77 | float stack_angle = Math_PI / 2 - i * stack_step; // starting from pi/2 to -pi/2 |
78 | float xy = radius * Math::cos(stack_angle); // r * cos(u) |
79 | float z = radius * Math::sin(stack_angle); // r * sin(u) |
80 | |
81 | // add (sector_count+1) vertices per stack |
82 | // the first and last vertices have same position and normal, but different tex coords |
83 | for (int j = 0; j <= sector_count; ++j) { |
84 | float sector_angle = j * sector_step; // starting from 0 to 2pi |
85 | |
86 | // vertex position (x, y, z) |
87 | float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v) |
88 | float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v) |
89 | |
90 | Vector3 n = Vector3(x, z, y); |
91 | vertices.push_back(n); |
92 | } |
93 | } |
94 | |
95 | for (int i = 0; i < stack_count; ++i) { |
96 | int k1 = i * (sector_count + 1); // beginning of current stack |
97 | int k2 = k1 + sector_count + 1; // beginning of next stack |
98 | |
99 | for (int j = 0; j < sector_count; ++j, ++k1, ++k2) { |
100 | // 2 triangles per sector excluding first and last stacks |
101 | // k1 => k2 => k1+1 |
102 | if (i != 0) { |
103 | lines.push_back(vertices[k1]); |
104 | lines.push_back(vertices[k2]); |
105 | lines.push_back(vertices[k1]); |
106 | lines.push_back(vertices[k1 + 1]); |
107 | } |
108 | |
109 | if (i != (stack_count - 1)) { |
110 | lines.push_back(vertices[k1 + 1]); |
111 | lines.push_back(vertices[k2]); |
112 | lines.push_back(vertices[k2]); |
113 | lines.push_back(vertices[k2 + 1]); |
114 | } |
115 | } |
116 | } |
117 | |
118 | const Ref<Material> icon = get_material("lightmap_probe_icon" , p_gizmo); |
119 | |
120 | p_gizmo->add_lines(lines, material_lines); |
121 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
122 | } |
123 | |