]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/nodes_shader/opengl_fragment.glsl
Cleanup shader generation code (#10663)
[dragonfireclient.git] / client / shaders / nodes_shader / opengl_fragment.glsl
1 uniform sampler2D baseTexture;
2
3 uniform vec4 skyBgColor;
4 uniform float fogDistance;
5 uniform vec3 eyePosition;
6
7 // The cameraOffset is the current center of the visible world.
8 uniform vec3 cameraOffset;
9 uniform float animationTimer;
10
11 varying vec3 vPosition;
12 // World position in the visible world (i.e. relative to the cameraOffset.)
13 // This can be used for many shader effects without loss of precision.
14 // If the absolute position is required it can be calculated with
15 // cameraOffset + worldPosition (for large coordinates the limits of float
16 // precision must be considered).
17 varying vec3 worldPosition;
18 varying lowp vec4 varColor;
19 centroid varying mediump vec2 varTexCoord;
20 varying vec3 eyeVec;
21
22 const float fogStart = FOG_START;
23 const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
24
25 #if ENABLE_TONE_MAPPING
26
27 /* Hable's UC2 Tone mapping parameters
28         A = 0.22;
29         B = 0.30;
30         C = 0.10;
31         D = 0.20;
32         E = 0.01;
33         F = 0.30;
34         W = 11.2;
35         equation used:  ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
36 */
37
38 vec3 uncharted2Tonemap(vec3 x)
39 {
40         return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
41 }
42
43 vec4 applyToneMapping(vec4 color)
44 {
45         color = vec4(pow(color.rgb, vec3(2.2)), color.a);
46         const float gamma = 1.6;
47         const float exposureBias = 5.5;
48         color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
49         // Precalculated white_scale from
50         //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
51         vec3 whiteScale = vec3(1.036015346);
52         color.rgb *= whiteScale;
53         return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
54 }
55 #endif
56
57 void main(void)
58 {
59         vec3 color;
60         vec2 uv = varTexCoord.st;
61
62         vec4 base = texture2D(baseTexture, uv).rgba;
63 #ifdef USE_DISCARD
64         // If alpha is zero, we can just discard the pixel. This fixes transparency
65         // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
66         // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
67         if (base.a == 0.0) {
68                 discard;
69         }
70 #endif
71
72         color = base.rgb;
73
74         vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
75
76 #if ENABLE_TONE_MAPPING
77         col = applyToneMapping(col);
78 #endif
79
80         // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
81         // the fog will only be rendered correctly if the last operation before the
82         // clamp() is an addition. Else, the clamp() seems to be ignored.
83         // E.g. the following won't work:
84         //      float clarity = clamp(fogShadingParameter
85         //              * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
86         // As additions usually come for free following a multiplication, the new formula
87         // should be more efficient as well.
88         // Note: clarity = (1 - fogginess)
89         float clarity = clamp(fogShadingParameter
90                 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
91         col = mix(skyBgColor, col, clarity);
92         col = vec4(col.rgb, base.a);
93
94         gl_FragColor = col;
95 }