Skip to content

Commit c20ab94

Browse files
committed
Apply sun scatter from lights with shadows in compatibility
1 parent 06827c9 commit c20ab94

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

drivers/gles3/rasterizer_scene_gles3.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,9 @@ void RasterizerSceneGLES3::_setup_environment(const RenderDataGLES3 *p_render_da
14991499
// Only render the lights without shadows in the base pass.
15001500
scene_state.data.directional_light_count = p_render_data->directional_light_count - p_render_data->directional_shadow_count;
15011501

1502+
// Lights with shadows still need to be applied to fog sun scatter.
1503+
scene_state.data.directional_shadow_count = p_render_data->directional_shadow_count;
1504+
15021505
scene_state.data.z_far = p_render_data->z_far;
15031506
scene_state.data.z_near = p_render_data->z_near;
15041507

@@ -3377,6 +3380,10 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,
33773380
} else {
33783381
spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
33793382
}
3383+
3384+
if (p_render_data->directional_light_count > 0 && is_environment(p_render_data->environment) && environment_get_fog_sun_scatter(p_render_data->environment) > 0.001) {
3385+
spec_constants |= SceneShaderGLES3::USE_SUN_SCATTER;
3386+
}
33803387
}
33813388
} else {
33823389
// Only base pass uses the radiance map.

drivers/gles3/rasterizer_scene_gles3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class RasterizerSceneGLES3 : public RendererSceneRender {
401401
float ambient_light_color_energy[4];
402402

403403
float ambient_color_sky_mix;
404-
uint32_t pad2;
404+
uint32_t directional_shadow_count;
405405
float emissive_exposure_normalization;
406406
uint32_t use_ambient_light = 0;
407407

drivers/gles3/shaders/scene.glsl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ DISABLE_LIGHT_SPOT = false
1515
DISABLE_REFLECTION_PROBE = true
1616
DISABLE_FOG = false
1717
USE_DEPTH_FOG = false
18+
USE_SUN_SCATTER = false
1819
USE_RADIANCE_MAP = true
1920
USE_LIGHTMAP = false
2021
USE_SH_LIGHTMAP = false
@@ -188,7 +189,7 @@ struct SceneData {
188189
mediump vec4 ambient_light_color_energy;
189190

190191
mediump float ambient_color_sky_mix;
191-
float pad2;
192+
uint directional_shadow_count;
192193
float emissive_exposure_normalization;
193194
bool use_ambient_light;
194195

@@ -1130,7 +1131,7 @@ struct SceneData {
11301131
mediump vec4 ambient_light_color_energy;
11311132

11321133
mediump float ambient_color_sky_mix;
1133-
float pad2;
1134+
uint directional_shadow_count;
11341135
float emissive_exposure_normalization;
11351136
bool use_ambient_light;
11361137

@@ -1205,7 +1206,7 @@ in vec3 additive_specular_light_interp;
12051206
#endif // USE_VERTEX_LIGHTING
12061207

12071208
// Directional light data.
1208-
#if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
1209+
#if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)) || defined(USE_SUN_SCATTER)
12091210

12101211
struct DirectionalLightData {
12111212
mediump vec3 direction;
@@ -1227,7 +1228,7 @@ layout(std140) uniform DirectionalLights { // ubo:7
12271228
uniform highp sampler2DShadow directional_shadow_atlas; // texunit:-3
12281229
#endif // defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
12291230

1230-
#endif // !DISABLE_LIGHT_DIRECTIONAL
1231+
#endif // !DISABLE_LIGHT_DIRECTIONAL || USE_SUN_SCATTER
12311232

12321233
// Omni and spot light data.
12331234
#if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
@@ -1770,18 +1771,21 @@ vec4 fog_process(vec3 vertex) {
17701771
*/
17711772
#endif
17721773

1773-
#ifndef DISABLE_LIGHT_DIRECTIONAL
1774-
if (scene_data_block.data.fog_sun_scatter > 0.001) {
1775-
vec4 sun_scatter = vec4(0.0);
1776-
float sun_total = 0.0;
1777-
vec3 view = normalize(vertex);
1778-
for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) {
1779-
vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
1780-
float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
1781-
fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
1782-
}
1774+
#ifdef USE_SUN_SCATTER
1775+
vec4 sun_scatter = vec4(0.0);
1776+
float sun_total = 0.0;
1777+
vec3 view = normalize(vertex);
1778+
for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) {
1779+
vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
1780+
float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
1781+
fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
17831782
}
1784-
#endif // !DISABLE_LIGHT_DIRECTIONAL
1783+
for (uint i = uint(MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS) - uint(scene_data_block.data.directional_shadow_count); i < uint(MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS); i++) {
1784+
vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
1785+
float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
1786+
fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
1787+
}
1788+
#endif // USE_SUN_SCATTER
17851789

17861790
float fog_amount = 0.0;
17871791

0 commit comments

Comments
 (0)