]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/object_shader/opengl_fragment.glsl
Translated using Weblate (Ukrainian)
[dragonfireclient.git] / client / shaders / object_shader / opengl_fragment.glsl
1 uniform sampler2D baseTexture;
2 uniform sampler2D normalTexture;
3 uniform sampler2D textureFlags;
4
5 uniform vec4 emissiveColor;
6 uniform vec4 skyBgColor;
7 uniform float fogDistance;
8 uniform vec3 eyePosition;
9
10 varying vec3 vNormal;
11 varying vec3 vPosition;
12 varying vec3 worldPosition;
13
14 varying vec3 eyeVec;
15 varying vec3 lightVec;
16 varying float vIDiff;
17
18 bool normalTexturePresent = false;
19 bool texTileableHorizontal = false;
20 bool texTileableVertical = false;
21 bool texSeamless = false;
22
23 const float e = 2.718281828459;
24 const float BS = 10.0;
25 const float fogStart = FOG_START;
26 const float fogShadingParameter = 1 / ( 1 - fogStart);
27
28 #ifdef ENABLE_TONE_MAPPING
29
30 /* Hable's UC2 Tone mapping parameters
31         A = 0.22;
32         B = 0.30;
33         C = 0.10;
34         D = 0.20;
35         E = 0.01;
36         F = 0.30;
37         W = 11.2;
38         equation used:  ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
39 */
40
41 vec3 uncharted2Tonemap(vec3 x)
42 {
43         return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
44 }
45
46 vec4 applyToneMapping(vec4 color)
47 {
48         color = vec4(pow(color.rgb, vec3(2.2)), color.a);
49         const float gamma = 1.6;
50         const float exposureBias = 5.5;
51         color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
52         // Precalculated white_scale from 
53         //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
54         vec3 whiteScale = vec3(1.036015346);
55         color.rgb *= whiteScale;
56         return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
57 }
58 #endif
59
60 void get_texture_flags()
61 {
62         vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
63         if (flags.r > 0.5) {
64                 normalTexturePresent = true;
65         }
66         if (flags.g > 0.5) {
67                 texTileableHorizontal = true;
68         }
69         if (flags.b > 0.5) {
70                 texTileableVertical = true;
71         }
72         if (texTileableHorizontal && texTileableVertical) {
73                 texSeamless = true;
74         }
75 }
76
77 float intensity(vec3 color)
78 {
79         return (color.r + color.g + color.b) / 3.0;
80 }
81
82 float get_rgb_height(vec2 uv)
83 {
84         if (texSeamless) {
85                 return intensity(texture2D(baseTexture, uv).rgb);
86         } else {
87                 return intensity(texture2D(baseTexture, clamp(uv, 0.0, 0.999)).rgb);
88         }
89 }
90
91 vec4 get_normal_map(vec2 uv)
92 {
93         vec4 bump = texture2D(normalTexture, uv).rgba;
94         bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
95         return bump;
96 }
97
98 void main(void)
99 {
100         vec3 color;
101         vec4 bump;
102         vec2 uv = gl_TexCoord[0].st;
103         bool use_normalmap = false;
104         get_texture_flags();
105
106 #if USE_NORMALMAPS == 1
107         if (normalTexturePresent) {
108                 bump = get_normal_map(uv);
109                 use_normalmap = true;
110         }
111 #endif
112
113 #if GENERATE_NORMALMAPS == 1
114         if (normalTexturePresent == false) {
115                 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
116                 float t  = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
117                 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
118                 float r  = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
119                 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
120                 float b  = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
121                 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
122                 float l  = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
123                 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
124                 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
125                 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
126                 use_normalmap = true;
127         }
128 #endif
129
130         vec4 base = texture2D(baseTexture, uv).rgba;
131
132 #ifdef ENABLE_BUMPMAPPING
133         if (use_normalmap) {
134                 vec3 L = normalize(lightVec);
135                 vec3 E = normalize(eyeVec);
136                 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
137                 float diffuse = dot(-E,bump.xyz);
138                 color = (diffuse + 0.1 * specular) * base.rgb;
139         } else {
140                 color = base.rgb;
141         }
142 #else
143         color = base.rgb;
144 #endif
145
146         vec4 col = vec4(color.rgb, base.a);
147
148         col.rgb *= gl_Color.rgb;
149
150         col.rgb *= emissiveColor.rgb * vIDiff;
151
152 #ifdef ENABLE_TONE_MAPPING
153         col = applyToneMapping(col);
154 #endif
155
156         // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
157         // the fog will only be rendered correctly if the last operation before the
158         // clamp() is an addition. Else, the clamp() seems to be ignored.
159         // E.g. the following won't work:
160         //      float clarity = clamp(fogShadingParameter
161         //              * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
162         // As additions usually come for free following a multiplication, the new formula
163         // should be more efficient as well.
164         // Note: clarity = (1 - fogginess)
165         float clarity = clamp(fogShadingParameter
166                 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
167         col = mix(skyBgColor, col, clarity);
168
169         gl_FragColor = vec4(col.rgb, base.a);
170 }