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;
11 varying float area_enable_parallax;
14 varying vec3 tsEyeVec;
15 varying vec3 lightVec;
16 varying vec3 tsLightVec;
18 bool normalTexturePresent = false;
20 const float e = 2.718281828459;
21 const float BS = 10.0;
23 #ifdef ENABLE_TONE_MAPPING
25 /* Hable's UC2 Tone mapping parameters
33 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
36 vec3 uncharted2Tonemap(vec3 x)
38 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03334;
41 vec4 applyToneMapping(vec4 color)
43 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
44 const float gamma = 1.6;
45 const float exposureBias = 5.5;
46 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
47 // Precalculated white_scale from
48 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
49 vec3 whiteScale = vec3(1.036015346);
50 color.rgb *= whiteScale;
51 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
55 void get_texture_flags()
57 vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
59 normalTexturePresent = true;
63 float intensity(vec3 color)
65 return (color.r + color.g + color.b) / 3.0;
68 float get_rgb_height(vec2 uv)
70 return intensity(texture2D(baseTexture, uv).rgb);
73 vec4 get_normal_map(vec2 uv)
75 vec4 bump = texture2D(normalTexture, uv).rgba;
76 bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
80 float find_intersection(vec2 dp, vec2 ds)
83 float best_depth = 0.0;
85 for (int i = 0; i < 15; i++) {
87 float h = texture2D(normalTexture, dp + ds * depth).a;
94 for (int i = 0; i < 4; i++) {
96 float h = texture2D(normalTexture,dp + ds * depth).a;
107 float find_intersectionRGB(vec2 dp, vec2 ds)
109 const float depth_step = 1.0 / 24.0;
111 for (int i = 0 ; i < 24 ; i++) {
112 float h = get_rgb_height(dp + ds * depth);
124 vec2 uv = gl_TexCoord[0].st;
125 bool use_normalmap = false;
128 #ifdef ENABLE_PARALLAX_OCCLUSION
129 vec2 eyeRay = vec2 (tsEyeVec.x, -tsEyeVec.y);
130 const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
131 const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
133 #if PARALLAX_OCCLUSION_MODE == 0
134 // Parallax occlusion with slope information
135 if (normalTexturePresent && area_enable_parallax > 0.0) {
136 for (int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
137 vec4 normal = texture2D(normalTexture, uv.xy);
138 float h = normal.a * scale - bias;
139 uv += h * normal.z * eyeRay;
143 #if PARALLAX_OCCLUSION_MODE == 1
145 if (normalTexturePresent && area_enable_parallax > 0.0) {
146 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
147 float dist = find_intersection(uv, ds);
150 } else if (GENERATE_NORMALMAPS == 1 && area_enable_parallax > 0.0) {
151 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
152 float dist = find_intersectionRGB(uv, ds);
157 #if USE_NORMALMAPS == 1
158 if (normalTexturePresent) {
159 bump = get_normal_map(uv);
160 use_normalmap = true;
164 if (GENERATE_NORMALMAPS == 1 && normalTexturePresent == false) {
165 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
166 float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
167 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
168 float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
169 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
170 float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
171 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
172 float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
173 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
174 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
175 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
176 use_normalmap = true;
179 vec4 base = texture2D(baseTexture, uv).rgba;
181 #ifdef ENABLE_BUMPMAPPING
183 vec3 L = normalize(lightVec);
184 vec3 E = normalize(eyeVec);
185 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
186 float diffuse = dot(-E,bump.xyz);
187 color = (diffuse + 0.1 * specular) * base.rgb;
195 vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0);
197 #if MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE
198 float alpha = gl_Color.a;
199 if (fogDistance != 0.0) {
200 float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
201 alpha = mix(alpha, 0.0, d);
203 col = vec4(col.rgb, alpha);
205 if (fogDistance != 0.0) {
206 float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
207 col = mix(col, skyBgColor, d);
209 col = vec4(col.rgb, base.a);
212 #ifdef ENABLE_TONE_MAPPING
213 gl_FragColor = applyToneMapping(col);