载入中...
搜索中...
未找到
VegetationField.cpp
浏览该文件的文档.
2
3#include <utility>
4
5#include <algorithm>
6#include <cmath>
7#include <glm/common.hpp>
8#include <glm/geometric.hpp>
9#include <limits>
10#include <new>
11
12namespace eve::graphics {
13namespace {
14bool finite(glm::vec3 v) { return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z); }
15bool finite(glm::vec4 v) { return finite(glm::vec3(v)) && std::isfinite(v.w); }
16bool unit(float v) { return std::isfinite(v) && v >= 0.f && v <= 1.f; }
17bool layersValid(std::array<uint8_t, 4> layers) {
18 return std::all_of(layers.begin(), layers.end(), [](uint8_t layer) { return layer <= 8; });
19}
20Diagnostic invalid(std::string message) {
21 return Diagnostic::error(DiagnosticCode::InvalidArgument, std::move(message), {}, {}, "graphics.vegetation");
22}
23Diagnostic exhausted() {
24 return Diagnostic::error(DiagnosticCode::Failed, "vegetation allocation failed", {}, {}, "graphics.vegetation");
25}
26glm::vec4 maskAt(const VegetationMask& mask, float u, float v) {
27 if (mask.pixels.empty()) return glm::vec4(1.f);
28 const float x = std::clamp(u * mask.width - 0.5f, 0.f, float(mask.width - 1));
29 const float y = std::clamp(v * mask.height - 0.5f, 0.f, float(mask.height - 1));
30 const auto x0 = uint32_t(x), y0 = uint32_t(y);
31 const auto x1 = std::min(x0 + 1, mask.width - 1), y1 = std::min(y0 + 1, mask.height - 1);
32 return glm::mix(
33 glm::mix(mask.pixels[size_t(y0) * mask.width + x0], mask.pixels[size_t(y0) * mask.width + x1], x - x0),
34 glm::mix(mask.pixels[size_t(y1) * mask.width + x0], mask.pixels[size_t(y1) * mask.width + x1], x - x0), y - y0);
35}
36glm::vec4 seasonalValue(const VegetationElement& e, float season) {
37 if (!e.seasonal) return e.value;
38 const float wrapped = season == 4.f ? 0.f : season;
39 const auto i = unsigned(wrapped);
40 const float f = wrapped - i;
41 return glm::mix(e.seasons[i], e.seasons[(i + 1) % 4], f * f * (3.f - 2.f * f));
42}
43glm::vec4 blend(glm::vec4 previous, glm::vec4 target, float weight, VegetationBlend mode) {
44 switch (mode) {
45 case VegetationBlend::Replace: return glm::mix(previous, target, weight);
47 case VegetationBlend::Multiply: return previous * glm::mix(glm::vec4(1.f), target, weight);
48 case VegetationBlend::Minimum: return glm::mix(previous, glm::min(previous, target), weight);
49 case VegetationBlend::Maximum: return glm::mix(previous, glm::max(previous, target), weight);
50 }
51 return previous;
52}
53} // namespace
54
56 try {
57 return Result<std::vector<VegetationElement>>::success(elements_);
58 } catch (const std::bad_alloc&) {
60 Diagnostic::error(DiagnosticCode::Failed, "vegetation element snapshot allocation failed", {},
61 {}, "graphics.vegetation"));
62 }
63}
64
65Result<void> VegetationField::replace(const VegetationGlobals& globals, std::span<const VegetationElement> elements) {
66 if (revision_ == std::numeric_limits<uint64_t>::max())
67 return Result<void>::failure(invalid("field publication revision exhausted"));
68 if (!finite(globals.color) || !finite(globals.extras) || !finite(globals.motion) || !finite(globals.vertex) ||
69 !std::isfinite(globals.season) || globals.season < 0.f || globals.season > 4.f || elements.size() > 4096)
70 return Result<void>::failure(invalid("invalid globals or element count exceeds 4096"));
71 size_t totalPixels = 0;
72 for (const auto& e : elements) {
73 if (unsigned(e.channel) > unsigned(VegetationChannel::Vertex) ||
74 unsigned(e.blend) > unsigned(VegetationBlend::Maximum) ||
75 unsigned(e.shape) > unsigned(VegetationShape::Box) || !finite(e.center) || !finite(e.extents) ||
76 e.extents.x < 1e-4f || e.extents.y < 1e-4f || e.extents.z < 1e-4f || !std::isfinite(e.yaw) ||
77 !unit(e.opacity) || !unit(e.edgeFade) || !finite(e.value) || e.layers == 0 || (e.layers & ~0x1ffu))
78 return Result<void>::failure(invalid("invalid element shape, channel, blend, layers or parameters"));
79 for (const auto& value : e.seasons)
80 if (!finite(value)) return Result<void>::failure(invalid("non-finite season value"));
81 const auto& m = e.mask;
82 if ((m.pixels.empty() && (m.width != 0 || m.height != 0)) ||
83 (!m.pixels.empty() && (m.width == 0 || m.height == 0 || m.width > 2048 || m.height > 2048 ||
84 m.pixels.size() != size_t(m.width) * m.height)))
85 return Result<void>::failure(invalid("invalid mask dimensions or pixel count"));
86 totalPixels += m.pixels.size();
87 if (totalPixels > 4 * 1024 * 1024) return Result<void>::failure(invalid("field mask pixel budget exceeded"));
88 for (auto pixel : m.pixels)
89 if (!finite(pixel) || !unit(pixel.a)) return Result<void>::failure(invalid("invalid mask pixel"));
90 }
91 try {
92 std::vector<VegetationElement> candidate(elements.begin(), elements.end());
93 std::stable_sort(candidate.begin(), candidate.end(),
94 [](const auto& a, const auto& b) { return a.priority < b.priority; });
95 elements_.swap(candidate);
96 globals_ = globals;
97 ++revision_;
98 return Result<void>::success();
99 } catch (const std::bad_alloc&) {
100 return Result<void>::failure(exhausted());
101 }
102}
103
104VegetationSample VegetationField::evaluate(glm::vec3 position, std::array<uint8_t, 4> layers) const {
105 std::array<glm::vec4, 4> values{globals_.color, globals_.extras, globals_.motion, globals_.vertex};
106 for (const auto& e : elements_) {
107 const size_t channel = size_t(e.channel);
108 if (!(e.layers & (1u << layers[channel]))) continue;
109 const auto d = position - e.center;
110 const float c = std::cos(e.yaw), s = std::sin(e.yaw);
111 const glm::vec3 q = glm::vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z) / e.extents;
112 if (!finite(q)) {
113 const glm::vec4 invalidValue(std::numeric_limits<float>::quiet_NaN());
114 return {invalidValue, invalidValue, invalidValue, invalidValue};
115 }
116 const float distance = e.shape == VegetationShape::Ellipsoid
117 ? glm::length(q)
118 : std::max({std::abs(q.x), std::abs(q.y), std::abs(q.z)});
119 if (distance >= 1.f) continue;
120 const float t = e.edgeFade > 0.f ? std::clamp((1.f - distance) / e.edgeFade, 0.f, 1.f) : 1.f;
121 const auto mask = maskAt(e.mask, q.x * 0.5f + 0.5f, q.z * 0.5f + 0.5f);
122 auto target = seasonalValue(e, globals_.season);
123 target *= glm::vec4(glm::vec3(mask), 1.f);
124 values[channel] = blend(values[channel], target, t * t * (3.f - 2.f * t) * e.opacity * mask.a, e.blend);
125 }
126 return {values[0], values[1], values[2], values[3]};
127}
128
129Result<VegetationSample> VegetationField::sample(glm::vec3 position, std::array<uint8_t, 4> layers) const {
130 if (!finite(position) || !layersValid(layers))
131 return Result<VegetationSample>::failure(invalid("invalid sample position or layer"));
132 auto result = evaluate(position, layers);
133 if (!finite(result.color) || !finite(result.extras) || !finite(result.motion) || !finite(result.vertex))
134 return Result<VegetationSample>::failure(invalid("field composition overflow"));
136}
137
138Result<VegetationAtlas> VegetationField::bake(glm::vec3 center, glm::vec3 extent, uint32_t width, uint32_t height,
139 std::array<uint8_t, 4> layers, bool snapToTexel) const {
140 if (!finite(center) || !finite(extent) || extent.x < 1e-4f || extent.y < 1e-4f || extent.z < 1e-4f || width == 0 ||
141 height == 0 || width > 2048 || height > 2048 || !layersValid(layers))
142 return Result<VegetationAtlas>::failure(invalid("invalid atlas geometry, dimensions or layer"));
143 const double dx = 2.0 * extent.x / width, dz = 2.0 * extent.z / height;
144 if (snapToTexel) {
145 center.x = float(std::round(double(center.x) / dx) * dx);
146 center.z = float(std::round(double(center.z) / dz) * dz);
147 }
148 if (!finite(center) || !finite(center + extent) || !finite(center - extent))
149 return Result<VegetationAtlas>::failure(invalid("atlas bounds overflow"));
150 try {
151 VegetationAtlas atlas;
152 atlas.width = width;
153 atlas.height = height;
154 atlas.center = center;
155 atlas.extent = extent;
156 for (auto& channel : atlas.channels) channel.resize(size_t(width) * height);
157 for (uint32_t y = 0; y < height; ++y) {
158 for (uint32_t x = 0; x < width; ++x) {
159 const glm::vec3 p(float(double(center.x) - extent.x + (x + 0.5) * dx), center.y,
160 float(double(center.z) - extent.z + (y + 0.5) * dz));
161 auto result = sample(p, layers);
162 if (!result.ok()) return Result<VegetationAtlas>::failure(result.status());
163 const auto& v = result.value();
164 const size_t index = size_t(y) * width + x;
165 atlas.channels[0][index] = v.color;
166 atlas.channels[1][index] = v.extras;
167 atlas.channels[2][index] = v.motion;
168 atlas.channels[3][index] = v.vertex;
169 }
170 }
171 return Result<VegetationAtlas>::success(std::move(atlas));
172 } catch (const std::bad_alloc&) {
173 return Result<VegetationAtlas>::failure(exhausted());
174 }
175}
176} // namespace eve::graphics
double y
double m[16]
double x
LogicalId target
const std::string & s
int mask
glm::vec4 p[6]
std::string message
float u
Definition Grass.cpp:233
std::array< double, 10 > q
float v
std::int32_t c
float blend
std::uint32_t height
std::uint32_t width
std::array< float, 3 > position
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
std::shared_ptr< const OceanSprayFrame > previous
bool finite
TileLayer * layer
float f
std::set< std::string > values
Object::Ptr value
float d
float t
std::size_t elements
TacticalUnit * unit
double weight
uint32_t index
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
Result< std::vector< VegetationElement > > snapshotElements() const
Return an owning, priority-ordered element snapshot for revision-safe editor previews.
Result< void > replace(const VegetationGlobals &globals, std::span< const VegetationElement > elements)
Validate and atomically replace the entire field; failure preserves prior state.
eve::Diagnostic Diagnostic
Status invalid(const char *message)
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
VegetationBlend
Ordered field composition operation.
Four owning arrays in row-major X/Z order at texel centers.
std::array< std::vector< glm::vec4 >, 4 > channels
Owning global defaults. Extras = emission, wetness, overlay, alpha. Motion = signed world X/Z directi...
Owning sampled field channels; no lifetime dependency on the field.