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