1 uniform sampler2D baseTexture;
3 uniform vec4 emissiveColor;
5 uniform vec4 skyBgColor;
6 uniform float fogDistance;
7 uniform vec3 eyePosition;
9 // The cameraOffset is the current center of the visible world.
10 uniform vec3 cameraOffset;
11 uniform float animationTimer;
12 #ifdef ENABLE_DYNAMIC_SHADOWS
14 uniform sampler2D ShadowMapSampler;
16 uniform vec3 v_LightDirection;
17 uniform float f_textureresolution;
18 uniform mat4 m_ShadowViewProj;
19 uniform float f_shadowfar;
20 uniform float f_shadow_strength;
21 uniform vec4 CameraPos;
22 uniform float xyPerspectiveBias0;
23 uniform float xyPerspectiveBias1;
25 varying float adj_shadow_strength;
26 varying float cosLight;
27 varying float f_normal_length;
28 varying vec3 shadow_position;
33 varying vec3 vPosition;
34 // World position in the visible world (i.e. relative to the cameraOffset.)
35 // This can be used for many shader effects without loss of precision.
36 // If the absolute position is required it can be calculated with
37 // cameraOffset + worldPosition (for large coordinates the limits of float
38 // precision must be considered).
39 varying vec3 worldPosition;
40 varying lowp vec4 varColor;
42 varying mediump vec2 varTexCoord;
44 centroid varying vec2 varTexCoord;
47 varying float nightRatio;
51 const float fogStart = FOG_START;
52 const float fogShadingParameter = 1.0 / (1.0 - fogStart);
54 #ifdef ENABLE_DYNAMIC_SHADOWS
56 // assuming near is always 1.0
57 float getLinearDepth()
59 return 2.0 * f_shadowfar / (f_shadowfar + 1.0 - (2.0 * gl_FragCoord.z - 1.0) * (f_shadowfar - 1.0));
62 vec3 getLightSpacePosition()
64 return shadow_position * 0.5 + 0.5;
66 // custom smoothstep implementation because it's not defined in glsl1.2
67 // https://docs.gl/sl4/smoothstep
68 float mtsmoothstep(in float edge0, in float edge1, in float x)
70 float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
71 return t * t * (3.0 - 2.0 * t);
74 #ifdef COLORED_SHADOWS
76 // c_precision of 128 fits within 7 base-10 digits
77 const float c_precision = 128.0;
78 const float c_precisionp1 = c_precision + 1.0;
80 float packColor(vec3 color)
82 return floor(color.b * c_precision + 0.5)
83 + floor(color.g * c_precision + 0.5) * c_precisionp1
84 + floor(color.r * c_precision + 0.5) * c_precisionp1 * c_precisionp1;
87 vec3 unpackColor(float value)
90 color.b = mod(value, c_precisionp1) / c_precision;
91 color.g = mod(floor(value / c_precisionp1), c_precisionp1) / c_precision;
92 color.r = floor(value / (c_precisionp1 * c_precisionp1)) / c_precision;
96 vec4 getHardShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
98 vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy).rgba;
100 float visibility = step(0.0, realDistance - texDepth.r);
101 vec4 result = vec4(visibility, vec3(0.0,0.0,0.0));//unpackColor(texDepth.g));
102 if (visibility < 0.1) {
103 visibility = step(0.0, realDistance - texDepth.b);
104 result = vec4(visibility, unpackColor(texDepth.a));
111 float getHardShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
113 float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
114 float visibility = step(0.0, realDistance - texDepth);
121 #if SHADOW_FILTER == 2
123 #define PCFSAMPLES 64.0
124 #elif SHADOW_FILTER == 1
126 #if defined(POISSON_FILTER)
127 #define PCFSAMPLES 32.0
129 #define PCFSAMPLES 16.0
133 #if defined(POISSON_FILTER)
134 #define PCFSAMPLES 4.0
136 #define PCFSAMPLES 1.0
139 #ifdef COLORED_SHADOWS
140 float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
142 vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy);
143 float depth = max(realDistance - texDepth.r, realDistance - texDepth.b);
147 float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
149 float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
150 float depth = realDistance - texDepth;
155 float getBaseLength(vec2 smTexCoord)
157 float l = length(2.0 * smTexCoord.xy - 1.0 - CameraPos.xy); // length in texture coords
158 return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords
161 float getDeltaPerspectiveFactor(float l)
163 return 0.04 * pow(512.0 / f_textureresolution, 0.4) / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10
166 float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier)
168 float baseLength = getBaseLength(smTexCoord);
169 float perspectiveFactor;
171 // Return fast if sharp shadows are requested
175 if (SOFTSHADOWRADIUS <= 1.0) {
176 perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
177 return max(2 * length(smTexCoord.xy) * 2048 / f_textureresolution / pow(perspectiveFactor, 3), SOFTSHADOWRADIUS);
181 float texture_size = 1.0 / (2048 /*f_textureresolution*/ * 0.5);
185 float maxRadius = SOFTSHADOWRADIUS * 5.0 * multiplier;
187 float bound = clamp(PCFBOUND * (1 - baseLength), 0.0, PCFBOUND);
190 for (y = -bound; y <= bound; y += 1.0)
191 for (x = -bound; x <= bound; x += 1.0) {
192 clampedpos = vec2(x,y);
193 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * maxRadius);
194 clampedpos = clampedpos * texture_size * perspectiveFactor * maxRadius * perspectiveFactor + smTexCoord.xy;
196 pointDepth = getHardShadowDepth(shadowsampler, clampedpos.xy, realDistance);
197 if (pointDepth > -0.01) {
204 depth = pow(clamp(depth, 0.0, 1000.0), 1.6) / 0.001;
206 perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
207 return max(length(smTexCoord.xy) * 2 * 2048 / f_textureresolution / pow(perspectiveFactor, 3), depth * maxRadius);
210 #ifdef POISSON_FILTER
211 const vec2[64] poissonDisk = vec2[64](
212 vec2(0.170019, -0.040254),
213 vec2(-0.299417, 0.791925),
214 vec2(0.645680, 0.493210),
215 vec2(-0.651784, 0.717887),
216 vec2(0.421003, 0.027070),
217 vec2(-0.817194, -0.271096),
218 vec2(-0.705374, -0.668203),
219 vec2(0.977050, -0.108615),
220 vec2(0.063326, 0.142369),
221 vec2(0.203528, 0.214331),
222 vec2(-0.667531, 0.326090),
223 vec2(-0.098422, -0.295755),
224 vec2(-0.885922, 0.215369),
225 vec2(0.566637, 0.605213),
226 vec2(0.039766, -0.396100),
227 vec2(0.751946, 0.453352),
228 vec2(0.078707, -0.715323),
229 vec2(-0.075838, -0.529344),
230 vec2(0.724479, -0.580798),
231 vec2(0.222999, -0.215125),
232 vec2(-0.467574, -0.405438),
233 vec2(-0.248268, -0.814753),
234 vec2(0.354411, -0.887570),
235 vec2(0.175817, 0.382366),
236 vec2(0.487472, -0.063082),
237 vec2(0.355476, 0.025357),
238 vec2(-0.084078, 0.898312),
239 vec2(0.488876, -0.783441),
240 vec2(0.470016, 0.217933),
241 vec2(-0.696890, -0.549791),
242 vec2(-0.149693, 0.605762),
243 vec2(0.034211, 0.979980),
244 vec2(0.503098, -0.308878),
245 vec2(-0.016205, -0.872921),
246 vec2(0.385784, -0.393902),
247 vec2(-0.146886, -0.859249),
248 vec2(0.643361, 0.164098),
249 vec2(0.634388, -0.049471),
250 vec2(-0.688894, 0.007843),
251 vec2(0.464034, -0.188818),
252 vec2(-0.440840, 0.137486),
253 vec2(0.364483, 0.511704),
254 vec2(0.034028, 0.325968),
255 vec2(0.099094, -0.308023),
256 vec2(0.693960, -0.366253),
257 vec2(0.678884, -0.204688),
258 vec2(0.001801, 0.780328),
259 vec2(0.145177, -0.898984),
260 vec2(0.062655, -0.611866),
261 vec2(0.315226, -0.604297),
262 vec2(-0.780145, 0.486251),
263 vec2(-0.371868, 0.882138),
264 vec2(0.200476, 0.494430),
265 vec2(-0.494552, -0.711051),
266 vec2(0.612476, 0.705252),
267 vec2(-0.578845, -0.768792),
268 vec2(-0.772454, -0.090976),
269 vec2(0.504440, 0.372295),
270 vec2(0.155736, 0.065157),
271 vec2(0.391522, 0.849605),
272 vec2(-0.620106, -0.328104),
273 vec2(0.789239, -0.419965),
274 vec2(-0.545396, 0.538133),
275 vec2(-0.178564, -0.596057)
278 #ifdef COLORED_SHADOWS
280 vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
283 vec4 visibility = vec4(0.0);
284 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
286 // we are in the middle of even brightness, no need for filtering
287 return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
290 float baseLength = getBaseLength(smTexCoord);
291 float perspectiveFactor;
293 float texture_size = 1.0 / (f_textureresolution * 0.5);
294 int samples = int(clamp(PCFSAMPLES * (1 - baseLength) * (1 - baseLength), PCFSAMPLES / 4, PCFSAMPLES));
295 int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
296 int end_offset = int(samples) + init_offset;
298 for (int x = init_offset; x < end_offset; x++) {
299 clampedpos = poissonDisk[x];
300 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius);
301 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor + smTexCoord.xy;
302 visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
305 return visibility / samples;
310 float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
313 float visibility = 0.0;
314 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
316 // we are in the middle of even brightness, no need for filtering
317 return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
320 float baseLength = getBaseLength(smTexCoord);
321 float perspectiveFactor;
323 float texture_size = 1.0 / (f_textureresolution * 0.5);
324 int samples = int(clamp(PCFSAMPLES * (1 - baseLength) * (1 - baseLength), PCFSAMPLES / 4, PCFSAMPLES));
325 int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
326 int end_offset = int(samples) + init_offset;
328 for (int x = init_offset; x < end_offset; x++) {
329 clampedpos = poissonDisk[x];
330 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius);
331 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor + smTexCoord.xy;
332 visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
335 return visibility / samples;
341 /* poisson filter disabled */
343 #ifdef COLORED_SHADOWS
345 vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
348 vec4 visibility = vec4(0.0);
349 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
351 // we are in the middle of even brightness, no need for filtering
352 return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
355 float baseLength = getBaseLength(smTexCoord);
356 float perspectiveFactor;
358 float texture_size = 1.0 / (f_textureresolution * 0.5);
360 float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
364 for (y = -bound; y <= bound; y += 1.0)
365 for (x = -bound; x <= bound; x += 1.0) {
366 clampedpos = vec2(x,y); // screen offset
367 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius / bound);
368 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor / bound + smTexCoord.xy; // both dx,dy and radius are adjusted
369 visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
373 return visibility / n;
377 float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
380 float visibility = 0.0;
381 float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
383 // we are in the middle of even brightness, no need for filtering
384 return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
387 float baseLength = getBaseLength(smTexCoord);
388 float perspectiveFactor;
390 float texture_size = 1.0 / (f_textureresolution * 0.5);
392 float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
396 for (y = -bound; y <= bound; y += 1.0)
397 for (x = -bound; x <= bound; x += 1.0) {
398 clampedpos = vec2(x,y); // screen offset
399 perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius / bound);
400 clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor / bound + smTexCoord.xy; // both dx,dy and radius are adjusted
401 visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
405 return visibility / n;
413 #if ENABLE_TONE_MAPPING
415 /* Hable's UC2 Tone mapping parameters
423 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
426 vec3 uncharted2Tonemap(vec3 x)
428 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
431 vec4 applyToneMapping(vec4 color)
433 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
434 const float gamma = 1.6;
435 const float exposureBias = 5.5;
436 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
437 // Precalculated white_scale from
438 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
439 vec3 whiteScale = vec3(1.036015346);
440 color.rgb *= whiteScale;
441 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
450 vec2 uv = varTexCoord.st;
452 vec4 base = texture2D(baseTexture, uv).rgba;
453 // If alpha is zero, we can just discard the pixel. This fixes transparency
454 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
455 // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
460 #ifdef USE_DISCARD_REF
466 vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
469 #ifdef ENABLE_DYNAMIC_SHADOWS
470 if (f_shadow_strength > 0.0) {
471 float shadow_int = 0.0;
472 vec3 shadow_color = vec3(0.0, 0.0, 0.0);
473 vec3 posLightSpace = getLightSpacePosition();
475 float distance_rate = (1.0 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 10.0));
476 if (max(abs(posLightSpace.x - 0.5), abs(posLightSpace.y - 0.5)) > 0.5)
478 float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0);
480 if (distance_rate > 1e-7) {
482 #ifdef COLORED_SHADOWS
484 if (cosLight > 0.0 || f_normal_length < 1e-3)
485 visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
487 visibility = vec4(1.0, 0.0, 0.0, 0.0);
488 shadow_int = visibility.r;
489 shadow_color = visibility.gba;
491 if (cosLight > 0.0 || f_normal_length < 1e-3)
493 shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
497 shadow_int *= distance_rate;
498 shadow_int = clamp(shadow_int, 0.0, 1.0);
502 // turns out that nightRatio falls off much faster than
503 // actual brightness of artificial light in relation to natual light.
504 // Power ratio was measured on torches in MTG (brightness = 14).
505 float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6);
507 // Apply self-shadowing when light falls at a narrow angle to the surface
508 // Cosine of the cut-off angle.
509 const float self_shadow_cutoff_cosine = 0.14;
510 if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) {
511 shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
512 shadow_color = mix(vec3(0.0), shadow_color, min(cosLight, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
515 shadow_int *= f_adj_shadow_strength;
517 // calculate fragment color from components:
519 adjusted_night_ratio * col.rgb + // artificial light
520 (1.0 - adjusted_night_ratio) * ( // natural light
521 col.rgb * (1.0 - shadow_int * (1.0 - shadow_color)) + // filtered texture color
522 dayLight * shadow_color * shadow_int); // reflected filtered sunlight/moonlight
526 #if ENABLE_TONE_MAPPING
527 col = applyToneMapping(col);
530 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
531 // the fog will only be rendered correctly if the last operation before the
532 // clamp() is an addition. Else, the clamp() seems to be ignored.
533 // E.g. the following won't work:
534 // float clarity = clamp(fogShadingParameter
535 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
536 // As additions usually come for free following a multiplication, the new formula
537 // should be more efficient as well.
538 // Note: clarity = (1 - fogginess)
539 float clarity = clamp(fogShadingParameter
540 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
541 col = mix(skyBgColor, col, clarity);
542 col = vec4(col.rgb, base.a);