载入中...
搜索中...
未找到
SurfaceCapture.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include "common/Assert.h"
5#include "common/Diagnostic.h"
7
8// SurfaceCapture.cpp
9namespace eve::gpuagents {
10namespace {
11
12bool pointInTriangle2D(const glm::vec2& p, const glm::vec2& a, const glm::vec2& b, const glm::vec2& c, float& w0,
13 float& w1, float& w2) {
14 const glm::vec2 v0 = b - a;
15 const glm::vec2 v1 = c - a;
16 const glm::vec2 v2 = p - a;
17 const float den = v0.x * v1.y - v1.x * v0.y;
18 if (std::abs(den) < 1e-12f) return false;
19 const float inv = 1.f / den;
20 w1 = (v2.x * v1.y - v1.x * v2.y) * inv;
21 w2 = (v0.x * v2.y - v2.x * v0.y) * inv;
22 w0 = 1.f - w1 - w2;
23 return w0 >= -1e-4f && w1 >= -1e-4f && w2 >= -1e-4f;
24}
25
26} // namespace
27
29 if (field.height.empty() || field.resolution < 2) return;
30 const int res = field.resolution;
31 const float cell = field.worldSize / static_cast<float>(res - 1);
32 field.normals.assign(field.height.size(), glm::vec3(0.f, 1.f, 0.f));
33 for (int z = 0; z < res; ++z) {
34 for (int x = 0; x < res; ++x) {
35 const int x0 = std::max(x - 1, 0);
36 const int x1 = std::min(x + 1, res - 1);
37 const int z0 = std::max(z - 1, 0);
38 const int z1 = std::min(z + 1, res - 1);
39 const float hx0 = field.height[static_cast<size_t>(x0 + res * z)];
40 const float hx1 = field.height[static_cast<size_t>(x1 + res * z)];
41 const float hz0 = field.height[static_cast<size_t>(x + res * z0)];
42 const float hz1 = field.height[static_cast<size_t>(x + res * z1)];
43 const glm::vec3 dx(2.f * cell, hx1 - hx0, 0.f);
44 const glm::vec3 dz(0.f, hz1 - hz0, 2.f * cell);
45 glm::vec3 n = glm::cross(dz, dx);
46 const float len = glm::length(n);
47 if (len > 1e-6f)
48 n /= len;
49 else
50 n = glm::vec3(0.f, 1.f, 0.f);
51 field.normals[static_cast<size_t>(x + res * z)] = n;
52 }
53 }
54}
55
57 std::span<const std::uint32_t> indices, const glm::vec3& originMin,
58 float worldSize, int resolution, float baseHeight) {
59 if (resolution < 2) {
61 DiagnosticCode::InvalidArgument, "surface capture resolution must be >= 2", "resolution", {}, "gpuagents"));
62 }
63 if (!(worldSize > 0.f)) {
65 "surface capture worldSize must be positive", "worldSize", {},
66 "gpuagents"));
67 }
68 if (indices.size() % 3 != 0) {
70 "triangle index count must be a multiple of 3", "indices", {},
71 "gpuagents"));
72 }
73 for (std::uint32_t idx : indices) {
74 if (idx >= positions.size()) {
76 "triangle index out of range", "indices", {}, "gpuagents"));
77 }
78 }
79
80 out.initFlat(originMin, worldSize, resolution, baseHeight);
81 const float cell = worldSize / static_cast<float>(resolution - 1);
82
83 for (size_t t = 0; t + 2 < indices.size(); t += 3) {
84 const glm::vec3& A = positions[indices[t]];
85 const glm::vec3& B = positions[indices[t + 1]];
86 const glm::vec3& C = positions[indices[t + 2]];
87 const glm::vec2 a(A.x, A.z), b(B.x, B.z), c(C.x, C.z);
88 const float minX = std::min({a.x, b.x, c.x});
89 const float maxX = std::max({a.x, b.x, c.x});
90 const float minZ = std::min({a.y, b.y, c.y});
91 const float maxZ = std::max({a.y, b.y, c.y});
92 const int x0 = std::clamp(static_cast<int>(std::floor((minX - originMin.x) / cell)), 0, resolution - 1);
93 const int x1 = std::clamp(static_cast<int>(std::ceil((maxX - originMin.x) / cell)), 0, resolution - 1);
94 const int z0 = std::clamp(static_cast<int>(std::floor((minZ - originMin.z) / cell)), 0, resolution - 1);
95 const int z1 = std::clamp(static_cast<int>(std::ceil((maxZ - originMin.z) / cell)), 0, resolution - 1);
96
97 for (int z = z0; z <= z1; ++z) {
98 for (int x = x0; x <= x1; ++x) {
99 const glm::vec2 p(originMin.x + cell * static_cast<float>(x),
100 originMin.z + cell * static_cast<float>(z));
101 float w0 = 0.f, w1 = 0.f, w2 = 0.f;
102 if (!pointInTriangle2D(p, a, b, c, w0, w1, w2)) continue;
103 const float h = w0 * A.y + w1 * B.y + w2 * C.y;
104 auto& dest = out.height[static_cast<size_t>(x + resolution * z)];
105 dest = std::max(dest, h);
106 }
107 }
108 }
109
111 return Result<void>::success();
112}
113
114} // namespace eve::gpuagents
115
116// SurfaceField.cpp
117namespace eve::gpuagents {
118
119void SurfaceField::initFlat(const glm::vec3& originMin, float size, int res, float planeY) {
120 EV_PARAM_CHECK(res > 1);
121 EV_PARAM_CHECK(size > 0.f);
122 origin = originMin;
123 worldSize = size;
124 resolution = res;
125 const size_t n = static_cast<size_t>(res) * static_cast<size_t>(res);
126 height.assign(n, planeY);
127 normals.assign(n, glm::vec3(0.f, 1.f, 0.f));
128 lifeField.assign(n, glm::vec4(0.f));
129}
130
131glm::vec2 SurfaceField::worldToUv(const glm::vec3& p) const {
132 return glm::vec2((p.x - origin.x) / worldSize, (p.z - origin.z) / worldSize);
133}
134
135int SurfaceField::index(int x, int z) const { return x + resolution * z; }
136
137bool SurfaceField::inBounds(int x, int z) const { return x >= 0 && z >= 0 && x < resolution && z < resolution; }
138
139float SurfaceField::sampleHeight(float x, float z) const {
140 if (height.empty()) return 0.f;
141 const glm::vec2 uv = worldToUv(glm::vec3(x, 0.f, z));
142 const float fx = uv.x * static_cast<float>(resolution - 1);
143 const float fz = uv.y * static_cast<float>(resolution - 1);
144 const int x0 = std::clamp(static_cast<int>(std::floor(fx)), 0, resolution - 1);
145 const int z0 = std::clamp(static_cast<int>(std::floor(fz)), 0, resolution - 1);
146 const int x1 = std::min(x0 + 1, resolution - 1);
147 const int z1 = std::min(z0 + 1, resolution - 1);
148 const float tx = fx - static_cast<float>(x0);
149 const float tz = fz - static_cast<float>(z0);
150 const float h00 = height[static_cast<size_t>(index(x0, z0))];
151 const float h10 = height[static_cast<size_t>(index(x1, z0))];
152 const float h01 = height[static_cast<size_t>(index(x0, z1))];
153 const float h11 = height[static_cast<size_t>(index(x1, z1))];
154 return glm::mix(glm::mix(h00, h10, tx), glm::mix(h01, h11, tx), tz);
155}
156
157glm::vec3 SurfaceField::sampleNormal(float x, float z) const {
158 if (normals.empty()) return glm::vec3(0.f, 1.f, 0.f);
159 const glm::vec2 uv = worldToUv(glm::vec3(x, 0.f, z));
160 const int ix = std::clamp(static_cast<int>(std::floor(uv.x * resolution)), 0, resolution - 1);
161 const int iz = std::clamp(static_cast<int>(std::floor(uv.y * resolution)), 0, resolution - 1);
162 return normals[static_cast<size_t>(index(ix, iz))];
163}
164
165glm::vec4 SurfaceField::sampleLifeTexel(float u, float v) const {
166 const float fx = u * static_cast<float>(resolution - 1);
167 const float fz = v * static_cast<float>(resolution - 1);
168 const int x0 = std::clamp(static_cast<int>(std::floor(fx)), 0, resolution - 1);
169 const int z0 = std::clamp(static_cast<int>(std::floor(fz)), 0, resolution - 1);
170 const int x1 = std::min(x0 + 1, resolution - 1);
171 const int z1 = std::min(z0 + 1, resolution - 1);
172 const float tx = fx - static_cast<float>(x0);
173 const float tz = fz - static_cast<float>(z0);
174 const auto at = [&](int x, int z) { return lifeField[static_cast<size_t>(index(x, z))]; };
175 return glm::mix(glm::mix(at(x0, z0), at(x1, z0), tx), glm::mix(at(x0, z1), at(x1, z1), tx), tz);
176}
177
178glm::vec4 SurfaceField::sampleLife(float x, float z) const {
179 if (lifeField.empty()) return glm::vec4(0.f);
180 const glm::vec2 uv = worldToUv(glm::vec3(x, 0.f, z));
181 return sampleLifeTexel(std::clamp(uv.x, 0.f, 1.f), std::clamp(uv.y, 0.f, 1.f));
182}
183
184glm::vec3 SurfaceField::project(const glm::vec3& p) const { return glm::vec3(p.x, sampleHeight(p.x, p.z), p.z); }
185
186void SurfaceField::stepLife(float dt, float decayRate, float halfLife, float diffusion,
187 const std::vector<glm::vec3>& deposits, float depositStrength) {
188 if (lifeField.empty()) return;
189 EV_PARAM_CHECK(dt >= 0.f);
190 const float decay = std::exp(-decayRate * dt);
191 const float freshDecay = halfLife > 1e-4f ? std::exp(-std::log(2.f) * dt / halfLife) : 0.f;
192
193 std::vector<glm::vec4> next = lifeField;
194 const int res = resolution;
195 for (int z = 0; z < res; ++z) {
196 for (int x = 0; x < res; ++x) {
197 const size_t i = static_cast<size_t>(index(x, z));
198 glm::vec4 c = lifeField[i];
199 c.r *= decay;
200 c.a *= freshDecay;
201 if (diffusion > 0.f) {
202 glm::vec4 sum(0.f);
203 int count = 0;
204 for (int dz = -1; dz <= 1; ++dz) {
205 for (int dx = -1; dx <= 1; ++dx) {
206 if (dx == 0 && dz == 0) continue;
207 const int nx = x + dx;
208 const int nz = z + dz;
209 if (!inBounds(nx, nz)) continue;
210 sum += lifeField[static_cast<size_t>(index(nx, nz))];
211 ++count;
212 }
213 }
214 if (count > 0) {
215 const glm::vec4 avg = sum / static_cast<float>(count);
216 c.r = glm::mix(c.r, avg.r, diffusion);
217 c.g = glm::mix(c.g, avg.g, diffusion * 0.5f);
218 c.b = glm::mix(c.b, avg.b, diffusion * 0.25f);
219 }
220 }
221 next[i] = c;
222 }
223 }
224
225 for (const glm::vec3& p : deposits) {
226 const glm::vec2 uv = worldToUv(p);
227 if (uv.x < 0.f || uv.y < 0.f || uv.x >= 1.f || uv.y >= 1.f) continue;
228 const int x = std::clamp(static_cast<int>(uv.x * res), 0, res - 1);
229 const int z = std::clamp(static_cast<int>(uv.y * res), 0, res - 1);
230 auto& c = next[static_cast<size_t>(index(x, z))];
231 c.r = std::min(c.r + depositStrength, 1.f);
232 c.a = 1.f;
233 }
234 lifeField.swap(next);
235}
236
237} // namespace eve::gpuagents
double z
double x
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
float uv
glm::vec4 p[6]
Stable, structured diagnostics shared by engine modules.
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::vector< std::uint32_t > indices
std::vector< float > positions
float v
std::int32_t c
int h
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
glm::dvec3 dz
double dt
int idx
float t
ScriptStructure out
int count
Cell cell
float size
Definition TreeMesh.cpp:158
std::size_t at
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
static Result< void > captureFromTriangles(SurfaceField &out, std::span< const glm::vec3 > positions, std::span< const std::uint32_t > indices, const glm::vec3 &originMin, float worldSize, int resolution, float baseHeight=0.f)
Bake triangle positions into out over the given XZ domain.
static void rebuildNormals(SurfaceField &field)
Rebuild normals from the current height grid (central differences).
2.5D surface + life-field textures for Life Network.
std::vector< glm::vec4 > lifeField
Life field RGBA: R=trail, G=nutrient, B=danger, A=freshness.
glm::vec3 sampleNormal(float x, float z) const
Bilinear sample of surface normal.
glm::vec3 project(const glm::vec3 &p) const
Project a world point onto the surface (Y from height).
void stepLife(float dt, float decayRate, float halfLife, float diffusion, const std::vector< glm::vec3 > &deposits, float depositStrength)
Advance trail deposit / decay / diffusion / danger for one fixed step.
float sampleHeight(float x, float z) const
Bilinear sample of height at world XZ.
std::vector< glm::vec3 > normals
Packed normals as vec3 per texel.
std::vector< float > height
Height samples, resolution^2, row-major X then Z.
void initFlat(const glm::vec3 &originMin, float size, int res, float planeY=0.f)
Allocate grids and fill a flat plane at y = planeY.
glm::vec2 worldToUv(const glm::vec3 &p) const
World XZ → texel UV in [0,1].
glm::vec4 sampleLife(float x, float z) const
Bilinear sample of life RGBA.
size_t size() const
Returns the size of .
Definition Json.cpp:352