1 uniform mat4 mWorldViewProj;
4 // Color of the light emitted by the sun.
6 uniform vec3 eyePosition;
8 // The cameraOffset is the current center of the visible world.
9 uniform vec3 cameraOffset;
10 uniform float animationTimer;
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;
21 varying vec3 lightVec;
22 varying vec3 tsEyeVec;
23 varying vec3 tsLightVec;
24 varying float area_enable_parallax;
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;
32 float smoothCurve(float x)
34 return x * x * (3.0 - 2.0 * x);
38 float triangleWave(float x)
40 return abs(fract(x + 0.5) * 2.0 - 1.0);
44 float smoothTriangleWave(float x)
46 return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
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
53 // Simple, fast noise function.
54 // See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
58 return mod(((x * 34.0) + 1.0) * x, 289.0);
65 d = d * d * (3.0 - 2.0 * d);
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);
73 vec4 k4 = perm(c + 1.0);
75 vec4 o1 = fract(k3 * (1.0 / 41.0));
76 vec4 o2 = fract(k4 * (1.0 / 41.0));
78 vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
79 vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
81 return o4.y * d.y + o4.x * (1.0 - d.y);
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;
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;
98 area_enable_parallax = 0.0;
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;
115 worldPosition = (mWorld * gl_Vertex).xyz;
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;
134 pos.y += disp_z * 0.1;
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) {
143 gl_Position = mWorldViewProj * pos;
145 gl_Position = mWorldViewProj * gl_Vertex;
149 vPosition = gl_Position.xyz;
151 // Don't generate heightmaps when too far from the eye
152 float dist = distance (vec3(0.0, 0.0, 0.0), vPosition);
154 area_enable_parallax = 0.0;
157 vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
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);
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);
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);
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.
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;
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) +
196 gl_FrontColor = gl_BackColor = clamp(color, 0.0, 1.0);