]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/object_shader/opengl_fragment.glsl
Remove "generate normal maps" feature (#10313)
[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 vec4 get_normal_map(vec2 uv)
78 {
79         vec4 bump = texture2D(normalTexture, uv).rgba;
80         bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
81         return bump;
82 }
83
84 void main(void)
85 {
86         vec3 color;
87         vec4 bump;
88         vec2 uv = gl_TexCoord[0].st;
89         bool use_normalmap = false;
90         get_texture_flags();
91
92 #if USE_NORMALMAPS == 1
93         if (normalTexturePresent) {
94                 bump = get_normal_map(uv);
95                 use_normalmap = true;
96         }
97 #endif
98
99         vec4 base = texture2D(baseTexture, uv).rgba;
100
101 #ifdef USE_DISCARD
102         // If alpha is zero, we can just discard the pixel. This fixes transparency
103         // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa.
104         if (base.a == 0.0) {
105                 discard;
106         }
107 #endif
108
109 #ifdef ENABLE_BUMPMAPPING
110         if (use_normalmap) {
111                 vec3 L = normalize(lightVec);
112                 vec3 E = normalize(eyeVec);
113                 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
114                 float diffuse = dot(-E,bump.xyz);
115                 color = (diffuse + 0.1 * specular) * base.rgb;
116         } else {
117                 color = base.rgb;
118         }
119 #else
120         color = base.rgb;
121 #endif
122
123         vec4 col = vec4(color.rgb, base.a);
124
125         col.rgb *= gl_Color.rgb;
126
127         col.rgb *= emissiveColor.rgb * vIDiff;
128
129 #ifdef ENABLE_TONE_MAPPING
130         col = applyToneMapping(col);
131 #endif
132
133         // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
134         // the fog will only be rendered correctly if the last operation before the
135         // clamp() is an addition. Else, the clamp() seems to be ignored.
136         // E.g. the following won't work:
137         //      float clarity = clamp(fogShadingParameter
138         //              * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
139         // As additions usually come for free following a multiplication, the new formula
140         // should be more efficient as well.
141         // Note: clarity = (1 - fogginess)
142         float clarity = clamp(fogShadingParameter
143                 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
144         col = mix(skyBgColor, col, clarity);
145
146         gl_FragColor = vec4(col.rgb, base.a);
147 }