2012-12-04 03:05:34 +00:00
|
|
|
uniform sampler2D u_TextureMap;
|
|
|
|
uniform sampler2D u_LevelsMap;
|
|
|
|
|
|
|
|
uniform vec4 u_Color;
|
|
|
|
|
2014-10-27 13:37:57 +00:00
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
uniform vec2 u_AutoExposureMinMax;
|
|
|
|
uniform vec3 u_ToneMinAvgMaxLinear;
|
|
|
|
|
|
|
|
varying vec2 var_TexCoords;
|
2014-10-27 13:37:57 +00:00
|
|
|
varying float var_InvWhite;
|
2012-12-04 03:05:34 +00:00
|
|
|
|
|
|
|
const vec3 LUMINANCE_VECTOR = vec3(0.2125, 0.7154, 0.0721); //vec3(0.299, 0.587, 0.114);
|
|
|
|
|
2014-10-27 13:37:57 +00:00
|
|
|
float FilmicTonemap(float x)
|
2012-12-04 03:05:34 +00:00
|
|
|
{
|
|
|
|
const float SS = 0.22; // Shoulder Strength
|
|
|
|
const float LS = 0.30; // Linear Strength
|
|
|
|
const float LA = 0.10; // Linear Angle
|
|
|
|
const float TS = 0.20; // Toe Strength
|
|
|
|
const float TAN = 0.01; // Toe Angle Numerator
|
|
|
|
const float TAD = 0.30; // Toe Angle Denominator
|
|
|
|
|
2014-10-27 13:37:57 +00:00
|
|
|
return ((x*(SS*x+LA*LS)+TS*TAN)/(x*(SS*x+LS)+TS*TAD)) - TAN/TAD;
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec4 color = texture2D(u_TextureMap, var_TexCoords) * u_Color;
|
2014-04-16 12:26:03 +00:00
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
2016-02-18 04:06:18 +00:00
|
|
|
color.rgb *= color.rgb;
|
2014-10-27 13:37:57 +00:00
|
|
|
#endif
|
2014-04-16 12:26:03 +00:00
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
vec3 minAvgMax = texture2D(u_LevelsMap, var_TexCoords).rgb;
|
|
|
|
vec3 logMinAvgMaxLum = clamp(minAvgMax * 20.0 - 10.0, -u_AutoExposureMinMax.y, -u_AutoExposureMinMax.x);
|
|
|
|
|
2014-10-27 13:37:57 +00:00
|
|
|
float invAvgLum = u_ToneMinAvgMaxLinear.y * exp2(-logMinAvgMaxLum.y);
|
|
|
|
|
|
|
|
color.rgb = color.rgb * invAvgLum - u_ToneMinAvgMaxLinear.xxx;
|
|
|
|
color.rgb = max(vec3(0.0), color.rgb);
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2014-10-27 13:37:57 +00:00
|
|
|
color.r = FilmicTonemap(color.r);
|
|
|
|
color.g = FilmicTonemap(color.g);
|
|
|
|
color.b = FilmicTonemap(color.b);
|
|
|
|
|
|
|
|
color.rgb = clamp(color.rgb * var_InvWhite, 0.0, 1.0);
|
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
2016-02-18 04:06:18 +00:00
|
|
|
color.rgb = sqrt(color.rgb);
|
2014-10-27 13:37:57 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-18 04:06:18 +00:00
|
|
|
// add a bit of dither to reduce banding
|
|
|
|
color.rgb += vec3(1.0/510.0 * mod(gl_FragCoord.x + gl_FragCoord.y, 2.0) - 1.0/1020.0);
|
|
|
|
|
2014-10-27 13:37:57 +00:00
|
|
|
gl_FragColor = color;
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|