载入中...
搜索中...
未找到
ShallowWaterField.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4#include "common/Result.h"
5
6#include <glm/vec2.hpp>
7#include <glm/vec3.hpp>
8#include <glm/vec4.hpp>
9
10#include <cstdint>
11#include <memory>
12#include <span>
13#include <vector>
14
15namespace eve::gpgpu {
16class ComputeShader;
17class Gpgpu;
18class GpuBuffer;
19class Sequence;
20} // namespace eve::gpgpu
21
22namespace eve::image {
23class ImageData;
24} // namespace eve::image
25
26namespace eve::fluids {
27
29enum class ShallowWaterColliderShape : uint8_t { Sphere = 0, Capsule = 1, Box = 2 };
30
40 unsigned label = 0;
44 glm::vec3 center{0.f, 1.f, 0.f};
46 glm::vec3 halfExtent{0.25f};
48 float radius = 0.25f;
50 glm::vec3 velocity{0.f};
52 float displacementScale = 1.f;
54 float dragScale = 1.f;
55};
56
60 int resolutionX = 64;
62 int resolutionZ = 64;
64 float cellSize = 0.25f;
66 glm::vec3 origin{0.f};
68 float gravity = 9.81f;
70 float viscosity = 0.08f;
72 float friction = 0.12f;
74 float minDepth = 1e-4f;
76 float maxDepth = 2.5f;
78 float cfl = 0.45f;
80 int maxSubsteps = 8;
82 bool preferGpu = true;
84 float initialDepth = 0.55f;
86 float bedHeight = 0.f;
87};
88
101public:
107 [[nodiscard("check shallow-water create")]] static Result<std::unique_ptr<ShallowWaterField>> create(
109
111
114
116 [[nodiscard("check shallow-water step")]] Result<void> step(float dt);
117
122 [[nodiscard("check shallow-water colliders")]] Result<void> setColliders(
123 std::span<const ShallowWaterCollider> colliders);
124
132 [[nodiscard("check shallow-water bed")]] Result<void> setBed(std::span<const float> beds);
133
135 [[nodiscard("check shallow-water fill")]] Result<void> fillDepth(float depth);
136
145 [[nodiscard("check shallow-water surface fill")]] Result<void> fillSurface(float surfaceY);
146
155 [[nodiscard("check shallow-water disturb")]] Result<void> disturbAt(float worldX, float worldZ, float radius,
156 float heightDelta, float radialSpeed = 0.f);
157
159 [[nodiscard("check shallow-water impulse")]] Result<void> addImpulse(float worldX, float worldZ, float radius,
160 float strength);
161
163 [[nodiscard]] int resolutionX() const noexcept { return resX_; }
165 [[nodiscard]] int resolutionZ() const noexcept { return resZ_; }
167 [[nodiscard]] float cellSize() const noexcept { return cellSize_; }
169 [[nodiscard]] int cellCount() const noexcept { return resX_ * resZ_; }
171 [[nodiscard]] glm::vec3 origin() const noexcept { return origin_; }
173 [[nodiscard]] bool usingGpu() const noexcept { return gpuOk_; }
175 [[nodiscard]] bool usedBackendFallback() const noexcept { return backendFallback_; }
176
178 [[nodiscard]] std::span<const float> depths() const noexcept { return heights_; }
180 [[nodiscard]] std::span<const float> beds() const noexcept { return beds_; }
182 [[nodiscard]] std::span<const float> momentumX() const noexcept { return momentumX_; }
184 [[nodiscard]] std::span<const float> momentumZ() const noexcept { return momentumZ_; }
186 [[nodiscard]] std::span<const ShallowWaterCollider> colliders() const noexcept { return colliders_; }
187
189 [[nodiscard]] float surfaceHeightAt(float worldX, float worldZ) const noexcept;
191 [[nodiscard]] float depthAt(float worldX, float worldZ) const noexcept;
193 [[nodiscard]] glm::vec2 velocityAt(float worldX, float worldZ) const noexcept;
194
207 [[nodiscard("check shallow-water mesh")]] Result<void> buildSurfaceMesh(
208 std::vector<float>& positionsXYZ, std::vector<float>& normalsXYZ, std::vector<float>& uvsST,
209 std::vector<uint32_t>& indices, float visualHeightGain = 1.f, std::vector<float>* colorsRGBA = nullptr) const;
210
226 [[nodiscard("check shallow-water field image")]] Result<void> copyFieldToImage(image::ImageData& image,
227 float velocityScale = 2.f,
228 float depthContrast = 0.f) const;
229
239 [[nodiscard("check shallow-water albedo image")]] Result<void> copyShadedAlbedoToImage(
240 image::ImageData& image, float restDepth, float velocityScale = 2.f) const;
241
249 [[nodiscard("check shallow-water normal image")]] Result<void> copyNormalToImage(image::ImageData& image,
250 float normalStrength = 6.f) const;
251
253 [[nodiscard]] ShallowWaterSettings settings() const noexcept;
254
255private:
257
258 [[nodiscard]] Result<void> validateSettings(const ShallowWaterSettings& settings) const;
259 void stepCpu(float dt);
260 void rasterizeCollidersCpu(float dt);
261 void integrateCpu(float dt);
262 [[nodiscard]] bool ensureGpu();
263 [[nodiscard]] Result<void> stepGpu(float dt);
264 void uploadState();
265 void downloadState();
266 void uploadColliders();
267 void setPushConstants(gpgpu::ComputeShader* shader, float dt) const;
268 [[nodiscard]] int index(int x, int z) const noexcept { return z * resX_ + x; }
269 [[nodiscard]] bool inside(int x, int z) const noexcept {
270 return x >= 0 && z >= 0 && x < resX_ && z < resZ_;
271 }
272 void worldToCell(float worldX, float worldZ, float& fx, float& fz) const noexcept;
273
274 int resX_ = 0;
275 int resZ_ = 0;
276 float cellSize_ = 0.25f;
277 glm::vec3 origin_{0.f};
278 float gravity_ = 9.81f;
279 float viscosity_ = 0.08f;
280 float friction_ = 0.12f;
281 float minDepth_ = 1e-4f;
282 float maxDepth_ = 8.f;
283 float cfl_ = 0.45f;
284 int maxSubsteps_ = 8;
285 bool preferGpu_ = true;
286 bool gpuOk_ = false;
287 bool backendFallback_ = false;
288
289 std::vector<float> heights_;
290 std::vector<float> restDepths_;
291 std::vector<float> momentumX_;
292 std::vector<float> momentumZ_;
293 std::vector<float> beds_;
294 std::vector<float> srcH_;
295 std::vector<float> srcHu_;
296 std::vector<float> srcHv_;
297 std::vector<float> scratchH_;
298 std::vector<float> scratchHu_;
299 std::vector<float> scratchHv_;
300 std::vector<ShallowWaterCollider> colliders_;
301
302 eve::gpgpu::Gpgpu* gpgpu_ = nullptr;
303 eve::gpgpu::ComputeShader* shClear_ = nullptr;
304 eve::gpgpu::ComputeShader* shRaster_ = nullptr;
305 eve::gpgpu::ComputeShader* shIntegrate_ = nullptr;
306 eve::gpgpu::GpuBuffer* bufStateA_ = nullptr;
307 eve::gpgpu::GpuBuffer* bufStateB_ = nullptr;
308 eve::gpgpu::GpuBuffer* bufBed_ = nullptr;
309 eve::gpgpu::GpuBuffer* bufSources_ = nullptr;
310 eve::gpgpu::GpuBuffer* bufColliders_ = nullptr;
311 eve::gpgpu::GpuBuffer* stageState_ = nullptr;
312 eve::gpgpu::Sequence* seq_ = nullptr;
313 bool stateOnGpu_ = false;
314 bool pingA_ = true;
315};
316
317} // namespace eve::fluids
double z
double x
float normalStrength
#define EVENGINE_API_DOMAINS
Definition Export.h:110
float strength
vk::UniqueImage image
std::vector< std::uint32_t > indices
double radius
Definition MeleeHit.cpp:42
OceanWakeSettings settings
double dt
Shader * shader
Move-only, checked operation results for the common layer.
float step
Definition TreeMesh.cpp:451
std::uint32_t depth
uint32_t index
std::vector< char > inside
Move-only operation result carrying either a value or Status.
Definition Result.h:176
GPU-first shallow-water (2D Saint-Venant) free-surface solver.
std::span< const float > depths() const noexcept
std::span< const float > momentumX() const noexcept
std::span< const float > momentumZ() const noexcept
float cellSize() const noexcept
std::span< const float > beds() const noexcept
ShallowWaterField & operator=(const ShallowWaterField &)=delete
bool usedBackendFallback() const noexcept
glm::vec3 origin() const noexcept
ShallowWaterField(const ShallowWaterField &)=delete
std::span< const ShallowWaterCollider > colliders() const noexcept
Backend-agnostic compute program. Bind storage buffers then dispatch via Gpgpu::dispatch....
GPGPU module — compute shaders + storage buffers via the active Graphics backend. Uses the graphics q...
Definition Gpgpu.h:23
Backend-agnostic GPU buffer for compute (storage) or CPU staging transfers. Squirrel-owned; derived c...
Definition GpuBuffer.h:20
EVENGINE_API_WORLD public API.
Definition Sequence.h:43
Represents raw pixel data.
Definition ImageData.h:38
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
ShallowWaterColliderShape
Analytic obstacle geometry sampled into the shallow-water grid.
Fluids module — interactive surface fluid simulation.
Definition Fluids.h:35
World-space interactor used for character legs / props.
glm::vec3 halfExtent
Box half extents; capsule uses halfExtent.y as half height.
ShallowWaterColliderShape shape
Analytic primitive.
unsigned label
Application label; must be unique within one setColliders call.
glm::vec3 velocity
World velocity sampled at the collider center.
float radius
Sphere or capsule radius.
float dragScale
Scales horizontal momentum transfer from collider velocity.
glm::vec3 center
World center; capsule Y axis is vertical.
float displacementScale
Scales displaced water volume.
Authoritative settings for a shallow-water heightfield.
float initialDepth
Initial water depth filled on create.
bool preferGpu
Prefer the gpgpu path when a device is available.
int resolutionZ
Grid resolution along world Z, in [8, 512].
glm::vec3 origin
World-space origin of cell (0,0) corner (Y is bed baseline).
float cellSize
Cell edge length in meters, in (0, 10].
float cfl
Courant number used to pick substeps.
int resolutionX
Grid resolution along world X, in [8, 512].
float viscosity
Artificial viscosity for Lax-Friedrichs stability, in [0, 1].
float minDepth
Depth treated as dry.
float gravity
Gravity magnitude used by Saint-Venant fluxes.
int maxSubsteps
Upper bound on substeps per step call.
float maxDepth
Hard clamp on water column height.
float friction
Linear momentum friction per second, in [0, 8].
float bedHeight
Flat bed height filled on create.
gpgpu::Gpgpu * gpgpu