载入中...
搜索中...
未找到
ShallowWaterField.cpp
浏览该文件的文档.
61[[nodiscard]] glm::vec3 bedFlux(glm::vec3 a, glm::vec3 b, float ba, float bb, float gravity, float minDepth, bool axisZ,
66 const glm::vec3 qa{ha, a.x > minDepth ? a.y * ha / a.x : 0.f, a.x > minDepth ? a.z * ha / a.x : 0.f};
67 const glm::vec3 qb{hb, b.x > minDepth ? b.y * hb / b.x : 0.f, b.x > minDepth ? b.z * hb / b.x : 0.f};
73 0.5f * ((axisZ ? fluxZ(qa.x, qa.y, qa.z, gravity, minDepth) : fluxX(qa.x, qa.y, qa.z, gravity, minDepth)) +
74 (axisZ ? fluxZ(qb.x, qb.y, qb.z, gravity, minDepth) : fluxX(qb.x, qb.y, qb.z, gravity, minDepth))) -
82[[nodiscard]] float colliderFootprintDistance(const ShallowWaterCollider& c, float worldX, float worldZ) noexcept {
88 const float outside = std::sqrt(std::max(ox, 0.f) * std::max(ox, 0.f) + std::max(oz, 0.f) * std::max(oz, 0.f));
108Result<std::unique_ptr<ShallowWaterField>> ShallowWaterField::create(const ShallowWaterSettings& settings) {
155 if (settings.resolutionX < 8 || settings.resolutionX > 512 || settings.resolutionZ < 8 || settings.resolutionZ > 512)
157 if (!(settings.cellSize > 0.f) || settings.cellSize > 10.f || !std::isfinite(settings.cellSize))
162 if (!(settings.viscosity >= 0.f) || settings.viscosity > 1.f || !std::isfinite(settings.viscosity))
164 if (!(settings.friction >= 0.f) || settings.friction > 8.f || !std::isfinite(settings.friction))
168 if (!(settings.maxDepth > settings.minDepth) || settings.maxDepth > 100.f || !std::isfinite(settings.maxDepth))
170 if (!(settings.cfl > 0.f) || settings.cfl > 1.f || !std::isfinite(settings.cfl)) return fail("cfl must be in (0, 1]");
171 if (settings.maxSubsteps < 1 || settings.maxSubsteps > 64) return fail("maxSubsteps must be in [1, 64]");
172 if (!(settings.initialDepth >= 0.f) || settings.initialDepth > settings.maxDepth || !std::isfinite(settings.initialDepth))
203 if (!finite3(c.center) || !finite3(c.halfExtent) || !finite3(c.velocity) || !std::isfinite(c.radius) ||
204 !std::isfinite(c.displacementScale) || !std::isfinite(c.dragScale) || c.radius < 0.001f || c.radius > 100.f ||
205 c.displacementScale < 0.f || c.displacementScale > 10.f || c.dragScale < 0.f || c.dragScale > 10.f ||
207 (c.shape != ShallowWaterColliderShape::Sphere && c.shape != ShallowWaterColliderShape::Capsule &&
233 if (!std::isfinite(depth) || depth < 0.f || depth > maxDepth_) return fail("fillDepth out of range");
254void ShallowWaterField::worldToCell(float worldX, float worldZ, float& fx, float& fz) const noexcept {
259Result<void> ShallowWaterField::disturbAt(float worldX, float worldZ, float radius, float heightDelta,
261 if (!std::isfinite(worldX) || !std::isfinite(worldZ) || !std::isfinite(radius) || !std::isfinite(heightDelta) ||
262 !std::isfinite(radialSpeed) || radius <= 0.f || radius > 50.f || std::abs(heightDelta) > maxDepth_ ||
289Result<void> ShallowWaterField::addImpulse(float worldX, float worldZ, float radius, float strength) {
349Result<void> ShallowWaterField::buildSurfaceMesh(std::vector<float>& positionsXYZ, std::vector<float>& normalsXYZ,
442 if (image.getFormat() != "RGBA8" || image.getWidth() != resX || image.getHeight() != resZ || !image.getData())
530Result<void> ShallowWaterField::copyNormalToImage(image::ImageData& image, float normalStrength) const {
645 const glm::vec3 fR = bedFlux(q, qR, bed, sampleBed(x + 1, z), gravity_, minDepth_, false, true);
646 const glm::vec3 fL = bedFlux(qL, q, sampleBed(x - 1, z), bed, gravity_, minDepth_, false, false);
648 const glm::vec3 fD = bedFlux(qD, q, sampleBed(x, z - 1), bed, gravity_, minDepth_, true, false);
887 Diagnostic::error(DiagnosticCode::Failed, "Shallow-water GPU step failed", "fluids.shallowWater.gpu"));
Stable, structured diagnostics shared by engine modules.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
GPU-first shallow-water (2D Saint-Venant) free-surface solver.
Definition ShallowWaterField.h:100
~ShallowWaterField()
Definition ShallowWaterField.cpp:140
float depthAt(float worldX, float worldZ) const noexcept
Bilinear water depth at a world XZ point.
Definition ShallowWaterField.cpp:315
Result< void > fillSurface(float surfaceY)
Fill to a world-space free-surface elevation, leaving high terrain dry.
Definition ShallowWaterField.cpp:242
Result< void > fillDepth(float depth)
Fill every cell with a uniform water depth (momentum cleared).
Definition ShallowWaterField.cpp:232
std::span< const float > beds() const noexcept
Definition ShallowWaterField.h:180
Result< void > setBed(std::span< const float > beds)
Replace bed heights; call fillSurface afterwards to initialize a level lake.
Definition ShallowWaterField.cpp:222
Result< void > step(float dt)
Advance the free surface by dt seconds (CFL substeps).
Definition ShallowWaterField.cpp:691
Result< void > copyNormalToImage(image::ImageData &image, float normalStrength=6.f) const
Write tangent-space normals from free-surface slopes (RGBA8, same grid size).
Definition ShallowWaterField.cpp:530
Result< void > addImpulse(float worldX, float worldZ, float radius, float strength)
Convenience splash: positive strength raises water, negative digs.
Definition ShallowWaterField.cpp:289
Result< void > disturbAt(float worldX, float worldZ, float radius, float heightDelta, float radialSpeed=0.f)
Inject a localized height bump and radial outflow (mouse / splash).
Definition ShallowWaterField.cpp:259
Result< void > buildSurfaceMesh(std::vector< float > &positionsXYZ, std::vector< float > &normalsXYZ, std::vector< float > &uvsST, std::vector< uint32_t > &indices, float visualHeightGain=1.f, std::vector< float > *colorsRGBA=nullptr) const
Build a Y-up triangle mesh of the free surface for Graphics upload.
Definition ShallowWaterField.cpp:349
Result< void > copyFieldToImage(image::ImageData &image, float velocityScale=2.f, float depthContrast=0.f) const
Pack per-cell depth / velocity into an RGBA8 image (resolutionX × resolutionZ).
Definition ShallowWaterField.cpp:449
static Result< std::unique_ptr< ShallowWaterField > > create(const ShallowWaterSettings &settings)
Validate settings and allocate a filled lake.
Definition ShallowWaterField.cpp:108
int cellCount() const noexcept
Definition ShallowWaterField.h:169
glm::vec2 velocityAt(float worldX, float worldZ) const noexcept
Bilinear horizontal velocity at a world XZ point.
Definition ShallowWaterField.cpp:331
float surfaceHeightAt(float worldX, float worldZ) const noexcept
Bilinear free-surface height b + h at a world XZ point.
Definition ShallowWaterField.cpp:294
Result< void > copyShadedAlbedoToImage(image::ImageData &image, float restDepth, float velocityScale=2.f) const
Write a shaded water albedo (RGBA8, same size as the grid) for Material upload.
Definition ShallowWaterField.cpp:487
Result< void > setColliders(std::span< const ShallowWaterCollider > colliders)
Replace analytic interactors atomically.
Definition ShallowWaterField.cpp:199
ShallowWaterField(const ShallowWaterField &)=delete
std::span< const ShallowWaterCollider > colliders() const noexcept
Definition ShallowWaterField.h:186
ShallowWaterSettings settings() const noexcept
Definition ShallowWaterField.cpp:180
virtual void bindBuffer(int binding, GpuBuffer *buffer)=0
Bind a storage buffer to set=0 binding. binding in [0, kMaxBindings).
ComputeShader * newShader(const std::string &source)
Compatibility-only raw-owning shader factory (Vulkan: GLSL; WebGPU: WGSL). Vulkan delegates to the ch...
Definition Gpgpu.cpp:324
bool isAvailable() const
True when the active Graphics backend can run compute (device initialized).
Definition Gpgpu.cpp:284
GpuBuffer * newBuffer(int byteSize, const std::string &usage="storage")
Allocate a GPU buffer. usage: "storage" (SSBO) | "staging" (host transfer) | "storage_vertex" (Vulkan...
Definition Gpgpu.cpp:372
Sequence * newSequence()
Create a Kompute-style command Sequence: record buffer transfers and compute dispatches into one comm...
Definition Gpgpu.cpp:384
virtual void writeFloat32s(const float *data, int count, int startIndex=0)=0
Bulk float upload/download (one transfer). startIndex is in floats.
virtual void downloadBytes(void *dst, uint64_t nbytes, uint64_t srcOffset=0) const =0
Downloads bytes.
void recordUpload(GpuBuffer *dst, const void *src, uint64_t nbytes, uint64_t dstOffset=0)
Record upload.
Definition Sequence.cpp:77
void recordDownload(GpuBuffer *src, GpuBuffer *staging, uint64_t nbytes, uint64_t srcOffset=0)
Record download.
Definition Sequence.cpp:82
void recordDispatch(ComputeShader *shader, int groupsX, int groupsY=1, int groupsZ=1)
Record dispatch.
Definition Sequence.cpp:87
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
Definition BuildingAssemblyInternal.h:35
const Value & field(const Value &value, const char *name)
Borrow a required child of value; throws on a missing field.
Definition OceanCodecInternal.h:20
const char * kShallowWaterClearSources
Zero the per-cell impulse source buffer. @lifetime The returned pointer remains valid for the process...
Definition ShallowWaterKernels.h:37
const char * kShallowWaterIntegrate
Apply source impulses then advance one Lax-Friedrichs shallow-water step. @lifetime The returned poin...
Definition ShallowWaterKernels.h:145
const char * kShallowWaterRasterizeColliders
Rasterize sphere/capsule/box colliders into height/momentum sources. @lifetime The returned pointer r...
Definition ShallowWaterKernels.h:57
@ InvalidArgument
@ Failed
Authoritative settings for a shallow-water heightfield.
Definition ShallowWaterField.h:58
glm::vec3 origin
World-space origin of cell (0,0) corner (Y is bed baseline).
Definition ShallowWaterField.h:66
float viscosity
Artificial viscosity for Lax-Friedrichs stability, in [0, 1].
Definition ShallowWaterField.h:70