1uniform sampler2D baseTexture;
2uniform sampler2D normalTexture;
3uniform sampler2D useNormalmap;
4
5uniform vec4 skyBgColor;
6uniform float fogDistance;
7uniform vec3 eyePosition;
8
9varying vec3 vPosition;
10varying vec3 worldPosition;
11
12varying vec3 eyeVec;
13varying vec3 tsEyeVec;
14varying vec3 lightVec;
15varying vec3 tsLightVec;
16
17uniform float wieldLight;
18
19bool normalTexturePresent = false;
20
21const float e = 2.718281828459;
22const float BS = 10.0;
23
24float intensity (vec3 color){
25	return (color.r + color.g + color.b) / 3.0;
26}
27
28float get_rgb_height (vec2 uv){
29	return intensity(texture2D(baseTexture,uv).rgb);
30}
31
32vec4 get_normal_map(vec2 uv){
33	vec4 bump = texture2D(normalTexture, uv).rgba;
34	bump.xyz = normalize(bump.xyz * 2.0 -1.0);
35	bump.y = -bump.y;
36	return bump;
37}
38
39void main (void)
40{
41	vec3 color;
42	vec4 bump;
43	vec2 uv = gl_TexCoord[0].st;
44	bool use_normalmap = false;
45
46#ifdef USE_NORMALMAPS
47	if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0){
48		normalTexturePresent = true;
49	}
50#endif
51
52#ifdef ENABLE_PARALLAX_OCCLUSION
53	if (normalTexturePresent){
54		vec3 tsEye = normalize(tsEyeVec);
55		float height = PARALLAX_OCCLUSION_SCALE * texture2D(normalTexture, uv).a - PARALLAX_OCCLUSION_BIAS;
56		uv = uv + texture2D(normalTexture, uv).z * height * vec2(tsEye.x,-tsEye.y);
57	}
58#endif
59
60#ifdef USE_NORMALMAPS
61	if (normalTexturePresent){
62		bump = get_normal_map(uv);
63		use_normalmap = true;
64	}
65#endif
66
67#ifdef GENERATE_NORMALMAPS
68	if (use_normalmap == false){
69		float tl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y+SAMPLE_STEP));
70		float t  = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
71		float tr = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y+SAMPLE_STEP));
72		float r  = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y));
73		float br = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y-SAMPLE_STEP));
74		float b  = get_rgb_height (vec2(uv.x,uv.y-SAMPLE_STEP));
75		float bl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
76		float l  = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y));
77		float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
78		float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
79		bump = vec4 (normalize(vec3 (dX, -dY, NORMALMAPS_STRENGTH)),1.0);
80		use_normalmap = true;
81	}
82#endif
83
84vec4 base = texture2D(baseTexture, uv).rgba;
85
86#ifdef ENABLE_BUMPMAPPING
87	if (use_normalmap){
88		vec3 L = normalize(lightVec);
89		vec3 E = normalize(eyeVec);
90		float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5);
91		float diffuse = dot(E,bump.xyz);
92		color = 0.05*base.rgb + diffuse*base.rgb + 0.2*specular*base.rgb;
93	} else {
94		color = base.rgb;
95	}
96#else
97	color = base.rgb;
98#endif
99
100	float light = max((wieldLight/2.0)/vPosition.z, 0.0);
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);
104	col *= min(gl_Color+vec4(light), 1.0);
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);
108	}
109	gl_FragColor = vec4(col.rgb, alpha);
110#else
111	vec4 col = vec4(color.rgb, base.a);
112	col *= min(gl_Color+vec4(light), 1.0);
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);
116	}
117	gl_FragColor = vec4(col.rgb, base.a);
118#endif
119}
120