载入中...
搜索中...
未找到
GrassWind.cpp
浏览该文件的文档.
2#include <array>
3#include <cmath>
4#include <string>
5#include "common/Assert.h"
7#include "graphics/Shader.h"
8#include "graphics/TreeWind.h"
10
11// GrassWind.cpp
12namespace eve::graphics {
14 double seconds) {
15 auto packet = packVegetationWind(state, profile, seconds);
16 if (!packet.ok()) return Result<void>::failure(packet.status());
17 // Construct names before publication. Existing uniform writes only check schema and copy bytes.
18 static const std::array<std::string, 5> names = {"windGlobals", "windPhaseDistance", "windFlex", "windFrequency",
19 "windSineTime"};
20 constexpr std::array<int, 5> offsets{18, 22, 24, 27, 30};
21 constexpr std::array<int, 5> sizes{4, 2, 3, 3, 2};
22 for (size_t i = 0; i < names.size(); ++i) {
23 if (shader.usedFloats() != 32 || !shader.hasUniform(names[i]) ||
24 shader.getUniformIndex(names[i]) != offsets[i] || shader.getUniformFloatCount(names[i]) != sizes[i])
26 "grass.wind: canonical grass wind uniform layout required"));
27 }
28 for (size_t i = 0; i < names.size(); ++i) {
29 const size_t bytes = size_t(sizes[i]) * sizeof(float);
30 const int written = shader.sendToVar(names[i], packet.value().data() + offsets[i] - 18, bytes);
31 EV_ASSERT(written == int(bytes));
32 }
33 return Result<void>::success();
34}
35} // namespace eve::graphics
36
37// TreeWind.cpp
38namespace eve::graphics {
39namespace {} // namespace
40
42 const TreeWindProfile& tree, double seconds) {
43 if (!std::isfinite(tree.widthHeight.x) || tree.widthHeight.x <= 0 || !std::isfinite(tree.widthHeight.y) ||
44 tree.widthHeight.y <= 0 || !std::isfinite(tree.bendFactor) || tree.bendFactor < 0)
46 "tree.wind: positive finite dimensions and bend required"));
47 auto scaled = vegetation;
48 scaled.flex *= tree.bendFactor;
49 auto packet = packVegetationWind(state, scaled, seconds);
50 if (!packet.ok()) return Result<void>::failure(packet.status());
51 static const std::array<std::string, 6> names = {"windGlobals", "windPhaseDistance", "windFlex",
52 "windFrequency", "windSineTime", "treeDimensions"};
53 constexpr std::array<int, 6> offsets{0, 4, 6, 9, 12, 14};
54 constexpr std::array<int, 6> sizes{4, 2, 3, 3, 2, 2};
55 for (size_t i = 0; i < names.size(); ++i)
56 if (shader.usedFloats() != 16 || !shader.hasUniform(names[i]) ||
57 shader.getUniformIndex(names[i]) != offsets[i] || shader.getUniformFloatCount(names[i]) != sizes[i])
59 "tree.wind: canonical 16-float shader layout required"));
60 std::array<float, 16> values{};
61 std::copy(packet.value().begin(), packet.value().end(), values.begin());
62 values[14] = tree.widthHeight.x;
63 values[15] = tree.widthHeight.y;
64 for (size_t i = 0; i < names.size(); ++i) {
65 const auto bytes = size_t(sizes[i]) * sizeof(float);
66 const int written = shader.sendToVar(names[i], values.data() + offsets[i], bytes);
67 EV_ASSERT(written == int(bytes));
68 }
69 return Result<void>::success();
70}
71} // namespace eve::graphics
72
73// GrassWindBindings.cpp
74namespace eve::graphics {
75namespace {} // namespace
76void exposeGrassWindBindings(ssq::Table& table) {
77 auto state = table.addClass("VegetationWindState", ssq::Class::Ctor<VegetationWindState()>());
78 state.addVar("strength", &VegetationWindState::strength);
79 state.addVar("phase", &VegetationWindState::phase);
80 state.addVar("updatePhase", &VegetationWindState::updatePhase);
81 state.addFunc("setDirection", [](VegetationWindState* s, float x, float y, float z) { s->direction = {x, y, z}; });
82 state.addFunc("getDirectionX", [](const VegetationWindState* s) { return s->direction.x; });
83 state.addFunc("getDirectionY", [](const VegetationWindState* s) { return s->direction.y; });
84 state.addFunc("getDirectionZ", [](const VegetationWindState* s) { return s->direction.z; });
85 auto audioState = table.addClass("VegetationWindAudioState", ssq::Class::Ctor<VegetationWindAudioState()>());
86 audioState.addVar("currentWindSpeed", &VegetationWindAudioState::currentWindSpeed);
87 audioState.addVar("anchorVolume", &VegetationWindAudioState::anchorVolume);
88 audioState.addVar("volume", &VegetationWindAudioState::volume);
89 audioState.addVar("processing", &VegetationWindAudioState::processing);
90 audioState.addVar("playing", &VegetationWindAudioState::playing);
91 auto profile = table.addClass("VegetationWindProfile", ssq::Class::Ctor<VegetationWindProfile()>());
92 profile.addVar("maximumDistance", &VegetationWindProfile::maximumDistance);
93 profile.addVar("enabled", &VegetationWindProfile::enabled);
94 profile.addVar("billboard", &VegetationWindProfile::billboard);
95 profile.addVar("alphaTest", &VegetationWindProfile::alphaTest);
96 profile.addFunc("setFlex", [](VegetationWindProfile* p, float x, float y, float z) { p->flex = {x, y, z}; });
97 profile.addFunc("setFrequency",
98 [](VegetationWindProfile* p, float x, float y, float z) { p->frequency = {x, y, z}; });
99 auto tree = table.addClass("TreeWindProfile", ssq::Class::Ctor<TreeWindProfile()>());
100 tree.addVar("bendFactor", &TreeWindProfile::bendFactor);
101 tree.addFunc("setDimensions",
102 [](TreeWindProfile* p, float width, float height) { p->widthHeight = {width, height}; });
103 table.addFunc("advanceVegetationWind", [vm = table.getHandle()](VegetationWindState* s, float x, float y, float z,
104 float strength, float dt) {
105 auto result =
106 s ? advanceVegetationWind(*s, {x, y, z}, strength, dt)
108 "grass.wind: non-null shader, state and profile required"));
109 return eve::script::projectResult(vm, std::move(result));
110 });
111 table.addFunc("initializeVegetationWind", [vm = table.getHandle()](VegetationWindState* s, float x, float y,
112 float z, float strength) {
113 auto result =
115 : Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument,
116 "grass.wind: non-null shader, state and profile required"));
117 return eve::script::projectResult(vm, std::move(result));
118 });
119 table.addFunc("applyGrassWind", [vm = table.getHandle()](Shader* shader, const VegetationWindState* s,
120 const VegetationWindProfile* p, float seconds) {
121 auto result =
122 shader && s && p
123 ? applyGrassWind(*shader, *s, *p, seconds)
125 "grass.wind: non-null shader, state and profile required"));
126 return eve::script::projectResult(vm, std::move(result));
127 });
128 table.addFunc("applyTreeWind", [vm = table.getHandle()](Shader* shader, const VegetationWindState* s,
129 const VegetationWindProfile* p,
130 const TreeWindProfile* treeProfile, float seconds) {
131 auto result =
132 shader && s && p && treeProfile
133 ? applyTreeWind(*shader, *s, *p, *treeProfile, seconds)
135 "grass.wind: non-null shader, state and profile required"));
136 return eve::script::projectResult(vm, std::move(result));
137 });
138 table.addFunc("advanceVegetationWindAudio", [vm = table.getHandle()](VegetationWindAudioState* state,
139 float strength, float transitionTime, float dt,
140 bool enabled, bool clipAvailable) {
141 auto result =
142 state ? advanceVegetationWindAudio(*state, strength, transitionTime, dt, enabled, clipAvailable)
144 "grass.wind: non-null shader, state and profile required"));
145 return eve::script::projectResult(vm, std::move(result));
146 });
147}
148} // namespace eve::graphics
double z
double y
double x
EVEngine assertion entry point, backed by zeroerr.
#define EV_ASSERT(cond,...)
Assert an internal engine invariant (state that must always hold).
Definition Assert.h:37
const std::string & s
glm::vec4 p[6]
HSQUIRRELVM vm
Definition ECS.cpp:38
float strength
std::uint32_t height
std::uint32_t width
Poco::JSON::Object::Ptr result
std::uint64_t bytes
double dt
std::string error
Definition Package.cpp:174
std::set< std::string > values
Shader * shader
The single Squirrel projection for common Result, Status and Value.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Custom GPU program.
Definition Shader.h:41
bool hasUniform(const std::string &name) const
Definition Shader.cpp:71
int usedFloats() const
Definition Shader.h:123
int getUniformIndex(const std::string &name) const
Definition Shader.cpp:73
int getUniformFloatCount(const std::string &name) const
Definition Shader.cpp:79
int sendToVar(const std::string &name, const void *data, size_t size)
Low-level blob write into a declared uniform (size must match declaration).
Definition Shader.cpp:85
eve::Diagnostic Diagnostic
eve::Result< T > Result
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Result< std::array< float, 14 > > packVegetationWind(const VegetationWindState &state, const VegetationWindProfile &profile, double seconds)
Pack a wind snapshot into the grass shader's final 14 float slots.
void exposeGrassWindBindings(ssq::Table &table)
Register wind state/profile and checked operations on the VM owner thread, retaining no table referen...
Definition GrassWind.cpp:76
Result< void > applyTreeWind(Shader &shader, const VegetationWindState &state, const VegetationWindProfile &vegetation, const TreeWindProfile &tree, double seconds)
Atomically publish one validated tree wind snapshot to CPU-side shader parameters.
Definition GrassWind.cpp:41
Result< void > initializeVegetationWind(VegetationWindState &state, glm::vec3 forward, float strength)
Apply Pcg WindManager.InstantWindApply semantics without smoothing.
Result< void > advanceVegetationWindAudio(VegetationWindAudioState &state, float windStrength, float transitionTime, float dt, bool enabled, bool clipAvailable)
Advance Pcg WindManager CheckWindVolume and ProcessWindAudio as a pure state transition.
Result< void > applyGrassWind(Shader &shader, const VegetationWindState &state, const VegetationWindProfile &profile, double seconds)
Atomically apply a validated wind snapshot to a grass shader's CPU uniform storage.
Definition GrassWind.cpp:13
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
bool enabled
Caller-owned tree prototype wind geometry matching Pcg ResourceProtoTree inputs.
Definition TreeWind.h:12
glm::vec2 widthHeight
Object-space crown width and source prototype height; both must be positive.
Definition TreeWind.h:14
float bendFactor
Pcg tree prototype bend factor. Zero disables deformation.
Definition TreeWind.h:16
Caller-owned state for Pcg WindManager's looping wind-audio volume controller.
Caller-owned material wind controls; billboard suppresses source branch and leaf motion....
Caller-owned wind globals; no hidden clock, singleton or retained scene link.
float updatePhase
WindManager's private update accumulator; InstantWindApply deliberately preserves it.
float phase
Phase currently published to shaders.