1 uniform sampler2D baseTexture;
2 uniform sampler2D normalTexture;
3 uniform sampler2D useNormalmap;
5 uniform vec4 skyBgColor;
6 uniform float fogDistance;
7 uniform vec3 eyePosition;
9 varying vec3 vPosition;
10 varying vec3 worldPosition;
13 varying vec3 tsEyeVec;
14 varying vec3 lightVec;
15 varying vec3 tsLightVec;
17 bool normalTexturePresent = false;
19 const float e = 2.718281828459;
20 const float BS = 10.0;
22 float intensity (vec3 color){
23 return (color.r + color.g + color.b) / 3.0;
26 float get_rgb_height (vec2 uv){
27 return intensity(texture2D(baseTexture,uv).rgb);
30 vec4 get_normal_map(vec2 uv){
31 vec4 bump = texture2D(normalTexture, uv).rgba;
32 bump.xyz = normalize(bump.xyz * 2.0 -1.0);
41 vec2 uv = gl_TexCoord[0].st;
42 bool use_normalmap = false;
45 if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0) {
46 normalTexturePresent = true;
50 #ifdef ENABLE_PARALLAX_OCCLUSION
51 if (normalTexturePresent) {
52 vec3 tsEye = normalize(tsEyeVec);
53 float height = PARALLAX_OCCLUSION_SCALE * texture2D(normalTexture, uv).a - PARALLAX_OCCLUSION_BIAS;
54 uv = uv + texture2D(normalTexture, uv).z * height * vec2(tsEye.x,-tsEye.y);
59 if (normalTexturePresent) {
60 bump = get_normal_map(uv);
65 if (GENERATE_NORMALMAPS == 1 && use_normalmap == false) {
66 float tl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y+SAMPLE_STEP));
67 float t = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
68 float tr = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y+SAMPLE_STEP));
69 float r = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y));
70 float br = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y-SAMPLE_STEP));
71 float b = get_rgb_height (vec2(uv.x,uv.y-SAMPLE_STEP));
72 float bl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
73 float l = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y));
74 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
75 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
76 bump = vec4 (normalize(vec3 (dX, -dY, NORMALMAPS_STRENGTH)),1.0);
80 vec4 base = texture2D(baseTexture, uv).rgba;
82 #ifdef ENABLE_BUMPMAPPING
84 vec3 L = normalize(lightVec);
85 vec3 E = normalize(eyeVec);
86 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5);
87 float diffuse = dot(E,bump.xyz);
88 /* Mathematic optimization
89 * Original: color = 0.05*base.rgb + diffuse*base.rgb + 0.2*specular*base.rgb;
90 * This optimization save 2 multiplications (orig: 4 multiplications + 3 additions
91 * end: 2 multiplications + 3 additions)
93 color = (0.05 + diffuse + 0.2 * specular) * base.rgb;
101 #if MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE
102 float alpha = gl_Color.a;
103 vec4 col = vec4(color.rgb, alpha);
105 if(fogDistance != 0.0){
106 float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
107 alpha = mix(alpha, 0.0, d);
109 gl_FragColor = vec4(col.rgb, alpha);
111 vec4 col = vec4(color.rgb, base.a);
113 if(fogDistance != 0.0){
114 float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
115 col = mix(col, skyBgColor, d);
117 gl_FragColor = vec4(col.rgb, base.a);