载入中...
搜索中...
未找到
OceanSurface.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <numbers>
6
7namespace eve::fluids {
8namespace {
9constexpr double tau = 2.0 * std::numbers::pi;
10
11bool finite(glm::dvec2 v) { return std::isfinite(v.x) && std::isfinite(v.y); }
12bool finite(glm::dvec3 v) { return finite(glm::dvec2(v)) && std::isfinite(v.z); }
13bool validQuery(glm::dvec2 p, double t) {
14 return finite(p) && std::abs(p.x) <= 1e9 && std::abs(p.y) <= 1e9 && std::isfinite(t) && std::abs(t) <= 1e9;
15}
16template <class T>
18 return Result<T>::failure(
19 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid ocean input", "fluids.ocean"));
20}
21
22struct Evaluation {
23 glm::dvec3 position{0.0};
24 glm::dvec3 tangentX{1.0, 0.0, 0.0};
25 glm::dvec3 tangentZ{0.0, 0.0, 1.0};
26 glm::dvec3 velocity{0.0};
27};
28
29Evaluation evaluate(const OceanSurfaceSettings& s, glm::dvec2 q, double time) {
30 Evaluation e;
31 e.position = {q.x, s.seaLevel, q.y};
32 e.velocity = {s.current.x, 0.0, s.current.y};
33 for (const auto& wave : s.waves) {
34 const double k = tau / wave.wavelength;
35 const double omega = std::sqrt(s.gravity * k);
36 const auto d = wave.direction;
37 const double phase = std::remainder(k * glm::dot(d, q - s.current * time) - omega * time + wave.phase, tau);
38 const double sn = std::sin(phase), cs = std::cos(phase);
39 const double horizontal = wave.choppiness * wave.amplitude;
40 e.position += glm::dvec3(horizontal * d.x * cs, wave.amplitude * sn, horizontal * d.y * cs);
41 e.tangentX += glm::dvec3(-horizontal * k * d.x * d.x * sn, wave.amplitude * k * d.x * cs,
42 -horizontal * k * d.x * d.y * sn);
43 e.tangentZ += glm::dvec3(-horizontal * k * d.x * d.y * sn, wave.amplitude * k * d.y * cs,
44 -horizontal * k * d.y * d.y * sn);
45 // Material derivative follows the uniform current, cancelling its phase advection.
46 e.velocity +=
47 glm::dvec3(horizontal * omega * d.x * sn, -wave.amplitude * omega * cs, horizontal * omega * d.y * sn);
48 }
49 return e;
50}
51} // namespace
52
54 if (!std::isfinite(settings.seaLevel) || std::abs(settings.seaLevel) > 1e6 || !std::isfinite(settings.gravity) ||
55 settings.gravity <= 0.0 || settings.gravity > 100.0 || !finite(settings.current) ||
56 glm::length(settings.current) > 100.0 || settings.waves.size() > 64)
57 return invalid<OceanSurface>();
58 auto normalized = settings;
59 double steepness = 0.0;
60 for (auto& w : normalized.waves) {
61 if (!finite(w.direction) || glm::length(w.direction) < 1e-8 || glm::length(w.direction) > 1e6 ||
62 !std::isfinite(w.amplitude) || w.amplitude < 0.0 || w.amplitude > 100.0 || !std::isfinite(w.wavelength) ||
63 w.wavelength < 0.05 || w.wavelength > 1e6 || !std::isfinite(w.phase) || std::abs(w.phase) > 1e9 ||
64 !std::isfinite(w.choppiness) || w.choppiness < 0.0 || w.choppiness > 1.0)
65 return invalid<OceanSurface>();
66 w.direction = glm::normalize(w.direction);
67 steepness += w.choppiness * w.amplitude * tau / w.wavelength;
68 }
69 if (steepness > 0.75) return invalid<OceanSurface>();
70 return Result<OceanSurface>::success(OceanSurface(std::move(normalized)));
71}
72
77
80 auto q = worldXZ;
81 // The admitted displacement is a contraction (Lipschitz <= .75). This
82 // safeguarded fixed-point solve cannot jump to a different folded branch.
83 for (int i = 0; i < 96; ++i) {
84 const auto e = evaluate(settings_, q, time);
85 const auto residual = glm::dvec2(e.position.x, e.position.z) - worldXZ;
86 q -= residual;
87 if (glm::length(residual) < 1e-7) break;
88 }
89 const auto e = evaluate(settings_, q, time);
91 sample.height = e.position.y;
92 sample.normal = glm::normalize(glm::cross(e.tangentZ, e.tangentX));
93 sample.velocity = e.velocity;
94 sample.parameterPosition = q;
95 sample.time = time;
96 sample.horizontalJacobian = e.tangentX.x * e.tangentZ.z - e.tangentX.z * e.tangentZ.x;
98}
99
101 double density, double gravity) {
102 if (!finite(cell.center) || !finite(cell.pointVelocity) || glm::length(cell.center) > 1e9 ||
103 glm::length(cell.pointVelocity) > 1e4 || !std::isfinite(cell.radius) || cell.radius <= 0.0 ||
104 cell.radius > 1e4 || !std::isfinite(cell.volume) || cell.volume <= 0.0 || cell.volume > 1e9 ||
105 !std::isfinite(cell.dragCoefficient) || cell.dragCoefficient < 0.0 || cell.dragCoefficient > 100.0 ||
106 !std::isfinite(water.height) || std::abs(water.height) > 1e9 || !finite(water.velocity) ||
107 glm::length(water.velocity) > 1e4 || !std::isfinite(density) || density <= 0.0 || density > 1e5 ||
108 !std::isfinite(gravity) || gravity <= 0.0 || gravity > 100.0)
110 const double depth = std::clamp(water.height - cell.center.y + cell.radius, 0.0, 2.0 * cell.radius);
111 const double h = depth / cell.radius;
112 const double fraction = h * h * (3.0 - h) * 0.25;
114 result.position = cell.center;
115 result.submergedVolume = cell.volume * fraction;
116 const auto relative = cell.pointVelocity - water.velocity;
117 const double area = std::numbers::pi * cell.radius * cell.radius * fraction;
118 result.force = -0.5 * density * cell.dragCoefficient * area * glm::length(relative) * relative;
119 result.force.y += density * gravity * result.submergedVolume;
121}
122} // namespace eve::fluids
float w
Definition AnimClip.cpp:738
Vec3 relative
Definition AnimSmr.cpp:164
const std::string & s
glm::vec4 p[6]
ScriptForceVector gravity
float density
float area
Definition Grass.cpp:62
std::array< double, 10 > q
float v
int h
std::array< float, 3 > position
Poco::JSON::Object::Ptr result
OceanWakeSettings settings
glm::dvec3 velocity
ssq::Object water
glm::dvec3 tangentZ
glm::dvec3 tangentX
bool finite
float d
float t
float phase
Cell cell
std::uint32_t depth
float fraction
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Immutable analytic sea shared by geometry and buoyancy consumers. Owns every wave; no scene,...
EVENGINE_API_DOMAINS Result< OceanSurfaceSample > sample(glm::dvec2 worldXZ, double time) const
Invert horizontal displacement and sample the actual world-space surface.
EVENGINE_API_DOMAINS Result< glm::dvec3 > position(glm::dvec2 parameterXZ, double time) const
Displace a material-space mesh point using the same wave function as queries.
static EVENGINE_API_DOMAINS Result< OceanSurface > create(const OceanSurfaceSettings &settings)
Validate and copy a wave set, normalizing direction vectors.
Status invalid(const char *message)
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
Result< OceanCellForce > evaluateOceanCellForce(const OceanBuoyancyCell &cell, const OceanSurfaceSample &water, double density, double gravity)
Integrate spherical-cap buoyancy and isotropic quadratic water drag.
std::vector< OnnxNamedTensor > evaluate(const ModelData &, std::span< const OnnxNamedTensor >, const std::vector< std::string > &, OnnxCompute *, OnnxRunOptions options)
Evaluate.
Sphere with assigned displacement volume; several disjoint volumes approximate a hull.
Owning force at a hull point; the rigid-body owner applies it once per fixed step.
Owning sample of the displaced surface at a requested world XZ and time.
Immutable authored sea inputs copied into OceanSurface.