diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index 67e9b019043..e850301ba0a 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -1499,6 +1499,9 @@ void RasterizerSceneGLES3::_setup_environment(const RenderDataGLES3 *p_render_da // Only render the lights without shadows in the base pass. scene_state.data.directional_light_count = p_render_data->directional_light_count - p_render_data->directional_shadow_count; + // Lights with shadows still need to be applied to fog sun scatter. + scene_state.data.directional_shadow_count = p_render_data->directional_shadow_count; + scene_state.data.z_far = p_render_data->z_far; scene_state.data.z_near = p_render_data->z_near; @@ -3363,6 +3366,10 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params, } else { spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP; } + + 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) { + spec_constants |= SceneShaderGLES3::USE_SUN_SCATTER; + } } } else { // Only base pass uses the radiance map. diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.h index 3f049b20a3a..9434e5d9024 100644 --- a/drivers/gles3/rasterizer_scene_gles3.h +++ b/drivers/gles3/rasterizer_scene_gles3.h @@ -401,7 +401,7 @@ private: float ambient_light_color_energy[4]; float ambient_color_sky_mix; - uint32_t pad2; + uint32_t directional_shadow_count; float emissive_exposure_normalization; uint32_t use_ambient_light = 0; diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 54c3e905870..adc5aaf695f 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -15,6 +15,7 @@ DISABLE_LIGHT_SPOT = false DISABLE_REFLECTION_PROBE = true DISABLE_FOG = false USE_DEPTH_FOG = false +USE_SUN_SCATTER = false USE_RADIANCE_MAP = true USE_LIGHTMAP = false USE_SH_LIGHTMAP = false @@ -188,7 +189,7 @@ struct SceneData { mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; - float pad2; + uint directional_shadow_count; float emissive_exposure_normalization; bool use_ambient_light; @@ -1130,7 +1131,7 @@ struct SceneData { mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; - float pad2; + uint directional_shadow_count; float emissive_exposure_normalization; bool use_ambient_light; @@ -1205,7 +1206,7 @@ in vec3 additive_specular_light_interp; #endif // USE_VERTEX_LIGHTING // Directional light data. -#if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)) +#if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)) || defined(USE_SUN_SCATTER) struct DirectionalLightData { mediump vec3 direction; @@ -1227,7 +1228,7 @@ layout(std140) uniform DirectionalLights { // ubo:7 uniform highp sampler2DShadow directional_shadow_atlas; // texunit:-3 #endif // defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)) -#endif // !DISABLE_LIGHT_DIRECTIONAL +#endif // !DISABLE_LIGHT_DIRECTIONAL || USE_SUN_SCATTER // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT) @@ -1770,18 +1771,21 @@ vec4 fog_process(vec3 vertex) { */ #endif -#ifndef DISABLE_LIGHT_DIRECTIONAL - if (scene_data_block.data.fog_sun_scatter > 0.001) { - vec4 sun_scatter = vec4(0.0); - float sun_total = 0.0; - vec3 view = normalize(vertex); - for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) { - vec3 light_color = directional_lights[i].color * directional_lights[i].energy; - float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); - fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter; - } +#ifdef USE_SUN_SCATTER + vec4 sun_scatter = vec4(0.0); + float sun_total = 0.0; + vec3 view = normalize(vertex); + for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) { + vec3 light_color = directional_lights[i].color * directional_lights[i].energy; + float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); + fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter; } -#endif // !DISABLE_LIGHT_DIRECTIONAL + 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++) { + vec3 light_color = directional_lights[i].color * directional_lights[i].energy; + float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); + fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter; + } +#endif // USE_SUN_SCATTER float fog_amount = 0.0;