1 uniform sampler2D baseTexture;
4 uniform vec4 skyBgColor;
5 uniform float fogDistance;
6 uniform vec3 eyePosition;
8 // The cameraOffset is the current center of the visible world.
9 uniform vec3 cameraOffset;
10 uniform float animationTimer;
11 #ifdef ENABLE_DYNAMIC_SHADOWS
13 uniform sampler2D ShadowMapSampler;
15 uniform vec3 v_LightDirection;
16 uniform float f_textureresolution;
17 uniform mat4 m_ShadowViewProj;
18 uniform float f_shadowfar;
19 uniform float f_shadow_strength;
20 varying float normalOffsetScale;
21 varying float adj_shadow_strength;
22 varying float cosLight;
23 varying float f_normal_length;
28 varying vec3 vPosition;
29 // World position in the visible world (i.e. relative to the cameraOffset.)
30 // This can be used for many shader effects without loss of precision.
31 // If the absolute position is required it can be calculated with
32 // cameraOffset + worldPosition (for large coordinates the limits of float
33 // precision must be considered).
34 varying vec3 worldPosition;
35 varying lowp vec4 varColor;
37 varying mediump vec2 varTexCoord;
39 centroid varying vec2 varTexCoord;
42 varying float nightRatio;
44 const float fogStart = FOG_START;
45 const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
49 #ifdef ENABLE_DYNAMIC_SHADOWS
50 uniform float xyPerspectiveBias0;
51 uniform float xyPerspectiveBias1;
52 uniform float zPerspectiveBias;
54 vec4 getPerspectiveFactor(in vec4 shadowPosition)
57 float pDistance = length(shadowPosition.xy);
58 float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
60 shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias);
62 return shadowPosition;
65 // assuming near is always 1.0
66 float getLinearDepth()
68 return 2.0 * f_shadowfar / (f_shadowfar + 1.0 - (2.0 * gl_FragCoord.z - 1.0) * (f_shadowfar - 1.0));
71 vec3 getLightSpacePosition()
74 // some drawtypes have zero normals, so we need to handle it :(
75 #if DRAW_TYPE == NDT_PLANTLIKE
76 pLightSpace = m_ShadowViewProj * vec4(worldPosition, 1.0);
78 pLightSpace = m_ShadowViewProj * vec4(worldPosition + normalOffsetScale * normalize(vNormal), 1.0);
80 pLightSpace = getPerspectiveFactor(pLightSpace);
81 return pLightSpace.xyz * 0.5 + 0.5;
83 // custom smoothstep implementation because it's not defined in glsl1.2
84 // https://docs.gl/sl4/smoothstep
85 float mtsmoothstep(in float edge0, in float edge1, in float x)
87 float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
88 return t * t * (3.0 - 2.0 * t);
91 #ifdef COLORED_SHADOWS
93 // c_precision of 128 fits within 7 base-10 digits
94 const float c_precision = 128.0;
95 const float c_precisionp1 = c_precision + 1.0;
97 float packColor(vec3 color)
99 return floor(color.b * c_precision + 0.5)
100 + floor(color.g * c_precision + 0.5) * c_precisionp1
101 + floor(color.r * c_precision + 0.5) * c_precisionp1 * c_precisionp1;
104 vec3 unpackColor(float value)
107 color.b = mod(value, c_precisionp1) / c_precision;
108 color.g = mod(floor(value / c_precisionp1), c_precisionp1) / c_precision;
109 color.r = floor(value / (c_precisionp1 * c_precisionp1)) / c_precision;
113 vec4 getHardShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
115 vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy).rgba;
117 float visibility = step(0.0, realDistance - texDepth.r);
118 vec4 result = vec4(visibility, vec3(0.0,0.0,0.0));//unpackColor(texDepth.g));
119 if (visibility < 0.1) {
120 visibility = step(0.0, realDistance - texDepth.b);
121 result = vec4(visibility, unpackColor(texDepth.a));
128 float getHardShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
130 float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
131 float visibility = step(0.0, realDistance - texDepth);
138 #if SHADOW_FILTER == 2
140 #define PCFSAMPLES 64.0
141 #elif SHADOW_FILTER == 1
143 #if defined(POISSON_FILTER)
144 #define PCFSAMPLES 32.0
146 #define PCFSAMPLES 16.0
150 #if defined(POISSON_FILTER)
151 #define PCFSAMPLES 4.0
153 #define PCFSAMPLES 1.0
156 #ifdef COLORED_SHADOWS
157 float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
159 vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy);
160 float depth = max(realDistance - texDepth.r, realDistance - texDepth.b);
164 float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
166 float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
167 float depth = realDistance - texDepth;
172 float getBaseLength(vec2 smTexCoord)
174 float l = length(2.0 * smTexCoord.xy - 1.0); // length in texture coords
175 return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords
178 float getDeltaPerspectiveFactor(float l)
180 return 0.1 / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10
183 float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier)
185 float baseLength = getBaseLength(smTexCoord);
186 float perspectiveFactor;
188 if (PCFBOUND == 0.0) return 0.0;
189 // Return fast if sharp shadows are requested
193 if (SOFTSHADOWRADIUS <= 1.0) {
194 perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
195 return max(2 * length(smTexCoord.xy) * 2048 / f_textureresolution / pow(perspectiveFactor, 3), SOFTSHADOWRADIUS);
199 float texture_size = 1.0 / (2048 /*f_textureresolution*/ * 0.5);
203 float maxRadius = SOFTSHADOWRADIUS * 5.0 * multiplier;
205 float bound = clamp(PCFBOUND * (1 - baseLength), 0.0, PCFBOUND);
208 for (y = -bound; y <= bound; y += 1.0)
209 for (x = -bound; x <= bound; x += 1.0) {
210 clampedpos = vec2(x,y);
211 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * maxRadius);
212 clampedpos = clampedpos * texture_size * perspectiveFactor * maxRadius * perspectiveFactor + smTexCoord.xy;
214 pointDepth = getHardShadowDepth(shadowsampler, clampedpos.xy, realDistance);
215 if (pointDepth > -0.01) {
222 depth = pow(clamp(depth, 0.0, 1000.0), 1.6) / 0.001;
224 perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
225 return max(length(smTexCoord.xy) * 2 * 2048 / f_textureresolution / pow(perspectiveFactor, 3), depth * maxRadius);
228 #ifdef POISSON_FILTER
229 const vec2[64] poissonDisk = vec2[64](
230 vec2(0.170019, -0.040254),
231 vec2(-0.299417, 0.791925),
232 vec2(0.645680, 0.493210),
233 vec2(-0.651784, 0.717887),
234 vec2(0.421003, 0.027070),
235 vec2(-0.817194, -0.271096),
236 vec2(-0.705374, -0.668203),
237 vec2(0.977050, -0.108615),
238 vec2(0.063326, 0.142369),
239 vec2(0.203528, 0.214331),
240 vec2(-0.667531, 0.326090),
241 vec2(-0.098422, -0.295755),
242 vec2(-0.885922, 0.215369),
243 vec2(0.566637, 0.605213),
244 vec2(0.039766, -0.396100),
245 vec2(0.751946, 0.453352),
246 vec2(0.078707, -0.715323),
247 vec2(-0.075838, -0.529344),
248 vec2(0.724479, -0.580798),
249 vec2(0.222999, -0.215125),
250 vec2(-0.467574, -0.405438),
251 vec2(-0.248268, -0.814753),
252 vec2(0.354411, -0.887570),
253 vec2(0.175817, 0.382366),
254 vec2(0.487472, -0.063082),
255 vec2(0.355476, 0.025357),
256 vec2(-0.084078, 0.898312),
257 vec2(0.488876, -0.783441),
258 vec2(0.470016, 0.217933),
259 vec2(-0.696890, -0.549791),
260 vec2(-0.149693, 0.605762),
261 vec2(0.034211, 0.979980),
262 vec2(0.503098, -0.308878),
263 vec2(-0.016205, -0.872921),
264 vec2(0.385784, -0.393902),
265 vec2(-0.146886, -0.859249),
266 vec2(0.643361, 0.164098),
267 vec2(0.634388, -0.049471),
268 vec2(-0.688894, 0.007843),
269 vec2(0.464034, -0.188818),
270 vec2(-0.440840, 0.137486),
271 vec2(0.364483, 0.511704),
272 vec2(0.034028, 0.325968),
273 vec2(0.099094, -0.308023),
274 vec2(0.693960, -0.366253),
275 vec2(0.678884, -0.204688),
276 vec2(0.001801, 0.780328),
277 vec2(0.145177, -0.898984),
278 vec2(0.062655, -0.611866),
279 vec2(0.315226, -0.604297),
280 vec2(-0.780145, 0.486251),
281 vec2(-0.371868, 0.882138),
282 vec2(0.200476, 0.494430),
283 vec2(-0.494552, -0.711051),
284 vec2(0.612476, 0.705252),
285 vec2(-0.578845, -0.768792),
286 vec2(-0.772454, -0.090976),
287 vec2(0.504440, 0.372295),
288 vec2(0.155736, 0.065157),
289 vec2(0.391522, 0.849605),
290 vec2(-0.620106, -0.328104),
291 vec2(0.789239, -0.419965),
292 vec2(-0.545396, 0.538133),
293 vec2(-0.178564, -0.596057)
296 #ifdef COLORED_SHADOWS
298 vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
301 vec4 visibility = vec4(0.0);
302 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
304 // we are in the middle of even brightness, no need for filtering
305 return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
308 float baseLength = getBaseLength(smTexCoord);
309 float perspectiveFactor;
311 float texture_size = 1.0 / (f_textureresolution * 0.5);
312 int samples = int(clamp(PCFSAMPLES * (1 - baseLength) * (1 - baseLength), PCFSAMPLES / 4, PCFSAMPLES));
313 int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
314 int end_offset = int(samples) + init_offset;
316 for (int x = init_offset; x < end_offset; x++) {
317 clampedpos = poissonDisk[x];
318 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius);
319 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor + smTexCoord.xy;
320 visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
323 return visibility / samples;
328 float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
331 float visibility = 0.0;
332 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
334 // we are in the middle of even brightness, no need for filtering
335 return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
338 float baseLength = getBaseLength(smTexCoord);
339 float perspectiveFactor;
341 float texture_size = 1.0 / (f_textureresolution * 0.5);
342 int samples = int(clamp(PCFSAMPLES * (1 - baseLength) * (1 - baseLength), PCFSAMPLES / 4, PCFSAMPLES));
343 int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
344 int end_offset = int(samples) + init_offset;
346 for (int x = init_offset; x < end_offset; x++) {
347 clampedpos = poissonDisk[x];
348 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius);
349 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor + smTexCoord.xy;
350 visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
353 return visibility / samples;
359 /* poisson filter disabled */
361 #ifdef COLORED_SHADOWS
363 vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
366 vec4 visibility = vec4(0.0);
367 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
369 // we are in the middle of even brightness, no need for filtering
370 return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
373 float baseLength = getBaseLength(smTexCoord);
374 float perspectiveFactor;
376 float texture_size = 1.0 / (f_textureresolution * 0.5);
378 float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
382 for (y = -bound; y <= bound; y += 1.0)
383 for (x = -bound; x <= bound; x += 1.0) {
384 clampedpos = vec2(x,y); // screen offset
385 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius / bound);
386 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor / bound + smTexCoord.xy; // both dx,dy and radius are adjusted
387 visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
391 return visibility / n;
395 float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
398 float visibility = 0.0;
399 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
401 // we are in the middle of even brightness, no need for filtering
402 return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
405 float baseLength = getBaseLength(smTexCoord);
406 float perspectiveFactor;
408 float texture_size = 1.0 / (f_textureresolution * 0.5);
410 float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
414 for (y = -bound; y <= bound; y += 1.0)
415 for (x = -bound; x <= bound; x += 1.0) {
416 clampedpos = vec2(x,y); // screen offset
417 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius / bound);
418 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor / bound + smTexCoord.xy; // both dx,dy and radius are adjusted
419 visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
423 return visibility / n;
431 #if ENABLE_TONE_MAPPING
433 /* Hable's UC2 Tone mapping parameters
441 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
444 vec3 uncharted2Tonemap(vec3 x)
446 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
449 vec4 applyToneMapping(vec4 color)
451 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
452 const float gamma = 1.6;
453 const float exposureBias = 5.5;
454 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
455 // Precalculated white_scale from
456 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
457 vec3 whiteScale = vec3(1.036015346);
458 color.rgb *= whiteScale;
459 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
468 vec2 uv = varTexCoord.st;
470 vec4 base = texture2D(baseTexture, uv).rgba;
471 // If alpha is zero, we can just discard the pixel. This fixes transparency
472 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
473 // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
478 #ifdef USE_DISCARD_REF
484 vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
486 #ifdef ENABLE_DYNAMIC_SHADOWS
487 if (f_shadow_strength > 0.0) {
488 float shadow_int = 0.0;
489 vec3 shadow_color = vec3(0.0, 0.0, 0.0);
490 vec3 posLightSpace = getLightSpacePosition();
492 float distance_rate = (1 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 50.0));
493 float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0);
495 if (distance_rate > 1e-7) {
497 #ifdef COLORED_SHADOWS
500 visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
502 visibility = vec4(1.0, 0.0, 0.0, 0.0);
503 shadow_int = visibility.r;
504 shadow_color = visibility.gba;
507 shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
511 shadow_int *= distance_rate;
512 shadow_int = clamp(shadow_int, 0.0, 1.0);
516 // turns out that nightRatio falls off much faster than
517 // actual brightness of artificial light in relation to natual light.
518 // Power ratio was measured on torches in MTG (brightness = 14).
519 float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6);
521 // Apply self-shadowing when light falls at a narrow angle to the surface
522 // Cosine of the cut-off angle.
523 const float self_shadow_cutoff_cosine = 0.035;
524 if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) {
525 shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
526 shadow_color = mix(vec3(0.0), shadow_color, min(cosLight, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
529 shadow_int *= f_adj_shadow_strength;
531 // calculate fragment color from components:
533 adjusted_night_ratio * col.rgb + // artificial light
534 (1.0 - adjusted_night_ratio) * ( // natural light
535 col.rgb * (1.0 - shadow_int * (1.0 - shadow_color)) + // filtered texture color
536 dayLight * shadow_color * shadow_int); // reflected filtered sunlight/moonlight
540 #if ENABLE_TONE_MAPPING
541 col = applyToneMapping(col);
544 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
545 // the fog will only be rendered correctly if the last operation before the
546 // clamp() is an addition. Else, the clamp() seems to be ignored.
547 // E.g. the following won't work:
548 // float clarity = clamp(fogShadingParameter
549 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
550 // As additions usually come for free following a multiplication, the new formula
551 // should be more efficient as well.
552 // Note: clarity = (1 - fogginess)
553 float clarity = clamp(fogShadingParameter
554 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
555 col = mix(skyBgColor, col, clarity);
556 col = vec4(col.rgb, base.a);