1 uniform sampler2D baseTexture;
3 uniform vec4 skyBgColor;
4 uniform float fogDistance;
5 uniform vec3 eyePosition;
7 // The cameraOffset is the current center of the visible world.
8 uniform vec3 cameraOffset;
9 uniform float animationTimer;
11 varying vec3 vPosition;
12 // World position in the visible world (i.e. relative to the cameraOffset.)
13 // This can be used for many shader effects without loss of precision.
14 // If the absolute position is required it can be calculated with
15 // cameraOffset + worldPosition (for large coordinates the limits of float
16 // precision must be considered).
17 varying vec3 worldPosition;
18 varying lowp vec4 varColor;
19 centroid varying mediump vec2 varTexCoord;
22 const float fogStart = FOG_START;
23 const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
25 #if ENABLE_TONE_MAPPING
27 /* Hable's UC2 Tone mapping parameters
35 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
38 vec3 uncharted2Tonemap(vec3 x)
40 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
43 vec4 applyToneMapping(vec4 color)
45 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
46 const float gamma = 1.6;
47 const float exposureBias = 5.5;
48 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
49 // Precalculated white_scale from
50 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
51 vec3 whiteScale = vec3(1.036015346);
52 color.rgb *= whiteScale;
53 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
60 vec2 uv = varTexCoord.st;
62 vec4 base = texture2D(baseTexture, uv).rgba;
64 // If alpha is zero, we can just discard the pixel. This fixes transparency
65 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
66 // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
74 vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
76 #if ENABLE_TONE_MAPPING
77 col = applyToneMapping(col);
80 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
81 // the fog will only be rendered correctly if the last operation before the
82 // clamp() is an addition. Else, the clamp() seems to be ignored.
83 // E.g. the following won't work:
84 // float clarity = clamp(fogShadingParameter
85 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
86 // As additions usually come for free following a multiplication, the new formula
87 // should be more efficient as well.
88 // Note: clarity = (1 - fogginess)
89 float clarity = clamp(fogShadingParameter
90 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
91 col = mix(skyBgColor, col, clarity);
92 col = vec4(col.rgb, base.a);