]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/object_shader/opengl_fragment.glsl
7baf5826ffaa3ab4126b931de6a263fe89c2528d
[dragonfireclient.git] / client / shaders / object_shader / opengl_fragment.glsl
1 uniform sampler2D baseTexture;
2
3 uniform vec4 emissiveColor;
4 uniform vec3 dayLight;
5 uniform vec4 skyBgColor;
6 uniform float fogDistance;
7 uniform vec3 eyePosition;
8
9 // The cameraOffset is the current center of the visible world.
10 uniform vec3 cameraOffset;
11 uniform float animationTimer;
12 #ifdef ENABLE_DYNAMIC_SHADOWS
13         // shadow texture
14         uniform sampler2D ShadowMapSampler;
15         // shadow uniforms
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;
24         
25         varying float adj_shadow_strength;
26         varying float cosLight;
27         varying float f_normal_length;
28         varying vec3 shadow_position;
29 #endif
30
31
32 varying vec3 vNormal;
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;
41 #ifdef GL_ES
42 varying mediump vec2 varTexCoord;
43 #else
44 centroid varying vec2 varTexCoord;
45 #endif
46 varying vec3 eyeVec;
47 varying float nightRatio;
48
49 varying float vIDiff;
50
51 const float fogStart = FOG_START;
52 const float fogShadingParameter = 1.0 / (1.0 - fogStart);
53
54 #ifdef ENABLE_DYNAMIC_SHADOWS
55
56 // assuming near is always 1.0
57 float getLinearDepth()
58 {
59         return 2.0 * f_shadowfar / (f_shadowfar + 1.0 - (2.0 * gl_FragCoord.z - 1.0) * (f_shadowfar - 1.0));
60 }
61
62 vec3 getLightSpacePosition()
63 {
64         return shadow_position * 0.5 + 0.5;
65 }
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)
69 {
70         float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
71         return t * t * (3.0 - 2.0 * t);
72 }
73
74 #ifdef COLORED_SHADOWS
75
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;
79
80 float packColor(vec3 color)
81 {
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;
85 }
86
87 vec3 unpackColor(float value)
88 {
89         vec3 color;
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;
93         return color;
94 }
95
96 vec4 getHardShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
97 {
98         vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy).rgba;
99
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));
105         }
106         return result;
107 }
108
109 #else
110
111 float getHardShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
112 {
113         float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
114         float visibility = step(0.0, realDistance - texDepth);
115         return visibility;
116 }
117
118 #endif
119
120
121 #if SHADOW_FILTER == 2
122         #define PCFBOUND 3.5
123         #define PCFSAMPLES 64.0
124 #elif SHADOW_FILTER == 1
125         #define PCFBOUND 1.5
126         #if defined(POISSON_FILTER)
127                 #define PCFSAMPLES 32.0
128         #else
129                 #define PCFSAMPLES 16.0
130         #endif
131 #else
132         #define PCFBOUND 0.0
133         #if defined(POISSON_FILTER)
134                 #define PCFSAMPLES 4.0
135         #else
136                 #define PCFSAMPLES 1.0
137         #endif
138 #endif
139 #ifdef COLORED_SHADOWS
140 float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
141 {
142         vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy);
143         float depth = max(realDistance - texDepth.r, realDistance - texDepth.b);
144         return depth;
145 }
146 #else
147 float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
148 {
149         float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
150         float depth = realDistance - texDepth;
151         return depth;
152 }
153 #endif
154
155 float getBaseLength(vec2 smTexCoord)
156 {
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
159 }
160
161 float getDeltaPerspectiveFactor(float l)
162 {
163         return 0.04 * pow(512.0 / f_textureresolution, 0.4) / (xyPerspectiveBias0 * l + xyPerspectiveBias1);                      // original distortion factor, divided by 10
164 }
165
166 float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier)
167 {
168         float baseLength = getBaseLength(smTexCoord);
169         float perspectiveFactor;
170
171         // Return fast if sharp shadows are requested
172         if (PCFBOUND == 0.0)
173                 return 0.0;
174
175         if (SOFTSHADOWRADIUS <= 1.0) {
176                 perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
177                 return max(2 * length(smTexCoord.xy) * 2048 / f_textureresolution / pow(perspectiveFactor, 3), SOFTSHADOWRADIUS);
178         }
179
180         vec2 clampedpos;
181         float texture_size = 1.0 / (2048 /*f_textureresolution*/ * 0.5);
182         float y, x;
183         float depth = 0.0;
184         float pointDepth;
185         float maxRadius = SOFTSHADOWRADIUS * 5.0 * multiplier;
186
187         float bound = clamp(PCFBOUND * (1 - baseLength), 0.0, PCFBOUND);
188         int n = 0;
189
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;
195
196                 pointDepth = getHardShadowDepth(shadowsampler, clampedpos.xy, realDistance);
197                 if (pointDepth > -0.01) {
198                         depth += pointDepth;
199                         n += 1;
200                 }
201         }
202
203         depth = depth / n;
204         depth = pow(clamp(depth, 0.0, 1000.0), 1.6) / 0.001;
205
206         perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
207         return max(length(smTexCoord.xy) * 2 * 2048 / f_textureresolution / pow(perspectiveFactor, 3), depth * maxRadius);
208 }
209
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)
276 );
277
278 #ifdef COLORED_SHADOWS
279
280 vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
281 {
282         vec2 clampedpos;
283         vec4 visibility = vec4(0.0);
284         float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
285         if (radius < 0.1) {
286                 // we are in the middle of even brightness, no need for filtering
287                 return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
288         }
289
290         float baseLength = getBaseLength(smTexCoord);
291         float perspectiveFactor;
292
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;
297
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);
303         }
304
305         return visibility / samples;
306 }
307
308 #else
309
310 float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
311 {
312         vec2 clampedpos;
313         float visibility = 0.0;
314         float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
315         if (radius < 0.1) {
316                 // we are in the middle of even brightness, no need for filtering
317                 return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
318         }
319
320         float baseLength = getBaseLength(smTexCoord);
321         float perspectiveFactor;
322
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;
327
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);
333         }
334
335         return visibility / samples;
336 }
337
338 #endif
339
340 #else
341 /* poisson filter disabled */
342
343 #ifdef COLORED_SHADOWS
344
345 vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
346 {
347         vec2 clampedpos;
348         vec4 visibility = vec4(0.0);
349         float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
350         if (radius < 0.1) {
351                 // we are in the middle of even brightness, no need for filtering
352                 return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
353         }
354
355         float baseLength = getBaseLength(smTexCoord);
356         float perspectiveFactor;
357
358         float texture_size = 1.0 / (f_textureresolution * 0.5);
359         float y, x;
360         float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
361         int n = 0;
362
363         // basic PCF filter
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);
370                 n += 1;
371         }
372
373         return visibility / n;
374 }
375
376 #else
377 float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
378 {
379         vec2 clampedpos;
380         float visibility = 0.0;
381         float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
382         if (radius < 0.1) {
383                 // we are in the middle of even brightness, no need for filtering
384                 return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
385         }
386
387         float baseLength = getBaseLength(smTexCoord);
388         float perspectiveFactor;
389
390         float texture_size = 1.0 / (f_textureresolution * 0.5);
391         float y, x;
392         float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
393         int n = 0;
394
395         // basic PCF filter
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);
402                 n += 1;
403         }
404
405         return visibility / n;
406 }
407
408 #endif
409
410 #endif
411 #endif
412
413 #if ENABLE_TONE_MAPPING
414
415 /* Hable's UC2 Tone mapping parameters
416         A = 0.22;
417         B = 0.30;
418         C = 0.10;
419         D = 0.20;
420         E = 0.01;
421         F = 0.30;
422         W = 11.2;
423         equation used:  ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
424 */
425
426 vec3 uncharted2Tonemap(vec3 x)
427 {
428         return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
429 }
430
431 vec4 applyToneMapping(vec4 color)
432 {
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);
442 }
443 #endif
444
445
446
447 void main(void)
448 {
449         vec3 color;
450         vec2 uv = varTexCoord.st;
451
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.
456 #ifdef USE_DISCARD
457         if (base.a == 0.0)
458                 discard;
459 #endif
460 #ifdef USE_DISCARD_REF
461         if (base.a < 0.5)
462                 discard;
463 #endif
464
465         color = base.rgb;
466         vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
467         col.rgb *= vIDiff;
468
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();
474
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)
477                         distance_rate = 0.0;
478                 float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1,  posLightSpace.z),0.0);
479
480                 if (distance_rate > 1e-7) {
481
482 #ifdef COLORED_SHADOWS
483                         vec4 visibility;
484                         if (cosLight > 0.0 || f_normal_length < 1e-3)
485                                 visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
486                         else
487                                 visibility = vec4(1.0, 0.0, 0.0, 0.0);
488                         shadow_int = visibility.r;
489                         shadow_color = visibility.gba;
490 #else
491                         if (cosLight > 0.0 || f_normal_length < 1e-3)
492                         if (cosLight > 0.0)
493                                 shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
494                         else
495                                 shadow_int = 1.0;
496 #endif
497                         shadow_int *= distance_rate;
498                         shadow_int = clamp(shadow_int, 0.0, 1.0);
499
500                 }
501
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);
506
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);
513                 }
514
515                 shadow_int *= f_adj_shadow_strength;
516
517                 // calculate fragment color from components:
518                 col.rgb =
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
523         }
524 #endif
525
526 #if ENABLE_TONE_MAPPING
527         col = applyToneMapping(col);
528 #endif
529
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);
543         
544         gl_FragColor = col;
545 }