]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/nodes_shader/opengl_fragment.glsl
437b325d3f831e35afe9b7190edcc909ca91b5fa
[dragonfireclient.git] / client / shaders / nodes_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 // The cameraOffset is the current center of the visible world.
10 uniform vec3 cameraOffset;
11 uniform float animationTimer;
12
13 varying vec3 vPosition;
14 // World position in the visible world (i.e. relative to the cameraOffset.)
15 // This can be used for many shader effects without loss of precision.
16 // If the absolute position is required it can be calculated with
17 // cameraOffset + worldPosition (for large coordinates the limits of float
18 // precision must be considered).
19 varying vec3 worldPosition;
20 varying float area_enable_parallax;
21
22 varying vec3 eyeVec;
23 varying vec3 tsEyeVec;
24 varying vec3 lightVec;
25 varying vec3 tsLightVec;
26
27 bool normalTexturePresent = false;
28
29 const float e = 2.718281828459;
30 const float BS = 10.0;
31 const float fogStart = FOG_START;
32 const float fogShadingParameter = 1 / ( 1 - fogStart);
33
34 #ifdef ENABLE_TONE_MAPPING
35
36 /* Hable's UC2 Tone mapping parameters
37         A = 0.22;
38         B = 0.30;
39         C = 0.10;
40         D = 0.20;
41         E = 0.01;
42         F = 0.30;
43         W = 11.2;
44         equation used:  ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
45 */
46
47 vec3 uncharted2Tonemap(vec3 x)
48 {
49         return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
50 }
51
52 vec4 applyToneMapping(vec4 color)
53 {
54         color = vec4(pow(color.rgb, vec3(2.2)), color.a);
55         const float gamma = 1.6;
56         const float exposureBias = 5.5;
57         color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
58         // Precalculated white_scale from 
59         //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
60         vec3 whiteScale = vec3(1.036015346);
61         color.rgb *= whiteScale;
62         return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
63 }
64 #endif
65
66 void get_texture_flags()
67 {
68         vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
69         if (flags.r > 0.5) {
70                 normalTexturePresent = true;
71         }
72 }
73
74 vec4 get_normal_map(vec2 uv)
75 {
76         vec4 bump = texture2D(normalTexture, uv).rgba;
77         bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
78         return bump;
79 }
80
81 float find_intersection(vec2 dp, vec2 ds)
82 {
83         float depth = 1.0;
84         float best_depth = 0.0;
85         float size = 0.0625;
86         for (int i = 0; i < 15; i++) {
87                 depth -= size;
88                 float h = texture2D(normalTexture, dp + ds * depth).a;
89                 if (depth <= h) {
90                         best_depth = depth;
91                         break;
92                 }
93         }
94         depth = best_depth;
95         for (int i = 0; i < 4; i++) {
96                 size *= 0.5;
97                 float h = texture2D(normalTexture,dp + ds * depth).a;
98                 if (depth <= h) {
99                         best_depth = depth;
100                         depth += size;
101                 } else {
102                         depth -= size;
103                 }
104         }
105         return best_depth;
106 }
107
108 void main(void)
109 {
110         vec3 color;
111         vec4 bump;
112         vec2 uv = gl_TexCoord[0].st;
113         bool use_normalmap = false;
114         get_texture_flags();
115
116 #ifdef ENABLE_PARALLAX_OCCLUSION
117         vec2 eyeRay = vec2 (tsEyeVec.x, -tsEyeVec.y);
118         const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
119         const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
120
121 #if PARALLAX_OCCLUSION_MODE == 0
122         // Parallax occlusion with slope information
123         if (normalTexturePresent && area_enable_parallax > 0.0) {
124                 for (int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
125                         vec4 normal = texture2D(normalTexture, uv.xy);
126                         float h = normal.a * scale - bias;
127                         uv += h * normal.z * eyeRay;
128                 }
129         }
130 #endif
131 #if PARALLAX_OCCLUSION_MODE == 1
132         // Relief mapping
133         if (normalTexturePresent && area_enable_parallax > 0.0) {
134                 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
135                 float dist = find_intersection(uv, ds);
136                 uv += dist * ds;
137         }
138 #endif
139 #endif
140
141 #if USE_NORMALMAPS == 1
142         if (normalTexturePresent) {
143                 bump = get_normal_map(uv);
144                 use_normalmap = true;
145         }
146 #endif
147
148         vec4 base = texture2D(baseTexture, uv).rgba;
149
150 #ifdef USE_DISCARD
151         // If alpha is zero, we can just discard the pixel. This fixes transparency
152         // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa.
153         if (base.a == 0.0) {
154                 discard;
155         }
156 #endif
157
158 #ifdef ENABLE_BUMPMAPPING
159         if (use_normalmap) {
160                 vec3 L = normalize(lightVec);
161                 vec3 E = normalize(eyeVec);
162                 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
163                 float diffuse = dot(-E,bump.xyz);
164                 color = (diffuse + 0.1 * specular) * base.rgb;
165         } else {
166                 color = base.rgb;
167         }
168 #else
169         color = base.rgb;
170 #endif
171
172         vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0); 
173         
174 #ifdef ENABLE_TONE_MAPPING
175         col = applyToneMapping(col);
176 #endif
177
178         // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
179         // the fog will only be rendered correctly if the last operation before the
180         // clamp() is an addition. Else, the clamp() seems to be ignored.
181         // E.g. the following won't work:
182         //      float clarity = clamp(fogShadingParameter
183         //              * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
184         // As additions usually come for free following a multiplication, the new formula
185         // should be more efficient as well.
186         // Note: clarity = (1 - fogginess)
187         float clarity = clamp(fogShadingParameter
188                 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
189         col = mix(skyBgColor, col, clarity);
190         col = vec4(col.rgb, base.a);
191
192         gl_FragColor = col;
193 }