@@ -46,6 +46,43 @@ float smoothTriangleWave(float x)
46
46
return smoothCurve(triangleWave(x)) * 2.0 - 1.0 ;
47
47
}
48
48
49
+ #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || \
50
+ MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || \
51
+ MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC) && ENABLE_WAVING_WATER
52
+
53
+ //
54
+ // Simple, fast noise function.
55
+ // See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
56
+ //
57
+ vec4 perm(vec4 x)
58
+ {
59
+ return mod (((x * 34.0 ) + 1.0 ) * x, 289.0 );
60
+ }
61
+
62
+ float snoise(vec3 p)
63
+ {
64
+ vec3 a = floor (p);
65
+ vec3 d = p - a;
66
+ d = d * d * (3.0 - 2.0 * d);
67
+
68
+ vec4 b = a.xxyy + vec4 (0.0 , 1.0 , 0.0 , 1.0 );
69
+ vec4 k1 = perm(b.xyxy);
70
+ vec4 k2 = perm(k1.xyxy + b.zzww);
71
+
72
+ vec4 c = k2 + a.zzzz;
73
+ vec4 k3 = perm(c);
74
+ vec4 k4 = perm(c + 1.0 );
75
+
76
+ vec4 o1 = fract (k3 * (1.0 / 41.0 ));
77
+ vec4 o2 = fract (k4 * (1.0 / 41.0 ));
78
+
79
+ vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
80
+ vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
81
+
82
+ return o4.y * d.y + o4.x * (1.0 - d.y);
83
+ }
84
+
85
+ #endif
49
86
50
87
void main(void )
51
88
{
@@ -65,7 +102,8 @@ void main(void)
65
102
66
103
float disp_x;
67
104
float disp_z;
68
- #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
105
+ #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || \
106
+ (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
69
107
vec4 pos2 = mWorld * gl_Vertex ;
70
108
float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002 ;
71
109
disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
@@ -75,12 +113,22 @@ float disp_z;
75
113
smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5 ;
76
114
#endif
77
115
116
+ worldPosition = (mWorld * gl_Vertex ).xyz;
78
117
79
- #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
118
+ #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || \
119
+ MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || \
120
+ MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC) && ENABLE_WAVING_WATER
121
+ // Generate waves with Perlin-type noise.
122
+ // The constants are calibrated such that they roughly
123
+ // correspond to the old sine waves.
80
124
vec4 pos = gl_Vertex ;
81
- pos.y -= 2.0 ;
82
- float posYbuf = (pos.z / WATER_WAVE_LENGTH + animationTimer * WATER_WAVE_SPEED * WATER_WAVE_LENGTH);
83
- pos.y -= sin (posYbuf) * WATER_WAVE_HEIGHT + sin (posYbuf / 7.0 ) * WATER_WAVE_HEIGHT;
125
+ vec3 wavePos = worldPosition + cameraOffset;
126
+ // The waves are slightly compressed along the z-axis to get
127
+ // wave-fronts along the x-axis.
128
+ wavePos.x /= WATER_WAVE_LENGTH * 3 ;
129
+ wavePos.z /= WATER_WAVE_LENGTH * 2 ;
130
+ wavePos.z += animationTimer * WATER_WAVE_SPEED * 10 ;
131
+ pos.y += (snoise(wavePos) - 1 ) * WATER_WAVE_HEIGHT * 5 ;
84
132
gl_Position = mWorldViewProj * pos;
85
133
#elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
86
134
vec4 pos = gl_Vertex ;
@@ -101,7 +149,6 @@ float disp_z;
101
149
102
150
103
151
vPosition = gl_Position .xyz;
104
- worldPosition = (mWorld * gl_Vertex ).xyz;
105
152
106
153
// Don't generate heightmaps when too far from the eye
107
154
float dist = distance (vec3 (0.0 , 0.0 , 0.0 ), vPosition);
0 commit comments