Skip to content

Instantly share code, notes, and snippets.

@yorung
Last active October 30, 2018 11:06
Show Gist options
  • Save yorung/fea04183826faf798c8c22caa16f097f to your computer and use it in GitHub Desktop.
Save yorung/fea04183826faf798c8c22caa16f097f to your computer and use it in GitHub Desktop.
Gerstner wave implementation in HLSL based on GPU Gems
struct Wave
{
float2 dir;
float amplitude;
float waveLength;
};
cbuffer cb2 : register(b2)
{
Wave waves[100];
};
static int numWaves = 20;
static float steepness = 0.8;
static float speed = 15;
float3 CalcGerstnerWaveOffset(float3 v)
{
float3 sum = float3(0, 0, 0);
[unroll]
for (int i = 0; i < numWaves; i++)
{
Wave wave = waves[i];
float wi = 2 / wave.waveLength;
float Qi = steepness / (wave.amplitude * wi * numWaves);
float phi = speed * wi;
float rad = dot(wave.dir, v.xz) * wi + g_time * phi;
sum.y += sin(rad) * wave.amplitude;
sum.xz += cos(rad) * wave.amplitude * Qi * wave.dir;
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment