1 uniform sampler2D baseTexture;
3 uniform vec4 emissiveColor;
4 uniform vec4 skyBgColor;
5 uniform float fogDistance;
6 uniform vec3 eyePosition;
9 varying vec3 vPosition;
10 varying vec3 worldPosition;
11 varying lowp vec4 varColor;
12 centroid varying mediump vec2 varTexCoord;
17 const float e = 2.718281828459;
18 const float BS = 10.0;
19 const float fogStart = FOG_START;
20 const float fogShadingParameter = 1.0 / (1.0 - fogStart);
22 #if ENABLE_TONE_MAPPING
24 /* Hable's UC2 Tone mapping parameters
32 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
35 vec3 uncharted2Tonemap(vec3 x)
37 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
40 vec4 applyToneMapping(vec4 color)
42 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
43 const float gamma = 1.6;
44 const float exposureBias = 5.5;
45 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
46 // Precalculated white_scale from
47 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
48 vec3 whiteScale = vec3(1.036015346);
49 color.rgb *= whiteScale;
50 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
57 vec2 uv = varTexCoord.st;
59 vec4 base = texture2D(baseTexture, uv).rgba;
62 // If alpha is zero, we can just discard the pixel. This fixes transparency
63 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
64 // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
72 vec4 col = vec4(color.rgb, base.a);
74 col.rgb *= varColor.rgb;
76 col.rgb *= emissiveColor.rgb * vIDiff;
78 #if ENABLE_TONE_MAPPING
79 col = applyToneMapping(col);
82 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
83 // the fog will only be rendered correctly if the last operation before the
84 // clamp() is an addition. Else, the clamp() seems to be ignored.
85 // E.g. the following won't work:
86 // float clarity = clamp(fogShadingParameter
87 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
88 // As additions usually come for free following a multiplication, the new formula
89 // should be more efficient as well.
90 // Note: clarity = (1 - fogginess)
91 float clarity = clamp(fogShadingParameter
92 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
93 col = mix(skyBgColor, col, clarity);
95 gl_FragColor = vec4(col.rgb, base.a);