Merge pull request #91642 from BastiaanOlij/fix_transparent_sky

Skip rendering sky if viewport is set to transparent background
This commit is contained in:
Rémi Verschelde 2024-05-14 12:06:44 +02:00
commit c9fdcde20f
No known key found for this signature in database
GPG key ID: C3336907360768E1
5 changed files with 23 additions and 12 deletions

View file

@ -2394,6 +2394,7 @@ void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_
RS::EnvironmentBG bg_mode = environment_get_background(render_data.environment); RS::EnvironmentBG bg_mode = environment_get_background(render_data.environment);
float bg_energy_multiplier = environment_get_bg_energy_multiplier(render_data.environment); float bg_energy_multiplier = environment_get_bg_energy_multiplier(render_data.environment);
bg_energy_multiplier *= environment_get_bg_intensity(render_data.environment); bg_energy_multiplier *= environment_get_bg_intensity(render_data.environment);
RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(render_data.environment);
if (render_data.camera_attributes.is_valid()) { if (render_data.camera_attributes.is_valid()) {
bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(render_data.camera_attributes); bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(render_data.camera_attributes);
@ -2404,7 +2405,7 @@ void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_
clear_color.r *= bg_energy_multiplier; clear_color.r *= bg_energy_multiplier;
clear_color.g *= bg_energy_multiplier; clear_color.g *= bg_energy_multiplier;
clear_color.b *= bg_energy_multiplier; clear_color.b *= bg_energy_multiplier;
if (environment_get_fog_enabled(render_data.environment)) { if (!render_data.transparent_bg && environment_get_fog_enabled(render_data.environment)) {
draw_sky_fog_only = true; draw_sky_fog_only = true;
GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color)); GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
} }
@ -2414,13 +2415,13 @@ void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_
clear_color.r *= bg_energy_multiplier; clear_color.r *= bg_energy_multiplier;
clear_color.g *= bg_energy_multiplier; clear_color.g *= bg_energy_multiplier;
clear_color.b *= bg_energy_multiplier; clear_color.b *= bg_energy_multiplier;
if (environment_get_fog_enabled(render_data.environment)) { if (!render_data.transparent_bg && environment_get_fog_enabled(render_data.environment)) {
draw_sky_fog_only = true; draw_sky_fog_only = true;
GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color)); GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
} }
} break; } break;
case RS::ENV_BG_SKY: { case RS::ENV_BG_SKY: {
draw_sky = true; draw_sky = !render_data.transparent_bg;
} break; } break;
case RS::ENV_BG_CANVAS: { case RS::ENV_BG_CANVAS: {
keep_color = true; keep_color = true;
@ -2433,8 +2434,9 @@ void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_
default: { default: {
} }
} }
// setup sky if used for ambient, reflections, or background // setup sky if used for ambient, reflections, or background
if (draw_sky || draw_sky_fog_only || environment_get_reflection_source(render_data.environment) == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(render_data.environment) == RS::ENV_AMBIENT_SOURCE_SKY) { if (draw_sky || draw_sky_fog_only || (reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY) || reflection_source == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(render_data.environment) == RS::ENV_AMBIENT_SOURCE_SKY) {
RENDER_TIMESTAMP("Setup Sky"); RENDER_TIMESTAMP("Setup Sky");
Projection projection = render_data.cam_projection; Projection projection = render_data.cam_projection;
if (is_reflection_probe) { if (is_reflection_probe) {

View file

@ -1812,6 +1812,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co
RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment); RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment);
float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment); float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment);
bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment); bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment);
RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(p_render_data->environment);
if (p_render_data->camera_attributes.is_valid()) { if (p_render_data->camera_attributes.is_valid()) {
bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
@ -1823,7 +1824,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co
clear_color.r *= bg_energy_multiplier; clear_color.r *= bg_energy_multiplier;
clear_color.g *= bg_energy_multiplier; clear_color.g *= bg_energy_multiplier;
clear_color.b *= bg_energy_multiplier; clear_color.b *= bg_energy_multiplier;
if ((rb->has_custom_data(RB_SCOPE_FOG)) || environment_get_fog_enabled(p_render_data->environment)) { if (!p_render_data->transparent_bg && rb->has_custom_data(RB_SCOPE_FOG) && environment_get_fog_enabled(p_render_data->environment)) {
draw_sky_fog_only = true; draw_sky_fog_only = true;
RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear()));
} }
@ -1833,13 +1834,13 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co
clear_color.r *= bg_energy_multiplier; clear_color.r *= bg_energy_multiplier;
clear_color.g *= bg_energy_multiplier; clear_color.g *= bg_energy_multiplier;
clear_color.b *= bg_energy_multiplier; clear_color.b *= bg_energy_multiplier;
if ((rb->has_custom_data(RB_SCOPE_FOG)) || environment_get_fog_enabled(p_render_data->environment)) { if (!p_render_data->transparent_bg && rb->has_custom_data(RB_SCOPE_FOG) && environment_get_fog_enabled(p_render_data->environment)) {
draw_sky_fog_only = true; draw_sky_fog_only = true;
RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear()));
} }
} break; } break;
case RS::ENV_BG_SKY: { case RS::ENV_BG_SKY: {
draw_sky = true; draw_sky = !p_render_data->transparent_bg;
} break; } break;
case RS::ENV_BG_CANVAS: { case RS::ENV_BG_CANVAS: {
if (!is_reflection_probe) { if (!is_reflection_probe) {
@ -1859,7 +1860,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co
} }
// setup sky if used for ambient, reflections, or background // setup sky if used for ambient, reflections, or background
if (draw_sky || draw_sky_fog_only || environment_get_reflection_source(p_render_data->environment) == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(p_render_data->environment) == RS::ENV_AMBIENT_SOURCE_SKY) { if (draw_sky || draw_sky_fog_only || (reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY) || reflection_source == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(p_render_data->environment) == RS::ENV_AMBIENT_SOURCE_SKY) {
RENDER_TIMESTAMP("Setup Sky"); RENDER_TIMESTAMP("Setup Sky");
RD::get_singleton()->draw_command_begin_label("Setup Sky"); RD::get_singleton()->draw_command_begin_label("Setup Sky");

View file

@ -830,6 +830,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment); RS::EnvironmentBG bg_mode = environment_get_background(p_render_data->environment);
float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment); float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment);
bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment); bg_energy_multiplier *= environment_get_bg_intensity(p_render_data->environment);
RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(p_render_data->environment);
if (p_render_data->camera_attributes.is_valid()) { if (p_render_data->camera_attributes.is_valid()) {
bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes); bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
@ -841,7 +842,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
clear_color.r *= bg_energy_multiplier; clear_color.r *= bg_energy_multiplier;
clear_color.g *= bg_energy_multiplier; clear_color.g *= bg_energy_multiplier;
clear_color.b *= bg_energy_multiplier; clear_color.b *= bg_energy_multiplier;
if (environment_get_fog_enabled(p_render_data->environment)) { if (!p_render_data->transparent_bg && environment_get_fog_enabled(p_render_data->environment)) {
draw_sky_fog_only = true; draw_sky_fog_only = true;
RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear()));
} }
@ -851,13 +852,13 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
clear_color.r *= bg_energy_multiplier; clear_color.r *= bg_energy_multiplier;
clear_color.g *= bg_energy_multiplier; clear_color.g *= bg_energy_multiplier;
clear_color.b *= bg_energy_multiplier; clear_color.b *= bg_energy_multiplier;
if (environment_get_fog_enabled(p_render_data->environment)) { if (!p_render_data->transparent_bg && environment_get_fog_enabled(p_render_data->environment)) {
draw_sky_fog_only = true; draw_sky_fog_only = true;
RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear())); RendererRD::MaterialStorage::get_singleton()->material_set_param(sky.sky_scene_state.fog_material, "clear_color", Variant(clear_color.srgb_to_linear()));
} }
} break; } break;
case RS::ENV_BG_SKY: { case RS::ENV_BG_SKY: {
draw_sky = true; draw_sky = !p_render_data->transparent_bg;
} break; } break;
case RS::ENV_BG_CANVAS: { case RS::ENV_BG_CANVAS: {
if (rb_data.is_valid()) { if (rb_data.is_valid()) {
@ -874,7 +875,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
} }
// setup sky if used for ambient, reflections, or background // setup sky if used for ambient, reflections, or background
if (draw_sky || draw_sky_fog_only || environment_get_reflection_source(p_render_data->environment) == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(p_render_data->environment) == RS::ENV_AMBIENT_SOURCE_SKY) { if (draw_sky || draw_sky_fog_only || (reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY) || reflection_source == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(p_render_data->environment) == RS::ENV_AMBIENT_SOURCE_SKY) {
RENDER_TIMESTAMP("Setup Sky"); RENDER_TIMESTAMP("Setup Sky");
RD::get_singleton()->draw_command_begin_label("Setup Sky"); RD::get_singleton()->draw_command_begin_label("Setup Sky");

View file

@ -1149,6 +1149,10 @@ void RendererSceneRenderRD::render_scene(const Ref<RenderSceneBuffers> &p_render
render_data.sdfgi_update_data = p_sdfgi_update_data; render_data.sdfgi_update_data = p_sdfgi_update_data;
render_data.render_info = r_render_info; render_data.render_info = r_render_info;
if (p_render_buffers.is_valid() && p_reflection_probe.is_null()) {
render_data.transparent_bg = texture_storage->render_target_get_transparent(rb->get_render_target());
}
} }
PagedArray<RID> empty; PagedArray<RID> empty;

View file

@ -78,6 +78,9 @@ public:
RenderingMethod::RenderInfo *render_info = nullptr; RenderingMethod::RenderInfo *render_info = nullptr;
/* Viewport data */
bool transparent_bg = false;
/* Shadow data */ /* Shadow data */
const RendererSceneRender::RenderShadowData *render_shadows = nullptr; const RendererSceneRender::RenderShadowData *render_shadows = nullptr;
int render_shadow_count = 0; int render_shadow_count = 0;