3636#include " scene/3d/lightmap_gi.h"
3737
3838LightmapGIGizmoPlugin::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.
3941 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" );
4043
4144 gizmo_color.a = 0.1 ;
4245 create_material (" lightmap_lines" , gizmo_color);
@@ -45,8 +48,8 @@ LightmapGIGizmoPlugin::LightmapGIGizmoPlugin() {
4548 mat->set_shading_mode (StandardMaterial3D::SHADING_MODE_UNSHADED);
4649 // Fade out probes when camera gets too close to them.
4750 mat->set_distance_fade (StandardMaterial3D::DISTANCE_FADE_PIXEL_DITHER);
48- mat->set_distance_fade_min_distance (0.5 );
49- mat->set_distance_fade_max_distance (1.5 );
51+ mat->set_distance_fade_min_distance (probe_size * 0.5 );
52+ mat->set_distance_fade_max_distance (probe_size * 1.5 );
5053 mat->set_flag (StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true );
5154 mat->set_flag (StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, false );
5255 mat->set_flag (StandardMaterial3D::FLAG_DISABLE_FOG, true );
@@ -129,91 +132,93 @@ void LightmapGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
129132 LocalVector<Vector3> vertices;
130133 LocalVector<Color> colors;
131134 LocalVector<int > indices;
132- float radius = 0.3 ;
133-
134- // L2 Spherical Harmonics evaluation and diffuse convolution coefficients.
135- const float sh_coeffs[5 ] = {
136- static_cast <float >(sqrt (1.0 / (4.0 * Math::PI)) * Math::PI),
137- static_cast <float >(sqrt (3.0 / (4.0 * Math::PI)) * Math::PI * 2.0 / 3.0 ),
138- static_cast <float >(sqrt (15.0 / (4.0 * Math::PI)) * Math::PI * 1.0 / 4.0 ),
139- static_cast <float >(sqrt (5.0 / (16.0 * Math::PI)) * Math::PI * 1.0 / 4.0 ),
140- static_cast <float >(sqrt (15.0 / (16.0 * Math::PI)) * Math::PI * 1.0 / 4.0 )
141- };
142-
143- for (int p = 0 ; p < points.size (); p++) {
144- int vertex_base = vertices.size ();
145- Vector3 sh_col[9 ];
146- for (int i = 0 ; i < 9 ; i++) {
147- sh_col[i].x = sh[p * 9 + i].r ;
148- sh_col[i].y = sh[p * 9 + i].g ;
149- sh_col[i].z = sh[p * 9 + i].b ;
150- }
151-
152- for (int i = 0 ; i <= stack_count; ++i) {
153- float stack_angle = Math::PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
154- float xy = radius * Math::cos (stack_angle); // r * cos(u)
155- float z = radius * Math::sin (stack_angle); // r * sin(u)
156-
157- // add (sector_count+1) vertices per stack
158- // the first and last vertices have same position and normal, but different tex coords
159- for (int j = 0 ; j <= sector_count; ++j) {
160- float sector_angle = j * sector_step; // starting from 0 to 2pi
161-
162- // vertex position (x, y, z)
163- float x = xy * Math::cos (sector_angle); // r * cos(u) * cos(v)
164- float y = xy * Math::sin (sector_angle); // r * cos(u) * sin(v)
165-
166- Vector3 n = Vector3 (x, z, y);
167- vertices.push_back (points[p] + n);
168- n.normalize ();
169-
170- const Vector3 light = (sh_coeffs[0 ] * sh_col[0 ] +
171- sh_coeffs[1 ] * sh_col[1 ] * n.y +
172- sh_coeffs[1 ] * sh_col[2 ] * n.z +
173- sh_coeffs[1 ] * sh_col[3 ] * n.x +
174- sh_coeffs[2 ] * sh_col[4 ] * n.x * n.y +
175- sh_coeffs[2 ] * sh_col[5 ] * n.y * n.z +
176- sh_coeffs[3 ] * sh_col[6 ] * (3.0 * n.z * n.z - 1.0 ) +
177- sh_coeffs[2 ] * sh_col[7 ] * n.x * n.z +
178- sh_coeffs[4 ] * sh_col[8 ] * (n.x * n.x - n.y * n.y ));
179-
180- colors.push_back (Color (light.x , light.y , light.z , 1 ));
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 ;
181154 }
182- }
183155
184- for (int i = 0 ; i < stack_count; ++i) {
185- int k1 = i * (sector_count + 1 ); // beginning of current stack
186- int k2 = k1 + sector_count + 1 ; // beginning of next stack
187-
188- for (int j = 0 ; j < sector_count; ++j, ++k1, ++k2) {
189- // 2 triangles per sector excluding first and last stacks
190- // k1 => k2 => k1+1
191- if (i != 0 ) {
192- indices.push_back (vertex_base + k1);
193- indices.push_back (vertex_base + k2);
194- indices.push_back (vertex_base + k1 + 1 );
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 ));
195185 }
186+ }
196187
197- // k1+1 => k2 => k2+1
198- if (i != (stack_count - 1 )) {
199- indices.push_back (vertex_base + k1 + 1 );
200- indices.push_back (vertex_base + k2);
201- indices.push_back (vertex_base + k2 + 1 );
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+ }
202207 }
203208 }
204209 }
205- }
206210
207- Array array;
208- array.resize (RS::ARRAY_MAX);
209- array[RS::ARRAY_VERTEX] = Vector<Vector3>(vertices);
210- array[RS::ARRAY_INDEX] = Vector<int >(indices);
211- array[RS::ARRAY_COLOR] = Vector<Color>(colors);
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);
212216
213- Ref<ArrayMesh> mesh;
214- mesh.instantiate ();
215- mesh->add_surface_from_arrays (Mesh::PRIMITIVE_TRIANGLES, array, Array (), Dictionary (), 0 ); // no compression
216- mesh->surface_set_material (0 , material_probes);
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);
217221
218- p_gizmo->add_mesh (mesh);
222+ p_gizmo->add_mesh (mesh);
223+ }
219224}
0 commit comments