@@ -5,14 +5,27 @@ layout(location = 0) out vec4 outColor;
55
66layout (binding = 0 ) uniform sampler2D uInputImage;
77
8+ // Binding 2: Multisampled depth texture from geometry pass
9+ layout (binding = 2 ) uniform sampler2DMS uDepthImage;
10+
811layout (set = 0 , binding = 1 ) uniform CompositeParams {
9- float uExposure; // Default: 1.0
10- float uContrast; // Default: 1.0
11- float uSaturation; // Default: 1.0
12- float uVignetteIntensity; // Default: 0.5
13- float uVignetteFalloff; // Default 0.5
12+ float uExposure;// Default: 1.0
13+ float uContrast;// Default: 1.0
14+ float uSaturation;// Default: 1.0
15+ float uVignetteIntensity;// Default: 0.5
16+ float uVignetteFalloff;// Default 0.5
17+ float uFogDensity;
1418};
1519
20+ // add camera projection uniforms to the UBO or pass them in another way
21+ const float Z_NEAR = 0.1 ;
22+ const float Z_FAR = 100.0 ;
23+
24+ float linearizeDepth(float depth) {
25+ float z_n = 2.0 * depth - 1.0 ;
26+ return (2.0 * Z_NEAR * Z_FAR) / (Z_FAR + Z_NEAR - z_n * (Z_FAR - Z_NEAR));
27+ }
28+
1629// ACES Filmic Tone Mapping Curve
1730vec3 tonemapACES(vec3 x) {
1831 const float a = 2.51 ;
@@ -24,6 +37,21 @@ vec3 tonemapACES(vec3 x) {
2437}
2538
2639void main() {
40+
41+ ivec2 texelCoord = ivec2 (gl_FragCoord .xy);
42+
43+ float depth = texelFetch(uDepthImage, texelCoord, 0 ).r;
44+
45+ // --- Fog Calculation ---
46+ vec3 fogColor = vec3 (0.5 , 0.6 , 0.7 );// A cool, hazy blue
47+ float fogStart = 0.5 ;
48+ float fogEnd = 120.0 ;
49+ float viewDistance = linearizeDepth(depth);
50+ // Calculate fog amount (0.0 = no fog, 1.0 = full fog)
51+ float fogFactor = 1.0 - exp (- viewDistance * uFogDensity);
52+ // float fogFactor = uFogDensity;
53+ // ----------------------
54+
2755 // Sample the HDR input image
2856 vec3 hdrColor = texture(uInputImage, fragUV).rgb;
2957
@@ -43,5 +71,8 @@ void main() {
4371 vec3 grayscale = vec3 (dot (finalColor, vec3 (0.299 , 0.587 , 0.114 )));
4472 finalColor = mix (grayscale, finalColor, uSaturation);
4573
74+ // Mix the final scene color with the fog color
75+ finalColor = mix (finalColor, fogColor, fogFactor);
76+
4677 outColor = vec4 (finalColor, 1.0 );
4778}
0 commit comments