From b8b51dd41e6c1516f398e0480523732c1e708ac1 Mon Sep 17 00:00:00 2001 From: apistol78 Date: Tue, 21 May 2024 16:54:51 +0200 Subject: [PATCH] Traktor: Begun implementing FFT based ocean simulation. --- data/Source/System/Shaders/Modules/Hash.xdi | 11 ++ .../System/Terrain/Ocean/Shaders/Wave/FFT.xdi | 150 ++++++++++++++++++ .../Terrain/Ocean/Shaders/Wave/Wave.xdi | 23 ++- 3 files changed, 171 insertions(+), 13 deletions(-) diff --git a/data/Source/System/Shaders/Modules/Hash.xdi b/data/Source/System/Shaders/Modules/Hash.xdi index 75f8d14015..9b495c79cd 100644 --- a/data/Source/System/Shaders/Modules/Hash.xdi +++ b/data/Source/System/Shaders/Modules/Hash.xdi @@ -4,6 +4,16 @@ diff --git a/data/Source/System/Terrain/Ocean/Shaders/Wave/FFT.xdi b/data/Source/System/Terrain/Ocean/Shaders/Wave/FFT.xdi index e54cbf6afc..06872e13dc 100644 --- a/data/Source/System/Terrain/Ocean/Shaders/Wave/FFT.xdi +++ b/data/Source/System/Terrain/Ocean/Shaders/Wave/FFT.xdi @@ -3,9 +3,25 @@ peakOmega) + return 9.77f * pow(abs(omega / peakOmega), -2.5f); + else + return 6.97f * pow(abs(omega / peakOmega), 5.0f); +} + +float NormalizationFactor(float s) +{ + const float s2 = s * s; + const float s3 = s2 * s; + const float s4 = s3 * s; + if (s < 5) + return -0.000564f * s4 + 0.00776f * s3 - 0.044f * s2 + 0.192f * s + 0.163f; + else + return -4.80e-08f * s4 + 1.07e-05f * s3 - 9.53e-04f * s2 + 5.90e-02f * s + 3.93e-01f; +} + +float Cosine2s(float theta, float s) +{ + return NormalizationFactor(s) * pow(abs(cos(0.5f * theta)), 2.0f * s); +} + +float DirectionSpectrum(SpectrumParameters spectrum, float theta, float omega) +{ + const float s = SpreadPower(omega, spectrum.peakOmega) + 16 * tanh(min(omega / spectrum.peakOmega, 20)) * spectrum.swell * spectrum.swell; + return mix(2.0f / 3.1415f * cos(theta) * cos(theta), Cosine2s(theta - spectrum.angle, s), spectrum.spreadBlend); +} + +float ShortWavesFade(SpectrumParameters spectrum, float kLength) +{ + return exp(-spectrum.shortWavesFade * spectrum.shortWavesFade * kLength * kLength); +} + +vec4 CalculateSpectrum(Configuration configuration, SpectrumParameters spectrum, ivec3 id, uint tile) +{ + uint seed = id.x + configuration.N * id.y + configuration.N; + seed += configuration.seed; + seed += tile + uint(hash(seed)) * 10; + + const float lengthScale = configuration.lengthScales[tile]; + + const float halfN = configuration.N / 2.0f; + + const float deltaK = 2.0f * PI / lengthScale; + const vec2 K = (id.xy - halfN) * deltaK; + const float kLength = length(K); + + const vec4 uniformRandSamples = vec4(hash(seed), hash(seed * 2), hash(seed * 3), hash(seed * 4)); + const vec2 gauss1 = UniformToGaussian(uniformRandSamples.x, uniformRandSamples.y); + const vec2 gauss2 = UniformToGaussian(uniformRandSamples.z, uniformRandSamples.w); + + if (configuration.lowCutoff <= kLength && kLength <= configuration.highCutoff) + { + const float kAngle = atan(K.y, K.x); + const float omega = Dispersion(configuration, kLength); + const float dOmegadk = DispersionDerivative(configuration, kLength); + + const float fSpectrum = JONSWAP(configuration, spectrum, omega) * DirectionSpectrum(spectrum, kAngle, omega) * ShortWavesFade(spectrum, kLength); + + //if (_Spectrums[i * 2 + 1].scale > 0) + // fSpectrum += JONSWAP(omega, _Spectrums[i * 2 + 1]) * DirectionSpectrum(kAngle, omega, _Spectrums[i * 2 + 1]) * ShortWavesFade(kLength, _Spectrums[i * 2 + 1]); + + return vec4(vec2(gauss2.x, gauss1.y) * sqrt(2 * fSpectrum * abs(dOmegadk) / kLength * deltaK * deltaK), 0.0f, 0.0f); + } + else + { + return vec4(0.0f); + } +} + +// Displacement + +float CalculateDisplacement(vec2 position, float time) +{ + float v = 0.0f; + for (int i = 0; i < 4; ++i) + { + const float f = pow(2.0f, i); + const float a = sin(position.x * TWO_PI * f); + const float b = sin(position.y * TWO_PI * f); + v += (a * b) * sin(time * (1.0f + i / 7.0f)) * 1.0f / (i + 1.0f); + } + return v; +} + ]]> diff --git a/data/Source/System/Terrain/Ocean/Shaders/Wave/Wave.xdi b/data/Source/System/Terrain/Ocean/Shaders/Wave/Wave.xdi index c0638b6f75..ade76bd2e0 100644 --- a/data/Source/System/Terrain/Ocean/Shaders/Wave/Wave.xdi +++ b/data/Source/System/Terrain/Ocean/Shaders/Wave/Wave.xdi @@ -10,8 +10,8 @@ Default - 1 - 1 + 16 + 16 1 @@ -77,7 +77,9 @@ 1 1 - + + {E21A21CC-F70C-474E-87DD-9D7B89AAC05F} + {0251C710-E6D5-1745-9186-28E3773397DC} @@ -97,15 +99,10 @@ @@ -154,7 +151,7 @@ $Output = 1.0f * v; -316 204 - 256 + 512 {843EAF52-6A4A-F142-A52C-E241E798E07C}