Waves generated with Perlin-type noise #8994

This commit is contained in:
Lars Hofhansl 2019-11-19 19:42:52 -08:00
parent b3c245bb46
commit 60bff1e6cb
2 changed files with 64 additions and 7 deletions

@ -46,6 +46,43 @@ float smoothTriangleWave(float x)
return smoothCurve(triangleWave(x)) * 2.0 - 1.0; return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
} }
#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
//
// Simple, fast noise function.
// See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
//
vec4 perm(vec4 x)
{
return mod(((x * 34.0) + 1.0) * x, 289.0);
}
float snoise(vec3 p)
{
vec3 a = floor(p);
vec3 d = p - a;
d = d * d * (3.0 - 2.0 * d);
vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
vec4 k1 = perm(b.xyxy);
vec4 k2 = perm(k1.xyxy + b.zzww);
vec4 c = k2 + a.zzzz;
vec4 k3 = perm(c);
vec4 k4 = perm(c + 1.0);
vec4 o1 = fract(k3 * (1.0 / 41.0));
vec4 o2 = fract(k4 * (1.0 / 41.0));
vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
return o4.y * d.y + o4.x * (1.0 - d.y);
}
#endif
void main(void) void main(void)
{ {
@ -65,7 +102,8 @@ void main(void)
float disp_x; float disp_x;
float disp_z; float disp_z;
#if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS) #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || \
(MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
vec4 pos2 = mWorld * gl_Vertex; vec4 pos2 = mWorld * gl_Vertex;
float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002; float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) + disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
@ -75,12 +113,22 @@ float disp_z;
smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5; smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5;
#endif #endif
worldPosition = (mWorld * gl_Vertex).xyz;
#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 #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
// Generate waves with Perlin-type noise.
// The constants are calibrated such that they roughly
// correspond to the old sine waves.
vec4 pos = gl_Vertex; vec4 pos = gl_Vertex;
pos.y -= 2.0; vec3 wavePos = worldPosition + cameraOffset;
float posYbuf = (pos.z / WATER_WAVE_LENGTH + animationTimer * WATER_WAVE_SPEED * WATER_WAVE_LENGTH); // The waves are slightly compressed along the z-axis to get
pos.y -= sin(posYbuf) * WATER_WAVE_HEIGHT + sin(posYbuf / 7.0) * WATER_WAVE_HEIGHT; // wave-fronts along the x-axis.
wavePos.x /= WATER_WAVE_LENGTH * 3;
wavePos.z /= WATER_WAVE_LENGTH * 2;
wavePos.z += animationTimer * WATER_WAVE_SPEED * 10;
pos.y += (snoise(wavePos) - 1) * WATER_WAVE_HEIGHT * 5;
gl_Position = mWorldViewProj * pos; gl_Position = mWorldViewProj * pos;
#elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
vec4 pos = gl_Vertex; vec4 pos = gl_Vertex;
@ -101,7 +149,6 @@ float disp_z;
vPosition = gl_Position.xyz; vPosition = gl_Position.xyz;
worldPosition = (mWorld * gl_Vertex).xyz;
// Don't generate heightmaps when too far from the eye // Don't generate heightmaps when too far from the eye
float dist = distance (vec3(0.0, 0.0, 0.0), vPosition); float dist = distance (vec3(0.0, 0.0, 0.0), vPosition);

@ -795,6 +795,7 @@ static void getTileInfo(
v3s16 &p_corrected, v3s16 &p_corrected,
v3s16 &face_dir_corrected, v3s16 &face_dir_corrected,
u16 *lights, u16 *lights,
u8 &waving,
TileSpec &tile TileSpec &tile
) )
{ {
@ -842,6 +843,7 @@ static void getTileInfo(
getNodeTile(n, p_corrected, face_dir_corrected, data, tile); getNodeTile(n, p_corrected, face_dir_corrected, data, tile);
const ContentFeatures &f = ndef->get(n); const ContentFeatures &f = ndef->get(n);
waving = f.waving;
tile.emissive_light = f.light_source; tile.emissive_light = f.light_source;
// eg. water and glass // eg. water and glass
@ -876,6 +878,10 @@ static void updateFastFaceRow(
const v3s16 &&face_dir, const v3s16 &&face_dir,
std::vector<FastFace> &dest) std::vector<FastFace> &dest)
{ {
static thread_local const bool waving_liquids =
g_settings->getBool("enable_shaders") &&
g_settings->getBool("enable_waving_water");
v3s16 p = startpos; v3s16 p = startpos;
u16 continuous_tiles_count = 1; u16 continuous_tiles_count = 1;
@ -884,10 +890,11 @@ static void updateFastFaceRow(
v3s16 p_corrected; v3s16 p_corrected;
v3s16 face_dir_corrected; v3s16 face_dir_corrected;
u16 lights[4] = {0, 0, 0, 0}; u16 lights[4] = {0, 0, 0, 0};
u8 waving;
TileSpec tile; TileSpec tile;
getTileInfo(data, p, face_dir, getTileInfo(data, p, face_dir,
makes_face, p_corrected, face_dir_corrected, makes_face, p_corrected, face_dir_corrected,
lights, tile); lights, waving, tile);
// Unroll this variable which has a significant build cost // Unroll this variable which has a significant build cost
TileSpec next_tile; TileSpec next_tile;
@ -910,12 +917,15 @@ static void updateFastFaceRow(
getTileInfo(data, p_next, face_dir, getTileInfo(data, p_next, face_dir,
next_makes_face, next_p_corrected, next_makes_face, next_p_corrected,
next_face_dir_corrected, next_lights, next_face_dir_corrected, next_lights,
waving,
next_tile); next_tile);
if (next_makes_face == makes_face if (next_makes_face == makes_face
&& next_p_corrected == p_corrected + translate_dir && next_p_corrected == p_corrected + translate_dir
&& next_face_dir_corrected == face_dir_corrected && next_face_dir_corrected == face_dir_corrected
&& memcmp(next_lights, lights, ARRLEN(lights) * sizeof(u16)) == 0 && memcmp(next_lights, lights, ARRLEN(lights) * sizeof(u16)) == 0
// Don't apply fast faces to waving water.
&& (waving != 3 || !waving_liquids)
&& next_tile.isTileable(tile)) { && next_tile.isTileable(tile)) {
next_is_different = false; next_is_different = false;
continuous_tiles_count++; continuous_tiles_count++;