1 #define rendered texture0
4 struct ExposureParams {
5 float compensationFactor;
8 uniform sampler2D rendered;
9 uniform sampler2D bloom;
11 uniform ExposureParams exposureParams;
12 uniform lowp float bloomIntensity;
13 uniform lowp float saturation;
16 varying mediump vec2 varTexCoord;
18 centroid varying vec2 varTexCoord;
21 #ifdef ENABLE_AUTO_EXPOSURE
22 varying float exposure; // linear exposure factor, see vertex shader
27 vec4 applyBloom(vec4 color, vec2 uv)
29 vec3 light = texture2D(bloom, uv).rgb;
30 #ifdef ENABLE_BLOOM_DEBUG
31 if (uv.x > 0.5 && uv.y < 0.5)
32 return vec4(light, color.a);
36 color.rgb = mix(color.rgb, light, bloomIntensity);
42 #if ENABLE_TONE_MAPPING
44 /* Hable's UC2 Tone mapping parameters
52 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
55 vec3 uncharted2Tonemap(vec3 x)
57 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
60 vec4 applyToneMapping(vec4 color)
62 const float exposureBias = 2.0;
63 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
64 // Precalculated white_scale from
65 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
66 vec3 whiteScale = vec3(1.036015346);
67 color.rgb *= whiteScale;
71 vec3 applySaturation(vec3 color, float factor)
73 // Calculate the perceived luminosity from the RGB color.
74 // See also: https://www.w3.org/WAI/GL/wiki/Relative_luminance
75 float brightness = dot(color, vec3(0.2125, 0.7154, 0.0721));
76 return mix(vec3(brightness), color, factor);
82 vec2 uv = varTexCoord.st;
83 vec4 color = texture2D(rendered, uv).rgba;
85 // translate to linear colorspace (approximate)
86 color.rgb = pow(color.rgb, vec3(2.2));
88 #ifdef ENABLE_BLOOM_DEBUG
89 if (uv.x > 0.5 || uv.y > 0.5)
92 color.rgb *= exposureParams.compensationFactor;
93 #ifdef ENABLE_AUTO_EXPOSURE
94 color.rgb *= exposure;
100 color = applyBloom(color, uv);
103 #ifdef ENABLE_BLOOM_DEBUG
104 if (uv.x > 0.5 || uv.y > 0.5)
107 #if ENABLE_TONE_MAPPING
108 color = applyToneMapping(color);
109 color.rgb = applySaturation(color.rgb, saturation);
113 color.rgb = clamp(color.rgb, vec3(0.), vec3(1.));
115 // return to sRGB colorspace (approximate)
116 color.rgb = pow(color.rgb, vec3(1.0 / 2.2));
118 gl_FragColor = vec4(color.rgb, 1.0); // force full alpha to avoid holes in the image.