Blender V4.5
ocean_spectrum.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base.hh"
10
11#include "BKE_ocean.h"
12#include "ocean_intern.h"
13
14#include <algorithm>
15#include <cmath>
16
17#ifdef WITH_OCEANSIM
18
19/* -------------------------------------------------------------------- */
24
25/*
26 * Original code from EncinoWaves project Copyright (c) 2015 Christopher Jon Horvath
27 * Modifications made to work within blender.
28 *
29 * Licensed under the Apache License, Version 2.0 (the "License");
30 * you may not use this file except in compliance with the License.
31 * You may obtain a copy of the License at
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 */
35
40static float alpha_beta_spectrum(const float alpha,
41 const float beta,
42 const float gamma,
43 const float omega,
44 const float peakomega)
45{
46 return (alpha * sqrt(gamma) / pow(omega, 5.0)) * exp(-beta * pow(peakomega / omega, 4.0));
47}
48
49static float peak_sharpen(const float omega, const float peakomega, const float gamma)
50{
52 const float sigma = (omega < peakomega) ? 0.07 : 0.09;
53 const float exponent = -square((omega - peakomega) / (sigma * peakomega)) / 2.0;
54 return pow(gamma, exp(exponent));
55}
56
60static float ocean_spectrum_wind_and_damp(const Ocean *oc,
61 const float kx,
62 const float kz,
63 const float val)
64{
65 const float k2 = kx * kx + kz * kz;
66 const float k_mag_inv = 1.0f / k2;
67 const float k_dot_w = (kx * k_mag_inv * oc->_wx) + (kz * k_mag_inv * oc->_wz);
68
69 /* Bias towards wind direction. */
70 float newval = val * pow(fabs(k_dot_w), oc->_wind_alignment);
71
72 /* Eliminate wavelengths smaller than cutoff. */
73 // val *= exp(-k2 * m_cutoff);
74
75 /* Reduce reflected waves. */
76 if (k_dot_w < 0.0f) {
77 if (oc->_wind_alignment > 0.0) {
78 newval *= oc->_damp_reflections;
79 }
80 }
81
82 return newval;
83}
84
85static float jonswap(const Ocean *oc, const float k2)
86{
87 /* Get our basic JONSWAP value from #alpha_beta_spectrum. */
88 const float k_mag = sqrt(k2);
89
90 const float m_omega = GRAVITY * k_mag * tanh(k_mag * oc->_depth);
91 const float omega = sqrt(m_omega);
92
93 const float m_fetch = oc->_fetch_jonswap;
94
95 /* Strictly, this should be a random value from a Gaussian (mean 3.3, variance 0.67),
96 * clamped 1.0 to 6.0. */
97 float m_gamma = oc->_sharpen_peak_jonswap;
98 m_gamma = std::max<double>(m_gamma, 1.0);
99 m_gamma = std::min<double>(m_gamma, 6.0);
100
101 const float m_windspeed = oc->_V;
102
103 /* NOTE(@ideasman42): from upstream project in: `src/EncinoWaves/Spectra.h`,
104 * `square(m_windspeed)` is used, *not* `sqrt(m_windspeed)`, this change makes geometry
105 * significantly more *choppy* as well as causing this spectrum to differed significantly
106 * from the "Established Ocean". Keep as is unless a larger refactor/validation of this
107 * algorithm is undertaken. */
108 const float m_dimensionlessFetch = fabs(GRAVITY * m_fetch / sqrt(m_windspeed));
109 const float m_alpha = 0.076 * pow(m_dimensionlessFetch, -0.22);
110
111 const float m_tau = M_PI * 2;
112 const float m_peakomega = m_tau * 3.5 * fabs(GRAVITY / oc->_V) *
113 pow(m_dimensionlessFetch, -0.33);
114
115 const float beta = 1.25f;
116
117 float val = alpha_beta_spectrum(m_alpha, beta, GRAVITY, omega, m_peakomega);
118
119 /* Peak sharpening. */
120 val *= peak_sharpen(omega, m_peakomega, m_gamma);
121
122 return val;
123}
124
125float BLI_ocean_spectrum_piersonmoskowitz(const Ocean *oc, const float kx, const float kz)
126{
127 const float k2 = kx * kx + kz * kz;
128
129 if (k2 == 0.0f) {
130 /* No DC component. */
131 return 0.0f;
132 }
133
134 /* Get Pierson-Moskowitz value from #alpha_beta_spectrum. */
135 const float peak_omega_PM = 0.87f * GRAVITY / oc->_V;
136
137 const float k_mag = sqrt(k2);
138 const float m_omega = GRAVITY * k_mag * tanh(k_mag * oc->_depth);
139
140 const float omega = sqrt(m_omega);
141 const float alpha = 0.0081f;
142 const float beta = 1.291f;
143
144 float val = alpha_beta_spectrum(alpha, beta, GRAVITY, omega, peak_omega_PM);
145
146 val = ocean_spectrum_wind_and_damp(oc, kx, kz, val);
147
148 return val;
149}
150
151float BLI_ocean_spectrum_texelmarsenarsloe(const Ocean *oc, const float kx, const float kz)
152{
153 const float k2 = kx * kx + kz * kz;
154
155 if (k2 == 0.0f) {
156 /* No DC component. */
157 return 0.0f;
158 }
159
160 float val = jonswap(oc, k2);
161
162 val = ocean_spectrum_wind_and_damp(oc, kx, kz, val);
163
164 /* TMA modifications to JONSWAP. */
165 const float m_depth = oc->_depth;
166 const float gain = sqrt(m_depth / GRAVITY);
167
168 const float k_mag = sqrt(k2);
169
170 const float m_omega = GRAVITY * k_mag * tanh(k_mag * oc->_depth);
171 const float omega = sqrt(m_omega);
172
173 const float kitaigorodskiiDepth_wh = omega * gain;
174 const float kitaigorodskiiDepth = 0.5 + (0.5 * tanh(1.8 * (kitaigorodskiiDepth_wh - 1.125)));
175
176 val *= kitaigorodskiiDepth;
177
178 val = ocean_spectrum_wind_and_damp(oc, kx, kz, val);
179
180 return val;
181}
182
183float BLI_ocean_spectrum_jonswap(const Ocean *oc, const float kx, const float kz)
184{
185 const float k2 = kx * kx + kz * kz;
186
187 if (k2 == 0.0f) {
188 /* No DC component. */
189 return 0.0f;
190 }
191
192 float val = jonswap(oc, k2);
193
194 val = ocean_spectrum_wind_and_damp(oc, kx, kz, val);
195
196 return val;
197}
198
200
201#endif /* WITH_OCEANSIM */
float BLI_ocean_spectrum_piersonmoskowitz(const struct Ocean *oc, float kx, float kz)
float BLI_ocean_spectrum_jonswap(const struct Ocean *oc, float kx, float kz)
float BLI_ocean_spectrum_texelmarsenarsloe(const struct Ocean *oc, float kx, float kz)
#define M_PI
btVector3 m_depth
#define pow
#define exp
#define sqrt
#define tanh
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:651
ccl_device_inline float2 fabs(const float2 a)
T square(const T &a)