From 1c62ba40b787321680c61ffed657745dcfe45bf3 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Wed, 2 Jul 2025 17:53:50 +0200 Subject: [PATCH] Disable unsupported SSR, SSS, DoF on transparent viewports For technical reasons, transparent viewports cannot support screen-space reflections, subsurface scattering and depth of field. Previously, enabling any of these would turn transparent viewports invisible. --- doc/classes/BaseMaterial3D.xml | 1 + doc/classes/CameraAttributesPractical.xml | 2 ++ doc/classes/Environment.xml | 1 + doc/classes/Viewport.xml | 1 + .../render_forward_clustered.cpp | 15 ++++++++++++--- .../renderer_rd/renderer_scene_render_rd.cpp | 9 ++++++++- 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/classes/BaseMaterial3D.xml b/doc/classes/BaseMaterial3D.xml index a25e1afcfcc..836555cff6c 100644 --- a/doc/classes/BaseMaterial3D.xml +++ b/doc/classes/BaseMaterial3D.xml @@ -398,6 +398,7 @@ If [code]true[/code], subsurface scattering is enabled. Emulates light that penetrates an object's surface, is scattered, and then emerges. Subsurface scattering quality is controlled by [member ProjectSettings.rendering/environment/subsurface_scattering/subsurface_scattering_quality]. + [b]Note:[/b] Subsurface scattering is not supported on viewports that have a transparent background (where [member Viewport.transparent_bg] is [code]true[/code]). If [code]true[/code], subsurface scattering will use a special mode optimized for the color and density of human skin, such as boosting the intensity of the red channel in subsurface scattering. diff --git a/doc/classes/CameraAttributesPractical.xml b/doc/classes/CameraAttributesPractical.xml index 893bc27f91e..ecf755f6123 100644 --- a/doc/classes/CameraAttributesPractical.xml +++ b/doc/classes/CameraAttributesPractical.xml @@ -25,6 +25,7 @@ Enables depth of field blur for objects further than [member dof_blur_far_distance]. Strength of blur is controlled by [member dof_blur_amount] and modulated by [member dof_blur_far_transition]. [b]Note:[/b] Depth of field blur is only supported in the Forward+ and Mobile rendering methods, not Compatibility. + [b]Note:[/b] Depth of field blur is not supported on viewports that have a transparent background (where [member Viewport.transparent_bg] is [code]true[/code]). When positive, distance over which (starting from [member dof_blur_far_distance]) blur effect will scale from 0 to [member dof_blur_amount]. When negative, uses physically-based scaling so depth of field effect will scale from 0 at [member dof_blur_far_distance] and will increase in a physically accurate way as objects get further from the [Camera3D]. @@ -35,6 +36,7 @@ Enables depth of field blur for objects closer than [member dof_blur_near_distance]. Strength of blur is controlled by [member dof_blur_amount] and modulated by [member dof_blur_near_transition]. [b]Note:[/b] Depth of field blur is only supported in the Forward+ and Mobile rendering methods, not Compatibility. + [b]Note:[/b] Depth of field blur is not supported on viewports that have a transparent background (where [member Viewport.transparent_bg] is [code]true[/code]). When positive, distance over which blur effect will scale from 0 to [member dof_blur_amount], ending at [member dof_blur_near_distance]. When negative, uses physically-based scaling so depth of field effect will scale from 0 at [member dof_blur_near_distance] and will increase in a physically accurate way as objects get closer to the [Camera3D]. diff --git a/doc/classes/Environment.xml b/doc/classes/Environment.xml index e87490a203f..57eb1d7526a 100644 --- a/doc/classes/Environment.xml +++ b/doc/classes/Environment.xml @@ -303,6 +303,7 @@ If [code]true[/code], screen-space reflections are enabled. Screen-space reflections are more accurate than reflections from [VoxelGI]s or [ReflectionProbe]s, but are slower and can't reflect surfaces occluded by others. [b]Note:[/b] SSR is only supported in the Forward+ rendering method, not Mobile or Compatibility. + [b]Note:[/b] SSR is not supported on viewports that have a transparent background (where [member Viewport.transparent_bg] is [code]true[/code]). The fade-in distance for screen-space reflections. Affects the area from the reflected material to the screen-space reflection. Only positive values are valid (negative values will be clamped to [code]0.0[/code]). diff --git a/doc/classes/Viewport.xml b/doc/classes/Viewport.xml index f6f9895cb4b..fc465c456d7 100644 --- a/doc/classes/Viewport.xml +++ b/doc/classes/Viewport.xml @@ -442,6 +442,7 @@ If [code]true[/code], the viewport should render its background as transparent. + [b]Note:[/b] Due to technical limitations, certain rendering features are disabled when a viewport has a transparent background. This currently applies to screen-space reflections, subsurface scattering, and depth of field. If [code]true[/code], uses a fast post-processing filter to make banding significantly less visible in 3D. 2D rendering is [i]not[/i] affected by debanding unless the [member Environment.background_mode] is [constant Environment.BG_CANVAS]. diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp index 456661d7126..857f2485373 100644 --- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp +++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp @@ -1820,9 +1820,13 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co using_sdfgi = true; } if (environment_get_ssr_enabled(p_render_data->environment)) { - using_separate_specular = true; - using_ssr = true; - color_pass_flags |= COLOR_PASS_FLAG_SEPARATE_SPECULAR; + if (!p_render_data->transparent_bg) { + using_separate_specular = true; + using_ssr = true; + color_pass_flags |= COLOR_PASS_FLAG_SEPARATE_SPECULAR; + } else { + WARN_PRINT_ONCE("Screen-space reflections are not supported in viewports with a transparent background. Disabling SSR in transparent viewport."); + } } } @@ -1900,6 +1904,11 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co bool using_sss = rb_data.is_valid() && !is_reflection_probe && scene_state.used_sss && ss_effects->sss_get_quality() != RS::SUB_SURFACE_SCATTERING_QUALITY_DISABLED; + if (using_sss && p_render_data->transparent_bg) { + WARN_PRINT_ONCE("Sub-surface scattering is not supported in viewports with a transparent background. Disabling SSS in transparent viewport."); + using_sss = false; + } + if ((using_sss || ce_needs_separate_specular) && !using_separate_specular) { using_separate_specular = true; color_pass_flags |= COLOR_PASS_FLAG_SEPARATE_SPECULAR; diff --git a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp index 2a037d241e2..4d4c73fd6af 100644 --- a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp @@ -478,7 +478,14 @@ void RendererSceneRenderRD::_render_buffers_post_process_and_tonemap(const Rende bool dest_is_msaa_2d = rb->get_view_count() == 1 && texture_storage->render_target_get_msaa(render_target) != RS::VIEWPORT_MSAA_DISABLED; - if (can_use_effects && RSG::camera_attributes->camera_attributes_uses_dof(p_render_data->camera_attributes)) { + bool using_dof = RSG::camera_attributes->camera_attributes_uses_dof(p_render_data->camera_attributes); + + if (using_dof && p_render_data->transparent_bg) { + WARN_PRINT_ONCE("Depth of field is not supported in viewports with a transparent background. Disabling DoF in transparent viewport."); + using_dof = false; + } + + if (can_use_effects && using_dof) { RENDER_TIMESTAMP("Depth of Field"); RD::get_singleton()->draw_command_begin_label("DOF");