Debian Codegraph @

editor/scene/3d/gizmos/lightmap_gi_gizmo_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* lightmap_gi_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_gi_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_gi.h"
37
38 LightmapGIGizmoPlugin::LightmapGIGizmoPlugin() {
39 // NOTE: This gizmo only renders solid spheres for previewing indirect lighting on dynamic objects.
40 // The wireframe representation for LightmapProbe nodes is handled in LightmapProbeGizmoPlugin.
41 Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/lightmap_lines");
42 probe_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/lightmap_gi_probe_size");
43
44 gizmo_color.a = 0.1;
45 create_material("lightmap_lines", gizmo_color);
46
47 Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
48 mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
49 // Fade out probes when camera gets too close to them.
50 mat->set_distance_fade(StandardMaterial3D::DISTANCE_FADE_PIXEL_DITHER);
51 mat->set_distance_fade_min_distance(probe_size * 0.5);
52 mat->set_distance_fade_max_distance(probe_size * 1.5);
53 mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
54 mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, false);
55 mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
56
57 add_material("lightmap_probe_material", mat);
58
59 create_icon_material("baked_indirect_light_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapGI"), EditorStringName(EditorIcons)));
60 }
61
62 bool LightmapGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {
63 return Object::cast_to<LightmapGI>(p_spatial) != nullptr;
64 }
65
66 String LightmapGIGizmoPlugin::get_gizmo_name() const {
67 return "LightmapGI";
68 }
69
70 int LightmapGIGizmoPlugin::get_priority() const {
71 return -1;
72 }
73
74 void LightmapGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
75 Ref<Material> icon = get_material("baked_indirect_light_icon", p_gizmo);
76 LightmapGI *baker = Object::cast_to<LightmapGI>(p_gizmo->get_node_3d());
77 Ref<LightmapGIData> data = baker->get_light_data();
78
79 p_gizmo->clear();
80
81 p_gizmo->add_unscaled_billboard(icon, 0.05);
82
83 if (data.is_null() || !p_gizmo->is_selected()) {
84 return;
85 }
86
87 Ref<Material> material_lines = get_material("lightmap_lines", p_gizmo);
88 Ref<Material> material_probes = get_material("lightmap_probe_material", p_gizmo);
89
90 Vector<Vector3> lines;
91 HashSet<Vector2i> lines_found;
92
93 Vector<Vector3> points = data->get_capture_points();
94 if (points.is_empty()) {
95 return;
96 }
97 Vector<Color> sh = data->get_capture_sh();
98 if (sh.size() != points.size() * 9) {
99 return;
100 }
101
102 Vector<int> tetrahedrons = data->get_capture_tetrahedra();
103
104 for (int i = 0; i < tetrahedrons.size(); i += 4) {
105 for (int j = 0; j < 4; j++) {
106 for (int k = j + 1; k < 4; k++) {
107 Vector2i pair;
108 pair.x = tetrahedrons[i + j];
109 pair.y = tetrahedrons[i + k];
110
111 if (pair.y < pair.x) {
112 SWAP(pair.x, pair.y);
113 }
114 if (lines_found.has(pair)) {
115 continue;
116 }
117 lines_found.insert(pair);
118 lines.push_back(points[pair.x]);
119 lines.push_back(points[pair.y]);
120 }
121 }
122 }
123
124 p_gizmo->add_lines(lines, material_lines);
125
126 int stack_count = 8;
127 int sector_count = 16;
128
129 float sector_step = (Math::PI * 2.0) / sector_count;
130 float stack_step = Math::PI / stack_count;
131
132 LocalVector<Vector3> vertices;
133 LocalVector<Color> colors;
134 LocalVector<int> indices;
135 float radius = probe_size * 0.5f;
136
137 if (!Math::is_zero_approx(radius)) {
138 // L2 Spherical Harmonics evaluation and diffuse convolution coefficients.
139 const float sh_coeffs[5] = {
140 static_cast<float>(sqrt(1.0 / (4.0 * Math::PI)) * Math::PI),
141 static_cast<float>(sqrt(3.0 / (4.0 * Math::PI)) * Math::PI * 2.0 / 3.0),
142 static_cast<float>(sqrt(15.0 / (4.0 * Math::PI)) * Math::PI * 1.0 / 4.0),
143 static_cast<float>(sqrt(5.0 / (16.0 * Math::PI)) * Math::PI * 1.0 / 4.0),
144 static_cast<float>(sqrt(15.0 / (16.0 * Math::PI)) * Math::PI * 1.0 / 4.0)
145 };
146
147 for (int p = 0; p < points.size(); p++) {
148 int vertex_base = vertices.size();
149 Vector3 sh_col[9];
150 for (int i = 0; i < 9; i++) {
151 sh_col[i].x = sh[p * 9 + i].r;
152 sh_col[i].y = sh[p * 9 + i].g;
153 sh_col[i].z = sh[p * 9 + i].b;
154 }
155
156 for (int i = 0; i <= stack_count; ++i) {
157 float stack_angle = Math::PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
158 float xy = radius * Math::cos(stack_angle); // r * cos(u)
159 float z = radius * Math::sin(stack_angle); // r * sin(u)
160
161 // add (sector_count+1) vertices per stack
162 // the first and last vertices have same position and normal, but different tex coords
163 for (int j = 0; j <= sector_count; ++j) {
164 float sector_angle = j * sector_step; // starting from 0 to 2pi
165
166 // vertex position (x, y, z)
167 float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)
168 float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)
169
170 Vector3 n = Vector3(x, z, y);
171 vertices.push_back(points[p] + n);
172 n.normalize();
173
174 const Vector3 light = (sh_coeffs[0] * sh_col[0] +
175 sh_coeffs[1] * sh_col[1] * n.y +
176 sh_coeffs[1] * sh_col[2] * n.z +
177 sh_coeffs[1] * sh_col[3] * n.x +
178 sh_coeffs[2] * sh_col[4] * n.x * n.y +
179 sh_coeffs[2] * sh_col[5] * n.y * n.z +
180 sh_coeffs[3] * sh_col[6] * (3.0 * n.z * n.z - 1.0) +
181 sh_coeffs[2] * sh_col[7] * n.x * n.z +
182 sh_coeffs[4] * sh_col[8] * (n.x * n.x - n.y * n.y));
183
184 colors.push_back(Color(light.x, light.y, light.z, 1));
185 }
186 }
187
188 for (int i = 0; i < stack_count; ++i) {
189 int k1 = i * (sector_count + 1); // beginning of current stack
190 int k2 = k1 + sector_count + 1; // beginning of next stack
191
192 for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {
193 // 2 triangles per sector excluding first and last stacks
194 // k1 => k2 => k1+1
195 if (i != 0) {
196 indices.push_back(vertex_base + k1);
197 indices.push_back(vertex_base + k2);
198 indices.push_back(vertex_base + k1 + 1);
199 }
200
201 // k1+1 => k2 => k2+1
202 if (i != (stack_count - 1)) {
203 indices.push_back(vertex_base + k1 + 1);
204 indices.push_back(vertex_base + k2);
205 indices.push_back(vertex_base + k2 + 1);
206 }
207 }
208 }
209 }
210
211 Array array;
212 array.resize(RS::ARRAY_MAX);
213 array[RS::ARRAY_VERTEX] = Vector<Vector3>(vertices);
214 array[RS::ARRAY_INDEX] = Vector<int>(indices);
215 array[RS::ARRAY_COLOR] = Vector<Color>(colors);
216
217 Ref<ArrayMesh> mesh;
218 mesh.instantiate();
219 mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array, Array(), Dictionary(), 0); //no compression
220 mesh->surface_set_material(0, material_probes);
221
222 p_gizmo->add_mesh(mesh);
223 }
224 }