载入中...
搜索中...
未找到
Waterfall.cpp
浏览该文件的文档.
2
3#include "graphics/Graphics.h"
4#include "graphics/Mesh.h"
5#include "graphics/shaders/waterfall_mesh_frag_spv.inc"
6
7#include <algorithm>
8#include <cmath>
9#include <string>
10#include <vector>
11
12namespace eve::graphics {
13
14namespace {
15
16// Push-constant layout (data[32]):
17// 0 time, 1 flowSpeed, 2 turbulence, 3 streakCount, 4 streakScale,
18// 5 topFoam, 6 bottomFoam, 7 foamAmt, 8 reflIntensity,
19// 9 sunIntensity, 10..12 waterColor.
20// Fragment shader source lives in graphics/shaders/waterfall_mesh.frag; it is
21// precompiled to SPIR-V (waterfall_mesh_frag_spv.inc) so it works on every
22// backend without a runtime glslc.
23const char *kUniformNames[] = {
24 "time", "flowSpeed", "turbulence", "streakCnt", "streakScale",
25 "topFoam", "bottomFoam", "foamAmt", "reflInten", "sunInten", "waterCol",
26};
27const int kUniformCount = int(sizeof(kUniformNames) / sizeof(kUniformNames[0]));
28
29} // namespace
30
32 std::vector<uint32_t> frag(waterfall_mesh_frag_spv,
33 waterfall_mesh_frag_spv + waterfall_mesh_frag_spv_count);
34 Shader *sh = gfx->newMeshShaderFromSpv({}, frag);
35 for (int i = 0; i < kUniformCount; ++i) {
36 if (std::string(kUniformNames[i]) == "waterCol")
37 sh->declareVec3(kUniformNames[i]);
38 else
39 sh->declareFloat(kUniformNames[i]);
40 }
41 return sh;
42}
43
44int Waterfall::paramCount() { return kUniformCount; }
45
46std::string Waterfall::paramName(int index) {
47 if (index < 0 || index >= kUniformCount) return {};
48 return kUniformNames[index];
49}
50
51Waterfall::Waterfall(Graphics *gfx) : gfx_(gfx) {
52 shader_ = newWaterfallShader(gfx);
53 bindParams();
54}
55
56void Waterfall::createSheet(float width, float height, int segX, int segY) {
57 segX = std::max(1, segX);
58 segY = std::max(1, segY);
59 std::vector<float> pos, nrm, uv;
60 std::vector<uint32_t> idx;
61 for (int y = 0; y <= segY; ++y) {
62 for (int x = 0; x <= segX; ++x) {
63 pos.push_back(-width * 0.5f + width * float(x) / segX);
64 pos.push_back(-height * 0.5f + height * float(y) / segY);
65 pos.push_back(0.f);
66 nrm.push_back(0.f);
67 nrm.push_back(0.f);
68 nrm.push_back(1.f);
69 uv.push_back(float(x) / segX);
70 uv.push_back(float(y) / segY);
71 }
72 }
73 for (int y = 0; y < segY; ++y) {
74 for (int x = 0; x < segX; ++x) {
75 const uint32_t a = uint32_t(y * (segX + 1) + x);
76 const uint32_t b = a + 1;
77 const uint32_t c = a + uint32_t(segX + 1);
78 const uint32_t d = c + 1;
79 idx.push_back(a);
80 idx.push_back(b);
81 idx.push_back(c);
82 idx.push_back(b);
83 idx.push_back(d);
84 idx.push_back(c);
85 }
86 }
87 mesh_ = gfx_->newMeshFromArrays(pos.data(), nrm.data(), uv.data(), int(pos.size() / 3),
88 idx.data(), int(idx.size()));
89}
90
91void Waterfall::update(float dt) {
92 time_ += dt;
93 bindParams();
94}
95
96void Waterfall::setTime(float seconds) {
97 time_ = seconds;
98 bindParams();
99}
100
101void Waterfall::setFlowSpeed(float v) { flowSpeed_ = v; }
102void Waterfall::setTurbulence(float v) { turbulence_ = std::max(0.f, v); }
103void Waterfall::setStreakCount(int v) { streakCount_ = std::max(0, v); }
104void Waterfall::setStreakScale(float v) { streakScale_ = std::max(0.1f, v); }
105void Waterfall::setTopFoam(float v) { topFoam_ = std::clamp(v, 0.001f, 0.5f); }
106void Waterfall::setBottomFoam(float v) { bottomFoam_ = std::clamp(v, 0.001f, 0.5f); }
107void Waterfall::setFoamAmount(float v) { foamAmount_ = std::max(0.f, v); }
108void Waterfall::setWaterColor(float r, float g, float b) {
109 waterColor_[0] = r;
110 waterColor_[1] = g;
111 waterColor_[2] = b;
112}
113void Waterfall::setReflectionIntensity(float v) { reflectionIntensity_ = std::max(0.f, v); }
114void Waterfall::setSunIntensity(float v) { sunIntensity_ = std::max(0.f, v); }
115
117 if (!shader_) return;
118 shader_->sendFloat("time", time_);
119 shader_->sendFloat("flowSpeed", flowSpeed_);
120 shader_->sendFloat("turbulence", turbulence_);
121 shader_->sendFloat("streakCnt", float(streakCount_));
122 shader_->sendFloat("streakScale", streakScale_);
123 shader_->sendFloat("topFoam", topFoam_);
124 shader_->sendFloat("bottomFoam", bottomFoam_);
125 shader_->sendFloat("foamAmt", foamAmount_);
126 shader_->sendFloat("reflInten", reflectionIntensity_);
127 shader_->sendFloat("sunInten", sunIntensity_);
128 shader_->sendVec3("waterCol", waterColor_[0], waterColor_[1], waterColor_[2]);
129}
130
132 if (!gfx_ || !mesh_ || !shader_) return;
133 gfx_->drawMeshShader(mesh_, glm::mat4(1.f), nullptr, glm::vec4(1.f), shader_);
134}
135
136} // namespace eve::graphics
vk::ShaderModule frag
int y
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
uint32_t a
uint32_t b
uint32_t c
int width
int idx
int d
int v
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)....
Custom GPU program.
Definition Shader.h:30
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:82
void sendVec3(const std::string &name, float x, float y, float z)
Definition Shader.cpp:89
int declareVec3(const std::string &name)
Definition Shader.cpp:32
int declareFloat(const std::string &name)
Reserve sequential float slots in the push-constant block. Returns start index.
Definition Shader.cpp:30
void setSunIntensity(float intensity)
void setReflectionIntensity(float intensity)
static int paramCount()
Names of the push-constant parameters (for UI / inspection).
Definition Waterfall.cpp:44
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:56
void setStreakCount(int count)
How many layered falling streaks are drawn.
void setFoamAmount(float v)
void draw()
Draw the waterfall sheet (uses default mesh3d camera / lighting state).
void setWaterColor(float r, float g, float b)
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)
void setStreakScale(float scale)
Horizontal stretch of the falling streaks (1 = circular, >1 elongated).
Waterfall(Graphics *gfx)
Definition Waterfall.cpp:51
void update(float dt)
Advance the animation clock by dt seconds.
Definition Waterfall.cpp:91
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)
Definition Waterfall.cpp:96
static std::string paramName(int index)
Definition Waterfall.cpp:46
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition AnimTrail.h:6
Shader * newWaterfallShader(Graphics *gfx)
Create the embedded waterfall fragment shader (owned by Graphics).
Definition Waterfall.cpp:31