载入中...
搜索中...
未找到
Grass.h
浏览该文件的文档.
1#pragma once
2
3#include "graphics/Shader.h"
4
5#include <cstdint>
6#include <string>
7#include <vector>
8
9#include <glm/glm.hpp>
10
11namespace eve::graphics {
12
13class Graphics;
14class Mesh;
15class Texture;
16
28namespace grass {
29
30struct Point {
31 glm::vec3 position{0.f};
32 glm::vec3 normal{0.f, 1.f, 0.f};
33 uint32_t id = 0;
34 float scale = 1.f;
35};
36
38 float radius = 0.14f;
39 int maxPoints = 8192;
40 uint32_t seed = 1;
42 float minSlopeDot = 0.25f;
43};
44
46 std::vector<float> posXYZ;
47 std::vector<float> nrmXYZ;
48 std::vector<float> uvST;
49 std::vector<uint32_t> indices;
50};
51
53std::vector<Point> samplePoisson(const float *posXYZ, const float *nrmXYZ, int vertexCount,
54 const uint32_t *indices, int indexCount,
55 const SampleParams &params = {});
56
58std::vector<Point> sampleHalton(const float *posXYZ, const float *nrmXYZ, int vertexCount,
59 const uint32_t *indices, int indexCount, int count,
60 uint32_t seed = 1, float minSlopeDot = 0.25f);
61
68BillboardMesh buildBillboards(const std::vector<Point> &points, float width = 0.62f,
69 float height = 0.95f, bool alwaysDark = false);
70
72int swayFrame(float time, float frameDuration, uint32_t instanceId, int frameCount = 4);
73
78void makeSwayAtlasRGBA(int frameW, int frameH, int frames, std::vector<uint8_t> &rgbaOut);
79int swayAtlasWidth(int frameW, int frames);
80int swayAtlasHeight(int frameH);
81
82Texture *createSwayAtlas(Graphics *gfx, int frameW = 64, int frameH = 64, int frames = 4);
83
86 int width = 0;
87 int height = 0;
88 int atlasCols = 2;
89 int atlasRows = 2;
91 int leafVariants = 1;
93 int frames = 4;
94};
95
97void packSwayAtlasRGBA(const std::vector<std::string> &grassFiles,
98 const std::vector<std::string> &leafFiles, std::vector<uint8_t> &rgbaOut,
99 PackedAtlasInfo &info);
100Texture *createSwayAtlasFromFiles(Graphics *gfx, const std::vector<std::string> &grassFiles,
101 const std::vector<std::string> &leafFiles,
102 PackedAtlasInfo *infoOut = nullptr);
103
106void bindLayer(Shader *shader, bool alwaysDark);
107void bindAtlasLayout(Shader *shader, const PackedAtlasInfo &info);
108void setTime(Shader *shader, float seconds);
109void setFrameDuration(Shader *shader, float seconds);
110
111int paramCount();
112std::string paramName(int index);
113
115void makePlane(float sizeX, float sizeZ, int segX, int segZ, std::vector<float> &posXYZ,
116 std::vector<float> &nrmXYZ, std::vector<uint32_t> &indices);
117
118} // namespace grass
119
125public:
126 struct BakeParams {
128 float denseRadius = 0.14f;
129 float sparseRadius = 0.62f;
130 int maxDense = 8192;
131 int maxSparse = 384;
132 uint32_t seed = 1;
133 float width = 0.62f;
134 float height = 0.95f;
135 float minSlopeDot = 0.25f;
136 int atlasFrameW = 64;
137 int atlasFrameH = 64;
138 int atlasFrames = 4;
140 std::vector<std::string> grassAtlasFiles;
141 std::vector<std::string> leafAtlasFiles;
142 };
143
144 explicit GrassField(Graphics *gfx);
145 ~GrassField() = default;
146
147 GrassField(const GrassField &) = delete;
148 GrassField &operator=(const GrassField &) = delete;
149
150 void bake(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices,
151 int indexCount, const BakeParams &params);
152 void bakePlane(float sizeX, float sizeZ, int segX, int segZ);
153 void bakePlane(float sizeX, float sizeZ, int segX, int segZ, const BakeParams &params);
154
155 void update(float dt);
156 void setTime(float seconds);
157 float getTime() const { return time_; }
158 void setFrameDuration(float seconds);
159 float getFrameDuration() const { return frameDuration_; }
160
161 void draw(const glm::mat4 &model);
162 void draw();
163
164 Mesh *getDenseMesh() const { return denseMesh_; }
165 Mesh *getSparseMesh() const { return sparseMesh_; }
166 Shader *getShader() const { return shader_; }
167 Texture *getAtlas() const { return atlas_; }
168
169 int getDenseCount() const { return denseCount_; }
170 int getSparseCount() const { return sparseCount_; }
171
172private:
173 Graphics *gfx_ = nullptr;
174 Shader *shader_ = nullptr;
175 Texture *atlas_ = nullptr;
176 Mesh *denseMesh_ = nullptr;
177 Mesh *sparseMesh_ = nullptr;
178 int denseCount_ = 0;
179 int sparseCount_ = 0;
180 float time_ = 0.f;
181 float frameDuration_ = 0.12f;
182};
183
184} // namespace eve::graphics
uint32_t seed
float height
Definition Grass.cpp:235
int width
Shader * shader
glm::mat4 model
std::vector< V3 > points
Definition TreeMesh.cpp:126
Dense grass + sparse dark tufts on a mesh. Caller owns GrassField*; GPU Mesh / Shader / Texture are o...
Definition Grass.h:124
void setFrameDuration(float seconds)
Definition Grass.cpp:752
Shader * getShader() const
Definition Grass.h:166
GrassField(const GrassField &)=delete
Mesh * getDenseMesh() const
Definition Grass.h:164
Texture * getAtlas() const
Definition Grass.h:167
GrassField & operator=(const GrassField &)=delete
Mesh * getSparseMesh() const
Definition Grass.h:165
int getDenseCount() const
Definition Grass.h:169
void bake(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices, int indexCount, const BakeParams &params)
Definition Grass.cpp:669
void bakePlane(float sizeX, float sizeZ, int segX, int segZ)
Definition Grass.cpp:731
void setTime(float seconds)
Definition Grass.cpp:747
float getTime() const
Definition Grass.h:157
void update(float dt)
Definition Grass.cpp:742
int getSparseCount() const
Definition Grass.h:170
float getFrameDuration() const
Definition Grass.h:159
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:18
Custom GPU program.
Definition Shader.h:30
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:17
void makeSwayAtlasRGBA(int frameW, int frameH, int frames, std::vector< uint8_t > &rgbaOut)
Procedural 4-frame fallback atlas (horizontal strip). GPU paths should load authored 2x2 PNG masks vi...
Definition Grass.cpp:369
int swayAtlasWidth(int frameW, int frames)
Definition Grass.cpp:366
void setFrameDuration(Shader *shader, float seconds)
Definition Grass.cpp:341
void makePlane(float sizeX, float sizeZ, int segX, int segZ, std::vector< float > &posXYZ, std::vector< float > &nrmXYZ, std::vector< uint32_t > &indices)
Unit XZ plane (Y-up) for tests / demos.
Definition Grass.cpp:618
Shader * createShader(Graphics *gfx)
Definition Grass.cpp:346
void packSwayAtlasRGBA(const std::vector< std::string > &grassFiles, const std::vector< std::string > &leafFiles, std::vector< uint8_t > &rgbaOut, PackedAtlasInfo &info)
Load white-on-black (or RGBA) 2x2 sway PNGs and pack them into one atlas.
Definition Grass.cpp:451
std::string paramName(int index)
Definition Grass.cpp:284
void bindDefaults(Shader *shader)
Definition Grass.cpp:289
int swayFrame(float time, float frameDuration, uint32_t instanceId, int frameCount)
Discrete 4-frame index with a per-instance phase offset.
Definition Grass.cpp:357
std::vector< Point > sampleHalton(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices, int indexCount, int count, uint32_t seed, float minSlopeDot)
Area-weighted Halton samples (deterministic, evenly spread).
Definition Grass.cpp:518
Texture * createSwayAtlasFromFiles(Graphics *gfx, const std::vector< std::string > &grassFiles, const std::vector< std::string > &leafFiles, PackedAtlasInfo *infoOut)
Definition Grass.cpp:505
int swayAtlasHeight(int frameH)
Definition Grass.cpp:367
std::vector< Point > samplePoisson(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices, int indexCount, const SampleParams &params)
Fast Poisson-disk / dart-throwing blue noise on a triangle mesh.
Definition Grass.cpp:545
void setTime(Shader *shader, float seconds)
Definition Grass.cpp:336
Texture * createSwayAtlas(Graphics *gfx, int frameW, int frameH, int frames)
Definition Grass.cpp:383
void bindLayer(Shader *shader, bool alwaysDark)
Definition Grass.cpp:331
BillboardMesh buildBillboards(const std::vector< Point > &points, float width, float height, bool alwaysDark)
Expand a unit rectangle per point. Vertex layout: pos = grass root uv = quad corner in [0,...
Definition Grass.cpp:583
void bindAtlasLayout(Shader *shader, const PackedAtlasInfo &info)
Definition Grass.cpp:321
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition AnimTrail.h:6
std::vector< std::string > grassAtlasFiles
Authored 2x2 sway masks (4 grass + 2 leaf). Empty = procedural strip.
Definition Grass.h:140
std::vector< std::string > leafAtlasFiles
Definition Grass.h:141
float denseRadius
Poisson spacing. Keep this well below width so tufts overlap.
Definition Grass.h:128
std::vector< float > nrmXYZ
Definition Grass.h:47
std::vector< float > posXYZ
Definition Grass.h:46
std::vector< uint32_t > indices
Definition Grass.h:49
std::vector< float > uvST
Definition Grass.h:48
Layout of a packed 2x2-per-variant sway atlas (4 grass + 2 leaf typical).
Definition Grass.h:85
float minSlopeDot
Skip faces whose Y-up slope is below this (0 = keep walls).
Definition Grass.h:42