| 1 |
uniform sampler2D u_TextureMap; |
| 2 |
uniform sampler2D u_LevelsMap; |
| 3 |
|
| 4 |
uniform vec4 u_Color; |
| 5 |
|
| 6 |
|
| 7 |
uniform vec2 u_AutoExposureMinMax; |
| 8 |
uniform vec3 u_ToneMinAvgMaxLinear; |
| 9 |
|
| 10 |
varying vec2 var_TexCoords; |
| 11 |
varying float var_InvWhite; |
| 12 |
|
| 13 |
const vec3 LUMINANCE_VECTOR = vec3(0.2125, 0.7154, 0.0721); //vec3(0.299, 0.587, 0.114); |
| 14 |
|
| 15 |
float FilmicTonemap(float x) |
| 16 |
{ |
| 17 |
const float SS = 0.22; // Shoulder Strength |
| 18 |
const float LS = 0.30; // Linear Strength |
| 19 |
const float LA = 0.10; // Linear Angle |
| 20 |
const float TS = 0.20; // Toe Strength |
| 21 |
const float TAN = 0.01; // Toe Angle Numerator |
| 22 |
const float TAD = 0.30; // Toe Angle Denominator |
| 23 |
|
| 24 |
return ((x*(SS*x+LA*LS)+TS*TAN)/(x*(SS*x+LS)+TS*TAD)) - TAN/TAD; |
| 25 |
} |
| 26 |
|
| 27 |
void main() |
| 28 |
{ |
| 29 |
vec4 color = texture2D(u_TextureMap, var_TexCoords) * u_Color; |
| 30 |
|
| 31 |
#if defined(USE_PBR) |
| 32 |
color.rgb *= color.rgb; |
| 33 |
#endif |
| 34 |
|
| 35 |
vec3 minAvgMax = texture2D(u_LevelsMap, var_TexCoords).rgb; |
| 36 |
vec3 logMinAvgMaxLum = clamp(minAvgMax * 20.0 - 10.0, -u_AutoExposureMinMax.y, -u_AutoExposureMinMax.x); |
| 37 |
|
| 38 |
float invAvgLum = u_ToneMinAvgMaxLinear.y * exp2(-logMinAvgMaxLum.y); |
| 39 |
|
| 40 |
color.rgb = color.rgb * invAvgLum - u_ToneMinAvgMaxLinear.xxx; |
| 41 |
color.rgb = max(vec3(0.0), color.rgb); |
| 42 |
|
| 43 |
color.r = FilmicTonemap(color.r); |
| 44 |
color.g = FilmicTonemap(color.g); |
| 45 |
color.b = FilmicTonemap(color.b); |
| 46 |
|
| 47 |
color.rgb = clamp(color.rgb * var_InvWhite, 0.0, 1.0); |
| 48 |
|
| 49 |
#if defined(USE_PBR) |
| 50 |
color.rgb = sqrt(color.rgb); |
| 51 |
#endif |
| 52 |
|
| 53 |
// add a bit of dither to reduce banding |
| 54 |
color.rgb += vec3(1.0/510.0 * mod(gl_FragCoord.x + gl_FragCoord.y, 2.0) - 1.0/1020.0); |
| 55 |
|
| 56 |
gl_FragColor = color; |
| 57 |
} |