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