]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/nodes_shader/opengl_vertex.glsl
Translated using Weblate (Ukrainian)
[dragonfireclient.git] / client / shaders / nodes_shader / opengl_vertex.glsl
1 uniform mat4 mWorldViewProj;
2 uniform mat4 mWorld;
3
4 // Color of the light emitted by the sun.
5 uniform vec3 dayLight;
6 uniform vec3 eyePosition;
7
8 // The cameraOffset is the current center of the visible world.
9 uniform vec3 cameraOffset;
10 uniform float animationTimer;
11
12 varying vec3 vPosition;
13 // World position in the visible world (i.e. relative to the cameraOffset.)
14 // This can be used for many shader effects without loss of precision.
15 // If the absolute position is required it can be calculated with
16 // cameraOffset + worldPosition (for large coordinates the limits of float
17 // precision must be considered).
18 varying vec3 worldPosition;
19
20 varying vec3 eyeVec;
21 varying vec3 lightVec;
22 varying vec3 tsEyeVec;
23 varying vec3 tsLightVec;
24 varying float area_enable_parallax;
25
26 // Color of the light emitted by the light sources.
27 const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
28 const float e = 2.718281828459;
29 const float BS = 10.0;
30
31
32 float smoothCurve(float x)
33 {
34         return x * x * (3.0 - 2.0 * x);
35 }
36
37
38 float triangleWave(float x)
39 {
40         return abs(fract(x + 0.5) * 2.0 - 1.0);
41 }
42
43
44 float smoothTriangleWave(float x)
45 {
46         return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
47 }
48
49 // OpenGL < 4.3 does not support continued preprocessor lines
50 #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC) && ENABLE_WAVING_WATER
51
52 //
53 // Simple, fast noise function.
54 // See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
55 //
56 vec4 perm(vec4 x)
57 {
58         return mod(((x * 34.0) + 1.0) * x, 289.0);
59 }
60
61 float snoise(vec3 p)
62 {
63         vec3 a = floor(p);
64         vec3 d = p - a;
65         d = d * d * (3.0 - 2.0 * d);
66
67         vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
68         vec4 k1 = perm(b.xyxy);
69         vec4 k2 = perm(k1.xyxy + b.zzww);
70
71         vec4 c = k2 + a.zzzz;
72         vec4 k3 = perm(c);
73         vec4 k4 = perm(c + 1.0);
74
75         vec4 o1 = fract(k3 * (1.0 / 41.0));
76         vec4 o2 = fract(k4 * (1.0 / 41.0));
77
78         vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
79         vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
80
81         return o4.y * d.y + o4.x * (1.0 - d.y);
82 }
83
84 #endif
85
86 void main(void)
87 {
88         gl_TexCoord[0] = gl_MultiTexCoord0;
89         //TODO: make offset depending on view angle and parallax uv displacement
90         //thats for textures that doesnt align vertically, like dirt with grass
91         //gl_TexCoord[0].y += 0.008;
92
93         //Allow parallax/relief mapping only for certain kind of nodes
94         //Variable is also used to control area of the effect
95 #if (DRAW_TYPE == NDT_NORMAL || DRAW_TYPE == NDT_LIQUID || DRAW_TYPE == NDT_FLOWINGLIQUID)
96         area_enable_parallax = 1.0;
97 #else
98         area_enable_parallax = 0.0;
99 #endif
100
101
102 float disp_x;
103 float disp_z;
104 // OpenGL < 4.3 does not support continued preprocessor lines
105 #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
106         vec4 pos2 = mWorld * gl_Vertex;
107         float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
108         disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
109                 smoothTriangleWave(animationTimer * 11.0 + tOffset)) * 0.4;
110         disp_z = (smoothTriangleWave(animationTimer * 31.0 + tOffset) +
111                 smoothTriangleWave(animationTimer * 29.0 + tOffset) +
112                 smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5;
113 #endif
114
115         worldPosition = (mWorld * gl_Vertex).xyz;
116
117 // OpenGL < 4.3 does not support continued preprocessor lines
118 #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC) && ENABLE_WAVING_WATER
119         // Generate waves with Perlin-type noise.
120         // The constants are calibrated such that they roughly
121         // correspond to the old sine waves.
122         vec4 pos = gl_Vertex;
123         vec3 wavePos = worldPosition + cameraOffset;
124         // The waves are slightly compressed along the z-axis to get
125         // wave-fronts along the x-axis.
126         wavePos.x /= WATER_WAVE_LENGTH * 3;
127         wavePos.z /= WATER_WAVE_LENGTH * 2;
128         wavePos.z += animationTimer * WATER_WAVE_SPEED * 10;
129         pos.y += (snoise(wavePos) - 1) * WATER_WAVE_HEIGHT * 5;
130         gl_Position = mWorldViewProj * pos;
131 #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
132         vec4 pos = gl_Vertex;
133         pos.x += disp_x;
134         pos.y += disp_z * 0.1;
135         pos.z += disp_z;
136         gl_Position = mWorldViewProj * pos;
137 #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
138         vec4 pos = gl_Vertex;
139         if (gl_TexCoord[0].y < 0.05) {
140                 pos.x += disp_x;
141                 pos.z += disp_z;
142         }
143         gl_Position = mWorldViewProj * pos;
144 #else
145         gl_Position = mWorldViewProj * gl_Vertex;
146 #endif
147
148
149         vPosition = gl_Position.xyz;
150
151         // Don't generate heightmaps when too far from the eye
152         float dist = distance (vec3(0.0, 0.0, 0.0), vPosition);
153         if (dist > 150.0) {
154                 area_enable_parallax = 0.0;
155         }
156
157         vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
158
159         vec3 normal, tangent, binormal;
160         normal = normalize(gl_NormalMatrix * gl_Normal);
161         tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
162         binormal = normalize(gl_NormalMatrix * gl_MultiTexCoord2.xyz);
163
164         vec3 v;
165
166         lightVec = sunPosition - worldPosition;
167         v.x = dot(lightVec, tangent);
168         v.y = dot(lightVec, binormal);
169         v.z = dot(lightVec, normal);
170         tsLightVec = normalize (v);
171
172         eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;
173         v.x = dot(eyeVec, tangent);
174         v.y = dot(eyeVec, binormal);
175         v.z = dot(eyeVec, normal);
176         tsEyeVec = normalize (v);
177
178         // Calculate color.
179         // Red, green and blue components are pre-multiplied with
180         // the brightness, so now we have to multiply these
181         // colors with the color of the incoming light.
182         // The pre-baked colors are halved to prevent overflow.
183         vec4 color;
184         // The alpha gives the ratio of sunlight in the incoming light.
185         float nightRatio = 1 - gl_Color.a;
186         color.rgb = gl_Color.rgb * (gl_Color.a * dayLight.rgb + 
187                 nightRatio * artificialLight.rgb) * 2;
188         color.a = 1;
189
190         // Emphase blue a bit in darker places
191         // See C++ implementation in mapblock_mesh.cpp final_color_blend()
192         float brightness = (color.r + color.g + color.b) / 3;
193         color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
194                 0.07 * brightness);
195
196         gl_FrontColor = gl_BackColor = clamp(color, 0.0, 1.0);
197 }