]> git.lizzy.rs Git - minetest.git/blob - client/shaders/wielded_shader/opengl_fragment.glsl
Server: Calculate maximal total block sends dynamically (#6393)
[minetest.git] / client / shaders / wielded_shader / opengl_fragment.glsl
1 uniform sampler2D baseTexture;
2 uniform sampler2D normalTexture;
3 uniform sampler2D textureFlags;
4
5 uniform vec4 skyBgColor;
6 uniform float fogDistance;
7 uniform vec3 eyePosition;
8
9 varying vec3 vPosition;
10 varying vec3 worldPosition;
11
12 varying vec3 eyeVec;
13 varying vec3 lightVec;
14
15 bool normalTexturePresent = false;
16 bool texTileableHorizontal = false;
17 bool texTileableVertical = false;
18 bool texSeamless = false;
19
20 const float e = 2.718281828459;
21 const float BS = 10.0;
22 const float fogStart = FOG_START;
23 const float fogShadingParameter = 1 / ( 1 - fogStart);
24
25 void get_texture_flags()
26 {
27         vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
28         if (flags.r > 0.5) {
29                 normalTexturePresent = true;
30         }
31         if (flags.g > 0.5) {
32                 texTileableHorizontal = true;
33         }
34         if (flags.b > 0.5) {
35                 texTileableVertical = true;
36         }
37         if (texTileableHorizontal && texTileableVertical) {
38                 texSeamless = true;
39         }
40 }
41
42 float intensity(vec3 color)
43 {
44         return (color.r + color.g + color.b) / 3.0;
45 }
46
47 float get_rgb_height(vec2 uv)
48 {
49         if (texSeamless) {
50                 return intensity(texture2D(baseTexture, uv).rgb);
51         } else {
52                 return intensity(texture2D(baseTexture, clamp(uv, 0.0, 0.999)).rgb);
53         }
54 }
55
56 vec4 get_normal_map(vec2 uv)
57 {
58         vec4 bump = texture2D(normalTexture, uv).rgba;
59         bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
60         return bump;
61 }
62
63 void main(void)
64 {
65         vec3 color;
66         vec4 bump;
67         vec2 uv = gl_TexCoord[0].st;
68         bool use_normalmap = false;
69         get_texture_flags();
70
71 #if USE_NORMALMAPS == 1
72         if (normalTexturePresent) {
73                 bump = get_normal_map(uv);
74                 use_normalmap = true;
75         }
76 #endif
77
78 #if GENERATE_NORMALMAPS == 1
79         if (normalTexturePresent == false) {
80                 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
81                 float t  = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
82                 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
83                 float r  = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
84                 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
85                 float b  = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
86                 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
87                 float l  = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
88                 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
89                 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
90                 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
91                 use_normalmap = true;
92         }
93 #endif
94
95         vec4 base = texture2D(baseTexture, uv).rgba;
96
97 #ifdef ENABLE_BUMPMAPPING
98         if (use_normalmap) {
99                 vec3 L = normalize(lightVec);
100                 vec3 E = normalize(eyeVec);
101                 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
102                 float diffuse = dot(-E,bump.xyz);
103                 color = (diffuse + 0.1 * specular) * base.rgb;
104         } else {
105                 color = base.rgb;
106         }
107 #else
108         color = base.rgb;
109 #endif
110
111         vec4 col = vec4(color.rgb, base.a);
112         col *= gl_Color;
113         // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
114         // the fog will only be rendered correctly if the last operation before the
115         // clamp() is an addition. Else, the clamp() seems to be ignored.
116         // E.g. the following won't work:
117         //      float clarity = clamp(fogShadingParameter
118         //              * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
119         // As additions usually come for free following a multiplication, the new formula
120         // should be more efficient as well.
121         // Note: clarity = (1 - fogginess)
122         float clarity = clamp(fogShadingParameter
123                 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
124         col = mix(skyBgColor, col, clarity);
125
126         gl_FragColor = vec4(col.rgb, base.a);
127 }