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 float intensity(vec3 color)
76 return (color.r + color.g + color.b) / 3.0;
79 float get_rgb_height(vec2 uv)
81 return intensity(texture2D(baseTexture, uv).rgb);
84 vec4 get_normal_map(vec2 uv)
86 vec4 bump = texture2D(normalTexture, uv).rgba;
87 bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
91 float find_intersection(vec2 dp, vec2 ds)
94 float best_depth = 0.0;
96 for (int i = 0; i < 15; i++) {
98 float h = texture2D(normalTexture, dp + ds * depth).a;
105 for (int i = 0; i < 4; i++) {
107 float h = texture2D(normalTexture,dp + ds * depth).a;
118 float find_intersectionRGB(vec2 dp, vec2 ds)
120 const float depth_step = 1.0 / 24.0;
122 for (int i = 0 ; i < 24 ; i++) {
123 float h = get_rgb_height(dp + ds * depth);
135 vec2 uv = gl_TexCoord[0].st;
136 bool use_normalmap = false;
139 #ifdef ENABLE_PARALLAX_OCCLUSION
140 vec2 eyeRay = vec2 (tsEyeVec.x, -tsEyeVec.y);
141 const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
142 const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
144 #if PARALLAX_OCCLUSION_MODE == 0
145 // Parallax occlusion with slope information
146 if (normalTexturePresent && area_enable_parallax > 0.0) {
147 for (int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
148 vec4 normal = texture2D(normalTexture, uv.xy);
149 float h = normal.a * scale - bias;
150 uv += h * normal.z * eyeRay;
154 #if PARALLAX_OCCLUSION_MODE == 1
156 if (normalTexturePresent && area_enable_parallax > 0.0) {
157 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
158 float dist = find_intersection(uv, ds);
161 } else if (GENERATE_NORMALMAPS == 1 && area_enable_parallax > 0.0) {
162 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
163 float dist = find_intersectionRGB(uv, ds);
168 #if USE_NORMALMAPS == 1
169 if (normalTexturePresent) {
170 bump = get_normal_map(uv);
171 use_normalmap = true;
175 #if GENERATE_NORMALMAPS == 1
176 if (normalTexturePresent == false) {
177 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
178 float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
179 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
180 float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
181 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
182 float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
183 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
184 float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
185 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
186 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
187 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
188 use_normalmap = true;
191 vec4 base = texture2D(baseTexture, uv).rgba;
194 // If alpha is zero, we can just discard the pixel. This fixes transparency
195 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa.
201 #ifdef ENABLE_BUMPMAPPING
203 vec3 L = normalize(lightVec);
204 vec3 E = normalize(eyeVec);
205 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
206 float diffuse = dot(-E,bump.xyz);
207 color = (diffuse + 0.1 * specular) * base.rgb;
215 vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0);
217 #ifdef ENABLE_TONE_MAPPING
218 col = applyToneMapping(col);
221 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
222 // the fog will only be rendered correctly if the last operation before the
223 // clamp() is an addition. Else, the clamp() seems to be ignored.
224 // E.g. the following won't work:
225 // float clarity = clamp(fogShadingParameter
226 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
227 // As additions usually come for free following a multiplication, the new formula
228 // should be more efficient as well.
229 // Note: clarity = (1 - fogginess)
230 float clarity = clamp(fogShadingParameter
231 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
232 col = mix(skyBgColor, col, clarity);
233 col = vec4(col.rgb, base.a);