]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/nodes_shader/opengl_fragment.glsl
7213612bd25d21c3dc30fcb3269f78c96d6da8a0
[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 float intensity(vec3 color)
75 {
76         return (color.r + color.g + color.b) / 3.0;
77 }
78
79 float get_rgb_height(vec2 uv)
80 {
81         return intensity(texture2D(baseTexture, uv).rgb);
82 }
83
84 vec4 get_normal_map(vec2 uv)
85 {
86         vec4 bump = texture2D(normalTexture, uv).rgba;
87         bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
88         return bump;
89 }
90
91 float find_intersection(vec2 dp, vec2 ds)
92 {
93         float depth = 1.0;
94         float best_depth = 0.0;
95         float size = 0.0625;
96         for (int i = 0; i < 15; i++) {
97                 depth -= size;
98                 float h = texture2D(normalTexture, dp + ds * depth).a;
99                 if (depth <= h) {
100                         best_depth = depth;
101                         break;
102                 }
103         }
104         depth = best_depth;
105         for (int i = 0; i < 4; i++) {
106                 size *= 0.5;
107                 float h = texture2D(normalTexture,dp + ds * depth).a;
108                 if (depth <= h) {
109                         best_depth = depth;
110                         depth += size;
111                 } else {
112                         depth -= size;
113                 }
114         }
115         return best_depth;
116 }
117
118 float find_intersectionRGB(vec2 dp, vec2 ds)
119 {
120         const float depth_step = 1.0 / 24.0;
121         float depth = 1.0;
122         for (int i = 0 ; i < 24 ; i++) {
123                 float h = get_rgb_height(dp + ds * depth);
124                 if (h >= depth)
125                         break;
126                 depth -= depth_step;
127         }
128         return depth;
129 }
130
131 void main(void)
132 {
133         vec3 color;
134         vec4 bump;
135         vec2 uv = gl_TexCoord[0].st;
136         bool use_normalmap = false;
137         get_texture_flags();
138
139 #ifdef ENABLE_PARALLAX_OCCLUSION
140         vec2 eyeRay = vec2 (tsEyeVec.x, -tsEyeVec.y);
141         const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
142         const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
143
144 #if PARALLAX_OCCLUSION_MODE == 0
145         // Parallax occlusion with slope information
146         if (normalTexturePresent && area_enable_parallax > 0.0) {
147                 for (int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
148                         vec4 normal = texture2D(normalTexture, uv.xy);
149                         float h = normal.a * scale - bias;
150                         uv += h * normal.z * eyeRay;
151                 }
152 #endif
153
154 #if PARALLAX_OCCLUSION_MODE == 1
155         // Relief mapping
156         if (normalTexturePresent && area_enable_parallax > 0.0) {
157                 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
158                 float dist = find_intersection(uv, ds);
159                 uv += dist * ds;
160 #endif
161         } else if (GENERATE_NORMALMAPS == 1 && area_enable_parallax > 0.0) {
162                 vec2 ds = eyeRay * PARALLAX_OCCLUSION_SCALE;
163                 float dist = find_intersectionRGB(uv, ds);
164                 uv += dist * ds;
165         }
166 #endif
167
168 #if USE_NORMALMAPS == 1
169         if (normalTexturePresent) {
170                 bump = get_normal_map(uv);
171                 use_normalmap = true;
172         }
173 #endif
174
175 #if GENERATE_NORMALMAPS == 1
176         if (normalTexturePresent == false) {
177                 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
178                 float t  = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
179                 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
180                 float r  = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
181                 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
182                 float b  = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
183                 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
184                 float l  = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
185                 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
186                 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
187                 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
188                 use_normalmap = true;
189         }
190 #endif
191         vec4 base = texture2D(baseTexture, uv).rgba;
192
193 #ifdef USE_DISCARD
194         // If alpha is zero, we can just discard the pixel. This fixes transparency
195         // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa.
196         if (base.a == 0.0) {
197                 discard;
198         }
199 #endif
200
201 #ifdef ENABLE_BUMPMAPPING
202         if (use_normalmap) {
203                 vec3 L = normalize(lightVec);
204                 vec3 E = normalize(eyeVec);
205                 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
206                 float diffuse = dot(-E,bump.xyz);
207                 color = (diffuse + 0.1 * specular) * base.rgb;
208         } else {
209                 color = base.rgb;
210         }
211 #else
212         color = base.rgb;
213 #endif
214
215         vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0); 
216         
217 #ifdef ENABLE_TONE_MAPPING
218         col = applyToneMapping(col);
219 #endif
220
221         // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
222         // the fog will only be rendered correctly if the last operation before the
223         // clamp() is an addition. Else, the clamp() seems to be ignored.
224         // E.g. the following won't work:
225         //      float clarity = clamp(fogShadingParameter
226         //              * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
227         // As additions usually come for free following a multiplication, the new formula
228         // should be more efficient as well.
229         // Note: clarity = (1 - fogginess)
230         float clarity = clamp(fogShadingParameter
231                 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
232         col = mix(skyBgColor, col, clarity);
233         col = vec4(col.rgb, base.a);
234
235         gl_FragColor = col;
236 }