Skip to content

Commit 8ee3897

Browse files
committed
Clean up Volumetric Fog blending behavior
Use proper blending everywhere to be consistent and avoid multiplying by opacity twice.
1 parent cb3af5a commit 8ee3897

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

servers/rendering/renderer_rd/shaders/environment/sky.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ void main() {
281281

282282
if (sky_scene_data.volumetric_fog_enabled) {
283283
vec4 fog = volumetric_fog_process(uv);
284-
frag_color.rgb = frag_color.rgb * (1.0 - fog.a * sky_scene_data.volumetric_fog_sky_affect) + fog.rgb * sky_scene_data.volumetric_fog_sky_affect;
284+
fog.rgb = frag_color.rgb * fog.a + fog.rgb;
285+
frag_color.rgb = mix(frag_color.rgb, fog.rgb, sky_scene_data.volumetric_fog_sky_affect);
285286
}
286287

287288
if (custom_fog.a > 0.0) {

servers/rendering/renderer_rd/shaders/environment/volumetric_fog_process.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ void main() {
719719

720720
prev_z = z;
721721

722-
imageStore(fog_map, fog_pos, vec4(fog_accum.rgb, 1.0 - fog_accum.a));
722+
imageStore(fog_map, fog_pos, vec4(fog_accum.rgb, fog_accum.a));
723723
}
724724

725725
#endif

servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ void fragment_shader(in SceneData scene_data) {
11931193
vec2 anisotropy_flow = vec2(1.0, 0.0);
11941194
vec3 energy_compensation = vec3(1.0);
11951195
#ifndef FOG_DISABLED
1196-
vec4 fog = vec4(0.0);
1196+
vec4 fog = vec4(0.0, 0.0, 0.0, 1.0);
11971197
#endif // !FOG_DISABLED
11981198
#if defined(CUSTOM_RADIANCE_USED)
11991199
vec4 custom_radiance = vec4(0.0);
@@ -1435,6 +1435,9 @@ void fragment_shader(in SceneData scene_data) {
14351435

14361436
if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_FOG)) {
14371437
fog = fog_process(vertex);
1438+
// Premultiply by opacity and convert opacity to transmittance to match volumetric fog.
1439+
fog.rgb *= fog.a;
1440+
fog.a = 1.0 - fog.a;
14381441
}
14391442

14401443
if (implementation_data.volumetric_fog_enabled) {
@@ -1446,19 +1449,17 @@ void fragment_shader(in SceneData scene_data) {
14461449
vec4 res = vec4(0.0);
14471450
if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_FOG)) {
14481451
//must use the full blending equation here to blend fogs
1449-
float sa = 1.0 - volumetric_fog.a;
1450-
res.a = fog.a * sa + volumetric_fog.a;
1451-
if (res.a > 0.0) {
1452-
res.rgb = (fog.rgb * fog.a * sa + volumetric_fog.rgb) / res.a;
1453-
}
1452+
res.a = fog.a * volumetric_fog.a;
1453+
res.rgb = fog.rgb * volumetric_fog.a + volumetric_fog.rgb;
14541454
} else {
1455-
res.a = volumetric_fog.a;
1456-
if (res.a > 0.0) {
1457-
res.rgb = volumetric_fog.rgb / res.a;
1458-
}
1455+
res = volumetric_fog;
14591456
}
14601457
fog = res;
14611458
}
1459+
#else
1460+
// Premultiply by opacity and convert opacity to transmittance to match volumetric fog.
1461+
fog.rgb *= fog.a;
1462+
fog.a = 1.0 - fog.a;
14621463
#endif //!CUSTOM_FOG_USED
14631464

14641465
uint fog_rg = packHalf2x16(fog.rg);
@@ -2889,8 +2890,8 @@ void fragment_shader(in SceneData scene_data) {
28892890
#endif
28902891

28912892
#ifndef FOG_DISABLED
2892-
diffuse_buffer.rgb = mix(diffuse_buffer.rgb, fog.rgb, fog.a);
2893-
specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
2893+
diffuse_buffer.rgb = diffuse_buffer.rgb * fog.a + fog.rgb;
2894+
specular_buffer.rgb = specular_buffer.rgb * fog.a;
28942895
#endif //!FOG_DISABLED
28952896

28962897
#else //MODE_SEPARATE_SPECULAR
@@ -2905,8 +2906,7 @@ void fragment_shader(in SceneData scene_data) {
29052906
#endif //USE_NO_SHADING
29062907

29072908
#ifndef FOG_DISABLED
2908-
// Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
2909-
frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
2909+
frag_color.rgb = frag_color.rgb * fog.a + fog.rgb;
29102910
#endif //!FOG_DISABLED
29112911

29122912
#endif //MODE_SEPARATE_SPECULAR

0 commit comments

Comments
 (0)