载入中...
搜索中...
未找到
OceanSpray.cpp
浏览该文件的文档.
1#include "fluids/OceanSpray.h"
2#include "fluids/OceanBoat.h"
4
5#include <algorithm>
6#include <cmath>
7#include <limits>
8
9namespace eve::fluids {
10namespace {
11template <class T>
14 DiagnosticCode::InvalidArgument, "Invalid ocean spray settings, source or timestamp", "fluids.ocean.spray"));
15}
16bool finite(glm::dvec3 p) {
17 return std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z) && glm::length(p) <= 1e9;
18}
19std::uint64_t mix(std::uint64_t x) {
20 x += 0x9e3779b97f4a7c15ULL;
21 x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
22 x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
23 return x ^ (x >> 31);
24}
25double random(std::uint64_t& state) {
26 state = mix(state);
27 return double(state >> 11) * 0x1.0p-53;
28}
29} // namespace
31 if (s.capacity == 0 || s.capacity > 16384 || !std::isfinite(s.gravity) || s.gravity <= 0 || s.gravity > 100 ||
32 !std::isfinite(s.lifetime) || s.lifetime < 0.05 || s.lifetime > 10 || !std::isfinite(s.radius) ||
33 s.radius < 0.001 || s.radius > 0.5 || !std::isfinite(s.emissionPerVolumeSpeed) ||
34 s.emissionPerVolumeSpeed < 0 || s.emissionPerVolumeSpeed > 1000)
37 out.settings_ = s;
38 return Result<OceanSprayFrame>::success(std::move(out));
39}
41 if (!std::isfinite(dt_) || dt_ <= 0 || dt_ > 1.0 / 30.0 || sources_.size() > 32 ||
42 std::abs(water_.time() - previous_.time_) >
43 std::max(1e-8, 4.0 * std::numeric_limits<double>::epsilon() * std::abs(water_.time())) ||
44 water_.time() + dt_ > 1e9 || previous_.tick_ == std::numeric_limits<std::uint64_t>::max())
46 std::vector<OceanSpraySource> sources(sources_.begin(), sources_.end());
47 // References make source records non-assignable: sort indices instead of records.
48 std::vector<unsigned> order(sources.size());
49 for (unsigned i = 0; i < order.size(); ++i) order[i] = i;
50 std::sort(order.begin(), order.end(), [&](unsigned a, unsigned b) { return sources[a].id < sources[b].id; });
51 for (unsigned i = 0; i < order.size(); ++i) {
52 const auto& source = sources[order[i]];
53 if (source.id == 0 || (i && source.id == sources[order[i - 1]].id) || !std::isfinite(source.receipt.time) ||
54 std::abs(source.receipt.time - water_.time()) > 1e-8 || source.receipt.contacts.size() > 64)
56 for (const auto& c : source.receipt.contacts)
57 if (!finite(c.position) || !finite(c.relativeVelocity) || glm::length(c.relativeVelocity) > 1e4 ||
58 !std::isfinite(c.submergedVolume) || c.submergedVolume < 0 || c.submergedVolume > 1e6)
60 }
62 out.settings_ = previous_.settings_;
63 out.time_ = water_.time() + dt_;
64 out.tick_ = previous_.tick_ + 1;
65 out.nextId_ = previous_.nextId_;
66 const auto& s = out.settings_;
67 out.particles_.reserve(s.capacity);
68 for (auto p : previous_.particles_) {
69 p.age += dt_;
70 if (p.age >= p.lifetime) {
71 ++out.report_.expired;
72 continue;
73 }
74 p.position += p.velocity * dt_ + glm::dvec3(0, -0.5 * s.gravity * dt_ * dt_, 0);
75 p.velocity.y -= s.gravity * dt_;
76 auto surface = water_.sample({p.position.x, p.position.z});
77 if (!surface) return Result<OceanSprayFrame>::failure(surface.status());
78 if (p.position.y <= surface.value().height) {
79 ++out.report_.returnedToWater;
80 continue;
81 }
82 out.particles_.push_back(p);
83 }
84 for (unsigned index : order) {
85 const auto& source = sources[index];
86 double remainder = 0;
87 for (const auto& old : previous_.streams_)
88 if (old.id == source.id) remainder = old.remainder;
89 std::uint64_t rng = mix(mix(mix(s.seed) ^ source.id) ^ out.tick_);
90 glm::dvec3 center(0.0), heading(0.0);
91 for (const auto& c : source.receipt.contacts) {
92 center += c.position;
93 heading += glm::dvec3(c.relativeVelocity.x, 0.0, c.relativeVelocity.z);
94 }
95 const bool hull = source.receipt.contacts.size() > 1 && glm::length(heading) > 1e-6;
96 double front = 0.0, rear = 0.0;
97 glm::dvec3 lateral(0.0);
98 if (hull) {
99 center /= double(source.receipt.contacts.size());
100 heading = glm::normalize(heading);
101 lateral = glm::dvec3(heading.z, 0.0, -heading.x);
102 for (const auto& c : source.receipt.contacts) {
103 const double along = glm::dot(c.position - center, heading);
104 front = std::max(front, along);
105 rear = std::min(rear, along);
106 }
107 }
108 for (const auto& c : source.receipt.contacts) {
109 // Displacement cells lie inside the hull. Emit at their outboard
110 // waterline and only on the leading quarter, otherwise the hull
111 // hides the spray while aft cells exhaust the particle budget.
112 if (hull && glm::dot(c.position - center, heading) < front - (front - rear) * 0.25) continue;
113 const auto outward = lateral * (glm::dot(c.position - center, lateral) < 0.0 ? -1.0 : 1.0);
114 const auto emission = c.position + outward * (0.85 * std::cbrt(c.submergedVolume));
115 const double speed = glm::length(c.relativeVelocity);
116 const double drive = std::max(0.0, speed - 1.0);
117 // Bounded contact contribution prevents a large displacement cell exhausting the budget.
118 const double requested =
119 remainder + std::min(c.submergedVolume, 1.0) * drive * s.emissionPerVolumeSpeed * dt_;
120 const auto count = static_cast<unsigned>(std::floor(requested));
121 remainder = requested - count;
122 if (!count) continue;
123 auto surface = water_.sample({emission.x, emission.z});
124 if (!surface) return Result<OceanSprayFrame>::failure(surface.status());
125 const unsigned accepted = std::min(count, s.capacity - static_cast<unsigned>(out.particles_.size()));
126 out.report_.dropped += count - accepted;
127 for (unsigned i = 0; i < accepted; ++i) {
128 if (out.nextId_ == std::numeric_limits<std::uint64_t>::max()) return invalid<OceanSprayFrame>();
129 const double a = random(rng), b = random(rng), d = random(rng);
131 p.id = out.nextId_++;
132 p.radius = s.radius * (0.7 + 0.6 * d);
133 p.lifetime = s.lifetime * (0.7 + 0.6 * a);
134 p.position = {emission.x, surface.value().height + p.radius * 2, emission.z};
135 p.velocity =
136 surface.value().velocity + c.relativeVelocity * 0.3 + outward * (drive * 0.35) +
137 glm::dvec3((a - 0.5) * drive * 0.5, 1.0 + drive * (0.25 + 0.25 * d), (b - 0.5) * drive * 0.5);
138 p.velocity.y = std::max(0.5, p.velocity.y);
139 out.particles_.push_back(p);
140 ++out.report_.emitted;
141 }
142 }
143 out.streams_.push_back({source.id, remainder});
144 }
145 return Result<OceanSprayFrame>::success(std::move(out));
146}
147} // namespace eve::fluids
double x
double lateral
const std::string & s
glm::vec4 p[6]
std::int32_t c
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< std::int32_t > order
glm::dvec2 center
std::vector< std::pair< std::uint64_t, OceanBoatReceipt > > sources
bool finite
float d
ScriptStructure out
std::string_view source
int count
Battle::Random random
V3 heading
Definition TreeMesh.cpp:429
uint32_t index
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 owning visual spray state. The application owns the current frame; replacing it with a succ...
Definition OceanSpray.h:45
static EVENGINE_API_DOMAINS Result< OceanSprayFrame > create(const OceanSpraySettings &settings)
Validate settings and create an empty state at time zero.
EVENGINE_API_DOMAINS Result< OceanSprayFrame > build() const
Validate the whole batch and construct a new owning state without input mutation.
double time() const
Shared injected simulation timestamp.
EVENGINE_API_DOMAINS Result< OceanSurfaceSample > sample(glm::dvec2 worldXZ) const
Query combined water in world coordinates; errors propagate without changing either owner.
Status invalid(const char *message)
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
Owning particle value; ID is local to the spray stream, never a body handle.
Definition OceanSpray.h:24
Bounded visual droplets in SI units, driven by the ocean simulation clock.
Definition OceanSpray.h:15