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 varying vec3 vPosition;
10 varying vec3 worldPosition;
13 varying vec3 lightVec;
15 bool normalTexturePresent = false;
16 bool texTileableHorizontal = false;
17 bool texTileableVertical = false;
18 bool texSeamless = false;
20 const float e = 2.718281828459;
21 const float BS = 10.0;
22 const float fogStart = FOG_START;
23 const float fogShadingParameter = 1 / ( 1 - fogStart);
25 void get_texture_flags()
27 vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
29 normalTexturePresent = true;
32 texTileableHorizontal = true;
35 texTileableVertical = true;
37 if (texTileableHorizontal && texTileableVertical) {
42 float intensity(vec3 color)
44 return (color.r + color.g + color.b) / 3.0;
47 float get_rgb_height(vec2 uv)
50 return intensity(texture2D(baseTexture, uv).rgb);
52 return intensity(texture2D(baseTexture, clamp(uv, 0.0, 0.999)).rgb);
56 vec4 get_normal_map(vec2 uv)
58 vec4 bump = texture2D(normalTexture, uv).rgba;
59 bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
67 vec2 uv = gl_TexCoord[0].st;
68 bool use_normalmap = false;
71 #if USE_NORMALMAPS == 1
72 if (normalTexturePresent) {
73 bump = get_normal_map(uv);
78 #if GENERATE_NORMALMAPS == 1
79 if (normalTexturePresent == false) {
80 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
81 float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
82 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
83 float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
84 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
85 float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
86 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
87 float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
88 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
89 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
90 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
95 vec4 base = texture2D(baseTexture, uv).rgba;
97 #ifdef ENABLE_BUMPMAPPING
99 vec3 L = normalize(lightVec);
100 vec3 E = normalize(eyeVec);
101 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
102 float diffuse = dot(-E,bump.xyz);
103 color = (diffuse + 0.1 * specular) * base.rgb;
111 vec4 col = vec4(color.rgb, base.a);
113 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
114 // the fog will only be rendered correctly if the last operation before the
115 // clamp() is an addition. Else, the clamp() seems to be ignored.
116 // E.g. the following won't work:
117 // float clarity = clamp(fogShadingParameter
118 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
119 // As additions usually come for free following a multiplication, the new formula
120 // should be more efficient as well.
121 // Note: clarity = (1 - fogginess)
122 float clarity = clamp(fogShadingParameter
123 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
124 col = mix(skyBgColor, col, clarity);
126 gl_FragColor = vec4(col.rgb, base.a);