Last active
August 9, 2024 01:57
-
-
Save yorung/fea04183826faf798c8c22caa16f097f to your computer and use it in GitHub Desktop.
Gerstner wave implementation in HLSL based on GPU Gems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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