]> git.lizzy.rs Git - minetest.git/blob - client/shaders/water_surface_shader/opengl_fragment.glsl
Minimal: Remove recently added unnecessary nodes
[minetest.git] / client / shaders / water_surface_shader / opengl_fragment.glsl
1 uniform sampler2D baseTexture;
2 uniform sampler2D normalTexture;
3 uniform sampler2D useNormalmap;
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 tsEyeVec;
14 varying vec3 lightVec;
15 varying vec3 tsLightVec;
16
17 bool normalTexturePresent = false;
18
19 const float e = 2.718281828459;
20 const float BS = 10.0;
21  
22 float intensity (vec3 color){
23         return (color.r + color.g + color.b) / 3.0;
24 }
25
26 float get_rgb_height (vec2 uv){
27         return intensity(texture2D(baseTexture,uv).rgb);
28 }
29
30 vec4 get_normal_map(vec2 uv){
31         vec4 bump = texture2D(normalTexture, uv).rgba;
32         bump.xyz = normalize(bump.xyz * 2.0 -1.0);
33         bump.y = -bump.y;
34         return bump;
35 }
36
37 void main (void)
38 {
39         vec3 color;
40         vec4 bump;
41         vec2 uv = gl_TexCoord[0].st;
42         bool use_normalmap = false;
43
44 #ifdef USE_NORMALMAPS
45         if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0) {
46                 normalTexturePresent = true;
47         }
48 #endif
49
50 #ifdef ENABLE_PARALLAX_OCCLUSION
51         if (normalTexturePresent) {
52                 vec3 tsEye = normalize(tsEyeVec);
53                 float height = PARALLAX_OCCLUSION_SCALE * texture2D(normalTexture, uv).a - PARALLAX_OCCLUSION_BIAS;
54                 uv = uv + texture2D(normalTexture, uv).z * height * vec2(tsEye.x,-tsEye.y);
55         }
56 #endif
57
58 #ifdef USE_NORMALMAPS
59         if (normalTexturePresent) {
60                 bump = get_normal_map(uv);
61                 use_normalmap = true;
62         } 
63 #endif
64
65         if (GENERATE_NORMALMAPS == 1 && use_normalmap == false) {
66                 float tl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y+SAMPLE_STEP));
67                 float t  = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
68                 float tr = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y+SAMPLE_STEP));
69                 float r  = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y));
70                 float br = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y-SAMPLE_STEP));
71                 float b  = get_rgb_height (vec2(uv.x,uv.y-SAMPLE_STEP));
72                 float bl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP));
73                 float l  = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y));
74                 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
75                 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
76                 bump = vec4 (normalize(vec3 (dX, -dY, NORMALMAPS_STRENGTH)),1.0);
77                 use_normalmap = true;
78         }
79
80 vec4 base = texture2D(baseTexture, uv).rgba;
81
82 #ifdef ENABLE_BUMPMAPPING
83         if (use_normalmap) {
84                 vec3 L = normalize(lightVec);
85                 vec3 E = normalize(eyeVec);
86                 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5);
87                 float diffuse = dot(E,bump.xyz);
88                 /* Mathematic optimization
89                 * Original: color = 0.05*base.rgb + diffuse*base.rgb + 0.2*specular*base.rgb;
90                 * This optimization save 2 multiplications (orig: 4 multiplications + 3 additions
91                 * end: 2 multiplications + 3 additions)
92                 */
93                 color = (0.05 + diffuse + 0.2 * specular) * base.rgb;
94         } else {
95                 color = base.rgb;
96         }
97 #else
98         color = base.rgb;
99 #endif
100
101 #if MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE
102         float alpha = gl_Color.a;
103         vec4 col = vec4(color.rgb, alpha);
104         col *= gl_Color;
105         if(fogDistance != 0.0){
106                 float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
107                 alpha = mix(alpha, 0.0, d);
108         }
109         gl_FragColor = vec4(col.rgb, alpha);
110 #else
111         vec4 col = vec4(color.rgb, base.a);
112         col *= gl_Color;
113         if(fogDistance != 0.0){
114                 float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
115                 col = mix(col, skyBgColor, d);
116         }
117         gl_FragColor = vec4(col.rgb, base.a);
118 #endif
119 }