载入中...
搜索中...
未找到
Waterfall.cpp
浏览该文件的文档.
2
3#include <glm/mat4x4.hpp>
4#include <glm/vec4.hpp>
5#include "common/Exception.h"
6#include "graphics/Graphics.h"
7#include "graphics/Mesh.h"
9#include "graphics/Shader.h"
11#include "graphics/shaders/waterfall_mesh_frag_spv.inc"
12
13#include <algorithm>
14#include <cmath>
15#include <string>
16#include <vector>
17
18namespace eve::graphics {
19
20namespace {
21
22// Push-constant layout (data[32]):
23// 0 time, 1 flowSpeed, 2 turbulence, 3 streakCount, 4 streakScale,
24// 5 topFoam, 6 bottomFoam, 7 foamAmt, 8 reflIntensity,
25// 9 sunIntensity, 10..12 waterColor.
26// Fragment shader source lives in graphics/shaders/waterfall_mesh.frag; it is
27// precompiled to SPIR-V (waterfall_mesh_frag_spv.inc) so it works on every
28// backend without a runtime glslc.
29const char *kUniformNames[] = {
30 "time", "flowSpeed", "turbulence", "streakCnt", "streakScale",
31 "topFoam", "bottomFoam", "foamAmt", "reflInten", "sunInten", "waterCol",
32};
33const int kUniformCount = int(sizeof(kUniformNames) / sizeof(kUniformNames[0]));
34
35} // namespace
36
38 Shader *sh = nullptr;
39 if (gfx->getBackendName() == "webgpu") {
41 } else {
42 std::vector<uint32_t> frag(waterfall_mesh_frag_spv,
43 waterfall_mesh_frag_spv + waterfall_mesh_frag_spv_count);
44 sh = gfx->newMeshShaderFromSpv({}, frag);
45 }
46 for (int i = 0; i < kUniformCount; ++i) {
47 if (std::string(kUniformNames[i]) == "waterCol")
48 sh->declareVec3(kUniformNames[i]);
49 else
50 sh->declareFloat(kUniformNames[i]);
51 }
52 return sh;
53}
54
55int Waterfall::paramCount() { return kUniformCount; }
56
57std::string Waterfall::paramName(int index) {
58 if (index < 0 || index >= kUniformCount) return {};
59 return kUniformNames[index];
60}
61
62Waterfall::Waterfall(Graphics *gfx) : gfx_(gfx) {
63 shader_ = newWaterfallShader(gfx);
64 bindParams();
65 captureDrawerToken_ = RenderSystem3D::addCaptureExtraDrawer(
66 0xffffffffu,
67 [this](Graphics &, const Camera3D::Data &, const glm::mat4 &, float, uint32_t mask) {
68 if (!reflectionCaptureEnabled_ || (reflectionCaptureMask_ & mask) == 0u) return;
69 draw();
70 });
71}
72
74
75void Waterfall::createSheet(float width, float height, int segX, int segY) {
76 createCurvedSheet(width, height, segX, segY, 0.f, 0.f);
77}
78
79void Waterfall::createCurvedSheet(float width, float height, int segX, int segY,
80 float curveDepth, float lipOverhang) {
81 segX = std::max(1, segX);
82 segY = std::max(1, segY);
83 curveDepth = std::max(0.f, curveDepth);
84 lipOverhang = std::max(0.f, lipOverhang);
85 std::vector<float> pos, nrm, uv;
86 std::vector<uint32_t> idx;
87 for (int y = 0; y <= segY; ++y) {
88 const float v = float(y) / float(segY);
89 const float lowerFan = 1.f + 0.12f * (1.f - v) * (1.f - v);
90 for (int x = 0; x <= segX; ++x) {
91 const float u = float(x) / float(segX);
92 const float across = u * 2.f - 1.f;
93 const float crown = std::max(0.f, 1.f - across * across);
94 const float bow = curveDepth * crown * (0.62f + 0.38f * v);
95 const float lip = lipOverhang * v * v;
96 pos.push_back(across * width * 0.5f * lowerFan);
97 pos.push_back(-height * 0.5f + height * v);
98 pos.push_back(bow + lip);
99 nrm.push_back(0.f);
100 nrm.push_back(0.f);
101 nrm.push_back(1.f);
102 uv.push_back(u);
103 uv.push_back(v);
104 }
105 }
106 for (int y = 0; y < segY; ++y) {
107 for (int x = 0; x < segX; ++x) {
108 const uint32_t a = uint32_t(y * (segX + 1) + x);
109 const uint32_t b = a + 1;
110 const uint32_t c = a + uint32_t(segX + 1);
111 const uint32_t d = c + 1;
112 idx.push_back(a);
113 idx.push_back(b);
114 idx.push_back(c);
115 idx.push_back(b);
116 idx.push_back(d);
117 idx.push_back(c);
118 }
119 }
120 mesh_ = gfx_->newMeshFromArrays(pos.data(), nrm.data(), uv.data(), int(pos.size() / 3),
121 idx.data(), int(idx.size()));
122}
123
125 time_ += dt;
126 bindParams();
127}
128
129void Waterfall::setTime(float seconds) {
130 time_ = seconds;
131 bindParams();
132}
133
134void Waterfall::setFlowSpeed(float v) { flowSpeed_ = v; }
135void Waterfall::setTurbulence(float v) { turbulence_ = std::max(0.f, v); }
136void Waterfall::setStreakCount(int v) { streakCount_ = std::max(0, v); }
137void Waterfall::setStreakScale(float v) { streakScale_ = std::max(0.1f, v); }
138void Waterfall::setTopFoam(float v) { topFoam_ = std::clamp(v, 0.001f, 0.5f); }
139void Waterfall::setBottomFoam(float v) { bottomFoam_ = std::clamp(v, 0.001f, 0.5f); }
140void Waterfall::setFoamAmount(float v) { foamAmount_ = std::max(0.f, v); }
141void Waterfall::setWaterColor(float r, float g, float b) {
142 waterColor_[0] = r;
143 waterColor_[1] = g;
144 waterColor_[2] = b;
145}
146void Waterfall::setReflectionIntensity(float v) { reflectionIntensity_ = std::max(0.f, v); }
147void Waterfall::setSunIntensity(float v) { sunIntensity_ = std::max(0.f, v); }
148
150 if (!shader_) return;
151 shader_->sendFloat("time", time_);
152 shader_->sendFloat("flowSpeed", flowSpeed_);
153 shader_->sendFloat("turbulence", turbulence_);
154 shader_->sendFloat("streakCnt", float(streakCount_));
155 shader_->sendFloat("streakScale", streakScale_);
156 shader_->sendFloat("topFoam", topFoam_);
157 shader_->sendFloat("bottomFoam", bottomFoam_);
158 shader_->sendFloat("foamAmt", foamAmount_);
159 shader_->sendFloat("reflInten", reflectionIntensity_);
160 shader_->sendFloat("sunInten", sunIntensity_);
161 shader_->sendVec3("waterCol", waterColor_[0], waterColor_[1], waterColor_[2]);
162}
163
165 if (!gfx_ || !mesh_ || !shader_) return;
166 gfx_->drawMeshShader(mesh_, glm::mat4(1.f), nullptr, glm::vec4(1.f), shader_);
167}
168
169} // namespace eve::graphics
double y
double x
int mask
float uv
tensor::Graph g
Definition GpuGraph.cpp:7
vk::ShaderModule frag
float u
Definition Grass.cpp:233
double r
float v
HexVec3 across
std::int32_t c
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
double dt
int idx
float d
uint32_t index
virtual std::string getBackendName() const =0
Renderer backend id used by sibling modules (e.g. Gpgpu).
virtual Shader * newMeshShaderFromSpv(const std::vector< uint32_t > &vertSpv, const std::vector< uint32_t > &fragSpv)=0
Create a Mesh3D custom shader (MeshVertex + Frame UBO + albedo). Empty vert → default mesh3d....
virtual void drawMeshShader(Mesh *mesh, const glm::mat4 &model, Texture *texture, const Color &tint, Shader *shader)=0
Draw mesh with an explicit Mesh3D Shader (nullptr = default PBR pipeline).
virtual Mesh * newMeshFromArrays(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
Upload a triangle mesh from packed CPU arrays. Owned by Graphics. posXYZ required (vertexCount*3)....
virtual Shader * newMeshShaderFromWgsl(const std::string &vertWgsl, const std::string &fragWgsl)=0
Create a Mesh3D custom shader from WGSL source (WebGPU backend). The WGSL must declare the engine's F...
static void removeCaptureExtraDrawer(uint64_t token)
Unregister an offscreen capture contributor.
static uint64_t addCaptureExtraDrawer(uint32_t reflectionCaptureMask, CaptureExtraDrawer drawer)
Register custom forward geometry for offscreen captures.
Custom GPU program.
Definition Shader.h:41
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:107
void sendVec3(const std::string &name, float x, float y, float z)
Definition Shader.cpp:114
int declareVec3(const std::string &name)
Definition Shader.cpp:57
int declareFloat(const std::string &name)
Reserve sequential float slots in the push-constant block. Returns start index.
Definition Shader.cpp:55
void setSunIntensity(float intensity)
Sets the sun intensity.
void setReflectionIntensity(float intensity)
Sets the reflection intensity.
static int paramCount()
Names of the push-constant parameters (for UI / inspection).
Definition Waterfall.cpp:55
void createSheet(float width, float height, int segX, int segY)
Build a vertical XY plane (facing +Z, Y-up world) sized w×h, UVs [0,1]².
Definition Waterfall.cpp:75
void createCurvedSheet(float width, float height, int segX, int segY, float curveDepth, float lipOverhang)
Build a shaped water curtain with a convex cross-section and projecting lip.
Definition Waterfall.cpp:79
void setStreakCount(int count)
How many layered falling streaks are drawn.
void setFoamAmount(float v)
Sets the foam amount.
void draw()
Draw the waterfall sheet (uses default mesh3d camera / lighting state).
void setWaterColor(float r, float g, float b)
Sets the water color.
void setTurbulence(float t)
Amount of turbulence / white-water streak in the body.
void bindParams()
Upload current params to the shader push constants.
void setBottomFoam(float v)
Sets the bottom foam.
void setStreakScale(float scale)
Horizontal stretch of the falling streaks (1 = circular, >1 elongated).
Waterfall(Graphics *gfx)
Waterfall.
Definition Waterfall.cpp:62
void update(float dt)
Advance the animation clock by dt seconds.
void setTopFoam(float v)
Relative height (0..1) of the top foam lip and bottom splash bands.
void setFlowSpeed(float speed)
Fall speed of the water (scales the downward scroll).
void setTime(float seconds)
Sets the time.
static std::string paramName(int index)
Param name.
Definition Waterfall.cpp:57
constexpr const char * kWaterfallFragWgsl
Waterfall fragment shader WGSL source. @ownership Borrowed immutable string literal; callers must not...
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Shader * newWaterfallShader(Graphics *gfx)
Create the embedded waterfall fragment shader (owned by Graphics).
Definition Waterfall.cpp:37