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_settings.h"
35#include "editor/editor_string_names.h"
36#include "editor/plugins/node_3d_editor_plugin.h"
37#include "scene/3d/lightmap_gi.h"
38
39LightmapGIGizmoPlugin::LightmapGIGizmoPlugin() {
40 Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/lightmap_lines", Color(0.5, 0.6, 1));
41
42 gizmo_color.a = 0.1;
43 create_material("lightmap_lines", gizmo_color);
44
45 Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
46 mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
47 mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
48 mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
49 mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, false);
50
51 add_material("lightmap_probe_material", mat);
52
53 create_icon_material("baked_indirect_light_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapGI"), EditorStringName(EditorIcons)));
54}
55
56bool LightmapGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {
57 return Object::cast_to<LightmapGI>(p_spatial) != nullptr;
58}
59
60String LightmapGIGizmoPlugin::get_gizmo_name() const {
61 return "LightmapGI";
62}
63
64int LightmapGIGizmoPlugin::get_priority() const {
65 return -1;
66}
67
68void LightmapGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
69 Ref<Material> icon = get_material("baked_indirect_light_icon", p_gizmo);
70 LightmapGI *baker = Object::cast_to<LightmapGI>(p_gizmo->get_node_3d());
71 Ref<LightmapGIData> data = baker->get_light_data();
72
73 p_gizmo->clear();
74
75 p_gizmo->add_unscaled_billboard(icon, 0.05);
76
77 if (data.is_null() || !p_gizmo->is_selected()) {
78 return;
79 }
80
81 Ref<Material> material_lines = get_material("lightmap_lines", p_gizmo);
82 Ref<Material> material_probes = get_material("lightmap_probe_material", p_gizmo);
83
84 Vector<Vector3> lines;
85 HashSet<Vector2i> lines_found;
86
87 Vector<Vector3> points = data->get_capture_points();
88 if (points.size() == 0) {
89 return;
90 }
91 Vector<Color> sh = data->get_capture_sh();
92 if (sh.size() != points.size() * 9) {
93 return;
94 }
95
96 Vector<int> tetrahedrons = data->get_capture_tetrahedra();
97
98 for (int i = 0; i < tetrahedrons.size(); i += 4) {
99 for (int j = 0; j < 4; j++) {
100 for (int k = j + 1; k < 4; k++) {
101 Vector2i pair;
102 pair.x = tetrahedrons[i + j];
103 pair.y = tetrahedrons[i + k];
104
105 if (pair.y < pair.x) {
106 SWAP(pair.x, pair.y);
107 }
108 if (lines_found.has(pair)) {
109 continue;
110 }
111 lines_found.insert(pair);
112 lines.push_back(points[pair.x]);
113 lines.push_back(points[pair.y]);
114 }
115 }
116 }
117
118 p_gizmo->add_lines(lines, material_lines);
119
120 int stack_count = 8;
121 int sector_count = 16;
122
123 float sector_step = (Math_PI * 2.0) / sector_count;
124 float stack_step = Math_PI / stack_count;
125
126 Vector<Vector3> vertices;
127 Vector<Color> colors;
128 Vector<int> indices;
129 float radius = 0.3;
130
131 for (int p = 0; p < points.size(); p++) {
132 int vertex_base = vertices.size();
133 Vector3 sh_col[9];
134 for (int i = 0; i < 9; i++) {
135 sh_col[i].x = sh[p * 9 + i].r;
136 sh_col[i].y = sh[p * 9 + i].g;
137 sh_col[i].z = sh[p * 9 + i].b;
138 }
139
140 for (int i = 0; i <= stack_count; ++i) {
141 float stack_angle = Math_PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
142 float xy = radius * Math::cos(stack_angle); // r * cos(u)
143 float z = radius * Math::sin(stack_angle); // r * sin(u)
144
145 // add (sector_count+1) vertices per stack
146 // the first and last vertices have same position and normal, but different tex coords
147 for (int j = 0; j <= sector_count; ++j) {
148 float sector_angle = j * sector_step; // starting from 0 to 2pi
149
150 // vertex position (x, y, z)
151 float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)
152 float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)
153
154 Vector3 n = Vector3(x, z, y);
155 vertices.push_back(points[p] + n);
156 n.normalize();
157
158 const float c1 = 0.429043;
159 const float c2 = 0.511664;
160 const float c3 = 0.743125;
161 const float c4 = 0.886227;
162 const float c5 = 0.247708;
163 Vector3 light = (c1 * sh_col[8] * (n.x * n.x - n.y * n.y) +
164 c3 * sh_col[6] * n.z * n.z +
165 c4 * sh_col[0] -
166 c5 * sh_col[6] +
167 2.0 * c1 * sh_col[4] * n.x * n.y +
168 2.0 * c1 * sh_col[7] * n.x * n.z +
169 2.0 * c1 * sh_col[5] * n.y * n.z +
170 2.0 * c2 * sh_col[3] * n.x +
171 2.0 * c2 * sh_col[1] * n.y +
172 2.0 * c2 * sh_col[2] * n.z);
173
174 colors.push_back(Color(light.x, light.y, light.z, 1));
175 }
176 }
177
178 for (int i = 0; i < stack_count; ++i) {
179 int k1 = i * (sector_count + 1); // beginning of current stack
180 int k2 = k1 + sector_count + 1; // beginning of next stack
181
182 for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {
183 // 2 triangles per sector excluding first and last stacks
184 // k1 => k2 => k1+1
185 if (i != 0) {
186 indices.push_back(vertex_base + k1);
187 indices.push_back(vertex_base + k2);
188 indices.push_back(vertex_base + k1 + 1);
189 }
190
191 // k1+1 => k2 => k2+1
192 if (i != (stack_count - 1)) {
193 indices.push_back(vertex_base + k1 + 1);
194 indices.push_back(vertex_base + k2);
195 indices.push_back(vertex_base + k2 + 1);
196 }
197 }
198 }
199 }
200
201 Array array;
202 array.resize(RS::ARRAY_MAX);
203 array[RS::ARRAY_VERTEX] = vertices;
204 array[RS::ARRAY_INDEX] = indices;
205 array[RS::ARRAY_COLOR] = colors;
206
207 Ref<ArrayMesh> mesh;
208 mesh.instantiate();
209 mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array, Array(), Dictionary(), 0); //no compression
210 mesh->surface_set_material(0, material_probes);
211
212 p_gizmo->add_mesh(mesh);
213}
214