1 uniform mat4 mWorldViewProj;
2 uniform mat4 mInvWorld;
3 uniform mat4 mTransWorld;
6 uniform float dayNightRatio;
7 uniform vec3 eyePosition;
8 uniform float animationTimer;
10 varying vec3 vPosition;
11 varying vec3 worldPosition;
14 varying vec3 lightVec;
15 varying vec3 tsEyeVec;
16 varying vec3 tsLightVec;
18 const float e = 2.718281828459;
19 const float BS = 10.0;
21 float smoothCurve(float x)
23 return x * x * (3.0 - 2.0 * x);
25 float triangleWave(float x)
27 return abs(fract( x + 0.5 ) * 2.0 - 1.0);
29 float smoothTriangleWave(float x)
31 return smoothCurve(triangleWave( x )) * 2.0 - 1.0;
36 gl_TexCoord[0] = gl_MultiTexCoord0;
38 #if (MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) && ENABLE_WAVING_WATER
42 float posYbuf = (pos.z / WATER_WAVE_LENGTH + animationTimer * WATER_WAVE_SPEED * WATER_WAVE_LENGTH);
44 pos.y -= sin(posYbuf) * WATER_WAVE_HEIGHT + sin(posYbuf / 7.0) * WATER_WAVE_HEIGHT;
45 gl_Position = mWorldViewProj * pos;
46 #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
48 vec4 pos2 = mWorld * gl_Vertex;
50 * Mathematic optimization: pos2.x * A + pos2.z * A (2 multiplications + 1 addition)
51 * replaced with: (pos2.x + pos2.z) * A (1 addition + 1 multiplication)
52 * And bufferize calcul to a float
54 float pos2XpZ = pos2.x + pos2.z;
55 pos.x += (smoothTriangleWave(animationTimer*10.0 + pos2XpZ * 0.01) * 2.0 - 1.0) * 0.4;
56 pos.y += (smoothTriangleWave(animationTimer*15.0 + pos2XpZ * -0.01) * 2.0 - 1.0) * 0.2;
57 pos.z += (smoothTriangleWave(animationTimer*10.0 + pos2XpZ * -0.01) * 2.0 - 1.0) * 0.4;
58 gl_Position = mWorldViewProj * pos;
59 #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
61 vec4 pos2 = mWorld * gl_Vertex;
62 if (gl_TexCoord[0].y < 0.05) {
64 * Mathematic optimization: pos2.x * A + pos2.z * A (2 multiplications + 1 addition)
65 * replaced with: (pos2.x + pos2.z) * A (1 addition + 1 multiplication)
66 * And bufferize calcul to a float
68 float pos2XpZ = pos2.x + pos2.z;
69 pos.x += (smoothTriangleWave(animationTimer * 20.0 + pos2XpZ * 0.1) * 2.0 - 1.0) * 0.8;
70 pos.y -= (smoothTriangleWave(animationTimer * 10.0 + pos2XpZ * -0.5) * 2.0 - 1.0) * 0.4;
72 gl_Position = mWorldViewProj * pos;
74 gl_Position = mWorldViewProj * gl_Vertex;
77 vPosition = gl_Position.xyz;
78 worldPosition = (mWorld * gl_Vertex).xyz;
79 vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
81 vec3 normal, tangent, binormal;
82 normal = normalize(gl_NormalMatrix * gl_Normal);
83 if (gl_Normal.x > 0.5) {
85 tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, -1.0));
86 binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
87 } else if (gl_Normal.x < -0.5) {
89 tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
90 binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
91 } else if (gl_Normal.y > 0.5) {
93 tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
94 binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
95 } else if (gl_Normal.y < -0.5) {
97 tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
98 binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0));
99 } else if (gl_Normal.z > 0.5) {
101 tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0));
102 binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
103 } else if (gl_Normal.z < -0.5) {
105 tangent = normalize(gl_NormalMatrix * vec3(-1.0, 0.0, 0.0));
106 binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0));
108 mat3 tbnMatrix = mat3(tangent.x, binormal.x, normal.x,
109 tangent.y, binormal.y, normal.y,
110 tangent.z, binormal.z, normal.z);
112 lightVec = sunPosition - worldPosition;
113 tsLightVec = lightVec * tbnMatrix;
114 eyeVec = (gl_ModelViewMatrix * gl_Vertex).xyz;
115 tsEyeVec = eyeVec * tbnMatrix;
118 float day = gl_Color.r;
119 float night = gl_Color.g;
120 float light_source = gl_Color.b;
122 float rg = mix(night, day, dayNightRatio);
123 rg += light_source * 2.5; // Make light sources brighter
127 b += (day - night) / 13.0;
128 rg -= (day - night) / 23.0;
130 // Emphase blue a bit in darker places
131 // See C++ implementation in mapblock_mesh.cpp finalColorBlend()
132 b += max(0.0, (1.0 - abs(b - 0.13)/0.17) * 0.025);
134 // Artificial light is yellow-ish
135 // See C++ implementation in mapblock_mesh.cpp finalColorBlend()
136 rg += max(0.0, (1.0 - abs(rg - 0.85)/0.15) * 0.065);
142 color.a = gl_Color.a;
143 gl_FrontColor = gl_BackColor = clamp(color,0.0,1.0);