@@ -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