载入中...
搜索中...
未找到
Grass.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/Result.h"
7#include "graphics/Shader.h"
8
9#include <cstdint>
10#include <string>
11#include <vector>
12
13#include <glm/glm.hpp>
14
15namespace eve::image {
16class ImageData;
17}
18
19namespace eve::graphics {
20
21class Graphics;
22class Mesh;
23class Texture;
24
36namespace grass {
37
41 float baseR = 1.f, baseG = 1.f, baseB = 1.f;
42 float alphaCutoff = 0.05f;
43 float normalStrength = 1.f;
44 float renderDistance = 75.f;
45 float fadeRange = 20.f;
46 float hardRenderDistance = 105.f;
47 float density = 1.f;
48 float snowMinimumHeight = 100.f;
49 float snowFadeDistance = 20.f;
50 float snowProgress = 0.f;
51 float snowR = 1.f, snowG = 1.f, snowB = 1.f;
52};
53
62
65
68
71 GrassFoliageSettings &foliage, const TerrainDetailOverwriteSettings &settings);
72
74struct Point {
75 glm::vec3 position{0.f};
76 glm::vec3 normal{0.f, 1.f, 0.f};
77 uint32_t id = 0;
78 float scale = 1.f;
80 float widthScale = 0.f;
82 glm::vec3 tint{-1.f};
83};
84
87 float radius = 0.14f;
88 int maxPoints = 8192;
89 uint32_t seed = 1;
91 float minSlopeDot = 0.25f;
92};
93
96 std::vector<float> posXYZ;
97 std::vector<float> nrmXYZ;
98 std::vector<float> uvST;
99 std::vector<uint32_t> indices;
100};
101
103EVENGINE_API_BACKENDS std::vector<Point> samplePoisson(const float *posXYZ, const float *nrmXYZ, int vertexCount,
104 const uint32_t *indices, int indexCount,
105 const SampleParams &params = {});
106
108EVENGINE_API_BACKENDS std::vector<Point> sampleHalton(const float *posXYZ, const float *nrmXYZ, int vertexCount,
109 const uint32_t *indices, int indexCount, int count,
110 uint32_t seed = 1, float minSlopeDot = 0.25f);
111
124EVENGINE_API_BACKENDS BillboardMesh buildBillboards(const std::vector<Point> &points, float width = 0.62f,
125 float height = 0.95f, bool alwaysDark = false);
126
128EVENGINE_API_BACKENDS int swayFrame(float time, float frameDuration, uint32_t instanceId, int frameCount = 4);
129
134EVENGINE_API_BACKENDS void makeSwayAtlasRGBA(int frameW, int frameH, int frames, std::vector<uint8_t> &rgbaOut);
136EVENGINE_API_BACKENDS int swayAtlasWidth(int frameW, int frames);
139
141Texture *createSwayAtlas(Graphics *gfx, int frameW = 64, int frameH = 64, int frames = 4);
142
145 int width = 0;
146 int height = 0;
147 int atlasCols = 2;
148 int atlasRows = 2;
152 int frames = 4;
153};
154
156EVENGINE_API_BACKENDS void packSwayAtlasRGBA(const std::vector<std::string> &grassFiles,
157 const std::vector<std::string> &leafFiles, std::vector<uint8_t> &rgbaOut,
160Texture *createSwayAtlasFromFiles(Graphics *gfx, const std::vector<std::string> &grassFiles,
161 const std::vector<std::string> &leafFiles, PackedAtlasInfo *infoOut = nullptr);
162
168EVENGINE_API_BACKENDS void bindLayer(Shader *shader, bool alwaysDark);
174void setTime(Shader *shader, float seconds);
176void setFrameDuration(Shader *shader, float seconds);
177
181EVENGINE_API_BACKENDS std::string paramName(int index);
182
184EVENGINE_API_BACKENDS void makePlane(float sizeX, float sizeZ, int segX, int segZ, std::vector<float> &posXYZ,
185 std::vector<float> &nrmXYZ, std::vector<uint32_t> &indices);
186
187} // namespace grass
188
194public:
196 struct BakeParams {
198 float denseRadius = 0.14f;
199 float sparseRadius = 0.62f;
200 int maxDense = 8192;
201 int maxSparse = 384;
202 uint32_t seed = 1;
203 float width = 0.62f;
204 float height = 0.95f;
205 float minSlopeDot = 0.25f;
206 int atlasFrameW = 64;
207 int atlasFrameH = 64;
208 int atlasFrames = 4;
210 std::vector<std::string> grassAtlasFiles;
211 std::vector<std::string> leafAtlasFiles;
212 };
213
214 explicit GrassField(Graphics *gfx);
215 ~GrassField();
216
217 GrassField(const GrassField &) = delete;
218 GrassField &operator=(const GrassField &) = delete;
219
220 void bake(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices, int indexCount,
221 const BakeParams &params);
222 void bakePlane(float sizeX, float sizeZ, int segX, int segZ);
223 void bakePlane(float sizeX, float sizeZ, int segX, int segZ, const BakeParams &params);
224
236 [[nodiscard]] Result<int> bakePoints(const std::vector<grass::Point> &points, float width, float height);
242 [[nodiscard]] Result<int> bakeTexturedPoints(const std::vector<grass::Point> &points, float width, float height,
243 const image::ImageData &image);
244
249 [[nodiscard]] Result<int> bakeFoliagePoints(const std::vector<grass::Point> &points, float width, float height,
251 const image::ImageData &mask,
253
259 [[nodiscard]] Result<int> setTerrainDetailOverwrite(const grass::TerrainDetailOverwriteSettings &settings);
261 float getTerrainDetailHardDistance() const noexcept { return detailHardDistance_; }
263 float getTerrainDetailDensity() const noexcept { return detailDensity_; }
265 void setPhotoModeAuthority(bool enabled);
float getPhotoModeDensity() const noexcept{return photoModeDensity_;} float getPhotoModeDistance() const noexcept{return photoModeDistance_;} float getPhotoModeCellDistance() const noexcept{return photoModeCellDistance_;} int getPhotoModeCellSubdivision() const noexcept{return photoModeCellSubdivision_;}
270 PhotoModeFieldAcceptance acceptsPhotoModeField(const PhotoModeAssignment& assignment) const noexcept override;
271 [[nodiscard]] Result<void> applyPhotoModeField(const PhotoModeAssignment& assignment) override;
272
273
274 void update(float dt);
275 void setTime(float seconds);
276 float getTime() const { return time_; }
277 void setFrameDuration(float seconds);
278 float getFrameDuration() const { return frameDuration_; }
279
280 void draw(const glm::mat4 &model);
281 void draw();
282
284 void setReflectionCaptureEnabled(bool enabled) { reflectionCaptureEnabled_ = enabled; }
286 bool getReflectionCaptureEnabled() const { return reflectionCaptureEnabled_; }
288 void setReflectionCaptureMask(uint32_t mask) { reflectionCaptureMask_ = mask; }
290 uint32_t getReflectionCaptureMask() const { return reflectionCaptureMask_; }
291
293 Mesh *getDenseMesh() const { return denseMesh_; }
295 Mesh *getSparseMesh() const { return sparseMesh_; }
297 Shader *getShader() const { return shader_; }
299 Texture *getAtlas() const { return atlas_; }
300
301 int getDenseCount() const { return denseCount_; }
302 int getSparseCount() const { return sparseCount_; }
303
304private:
305 void applyPhotoModeShaderState();
306 [[nodiscard]] Result<int> bakePointData(const std::vector<grass::Point> &points, float width, float height,
307 const image::ImageData *image, const image::ImageData *normal = nullptr,
308 const image::ImageData *mask = nullptr,
309 const grass::GrassFoliageSettings *foliage = nullptr);
310 Graphics *gfx_ = nullptr;
311 Shader *shader_ = nullptr;
312 Texture *atlas_ = nullptr;
313 Texture *normal_ = nullptr;
314 Texture *mask_ = nullptr;
315 Mesh *denseMesh_ = nullptr;
316 Mesh *sparseMesh_ = nullptr;
317 int denseCount_ = 0;
318 int sparseCount_ = 0;
319 float time_ = 0.f;
320 float frameDuration_ = 0.12f;
321 glm::mat4 lastModel_{1.f};
322 uint64_t captureDrawerToken_ = 0;
323 uint32_t reflectionCaptureMask_ = 0xffffffffu;
324 bool reflectionCaptureEnabled_ = true;
325 bool foliageProfile_ = false;
326 float detailHardDistance_ = 105.f;
327 float detailDensity_ = 1.f;
328 float baseDetailRenderDistance_ = 75.f;
329 float baseDetailFadeRange_ = 20.f;
330 float baseDetailHardDistance_ = 105.f;
331 float baseDetailDensity_ = 1.f;
332 bool photoModeAuthority_ = false;
333 float photoModeDensity_ = 1.f;
334 float photoModeDistance_ = 1.f;
335 float photoModeCellDistance_ = 1.f;
336 int photoModeCellSubdivision_ = 0;
337};
338
339} // namespace eve::graphics
int mask
graphics::Texture * albedo
#define EVENGINE_API_BACKENDS
Definition Export.h:108
std::uint32_t vertexCount
std::uint32_t indexCount
vk::UniqueImage image
std::vector< std::uint32_t > indices
std::uint32_t height
std::uint32_t width
MeshVfxBatchedDraw draw
Texture * normal
std::uint32_t seed
Definition PointSet.cpp:807
std::shared_ptr< const std::vector< glm::vec2 > > points
Shader * shader
glm::mat4 model
Move-only, checked operation results for the common layer.
std::uint32_t count
TerrainThermalSettings settings
uint32_t index
IPhotoModeFieldSink public API.
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Dense grass + sparse dark tufts on a mesh. Caller owns GrassField*; GPU Mesh / Shader / Texture are o...
Definition Grass.h:193
void setReflectionCaptureMask(uint32_t mask)
Set the reflection-capture visibility layer mask.
Definition Grass.h:288
uint32_t getReflectionCaptureMask() const
Return the reflection-capture visibility layer mask.
Definition Grass.h:290
bool getReflectionCaptureEnabled() const
Return whether this field is included in reflection-probe captures.
Definition Grass.h:286
Shader * getShader() const
Borrow the shader on the owner thread; valid until this field is destroyed.
Definition Grass.h:297
GrassField(const GrassField &)=delete
Mesh * getDenseMesh() const
Borrow the dense mesh on the owner thread; valid until this field rebakes or is destroyed.
Definition Grass.h:293
void setReflectionCaptureEnabled(bool enabled)
Enable or disable inclusion in reflection-probe captures.
Definition Grass.h:284
Texture * getAtlas() const
Borrow the atlas on the owner thread; valid until this field uploads another atlas or is destroyed.
Definition Grass.h:299
GrassField & operator=(const GrassField &)=delete
Mesh * getSparseMesh() const
Borrow the sparse mesh on the owner thread; valid until this field rebakes or is destroyed.
Definition Grass.h:295
int getDenseCount() const
Definition Grass.h:301
float getTerrainDetailHardDistance() const noexcept
Return the currently applied Unity-style hard detail distance.
Definition Grass.h:261
float getTerrainDetailDensity() const noexcept
Return the currently applied normalized detail density.
Definition Grass.h:263
float getTime() const
Definition Grass.h:276
int getSparseCount() const
Definition Grass.h:302
float getFrameDuration() const
Definition Grass.h:278
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:25
Custom GPU program.
Definition Shader.h:39
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
Represents raw pixel data.
Definition ImageData.h:38
std::vector< ParamSpec > params
void bindLayer(Shader *shader, bool alwaysDark)
Binds layer.
Definition Grass.cpp:291
void setFrameDuration(Shader *shader, float seconds)
Sets the frame duration.
Definition Grass.cpp:301
EVENGINE_API_BACKENDS TerrainDetailQuality terrainDetailQuality(int resolutionPerPatch) noexcept
Classify a Unity terrain detail patch resolution with Pcg's exact default mapping.
int swayFrame(float time, float frameDuration, uint32_t instanceId, int frameCount)
Discrete 4-frame index with a per-instance phase offset.
Definition Grass.cpp:322
Texture * createSwayAtlasFromFiles(Graphics *gfx, const std::vector< std::string > &grassFiles, const std::vector< std::string > &leafFiles, PackedAtlasInfo *infoOut)
Creates sway atlas from files.
Definition Grass.cpp:471
EVENGINE_API_BACKENDS void bindFoliage(Shader *shader, const GrassFoliageSettings &settings)
Select the static foliage profile and copy its validated surface parameters.
Shader * createShader(Graphics *gfx)
Creates shader.
Definition Grass.cpp:306
int swayAtlasWidth(int frameW, int frames)
Sway atlas width.
Definition Grass.cpp:331
EVENGINE_API_BACKENDS int paramCount()
Param count.
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:484
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:334
Texture * createSwayAtlas(Graphics *gfx, int frameW, int frameH, int frames)
Creates sway atlas.
Definition Grass.cpp:348
TerrainDetailQuality
Pcg quality category derived from detailResolutionPerPatch.
Definition Grass.h:64
EVENGINE_API_BACKENDS std::string paramName(int index)
Param name.
EVENGINE_API_BACKENDS void bindDefaults(Shader *shader)
Binds defaults.
int swayAtlasHeight(int frameH)
Sway atlas height.
Definition Grass.cpp:332
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:511
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:417
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:550
void setTime(Shader *shader, float seconds)
Sets the time.
Definition Grass.cpp:296
EVENGINE_API_BACKENDS Result< int > applyTerrainDetailOverwrite(GrassFoliageSettings &foliage, const TerrainDetailOverwriteSettings &settings)
Validate and copy TerrainDetailOverwrite distances/density into a foliage profile atomically.
EVENGINE_API_BACKENDS BillboardMesh buildBillboards(const std::vector< Point > &points, float width=0.62f, float height=0.95f, bool alwaysDark=false)
Expand a unit rectangle per point. Vertex layout: pos = grass root uv = quad corner in [0,...
void bindAtlasLayout(Shader *shader, const PackedAtlasInfo &info)
Binds atlas layout.
Definition Grass.cpp:281
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
PhotoModeFieldAcceptance
Capability implemented by the composition root that routes assignments to domain owners.
bool enabled
BakeParams public API.
Definition Grass.h:196
std::vector< std::string > grassAtlasFiles
Authored 2x2 sway masks (4 grass + 2 leaf). Empty = procedural strip.
Definition Grass.h:210
std::vector< std::string > leafAtlasFiles
Definition Grass.h:211
BillboardMesh public API.
Definition Grass.h:95
std::vector< float > nrmXYZ
Definition Grass.h:97
std::vector< float > posXYZ
Definition Grass.h:96
std::vector< uint32_t > indices
Definition Grass.h:99
std::vector< float > uvST
Definition Grass.h:98
Pcg-style static foliage surface controls packed into the grass shader profile. Values are copied dur...
Definition Grass.h:40
Layout of a packed 2x2-per-variant sway atlas (4 grass + 2 leaf typical).
Definition Grass.h:144
Point public API.
Definition Grass.h:74
float widthScale
Independent horizontal scale; zero retains the uniform scale behavior.
Definition Grass.h:80
glm::vec3 tint
Linear instance RGB; {-1,-1,-1} preserves legacy untinted encoding.
Definition Grass.h:82
SampleParams public API.
Definition Grass.h:86
float minSlopeDot
Skip faces whose Y-up slope is below this (0 = keep walls).
Definition Grass.h:91
Runtime terrain-detail overrides kept distinct from foliage material settings.
Definition Grass.h:55
glm::uvec4 info