1 uniform sampler2D baseTexture;
2 uniform sampler2D normalTexture;
3 uniform sampler2D textureFlags;
5 uniform vec4 skyBgColor;
6 uniform float fogDistance;
7 uniform vec3 eyePosition;
9 // The cameraOffset is the current center of the visible world.
10 uniform vec3 cameraOffset;
11 uniform float animationTimer;
13 varying vec3 vPosition;
14 // World position in the visible world (i.e. relative to the cameraOffset.)
15 // This can be used for many shader effects without loss of precision.
16 // If the absolute position is required it can be calculated with
17 // cameraOffset + worldPosition (for large coordinates the limits of float
18 // precision must be considered).
19 varying vec3 worldPosition;
20 varying float area_enable_parallax;
23 varying vec3 tsEyeVec;
24 varying vec3 lightVec;
25 varying vec3 tsLightVec;
27 bool normalTexturePresent = false;
29 const float e = 2.718281828459;
30 const float BS = 10.0;
31 const float fogStart = FOG_START;
32 const float fogShadingParameter = 1 / ( 1 - fogStart);
34 #ifdef ENABLE_TONE_MAPPING
36 /* Hable's UC2 Tone mapping parameters
44 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
47 vec3 uncharted2Tonemap(vec3 x)
49 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
52 vec4 applyToneMapping(vec4 color)
54 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
55 const float gamma = 1.6;
56 const float exposureBias = 5.5;
57 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
58 // Precalculated white_scale from
59 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
60 vec3 whiteScale = vec3(1.036015346);
61 color.rgb *= whiteScale;
62 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
66 void get_texture_flags()
68 vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
70 normalTexturePresent = true;
74 vec4 get_normal_map(vec2 uv)
76 vec4 bump = texture2D(normalTexture, uv).rgba;
77 bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
81 float find_intersection(vec2 dp, vec2 ds)
84 float best_depth = 0.0;
86 for (int i = 0; i < 15; i++) {
88 float h = texture2D(normalTexture, dp + ds * depth).a;
95 for (int i = 0; i < 4; i++) {
97 float h = texture2D(normalTexture,dp + ds * depth).a;
112 vec2 uv = gl_TexCoord[0].st;
113 bool use_normalmap = false;
116 #ifdef ENABLE_PARALLAX_OCCLUSION
117 vec2 eyeRay = vec2 (tsEyeVec.x, -tsEyeVec.y);
118 const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
119 const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
121 #if PARALLAX_OCCLUSION_MODE == 0
122 // Parallax occlusion with slope information
123 if (normalTexturePresent && area_enable_parallax > 0.0) {
124 for (int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
125 vec4 normal = texture2D(normalTexture, uv.xy);
126 float h = normal.a * scale - bias;
127 uv += h * normal.z * eyeRay;
131 #if PARALLAX_OCCLUSION_MODE == 1
133 if (normalTexturePresent && area_enable_parallax > 0.0) {
134 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
135 float dist = find_intersection(uv, ds);
141 #if USE_NORMALMAPS == 1
142 if (normalTexturePresent) {
143 bump = get_normal_map(uv);
144 use_normalmap = true;
148 vec4 base = texture2D(baseTexture, uv).rgba;
151 // If alpha is zero, we can just discard the pixel. This fixes transparency
152 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa.
158 #ifdef ENABLE_BUMPMAPPING
160 vec3 L = normalize(lightVec);
161 vec3 E = normalize(eyeVec);
162 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
163 float diffuse = dot(-E,bump.xyz);
164 color = (diffuse + 0.1 * specular) * base.rgb;
172 vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0);
174 #ifdef ENABLE_TONE_MAPPING
175 col = applyToneMapping(col);
178 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
179 // the fog will only be rendered correctly if the last operation before the
180 // clamp() is an addition. Else, the clamp() seems to be ignored.
181 // E.g. the following won't work:
182 // float clarity = clamp(fogShadingParameter
183 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
184 // As additions usually come for free following a multiplication, the new formula
185 // should be more efficient as well.
186 // Note: clarity = (1 - fogginess)
187 float clarity = clamp(fogShadingParameter
188 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
189 col = mix(skyBgColor, col, clarity);
190 col = vec4(col.rgb, base.a);