载入中...
搜索中...
未找到
BuildingFx.cpp
浏览该文件的文档.
2
4#include "building/Ghost.h"
8#include "grid/GridConfig.h"
9#include "common/ECS.h"
10#include "graphics/Graphics.h"
11#include "graphics/Mesh.h"
14
15#include <simplesquirrel/simplesquirrel.hpp>
16
17#include <glm/gtc/matrix_transform.hpp>
18#include <glm/gtx/euler_angles.hpp>
19
20#include <algorithm>
21#include <cmath>
22
23namespace eve::buildingfx {
24
25// Color lives in eve::graphics (see graphics/Canvas.h); keep the unqualified form.
27
29
30namespace {
31
32float toFloat(const std::string &s, float fallback) {
33 if (s.empty()) return fallback;
34 try {
35 return std::stof(s);
36 } catch (...) {
37 return fallback;
38 }
39}
40
41graphics::Graphics *gfxOrNull() { return getModInst(graphics, Graphics); }
42
43bool toBool(const std::string &value, bool fallback);
44
45std::string visualValue(const building::BuildingDefinition &definition,
46 const building::PlacedBuilding &placed,
47 const building::PlacementWorld &world, bool use3d,
48 const std::string &key) {
49 const std::string base = use3d ? definition.getVisual3d(key) : definition.getVisual2d(key);
50 if (placed.placementKind != "edge") return base;
51 const std::string variant = building::PlacementSystem::edgeVariant(world, placed.instanceId);
52 const int mask = building::PlacementSystem::edgeConnectionMask(world, placed.instanceId);
53 const std::string maskKey =
54 "variant." + variant + ".mask." + std::to_string(mask) + "." + key;
55 const std::string variantKey = "variant." + variant + "." + key;
56 const std::string classValue = use3d ? definition.getVisual3d(variantKey, base)
57 : definition.getVisual2d(variantKey, base);
58 return use3d ? definition.getVisual3d(maskKey, classValue)
59 : definition.getVisual2d(maskKey, classValue);
60}
61
62struct TopologyTransform {
63 std::string variant;
64 int mask = 0;
65 float rotationDegrees = 0.f;
66 bool mirrorX = false;
67 bool mirrorZ = false;
68};
69
70TopologyTransform topologyTransform(const building::BuildingDefinition &definition,
71 const building::PlacedBuilding &placed,
72 const building::PlacementWorld &world, bool use3d) {
73 TopologyTransform result;
74 if (placed.placementKind != "edge") return result;
75 result.variant = building::PlacementSystem::edgeVariant(world, placed.instanceId);
77 if (result.variant == "end") result.mirrorX = (result.mask & 0x01) != 0;
78 if (result.variant == "corner") {
79 result.mirrorX = (result.mask & 0x0c) != 0;
80 result.mirrorZ = (result.mask & 0x14) != 0;
81 }
82 result.rotationDegrees =
83 toFloat(visualValue(definition, placed, world, use3d, "rotationDeg"), 0.f);
84 result.mirrorX = toBool(visualValue(definition, placed, world, use3d, "mirrorX"),
85 result.mirrorX);
86 result.mirrorZ = toBool(visualValue(definition, placed, world, use3d, "mirrorZ"),
87 result.mirrorZ);
88 return result;
89}
90
91void placedWorldPosition(const building::PlacedBuilding &placed,
92 const building::PlacementWorld &world, float &x, float &y, float &z) {
93 if (world.getGrid().plane == grid::GridPlane::XZ) {
94 x = placed.worldX;
95 y = placed.elevation + float(placed.level) * world.getFloorHeight();
96 z = placed.worldY;
97 // Cell poses name the minimum footprint corner, while meshes are centered.
98 if (placed.placementKind == "cell") {
99 if (const auto *definition = building::BuildingRegistry::find(placed.buildingId)) {
100 const auto snapMode = definition->snapMode.empty() ? world.getSnapMode() : definition->snapMode;
101 if (snapMode != "grid") return;
102 int width = 1, depth = 1;
104 float startX = 0.f, startZ = 0.f, endX = 0.f, endZ = 0.f;
105 world.cellToWorldPlane(placed.originCellX, placed.originCellY, startX, startZ);
106 world.cellToWorldPlane(placed.originCellX + width, placed.originCellY + depth, endX, endZ);
107 x += (endX - startX) * 0.5f;
108 z += (endZ - startZ) * 0.5f;
109 }
110 }
111 } else {
112 x = placed.worldX;
113 y = placed.worldY;
114 z = placed.elevation + float(placed.level) * world.getFloorHeight();
115 }
116}
117
118bool toBool(const std::string &value, bool fallback) {
119 if (value.empty()) return fallback;
120 if (value == "1" || value == "true" || value == "yes") return true;
121 if (value == "0" || value == "false" || value == "no") return false;
122 return fallback;
123}
124
125float cornerVisualSize(const building::BuildingDefinition &definition,
126 const building::PlacedBuilding &placed,
127 const building::PlacementWorld &world, bool use3d,
128 const std::string &axisKey) {
129 const float fallback = std::min(world.getGrid().cellW, world.getGrid().cellH) * 0.2f;
130 const std::string axisValue = visualValue(definition, placed, world, use3d, axisKey);
131 if (!axisValue.empty()) return toFloat(axisValue, fallback);
132 return toFloat(visualValue(definition, placed, world, use3d, "size"), fallback);
133}
134
135float freeVisualSize(const building::BuildingDefinition &definition,
136 const building::PlacedBuilding &placed,
137 const building::PlacementWorld &world, bool use3d,
138 const std::string &axisKey) {
139 float fallback = placed.freeRadius > 0.f
140 ? placed.freeRadius * 2.f
141 : definition.freeRadiusCells * 2.f *
142 std::min(world.getGrid().cellW, world.getGrid().cellH);
143 if (placed.freeHalfWidth > 0.f && placed.freeHalfHeight > 0.f) {
144 fallback = axisKey == "width" ? placed.freeHalfWidth * 2.f
145 : placed.freeHalfHeight * 2.f;
146 }
147 return toFloat(visualValue(definition, placed, world, use3d, axisKey), fallback);
148}
149
150void applySurfaceRotation(const building::PlacedBuilding &placed, float rotationDegrees,
151 graphics::Renderable3D::Transform3D &transform) {
152 if (placed.surfaceId.empty()) return;
153 const glm::vec3 tangent(placed.surfaceTangentX, placed.surfaceTangentY,
154 placed.surfaceTangentZ);
155 const glm::vec3 normal(placed.surfaceNormalX, placed.surfaceNormalY,
156 placed.surfaceNormalZ);
157 const glm::vec3 bitangent = glm::normalize(glm::cross(tangent, normal));
158 glm::mat4 frame(1.f);
159 frame[0] = glm::vec4(tangent, 0.f);
160 frame[1] = glm::vec4(normal, 0.f);
161 frame[2] = glm::vec4(bitangent, 0.f);
162 frame *= glm::rotate(glm::mat4(1.f), glm::radians(rotationDegrees),
163 glm::vec3(0.f, 1.f, 0.f));
164 glm::extractEulerAngleYXZ(frame, transform.yaw, transform.pitch, transform.roll);
165}
166
167} // namespace
168
170 const building::PlacementWorld &world,
171 const std::vector<CurveControlPoint> &controlPoints, int subdivisions, float width,
172 float height, float elevation) {
173 if (controlPoints.size() != 4 || subdivisions < 2 || subdivisions > 4096 ||
174 !std::isfinite(width) || !std::isfinite(height) || !std::isfinite(elevation) ||
175 width <= 0.f || height <= 0.f) {
178 "curve mesh requires four controls, 2..4096 subdivisions, and positive dimensions",
179 {}, {}, "buildingfx.edge-curve-mesh"));
180 }
181 for (const CurveControlPoint &point : controlPoints) {
182 if (!std::isfinite(point.x) || !std::isfinite(point.y)) {
184 eve::DiagnosticCode::InvalidArgument, "curve mesh controls must be finite", {},
185 {}, "buildingfx.edge-curve-mesh"));
186 }
187 }
188 const auto layout = world.getGrid().layout;
192 "continuous curve mesh requires an affine rectangle or isometric grid", {}, {},
193 "buildingfx.edge-curve-mesh"));
194 }
195
196 float originX = 0.f, originY = 0.f;
197 float basisXX = 0.f, basisXY = 0.f;
198 float basisYX = 0.f, basisYY = 0.f;
199 world.cellToWorldPlane(0, 0, originX, originY);
200 world.cellToWorldPlane(1, 0, basisXX, basisXY);
201 world.cellToWorldPlane(0, 1, basisYX, basisYY);
202 basisXX -= originX;
203 basisXY -= originY;
204 basisYX -= originX;
205 basisYY -= originY;
206
207 struct Point {
208 float x = 0.f;
209 float y = 0.f;
210 };
211 const auto evaluate = [&](int sample) {
212 const double t = static_cast<double>(sample) / static_cast<double>(subdivisions);
213 const double u = 1.0 - t;
214 const double gx = u * u * u * controlPoints[0].x +
215 3.0 * u * u * t * controlPoints[1].x +
216 3.0 * u * t * t * controlPoints[2].x +
217 t * t * t * controlPoints[3].x;
218 const double gy = u * u * u * controlPoints[0].y +
219 3.0 * u * u * t * controlPoints[1].y +
220 3.0 * u * t * t * controlPoints[2].y +
221 t * t * t * controlPoints[3].y;
222 return Point{originX + static_cast<float>(gx) * basisXX +
223 static_cast<float>(gy) * basisYX,
224 originY + static_cast<float>(gx) * basisXY +
225 static_cast<float>(gy) * basisYY};
226 };
227 std::vector<Point> samples;
228 std::vector<float> distances(static_cast<size_t>(subdivisions + 1), 0.f);
229 samples.reserve(static_cast<size_t>(subdivisions + 1));
231 mesh.sampleCount = subdivisions + 1;
232 for (int sample = 0; sample <= subdivisions; ++sample) {
233 samples.push_back(evaluate(sample));
234 if (sample == 0) continue;
235 distances[sample] = distances[sample - 1] +
236 std::hypot(samples[sample].x - samples[sample - 1].x,
237 samples[sample].y - samples[sample - 1].y);
238 }
239 mesh.length = distances.back();
240 if (!(mesh.length > 1e-5f)) {
242 eve::DiagnosticCode::InvalidArgument, "curve mesh has zero projected length", {},
243 {}, "buildingfx.edge-curve-mesh"));
244 }
245
246 const bool xz = world.getGrid().plane == grid::GridPlane::XZ;
247 const auto appendVertex = [&](Point point, float vertical, float nx, float ny,
248 float normalVertical, float u, float v) {
249 if (xz) {
250 mesh.positions.insert(mesh.positions.end(), {point.x, elevation + vertical, point.y});
251 mesh.normals.insert(mesh.normals.end(), {nx, normalVertical, ny});
252 } else {
253 mesh.positions.insert(mesh.positions.end(), {point.x, point.y, elevation + vertical});
254 mesh.normals.insert(mesh.normals.end(), {nx, ny, normalVertical});
255 }
256 mesh.uvs.insert(mesh.uvs.end(), {u, v});
257 };
258 mesh.positions.reserve(static_cast<size_t>((subdivisions + 1) * 8 + 8) * 3);
259 mesh.normals.reserve(mesh.positions.capacity());
260 mesh.uvs.reserve(static_cast<size_t>((subdivisions + 1) * 8 + 8) * 2);
261 mesh.indices.reserve(static_cast<size_t>(subdivisions) * 24 + 12);
262 const float halfWidth = width * 0.5f;
263 std::vector<Point> left(samples.size()), right(samples.size()), tangents(samples.size());
264 for (int sample = 0; sample <= subdivisions; ++sample) {
265 const Point before = samples[static_cast<size_t>(sample == 0 ? 0 : sample - 1)];
266 const Point after = samples[static_cast<size_t>(
267 sample == subdivisions ? subdivisions : sample + 1)];
268 float tx = after.x - before.x;
269 float ty = after.y - before.y;
270 const float tangentLength = std::hypot(tx, ty);
271 if (!(tangentLength > 1e-5f)) {
274 "curve mesh contains an unresolved zero-length tangent", std::to_string(sample),
275 {}, "buildingfx.edge-curve-mesh"));
276 }
277 tx /= tangentLength;
278 ty /= tangentLength;
279 tangents[sample] = {tx, ty};
280 const float px = -ty;
281 const float py = tx;
282 left[sample] = {samples[sample].x - px * halfWidth,
283 samples[sample].y - py * halfWidth};
284 right[sample] = {samples[sample].x + px * halfWidth,
285 samples[sample].y + py * halfWidth};
286 const float u = distances[sample] / mesh.length;
287 appendVertex(left[sample], 0.f, -px, -py, 0.f, u, 0.f);
288 appendVertex(left[sample], height, -px, -py, 0.f, u, 1.f);
289 appendVertex(right[sample], 0.f, px, py, 0.f, u, 0.f);
290 appendVertex(right[sample], height, px, py, 0.f, u, 1.f);
291 appendVertex(left[sample], height, 0.f, 0.f, 1.f, u, 0.f);
292 appendVertex(right[sample], height, 0.f, 0.f, 1.f, u, 1.f);
293 appendVertex(left[sample], 0.f, 0.f, 0.f, -1.f, u, 0.f);
294 appendVertex(right[sample], 0.f, 0.f, 0.f, -1.f, u, 1.f);
295 }
296 for (int segment = 0; segment < subdivisions; ++segment) {
297 const uint32_t a = static_cast<uint32_t>(segment * 8);
298 const uint32_t b = a + 8;
299 const uint32_t faces[] = {a, b, b + 1, a, b + 1, a + 1,
300 a + 2, a + 3, b + 3, a + 2, b + 3, b + 2,
301 a + 4, b + 4, b + 5, a + 4, b + 5, a + 5,
302 a + 6, a + 7, b + 7, a + 6, b + 7, b + 6};
303 mesh.indices.insert(mesh.indices.end(), std::begin(faces), std::end(faces));
304 }
305 const auto appendCap = [&](int sample, bool start) {
306 const Point tangent = tangents[static_cast<size_t>(sample)];
307 const float sign = start ? -1.f : 1.f;
308 const uint32_t base = static_cast<uint32_t>(mesh.positions.size() / 3);
309 appendVertex(left[sample], 0.f, tangent.x * sign, tangent.y * sign, 0.f, 0.f, 0.f);
310 appendVertex(right[sample], 0.f, tangent.x * sign, tangent.y * sign, 0.f, 1.f, 0.f);
311 appendVertex(left[sample], height, tangent.x * sign, tangent.y * sign, 0.f, 0.f, 1.f);
312 appendVertex(right[sample], height, tangent.x * sign, tangent.y * sign, 0.f, 1.f, 1.f);
313 const uint32_t capStart[] = {base, base + 2, base + 3, base, base + 3, base + 1};
314 const uint32_t capEnd[] = {base, base + 3, base + 2, base, base + 1, base + 3};
315 const uint32_t *indices = start ? capStart : capEnd;
316 mesh.indices.insert(mesh.indices.end(), indices, indices + 6);
317 };
318 appendCap(0, true);
319 appendCap(subdivisions, false);
320 return eve::Result<CurveMeshData>::success(std::move(mesh));
321}
322
324 const std::vector<CurveSurfaceSample> &samples, float width, float height) {
325 if (samples.size() < 2 || !std::isfinite(width) || !std::isfinite(height) ||
326 width <= 0.f || height <= 0.f)
329 "surface curve mesh requires at least two frames and positive dimensions", {}, {},
330 "buildingfx.surface-curve-mesh"));
331 std::vector<glm::vec3> centers, normals, tangents, laterals;
332 std::vector<float> distances(samples.size(), 0.f);
333 centers.reserve(samples.size());
334 normals.reserve(samples.size());
335 for (const CurveSurfaceSample &sample : samples) {
336 const glm::vec3 center(sample.x, sample.y, sample.z);
337 const glm::vec3 normal(sample.normalX, sample.normalY, sample.normalZ);
338 if (!std::isfinite(center.x) || !std::isfinite(center.y) || !std::isfinite(center.z) ||
339 !std::isfinite(normal.x) || !std::isfinite(normal.y) ||
340 !std::isfinite(normal.z) || glm::length(normal) <= 1e-5f)
343 "surface curve frames require finite positions and non-zero normals", {}, {},
344 "buildingfx.surface-curve-mesh"));
345 centers.push_back(center);
346 normals.push_back(glm::normalize(normal));
347 }
348 for (size_t index = 1; index < centers.size(); ++index)
349 distances[index] = distances[index - 1] + glm::length(centers[index] - centers[index - 1]);
350 if (distances.back() <= 1e-5f)
352 eve::DiagnosticCode::InvalidArgument, "surface curve has zero length", {}, {},
353 "buildingfx.surface-curve-mesh"));
354 for (size_t index = 0; index < centers.size(); ++index) {
355 const glm::vec3 before = centers[index == 0 ? 0 : index - 1];
356 const glm::vec3 after = centers[index + 1 == centers.size() ? index : index + 1];
357 glm::vec3 tangent = after - before;
358 tangent -= normals[index] * glm::dot(tangent, normals[index]);
359 if (glm::length(tangent) <= 1e-5f)
362 "surface curve tangent is parallel to its normal", std::to_string(index), {},
363 "buildingfx.surface-curve-mesh"));
364 tangent = glm::normalize(tangent);
365 tangents.push_back(tangent);
366 laterals.push_back(glm::normalize(glm::cross(normals[index], tangent)));
367 }
369 mesh.sampleCount = static_cast<int>(samples.size());
370 mesh.length = distances.back();
371 const auto append = [&](const glm::vec3 &position, const glm::vec3 &normal, float u,
372 float v) {
373 mesh.positions.insert(mesh.positions.end(), {position.x, position.y, position.z});
374 mesh.normals.insert(mesh.normals.end(), {normal.x, normal.y, normal.z});
375 mesh.uvs.insert(mesh.uvs.end(), {u, v});
376 };
377 const float halfWidth = width * 0.5f;
378 for (size_t index = 0; index < centers.size(); ++index) {
379 const glm::vec3 left = centers[index] - laterals[index] * halfWidth;
380 const glm::vec3 right = centers[index] + laterals[index] * halfWidth;
381 const glm::vec3 leftTop = left + normals[index] * height;
382 const glm::vec3 rightTop = right + normals[index] * height;
383 const float u = distances[index] / mesh.length;
384 append(left, -laterals[index], u, 0.f);
385 append(leftTop, -laterals[index], u, 1.f);
386 append(right, laterals[index], u, 0.f);
387 append(rightTop, laterals[index], u, 1.f);
388 append(leftTop, normals[index], u, 0.f);
389 append(rightTop, normals[index], u, 1.f);
390 append(left, -normals[index], u, 0.f);
391 append(right, -normals[index], u, 1.f);
392 }
393 for (size_t segment = 0; segment + 1 < centers.size(); ++segment) {
394 const uint32_t a = static_cast<uint32_t>(segment * 8);
395 const uint32_t b = a + 8;
396 const uint32_t faces[] = {a, b, b + 1, a, b + 1, a + 1,
397 a + 2, a + 3, b + 3, a + 2, b + 3, b + 2,
398 a + 4, b + 4, b + 5, a + 4, b + 5, a + 5,
399 a + 6, a + 7, b + 7, a + 6, b + 7, b + 6};
400 mesh.indices.insert(mesh.indices.end(), std::begin(faces), std::end(faces));
401 }
402 const auto cap = [&](size_t index, bool start) {
403 const glm::vec3 left = centers[index] - laterals[index] * halfWidth;
404 const glm::vec3 right = centers[index] + laterals[index] * halfWidth;
405 const glm::vec3 capNormal = tangents[index] * (start ? -1.f : 1.f);
406 const uint32_t base = static_cast<uint32_t>(mesh.positions.size() / 3);
407 append(left, capNormal, 0.f, 0.f);
408 append(right, capNormal, 1.f, 0.f);
409 append(left + normals[index] * height, capNormal, 0.f, 1.f);
410 append(right + normals[index] * height, capNormal, 1.f, 1.f);
411 const uint32_t startIndices[] = {base, base + 2, base + 3, base, base + 3, base + 1};
412 const uint32_t endIndices[] = {base, base + 3, base + 2, base, base + 1, base + 3};
413 mesh.indices.insert(mesh.indices.end(), start ? std::begin(startIndices)
414 : std::begin(endIndices),
415 start ? std::end(startIndices) : std::end(endIndices));
416 };
417 cap(0, true);
418 cap(centers.size() - 1, false);
419 return eve::Result<CurveMeshData>::success(std::move(mesh));
420}
421
423 const building::PlacementWorld &world, int instanceId, float width, float height,
424 float elevation) {
425 if (instanceId <= 0) {
427 eve::DiagnosticCode::InvalidArgument, "curve mesh requires a positive instance id",
428 std::to_string(instanceId), {}, "buildingfx.edge-curve-group-mesh"));
429 }
430 auto groupResult = world.edgeCurveGroupForInstance(instanceId);
431 if (!groupResult) return eve::Result<CurveMeshData>::failure(*groupResult.error());
432
433 const building::EdgeCurveGroup &group = groupResult.value();
434 if (!group.surfaceSamples.empty()) {
435 std::vector<CurveSurfaceSample> samples;
436 samples.reserve(group.surfaceSamples.size());
437 for (const building::EdgeCurveSurfaceSample &sample : group.surfaceSamples)
438 samples.push_back({sample.worldX, sample.worldY, sample.worldZ, sample.normalX,
439 sample.normalY, sample.normalZ});
440 return buildSurfaceCurveMesh(samples, width, height);
441 }
442 std::vector<CurveControlPoint> controls;
443 controls.reserve(group.controlPoints.size());
444 for (const building::EdgeCurveControlPoint &point : group.controlPoints)
445 controls.push_back({point.x, point.y});
446 return buildEdgeCurveMesh(world, controls, group.subdivisions, width, height, elevation);
447}
448
449eve::Result<void> BuildingFx::updateEdgeCurvePreview(
450 building::PlacementWorld *world, const std::string &buildingId,
451 const std::vector<CurveControlPoint> &controlPoints, int subdivisions, int level) {
452 const auto state = states_.find(world);
453 const building::BuildingDefinition *definition =
455 if (!world || state == states_.end() || !definition || !is3d(*definition)) {
458 "curve preview requires an attached world and a 3D building definition",
459 buildingId, {}, "buildingfx.edge-curve-preview"));
460 }
461 const float width = toFloat(definition->getVisual3d("thickness"),
462 world->getCellSize() * 0.1f);
463 const float height = toFloat(definition->getVisual3d("height"), 1.f);
464 const float elevation = static_cast<float>(level) * world->getFloorHeight();
465 auto generated = buildEdgeCurveMesh(*world, controlPoints, subdivisions, width, height,
466 elevation);
467 if (!generated.ok()) return eve::Result<void>::failure(*generated.error());
468
469 return presentCurvePreview(state->second, std::move(generated).takeValue(), controlPoints,
470 subdivisions, width, height, elevation);
471}
472
473eve::Result<void> BuildingFx::updateEdgeCurveSurfacePreview(
475 const std::string &surfaceName) {
476 const auto state = states_.find(world);
477 if (!world || state == states_.end() || !session || session->getWorld() != world ||
478 !session->isActive()) {
481 "surface curve preview requires an attached world and its active session", {}, {},
482 "buildingfx.edge-curve-surface-preview"));
483 }
484 const building::BuildingDefinition *definition =
486 if (!definition || !is3d(*definition) ||
487 session->edgeCurveControlPoints().size() != 4) {
490 "surface curve preview requires a 3D edge definition and four controls",
491 session->getBuildingId(), {}, "buildingfx.edge-curve-surface-preview"));
492 }
494 *world, surfaceName, session->edgeCurveControlPoints(), subdivisions);
495 if (!sampled.ok()) return eve::Result<void>::failure(sampled.status());
496 std::vector<CurveSurfaceSample> samples;
497 samples.reserve(sampled.value().samples.size());
498 for (const building::EdgeCurveSurfaceSample &sample : sampled.value().samples)
499 samples.push_back({sample.worldX, sample.worldY, sample.worldZ, sample.normalX,
500 sample.normalY, sample.normalZ});
501 const float width =
502 toFloat(definition->getVisual3d("thickness"), world->getCellSize() * 0.1f);
503 const float height = toFloat(definition->getVisual3d("height"), 1.f);
504 auto generated = buildSurfaceCurveMesh(samples, width, height);
505 if (!generated.ok()) return eve::Result<void>::failure(generated.status());
506 std::vector<CurveControlPoint> controls;
507 controls.reserve(session->edgeCurveControlPoints().size());
509 session->edgeCurveControlPoints())
510 controls.push_back({point.x, point.y});
511 return presentCurvePreview(state->second, std::move(generated).takeValue(),
512 std::move(controls), subdivisions, width, height, 0.f,
513 sampled.value().surfaceId, sampled.value().surfaceRevision);
514}
515
516CurvePreviewUpdateStatus BuildingFx::updateEdgeCurveSurfacePreviewStatus(
518 const std::string &surfaceName) {
519 return updateEdgeCurveSurfacePreview(world, session, subdivisions, surfaceName).ok()
520 ? CurvePreviewUpdateStatus::Updated
521 : CurvePreviewUpdateStatus::Rejected;
522}
523
524eve::Result<void> BuildingFx::presentCurvePreview(
525 WorldState &state, CurveMeshData mesh, std::vector<CurveControlPoint> controls,
526 int subdivisions, float width, float height, float elevation, std::string surfaceId,
527 std::uint64_t surfaceRevision) {
528 WorldState::CurveVisual &preview = state.curvePreview;
529 preview.cpuMesh = std::move(mesh);
530 preview.controlPoints = std::move(controls);
531 preview.subdivisions = subdivisions;
532 preview.width = width;
533 preview.height = height;
534 preview.elevation = elevation;
535 preview.surfaceId = std::move(surfaceId);
536 preview.surfaceRevision = surfaceRevision;
537 preview.active = true;
538 preview.fallbackReason.clear();
539
540 graphics::Graphics *gfx = gfxOrNull();
541 if (!gfx) {
542 preview.fallbackReason = "graphics_unavailable";
543 if (preview.renderable) preview.renderable->meshRenderer()->visible = false;
545 }
546 if (preview.mesh &&
547 !gfx->updateMeshVertices(
548 preview.mesh, preview.cpuMesh.positions.data(), preview.cpuMesh.normals.data(),
549 preview.cpuMesh.uvs.data(), static_cast<int>(preview.cpuMesh.positions.size() / 3),
550 preview.cpuMesh.indices.data(), static_cast<int>(preview.cpuMesh.indices.size())))
551 preview.mesh = nullptr;
552 if (!preview.mesh) {
553 try {
554 preview.mesh = gfx->newMeshFromArrays(
555 preview.cpuMesh.positions.data(), preview.cpuMesh.normals.data(),
556 preview.cpuMesh.uvs.data(),
557 static_cast<int>(preview.cpuMesh.positions.size() / 3),
558 preview.cpuMesh.indices.data(),
559 static_cast<int>(preview.cpuMesh.indices.size()));
560 } catch (...) {
561 preview.mesh = nullptr;
562 }
563 }
564 if (!preview.mesh) {
565 preview.fallbackReason = "mesh_upload_failed";
567 }
568 if (!preview.renderable) preview.renderable = graphics::Renderable3D::create();
569 auto renderer = preview.renderable->meshRenderer();
570 renderer->mesh = preview.mesh;
571 renderer->r = 0.2f;
572 renderer->g = 0.9f;
573 renderer->b = 0.35f;
574 renderer->a = 0.55f;
575 renderer->visible = true;
576 renderer->castShadow = false;
577 renderer->receiveShadow = false;
579}
580
581void BuildingFx::clearEdgeCurvePreview(building::PlacementWorld *world) {
582 const auto state = states_.find(world);
583 if (state == states_.end()) return;
584 WorldState::CurveVisual &preview = state->second.curvePreview;
585 if (preview.renderable) {
586 ecs::DestroyEntity(preview.renderable);
587 preview.renderable = nullptr;
588 }
589 preview.active = false;
590 preview.cpuMesh = {};
591 preview.controlPoints.clear();
592 preview.fallbackReason.clear();
593}
594
595bool BuildingFx::hasEdgeCurvePreview(building::PlacementWorld *world) const {
596 const auto state = states_.find(world);
597 return state != states_.end() && state->second.curvePreview.active &&
598 !state->second.curvePreview.cpuMesh.positions.empty();
599}
600
601std::string BuildingFx::getEdgeCurvePreviewFallbackReason(
602 building::PlacementWorld *world) const {
603 const auto state = states_.find(world);
604 if (state == states_.end()) return "world_not_attached";
605 return state->second.curvePreview.active
606 ? state->second.curvePreview.fallbackReason
607 : "curve_preview_not_active";
608}
609
610std::string BuildingFx::getEdgeCurvePreviewSurfaceId(
611 building::PlacementWorld *world) const {
612 const auto state = states_.find(world);
613 if (state == states_.end() || !state->second.curvePreview.active) return {};
614 return state->second.curvePreview.surfaceId;
615}
616
617std::uint64_t BuildingFx::getEdgeCurvePreviewSurfaceRevision(
618 building::PlacementWorld *world) const {
619 const auto state = states_.find(world);
620 if (state == states_.end() || !state->second.curvePreview.active) return 0;
621 return state->second.curvePreview.surfaceRevision;
622}
623
624bool BuildingFx::attach(building::PlacementWorld *world) {
625 if (!world) return false;
626 states_[world]; // ensure entry
627 return true;
628}
629
630bool BuildingFx::detach(building::PlacementWorld *world) {
631 if (!world) return false;
632 auto it = states_.find(world);
633 if (it == states_.end()) return false;
634 destroyAll(it->second);
635 states_.erase(it);
636 return true;
637}
638
639bool BuildingFx::isAttached(building::PlacementWorld *world) const {
640 return world != nullptr && states_.count(world) > 0;
641}
642
643int BuildingFx::getAttachedCount() const { return int(states_.size()); }
644
645bool BuildingFx::is3d(const building::BuildingDefinition &def) const {
646 return def.renderMode == "3d" || def.renderMode == "3D";
647}
648
649graphics::Mesh *BuildingFx::cubeMesh(graphics::Graphics *gfx) {
650 if (!gfx) return nullptr;
651 if (!cubeMesh_) cubeMesh_ = gfx->newMeshCube(1.f);
652 return cubeMesh_;
653}
654
655void BuildingFx::createVisual(WorldState &st, const building::BuildingDefinition &def,
656 const building::PlacedBuilding &pb,
657 building::PlacementWorld *world, Visual &v, float alpha) {
658 (void)st;
659 const float cellW = world->getGrid().cellW;
660 const float cellH = world->getGrid().cellH;
661 int effW = def.footprintW;
662 int effH = def.footprintH;
663 building::PlacementSystem::effectiveFootprint(def, pb.rotationDeg, &effW, &effH);
664 const TopologyTransform topology = topologyTransform(def, pb, *world, is3d(def));
665 v.topologyVariant = topology.variant;
666 v.topologyMask = topology.mask;
667
668 if (is3d(def)) {
669 auto *r = graphics::Renderable3D::create();
670 float wx = 0.f, wy = 0.f, wz = 0.f;
671 placedWorldPosition(pb, *world, wx, wy, wz);
672 auto tr = r->transform();
673 tr->x = wx + toFloat(def.getVisual3d("offsetX"), 0.f);
674 tr->y = wy + toFloat(def.getVisual3d("offsetY"), 0.f);
675 tr->z = wz + toFloat(def.getVisual3d("offsetZ"), 0.f);
676 const float height = toFloat(visualValue(def, pb, *world, true, "height"), 1.f);
677 const bool edge = pb.placementKind == "edge";
678 const bool corner = pb.placementKind == "corner";
679 const bool free = pb.placementKind == "free";
680 tr->sx = (free ? freeVisualSize(def, pb, *world, true, "width")
681 : corner ? cornerVisualSize(def, pb, *world, true, "width")
682 : edge ? (pb.edge.axis == building::EdgeAxis::Horizontal ? cellW : cellH)
683 : float(def.footprintW) * cellW) *
684 (topology.mirrorX ? -1.f : 1.f);
685 tr->sy = height;
686 tr->sz = (free ? freeVisualSize(def, pb, *world, true, "depth")
687 : corner ? cornerVisualSize(def, pb, *world, true, "depth")
688 : edge ? toFloat(visualValue(def, pb, *world, true, "thickness"), std::min(cellW, cellH) * 0.1f)
689 : float(def.footprintH) * cellH) *
690 (topology.mirrorZ ? -1.f : 1.f);
691 const float visualRotation = pb.rotationDeg + topology.rotationDegrees;
692 const float rad = visualRotation * 3.14159265f / 180.f;
693 if (world->getGrid().plane == grid::GridPlane::XZ) {
694 tr->yaw = rad;
695 } else {
696 tr->roll = rad;
697 }
698 applySurfaceRotation(pb, visualRotation, *tr);
699 auto mr = r->meshRenderer();
700 v.resourceId = visualValue(def, pb, *world, true, "mesh");
701 graphics::Mesh *resolved = nullptr;
702 if (v.resourceId.empty()) {
703 v.fallbackReason = "primitive_default";
704 } else if (!meshResolver_) {
705 v.fallbackReason = "resolver_unavailable";
706 } else {
707 resolved = meshResolver_(v.resourceId);
708 v.fallbackReason = resolved ? std::string{} : "resource_unresolved";
709 }
710 mr->mesh = resolved ? resolved : cubeMesh(gfxOrNull());
711 mr->r = toFloat(visualValue(def, pb, *world, true, "colorR"), 0.62f);
712 mr->g = toFloat(visualValue(def, pb, *world, true, "colorG"), 0.62f);
713 mr->b = toFloat(visualValue(def, pb, *world, true, "colorB"), 0.62f);
714 mr->a = alpha;
715 mr->visible = true;
716 mr->castShadow = true;
717 mr->receiveShadow = true;
718 v.r3d = r;
719 } else {
720 auto *r = graphics::Renderable2D::create();
721 auto tr = r->transform();
722 tr->x = pb.worldX + toFloat(def.getVisual2d("offsetX"), 0.f);
723 tr->y = pb.worldY + toFloat(def.getVisual2d("offsetY"), 0.f);
724 tr->rot = pb.rotationDeg + topology.rotationDegrees;
725 tr->sx = topology.mirrorX ? -1.f : 1.f;
726 tr->sy = topology.mirrorZ ? -1.f : 1.f;
727 auto sp = r->sprite();
728 const bool edge = pb.placementKind == "edge";
729 const bool corner = pb.placementKind == "corner";
730 const bool free = pb.placementKind == "free";
731 sp->width = free ? freeVisualSize(def, pb, *world, false, "width")
732 : corner ? cornerVisualSize(def, pb, *world, false, "width")
733 : edge ? (pb.edge.axis == building::EdgeAxis::Horizontal ? cellW : cellH)
734 : float(effW) * cellW;
735 sp->height = free ? freeVisualSize(def, pb, *world, false, "height")
736 : corner ? cornerVisualSize(def, pb, *world, false, "height")
737 : edge ? toFloat(visualValue(def, pb, *world, false, "thickness"),
738 std::min(cellW, cellH) * 0.1f)
739 : float(effH) * cellH;
740 sp->r = toFloat(visualValue(def, pb, *world, false, "colorR"), 0.72f);
741 sp->g = toFloat(visualValue(def, pb, *world, false, "colorG"), 0.72f);
742 sp->b = toFloat(visualValue(def, pb, *world, false, "colorB"), 0.72f);
743 sp->a = alpha;
744 sp->layer = int(toFloat(def.getVisual2d("layer"), 0.f));
745 sp->visible = true;
746 const std::string texPath = visualValue(def, pb, *world, false, "texture");
747 v.resourceId = texPath;
748 v.fallbackReason = texPath.empty() ? "primitive_default" : std::string{};
749 if (!texPath.empty()) {
750 if (auto *gfx = gfxOrNull()) {
751 if (graphics::Texture *tex = gfx->newTextureFromFile(texPath)) {
752 sp->texture = tex;
753 } else {
754 v.fallbackReason = "resource_unresolved";
755 }
756 } else {
757 v.fallbackReason = "resolver_unavailable";
758 }
759 }
760 v.r2d = r;
761 }
762}
763
764void BuildingFx::updateVisual(const building::BuildingDefinition &def,
765 const building::PlacedBuilding &pb,
766 building::PlacementWorld *world, Visual &v) {
767 const float cellW = world->getGrid().cellW;
768 const float cellH = world->getGrid().cellH;
769 int effW = def.footprintW;
770 int effH = def.footprintH;
771 building::PlacementSystem::effectiveFootprint(def, pb.rotationDeg, &effW, &effH);
772 const TopologyTransform topology = topologyTransform(def, pb, *world, is3d(def));
773 v.topologyVariant = topology.variant;
774 v.topologyMask = topology.mask;
775
776 if (v.r3d) {
777 float wx = 0.f, wy = 0.f, wz = 0.f;
778 placedWorldPosition(pb, *world, wx, wy, wz);
779 auto tr = v.r3d->transform();
780 tr->x = wx + toFloat(def.getVisual3d("offsetX"), 0.f);
781 tr->y = wy + toFloat(def.getVisual3d("offsetY"), 0.f);
782 tr->z = wz + toFloat(def.getVisual3d("offsetZ"), 0.f);
783 const float height = toFloat(visualValue(def, pb, *world, true, "height"), 1.f);
784 const bool edge = pb.placementKind == "edge";
785 const bool corner = pb.placementKind == "corner";
786 const bool free = pb.placementKind == "free";
787 tr->sx = (free ? freeVisualSize(def, pb, *world, true, "width")
788 : corner ? cornerVisualSize(def, pb, *world, true, "width")
789 : edge ? (pb.edge.axis == building::EdgeAxis::Horizontal ? cellW : cellH)
790 : float(def.footprintW) * cellW) *
791 (topology.mirrorX ? -1.f : 1.f);
792 tr->sy = height;
793 tr->sz = (free ? freeVisualSize(def, pb, *world, true, "depth")
794 : corner ? cornerVisualSize(def, pb, *world, true, "depth")
795 : edge ? toFloat(visualValue(def, pb, *world, true, "thickness"), std::min(cellW, cellH) * 0.1f)
796 : float(def.footprintH) * cellH) *
797 (topology.mirrorZ ? -1.f : 1.f);
798 const float visualRotation = pb.rotationDeg + topology.rotationDegrees;
799 const float rad = visualRotation * 3.14159265f / 180.f;
800 if (world->getGrid().plane == grid::GridPlane::XZ) {
801 tr->yaw = rad;
802 } else {
803 tr->roll = rad;
804 }
805 applySurfaceRotation(pb, visualRotation, *tr);
806 auto renderer = v.r3d->meshRenderer();
807 v.resourceId = visualValue(def, pb, *world, true, "mesh");
808 graphics::Mesh *resolved = nullptr;
809 if (v.resourceId.empty()) {
810 v.fallbackReason = "primitive_default";
811 } else if (!meshResolver_) {
812 v.fallbackReason = "resolver_unavailable";
813 } else {
814 resolved = meshResolver_(v.resourceId);
815 v.fallbackReason = resolved ? std::string{} : "resource_unresolved";
816 }
817 renderer->mesh = resolved ? resolved : cubeMesh(gfxOrNull());
818 renderer->r = toFloat(visualValue(def, pb, *world, true, "colorR"), 0.62f);
819 renderer->g = toFloat(visualValue(def, pb, *world, true, "colorG"), 0.62f);
820 renderer->b = toFloat(visualValue(def, pb, *world, true, "colorB"), 0.62f);
821 } else if (v.r2d) {
822 auto tr = v.r2d->transform();
823 tr->x = pb.worldX + toFloat(def.getVisual2d("offsetX"), 0.f);
824 tr->y = pb.worldY + toFloat(def.getVisual2d("offsetY"), 0.f);
825 tr->rot = pb.rotationDeg + topology.rotationDegrees;
826 tr->sx = topology.mirrorX ? -1.f : 1.f;
827 tr->sy = topology.mirrorZ ? -1.f : 1.f;
828 auto sp = v.r2d->sprite();
829 const bool edge = pb.placementKind == "edge";
830 const bool corner = pb.placementKind == "corner";
831 const bool free = pb.placementKind == "free";
832 sp->width = free ? freeVisualSize(def, pb, *world, false, "width")
833 : corner ? cornerVisualSize(def, pb, *world, false, "width")
834 : edge ? (pb.edge.axis == building::EdgeAxis::Horizontal ? cellW : cellH)
835 : float(effW) * cellW;
836 sp->height = free ? freeVisualSize(def, pb, *world, false, "height")
837 : corner ? cornerVisualSize(def, pb, *world, false, "height")
838 : edge ? toFloat(visualValue(def, pb, *world, false, "thickness"),
839 std::min(cellW, cellH) * 0.1f)
840 : float(effH) * cellH;
841 sp->r = toFloat(visualValue(def, pb, *world, false, "colorR"), 0.72f);
842 sp->g = toFloat(visualValue(def, pb, *world, false, "colorG"), 0.72f);
843 sp->b = toFloat(visualValue(def, pb, *world, false, "colorB"), 0.72f);
844 const std::string resourceId = visualValue(def, pb, *world, false, "texture");
845 if (resourceId != v.resourceId) {
846 sp->texture = nullptr;
847 v.resourceId = resourceId;
848 if (resourceId.empty()) {
849 v.fallbackReason = "primitive_default";
850 } else if (auto *gfx = gfxOrNull()) {
851 sp->texture = gfx->newTextureFromFile(resourceId);
852 v.fallbackReason = sp->texture ? std::string{} : "resource_unresolved";
853 } else {
854 v.fallbackReason = "resolver_unavailable";
855 }
856 }
857 }
858}
859
860void BuildingFx::destroyVisual(Visual &v) {
861 if (v.r2d) {
862 ecs::DestroyEntity(v.r2d);
863 v.r2d = nullptr;
864 }
865 if (v.r3d) {
866 ecs::DestroyEntity(v.r3d);
867 v.r3d = nullptr;
868 }
869}
870
871void BuildingFx::setVisible(Visual &v, bool visible) {
872 if (v.r2d) v.r2d->sprite()->visible = visible;
873 if (v.r3d) v.r3d->meshRenderer()->visible = visible;
874}
875
876void BuildingFx::destroyAll(WorldState &st) {
877 for (auto &kv : st.visuals) destroyVisual(kv.second);
878 st.visuals.clear();
879 for (auto &[groupId, curve] : st.curveVisuals) {
880 (void)groupId;
881 if (curve.renderable) ecs::DestroyEntity(curve.renderable);
882 }
883 st.curveVisuals.clear();
884 if (st.curvePreview.renderable) ecs::DestroyEntity(st.curvePreview.renderable);
885 st.curvePreview = {};
886 destroyVisual(st.ghost);
887 destroyVisual(st.cursor);
888 st.ghostBuildingId.clear();
889 for (graphics::Renderable3D *line : st.gridLines) {
890 if (line) ecs::DestroyEntity(line);
891 }
892 st.gridLines.clear();
893 st.gridLineCount = -1;
894 destroyHeatmap(st);
895}
896
897void BuildingFx::destroyHeatmap(WorldState &st) {
898 for (graphics::Renderable3D *cell : st.heatCells3d)
899 if (cell) ecs::DestroyEntity(cell);
900 st.heatCells3d.clear();
901}
902
903void BuildingFx::sync(building::PlacementWorld *world) {
904 if (!world) return;
905 auto it = states_.find(world);
906 if (it == states_.end()) return;
907 WorldState &st = it->second;
908
909 std::unordered_map<int, Visual> next;
910 const int n = world->getBuildingCount();
911 for (int i = 0; i < n; ++i) {
912 const int inst = world->getBuildingInstanceAt(i);
913 auto bit = world->buildings().find(inst);
914 if (bit == world->buildings().end()) continue;
915 const auto &pb = bit->second;
918 if (!def) continue;
919 auto existing = st.visuals.find(inst);
920 if (existing != st.visuals.end()) {
921 next[inst] = existing->second;
922 updateVisual(*def, pb, world, next[inst]);
923 } else {
924 Visual v;
925 createVisual(st, *def, pb, world, v, 1.f);
926 next[inst] = v;
927 }
928 const bool visible =
929 st.levelVisibility == WorldState::LevelVisibility::All ||
930 (st.levelVisibility == WorldState::LevelVisibility::Active &&
931 pb.level == world->getActiveLevel()) ||
932 (st.levelVisibility == WorldState::LevelVisibility::ActiveAndBelow &&
933 pb.level <= world->getActiveLevel());
934 setVisible(next[inst], visible);
935 }
936 for (auto &kv : st.visuals) {
937 if (next.count(kv.first) == 0) destroyVisual(kv.second);
938 }
939 st.visuals = std::move(next);
940 syncCurveVisuals(st, world);
941}
942
943void BuildingFx::syncCurveVisuals(WorldState &st, building::PlacementWorld *world) {
944 for (auto &[groupId, curve] : st.curveVisuals) {
945 (void)groupId;
946 curve.active = false;
947 }
948 for (building::EdgeCurveGroupId groupId : world->edgeCurveGroupIds()) {
949 auto groupResult = world->edgeCurveGroup(groupId);
950 if (!groupResult.ok() || groupResult.value().instanceIds.empty()) continue;
951 const building::EdgeCurveGroup &group = groupResult.value();
952 const auto member = world->buildings().find(group.instanceIds.front());
953 if (member == world->buildings().end()) continue;
954 const building::PlacedBuilding &placed = member->second;
955 const building::BuildingDefinition *definition =
957 if (!definition) continue;
958
959 WorldState::CurveVisual &curve = st.curveVisuals[groupId.value];
960 curve.active = true;
961 curve.memberIds = group.instanceIds;
962 std::vector<CurveControlPoint> controls;
963 controls.reserve(group.controlPoints.size());
964 for (const building::EdgeCurveControlPoint &point : group.controlPoints)
965 controls.push_back({point.x, point.y});
966 const float width = toFloat(definition->getVisual3d("thickness"),
967 std::min(world->getGrid().cellW,
968 world->getGrid().cellH) *
969 0.1f);
970 const float height = toFloat(definition->getVisual3d("height"), 1.f);
971 const float elevation = placed.elevation +
972 static_cast<float>(placed.level) * world->getFloorHeight();
973 const bool changed = curve.controlPoints != controls ||
974 curve.subdivisions != group.subdivisions || curve.width != width ||
975 curve.height != height || curve.elevation != elevation ||
976 curve.surfaceId != group.surfaceId ||
977 curve.surfaceRevision != group.surfaceRevision;
978 curve.fallbackReason.clear();
979 if (!is3d(*definition)) {
980 curve.fallbackReason = "render_mode_2d";
981 } else if (changed || curve.cpuMesh.positions.empty()) {
982 eve::Result<CurveMeshData> generated = [&]() {
983 if (group.surfaceSamples.empty())
984 return buildEdgeCurveMesh(*world, controls, group.subdivisions, width,
986 std::vector<CurveSurfaceSample> samples;
987 samples.reserve(group.surfaceSamples.size());
988 for (const building::EdgeCurveSurfaceSample &sample : group.surfaceSamples)
989 samples.push_back({sample.worldX, sample.worldY, sample.worldZ,
990 sample.normalX, sample.normalY, sample.normalZ});
991 return buildSurfaceCurveMesh(samples, width, height);
992 }();
993 if (!generated.ok()) {
994 curve.fallbackReason = generated.error() ? generated.error()->message()
995 : "mesh_generation_failed";
996 } else {
997 curve.cpuMesh = std::move(generated).takeValue();
998 curve.controlPoints = std::move(controls);
999 curve.subdivisions = group.subdivisions;
1000 curve.width = width;
1001 curve.height = height;
1002 curve.elevation = elevation;
1003 curve.surfaceId = group.surfaceId;
1004 curve.surfaceRevision = group.surfaceRevision;
1005 }
1006 }
1007 graphics::Graphics *gfx = gfxOrNull();
1008 if (curve.fallbackReason.empty() && !gfx) curve.fallbackReason = "graphics_unavailable";
1009 if (curve.fallbackReason.empty() && changed && curve.mesh) {
1010 if (!gfx->updateMeshVertices(
1011 curve.mesh, curve.cpuMesh.positions.data(), curve.cpuMesh.normals.data(),
1012 curve.cpuMesh.uvs.data(), static_cast<int>(curve.cpuMesh.positions.size() / 3),
1013 curve.cpuMesh.indices.data(), static_cast<int>(curve.cpuMesh.indices.size())))
1014 curve.mesh = nullptr;
1015 }
1016 if (curve.fallbackReason.empty() && !curve.mesh) {
1017 try {
1018 curve.mesh = gfx->newMeshFromArrays(
1019 curve.cpuMesh.positions.data(), curve.cpuMesh.normals.data(),
1020 curve.cpuMesh.uvs.data(), static_cast<int>(curve.cpuMesh.positions.size() / 3),
1021 curve.cpuMesh.indices.data(), static_cast<int>(curve.cpuMesh.indices.size()));
1022 } catch (...) {
1023 curve.mesh = nullptr;
1024 }
1025 if (!curve.mesh) curve.fallbackReason = "mesh_upload_failed";
1026 }
1027 if (curve.fallbackReason.empty() && !curve.renderable) {
1028 curve.renderable = graphics::Renderable3D::create();
1029 curve.renderable->meshRenderer()->mesh = curve.mesh;
1030 }
1031 if (curve.renderable) {
1032 auto renderer = curve.renderable->meshRenderer();
1033 renderer->mesh = curve.mesh;
1034 renderer->r = toFloat(definition->getVisual3d("colorR"), 0.62f);
1035 renderer->g = toFloat(definition->getVisual3d("colorG"), 0.62f);
1036 renderer->b = toFloat(definition->getVisual3d("colorB"), 0.62f);
1037 renderer->a = 1.f;
1038 renderer->castShadow = true;
1039 renderer->receiveShadow = true;
1040 renderer->visible =
1041 curve.fallbackReason.empty() &&
1042 (st.levelVisibility == WorldState::LevelVisibility::All ||
1043 (st.levelVisibility == WorldState::LevelVisibility::Active &&
1044 placed.level == world->getActiveLevel()) ||
1045 (st.levelVisibility == WorldState::LevelVisibility::ActiveAndBelow &&
1046 placed.level <= world->getActiveLevel()));
1047 }
1048 if (curve.fallbackReason.empty()) {
1049 for (int instanceId : group.instanceIds) {
1050 const auto visual = st.visuals.find(instanceId);
1051 if (visual != st.visuals.end()) setVisible(visual->second, false);
1052 }
1053 }
1054 }
1055 for (auto &[groupId, curve] : st.curveVisuals) {
1056 (void)groupId;
1057 if (curve.active) continue;
1058 if (curve.renderable) {
1059 ecs::DestroyEntity(curve.renderable);
1060 curve.renderable = nullptr;
1061 }
1062 }
1063}
1064
1065int BuildingFx::getVisualCount(building::PlacementWorld *world) const {
1066 auto it = states_.find(world);
1067 return it == states_.end() ? 0 : int(it->second.visuals.size());
1068}
1069
1070int BuildingFx::getCurveGroupCount(building::PlacementWorld *world) const {
1071 const auto state = states_.find(world);
1072 if (state == states_.end()) return 0;
1073 return static_cast<int>(std::count_if(
1074 state->second.curveVisuals.begin(), state->second.curveVisuals.end(),
1075 [](const auto &entry) { return entry.second.active; }));
1076}
1077
1078int BuildingFx::getContinuousCurveVisualCount(building::PlacementWorld *world) const {
1079 const auto state = states_.find(world);
1080 if (state == states_.end()) return 0;
1081 return static_cast<int>(std::count_if(
1082 state->second.curveVisuals.begin(), state->second.curveVisuals.end(),
1083 [](const auto &entry) {
1084 return entry.second.active && entry.second.renderable &&
1085 entry.second.fallbackReason.empty();
1086 }));
1087}
1088
1089std::string BuildingFx::getCurveVisualFallbackReason(building::PlacementWorld *world,
1090 int instanceId) const {
1091 const auto state = states_.find(world);
1092 if (state == states_.end()) return "world_not_attached";
1093 for (const auto &[groupId, curve] : state->second.curveVisuals) {
1094 (void)groupId;
1095 if (curve.active &&
1096 std::find(curve.memberIds.begin(), curve.memberIds.end(), instanceId) !=
1097 curve.memberIds.end())
1098 return curve.fallbackReason;
1099 }
1100 return "curve_group_not_found";
1101}
1102
1103void BuildingFx::setLevelVisibilityMode(building::PlacementWorld *world,
1104 const std::string &mode) {
1105 if (!world) return;
1106 auto found = states_.find(world);
1107 if (found == states_.end()) return;
1108 if (mode == "all") {
1109 found->second.levelVisibility = WorldState::LevelVisibility::All;
1110 } else if (mode == "active") {
1111 found->second.levelVisibility = WorldState::LevelVisibility::Active;
1112 } else if (mode == "active_and_below") {
1113 found->second.levelVisibility = WorldState::LevelVisibility::ActiveAndBelow;
1114 } else {
1115 return;
1116 }
1117 sync(world);
1118}
1119
1120std::string BuildingFx::getLevelVisibilityMode(building::PlacementWorld *world) const {
1121 const auto found = states_.find(world);
1122 if (found == states_.end()) return {};
1123 switch (found->second.levelVisibility) {
1124 case WorldState::LevelVisibility::Active: return "active";
1125 case WorldState::LevelVisibility::ActiveAndBelow: return "active_and_below";
1126 default: return "all";
1127 }
1128}
1129
1130bool BuildingFx::isVisualVisible(building::PlacementWorld *world, int instanceId) const {
1131 const auto found = states_.find(world);
1132 if (found == states_.end()) return false;
1133 const auto visual = found->second.visuals.find(instanceId);
1134 if (visual == found->second.visuals.end()) return false;
1135 if (visual->second.r2d) return visual->second.r2d->sprite()->visible;
1136 return visual->second.r3d && visual->second.r3d->meshRenderer()->visible;
1137}
1138
1139std::string BuildingFx::getVisualVariant(building::PlacementWorld *world, int instanceId) const {
1140 const auto state = states_.find(world);
1141 if (state == states_.end()) return {};
1142 const auto visual = state->second.visuals.find(instanceId);
1143 return visual == state->second.visuals.end() ? std::string{}
1144 : visual->second.topologyVariant;
1145}
1146
1147std::string BuildingFx::getVisualResource(building::PlacementWorld *world, int instanceId) const {
1148 const auto state = states_.find(world);
1149 if (state == states_.end()) return {};
1150 const auto visual = state->second.visuals.find(instanceId);
1151 return visual == state->second.visuals.end() ? std::string{} : visual->second.resourceId;
1152}
1153
1154std::string BuildingFx::getVisualFallbackReason(building::PlacementWorld *world,
1155 int instanceId) const {
1156 const auto state = states_.find(world);
1157 if (state == states_.end()) return {};
1158 const auto visual = state->second.visuals.find(instanceId);
1159 return visual == state->second.visuals.end() ? std::string{}
1160 : visual->second.fallbackReason;
1161}
1162
1163void BuildingFx::updateGhost(building::PlacementWorld *world, building::Ghost *ghost) {
1164 if (!world || !ghost) {
1165 hideGhost(world);
1166 return;
1167 }
1168 auto it = states_.find(world);
1169 if (it == states_.end()) return;
1170 WorldState &st = it->second;
1171 const building::BuildingDefinition *def =
1173 if (!def) return;
1174
1175 if (st.ghostBuildingId != ghost->getBuildingId()) {
1176 destroyVisual(st.ghost);
1177 destroyVisual(st.cursor);
1178 st.ghostBuildingId = ghost->getBuildingId();
1179 }
1180 if (!st.ghost.r2d && !st.ghost.r3d) {
1181 // 用 ghost 当前位姿构造临时 PlacedBuilding 以复用 createVisual。
1183 pb.buildingId = ghost->getBuildingId();
1184 pb.placementKind = ghost->getPlacementKind();
1185 pb.originCellX = ghost->getCellX();
1186 pb.originCellY = ghost->getCellY();
1187 pb.corner = {ghost->getCellX(), ghost->getCellY()};
1188 pb.worldX = ghost->getWorldX();
1189 pb.worldY = ghost->getWorldY();
1190 pb.elevation = ghost->getElevation();
1191 pb.rotationDeg = ghost->getRotationDeg();
1192 pb.surfaceId = ghost->getSurfaceId();
1193 pb.surfaceNormalX = ghost->getSurfaceNormalX();
1194 pb.surfaceNormalY = ghost->getSurfaceNormalY();
1195 pb.surfaceNormalZ = ghost->getSurfaceNormalZ();
1196 pb.surfaceTangentX = ghost->getSurfaceTangentX();
1197 pb.surfaceTangentY = ghost->getSurfaceTangentY();
1198 pb.surfaceTangentZ = ghost->getSurfaceTangentZ();
1199 createVisual(st, *def, pb, world, st.ghost, 0.5f);
1200 }
1201 {
1203 pb.buildingId = ghost->getBuildingId();
1204 pb.placementKind = ghost->getPlacementKind();
1205 pb.originCellX = ghost->getCellX();
1206 pb.originCellY = ghost->getCellY();
1207 pb.corner = {ghost->getCellX(), ghost->getCellY()};
1208 pb.worldX = ghost->getWorldX();
1209 pb.worldY = ghost->getWorldY();
1210 pb.elevation = ghost->getElevation();
1211 pb.rotationDeg = ghost->getRotationDeg();
1212 pb.surfaceId = ghost->getSurfaceId();
1213 pb.surfaceNormalX = ghost->getSurfaceNormalX();
1214 pb.surfaceNormalY = ghost->getSurfaceNormalY();
1215 pb.surfaceNormalZ = ghost->getSurfaceNormalZ();
1216 pb.surfaceTangentX = ghost->getSurfaceTangentX();
1217 pb.surfaceTangentY = ghost->getSurfaceTangentY();
1218 pb.surfaceTangentZ = ghost->getSurfaceTangentZ();
1219 updateVisual(*def, pb, world, st.ghost);
1220 }
1221
1222 const bool valid = ghost->isValid();
1223 const float cr = valid ? 0.25f : 0.90f;
1224 const float cg = valid ? 0.85f : 0.25f;
1225 const float cb = valid ? 0.35f : 0.22f;
1226 if (st.ghost.r2d) {
1227 auto sp = st.ghost.r2d->sprite();
1228 sp->r = cr;
1229 sp->g = cg;
1230 sp->b = cb;
1231 }
1232 if (st.ghost.r3d) {
1233 auto mr = st.ghost.r3d->meshRenderer();
1234 mr->r = cr;
1235 mr->g = cg;
1236 mr->b = cb;
1237 }
1238 setVisible(st.ghost, true);
1239
1240 // 占地光标。
1241 if (!st.cursor.r2d && !st.cursor.r3d) {
1242 const float cellW = world->getGrid().cellW;
1243 const float cellH = world->getGrid().cellH;
1244 int effW = def->footprintW;
1245 int effH = def->footprintH;
1247 if (is3d(*def)) {
1248 auto *r = graphics::Renderable3D::create();
1249 auto tr = r->transform();
1251 placed.buildingId = ghost->getBuildingId();
1252 placed.originCellX = ghost->getCellX();
1253 placed.originCellY = ghost->getCellY();
1254 placed.worldX = ghost->getWorldX();
1255 placed.worldY = ghost->getWorldY();
1256 placed.placementKind = ghost->getPlacementKind();
1257 placed.elevation = ghost->getElevation();
1258 placed.rotationDeg = ghost->getRotationDeg();
1259 placed.surfaceId = ghost->getSurfaceId();
1260 placed.surfaceNormalX = ghost->getSurfaceNormalX();
1261 placed.surfaceNormalY = ghost->getSurfaceNormalY();
1262 placed.surfaceNormalZ = ghost->getSurfaceNormalZ();
1263 placed.surfaceTangentX = ghost->getSurfaceTangentX();
1264 placed.surfaceTangentY = ghost->getSurfaceTangentY();
1265 placed.surfaceTangentZ = ghost->getSurfaceTangentZ();
1266 placedWorldPosition(placed, *world, tr->x, tr->y, tr->z);
1267 tr->x += placed.surfaceNormalX * 0.03f;
1268 tr->y += placed.surfaceNormalY * 0.03f;
1269 tr->z += placed.surfaceNormalZ * 0.03f;
1270 if (world->getGrid().plane == grid::GridPlane::XZ) tr->yaw = placed.rotationDeg * 3.14159265f / 180.f;
1271 applySurfaceRotation(placed, placed.rotationDeg, *tr);
1272 tr->sx = placed.placementKind == "free" ? freeVisualSize(*def, placed, *world, true, "width")
1273 : placed.placementKind == "corner" ? cornerVisualSize(*def, placed, *world, true, "width")
1274 : float(def->footprintW) * cellW;
1275 tr->sy = 0.04f;
1276 tr->sz = placed.placementKind == "free" ? freeVisualSize(*def, placed, *world, true, "depth")
1277 : placed.placementKind == "corner" ? cornerVisualSize(*def, placed, *world, true, "depth")
1278 : float(def->footprintH) * cellH;
1279 auto mr = r->meshRenderer();
1280 mr->mesh = cubeMesh(gfxOrNull());
1281 mr->r = cr;
1282 mr->g = cg;
1283 mr->b = cb;
1284 mr->a = 0.35f;
1285 st.cursor.r3d = r;
1286 } else {
1287 auto *r = graphics::Renderable2D::create();
1288 auto tr = r->transform();
1289 tr->x = ghost->getWorldX();
1290 tr->y = ghost->getWorldY();
1291 auto sp = r->sprite();
1293 placed.buildingId = ghost->getBuildingId();
1294 placed.originCellX = ghost->getCellX();
1295 placed.originCellY = ghost->getCellY();
1296 placed.placementKind = ghost->getPlacementKind();
1297 sp->width = placed.placementKind == "free"
1298 ? freeVisualSize(*def, placed, *world, false, "width")
1299 : placed.placementKind == "corner"
1300 ? cornerVisualSize(*def, placed, *world, false, "width")
1301 : float(effW) * cellW;
1302 sp->height = placed.placementKind == "free"
1303 ? freeVisualSize(*def, placed, *world, false, "height")
1304 : placed.placementKind == "corner"
1305 ? cornerVisualSize(*def, placed, *world, false, "height")
1306 : float(effH) * cellH;
1307 sp->r = cr;
1308 sp->g = cg;
1309 sp->b = cb;
1310 sp->a = 0.35f;
1311 st.cursor.r2d = r;
1312 }
1313 } else {
1314 const float cellW = world->getGrid().cellW;
1315 const float cellH = world->getGrid().cellH;
1316 int effW = def->footprintW;
1317 int effH = def->footprintH;
1319 if (st.cursor.r3d) {
1320 auto tr = st.cursor.r3d->transform();
1322 placed.buildingId = ghost->getBuildingId();
1323 placed.originCellX = ghost->getCellX();
1324 placed.originCellY = ghost->getCellY();
1325 placed.worldX = ghost->getWorldX();
1326 placed.worldY = ghost->getWorldY();
1327 placed.placementKind = ghost->getPlacementKind();
1328 placed.elevation = ghost->getElevation();
1329 placed.rotationDeg = ghost->getRotationDeg();
1330 placed.surfaceId = ghost->getSurfaceId();
1331 placed.surfaceNormalX = ghost->getSurfaceNormalX();
1332 placed.surfaceNormalY = ghost->getSurfaceNormalY();
1333 placed.surfaceNormalZ = ghost->getSurfaceNormalZ();
1334 placed.surfaceTangentX = ghost->getSurfaceTangentX();
1335 placed.surfaceTangentY = ghost->getSurfaceTangentY();
1336 placed.surfaceTangentZ = ghost->getSurfaceTangentZ();
1337 placedWorldPosition(placed, *world, tr->x, tr->y, tr->z);
1338 tr->x += placed.surfaceNormalX * 0.03f;
1339 tr->y += placed.surfaceNormalY * 0.03f;
1340 tr->z += placed.surfaceNormalZ * 0.03f;
1341 if (world->getGrid().plane == grid::GridPlane::XZ) tr->yaw = placed.rotationDeg * 3.14159265f / 180.f;
1342 applySurfaceRotation(placed, placed.rotationDeg, *tr);
1343 tr->sx = placed.placementKind == "free" ? freeVisualSize(*def, placed, *world, true, "width")
1344 : placed.placementKind == "corner" ? cornerVisualSize(*def, placed, *world, true, "width")
1345 : float(def->footprintW) * cellW;
1346 tr->sz = placed.placementKind == "free" ? freeVisualSize(*def, placed, *world, true, "depth")
1347 : placed.placementKind == "corner" ? cornerVisualSize(*def, placed, *world, true, "depth")
1348 : float(def->footprintH) * cellH;
1349 auto mr = st.cursor.r3d->meshRenderer();
1350 mr->r = cr;
1351 mr->g = cg;
1352 mr->b = cb;
1353 } else if (st.cursor.r2d) {
1354 auto tr = st.cursor.r2d->transform();
1355 tr->x = ghost->getWorldX();
1356 tr->y = ghost->getWorldY();
1357 auto sp = st.cursor.r2d->sprite();
1359 placed.buildingId = ghost->getBuildingId();
1360 placed.originCellX = ghost->getCellX();
1361 placed.originCellY = ghost->getCellY();
1362 placed.placementKind = ghost->getPlacementKind();
1363 sp->width = placed.placementKind == "free"
1364 ? freeVisualSize(*def, placed, *world, false, "width")
1365 : placed.placementKind == "corner"
1366 ? cornerVisualSize(*def, placed, *world, false, "width")
1367 : float(effW) * cellW;
1368 sp->height = placed.placementKind == "free"
1369 ? freeVisualSize(*def, placed, *world, false, "height")
1370 : placed.placementKind == "corner"
1371 ? cornerVisualSize(*def, placed, *world, false, "height")
1372 : float(effH) * cellH;
1373 sp->r = cr;
1374 sp->g = cg;
1375 sp->b = cb;
1376 }
1377 }
1378 setVisible(st.cursor, true);
1379}
1380
1381void BuildingFx::hideGhost(building::PlacementWorld *world) {
1382 if (!world) return;
1383 auto it = states_.find(world);
1384 if (it == states_.end()) return;
1385 WorldState &st = it->second;
1386 setVisible(st.ghost, false);
1387 setVisible(st.cursor, false);
1388}
1389
1390void BuildingFx::updateAreaPreview(building::PlacementWorld *world,
1392 if (!world) return;
1393 auto stateIt = states_.find(world);
1394 if (stateIt == states_.end()) return;
1395 WorldState &st = stateIt->second;
1396 destroyHeatmap(st);
1397 st.heatCells.clear();
1398 if (!session) return;
1399 const int count = session->getAreaPreviewCount();
1400 st.heatCells.reserve(size_t(count));
1401 for (int i = 0; i < count; ++i)
1402 st.heatCells.push_back({session->getAreaPreviewCellX(i), session->getAreaPreviewCellY(i),
1403 session->getAreaPreviewAccepted(i)});
1404}
1405
1406void BuildingFx::clearAreaPreview(building::PlacementWorld *world) {
1407 auto it = states_.find(world);
1408 if (it == states_.end()) return;
1409 destroyHeatmap(it->second);
1410 it->second.heatCells.clear();
1411}
1412
1413int BuildingFx::getAreaPreviewCount(building::PlacementWorld *world) const {
1414 auto it = states_.find(world);
1415 return it == states_.end() ? 0 : int(it->second.heatCells.size());
1416}
1417
1418bool BuildingFx::getAreaPreviewAccepted(building::PlacementWorld *world, int index) const {
1419 auto it = states_.find(world);
1420 return it != states_.end() && index >= 0 && index < int(it->second.heatCells.size()) &&
1421 it->second.heatCells[size_t(index)].accepted;
1422}
1423
1424void BuildingFx::setGridVisible(building::PlacementWorld *world, bool visible) {
1425 if (!world) return;
1426 states_[world].gridVisible = visible;
1427}
1428
1429bool BuildingFx::getGridVisible(building::PlacementWorld *world) const {
1430 auto it = states_.find(world);
1431 return it != states_.end() && it->second.gridVisible;
1432}
1433
1434void BuildingFx::drawGrid2D(building::PlacementWorld *world, graphics::Graphics *gfx) {
1435 if (!world || !gfx) return;
1436 auto it = states_.find(world);
1437 if (it == states_.end() || !it->second.gridVisible) return;
1438 const float cellW = world->getGrid().cellW;
1439 const float cellH = world->getGrid().cellH;
1440 for (int y = 0; y < world->getHeight(); ++y) {
1441 for (int x = 0; x < world->getWidth(); ++x) {
1442 float px = 0.f, py = 0.f;
1443 world->cellToWorldPlane(x, y, px, py);
1444 gfx->drawSolidRect(px, py, cellW - 1.f, cellH - 1.f,
1445 Color{0.75f, 0.78f, 0.85f, 0.06f});
1446 }
1447 }
1448 for (const WorldState::HeatCell &cell : it->second.heatCells) {
1449 float px = 0.f, py = 0.f;
1450 world->cellToWorldPlane(cell.x, cell.y, px, py);
1451 gfx->drawSolidRect(px, py, cellW - 1.f, cellH - 1.f,
1452 cell.accepted ? Color{0.18f, 0.9f, 0.3f, 0.28f}
1453 : Color{0.95f, 0.16f, 0.12f, 0.34f});
1454 }
1455}
1456
1457void BuildingFx::rebuildGridLines(WorldState &st, building::PlacementWorld *world,
1458 graphics::Graphics *gfx, float height) {
1459 const int w = world->getWidth();
1460 const int h = world->getHeight();
1461 const float cellW = world->getGrid().cellW;
1462 const float cellH = world->getGrid().cellH;
1463 const int want = (w + 1) + (h + 1);
1464 if (int(st.gridLines.size()) == want && st.gridLineCount == want) return;
1465 for (graphics::Renderable3D *line : st.gridLines) {
1466 if (line) ecs::DestroyEntity(line);
1467 }
1468 st.gridLines.clear();
1469 graphics::Mesh *mesh = cubeMesh(gfx);
1470 const bool xz = world->getGrid().plane == grid::GridPlane::XZ;
1471 const float xExtent = float(w) * cellW;
1472 const float zExtent = float(h) * cellH;
1473 for (int x = 0; x <= w; ++x) {
1474 auto *r = graphics::Renderable3D::create();
1475 auto tr = r->transform();
1476 float px = 0.f, py = 0.f;
1477 world->cellToWorldPlane(x, 0, px, py);
1478 tr->x = px;
1479 tr->y = height;
1480 tr->z = xz ? 0.0f : 0.f;
1481 tr->sx = 0.03f;
1482 tr->sy = 0.02f;
1483 tr->sz = zExtent;
1484 auto mr = r->meshRenderer();
1485 mr->mesh = mesh;
1486 mr->r = 0.85f;
1487 mr->g = 0.88f;
1488 mr->b = 0.95f;
1489 mr->a = 0.5f;
1490 st.gridLines.push_back(r);
1491 }
1492 for (int y = 0; y <= h; ++y) {
1493 auto *r = graphics::Renderable3D::create();
1494 auto tr = r->transform();
1495 float px = 0.f, py = 0.f;
1496 world->cellToWorldPlane(0, y, px, py);
1497 tr->x = xz ? 0.0f : 0.f;
1498 tr->y = height;
1499 tr->z = py;
1500 tr->sx = xExtent;
1501 tr->sy = 0.02f;
1502 tr->sz = 0.03f;
1503 auto mr = r->meshRenderer();
1504 mr->mesh = mesh;
1505 mr->r = 0.85f;
1506 mr->g = 0.88f;
1507 mr->b = 0.95f;
1508 mr->a = 0.5f;
1509 st.gridLines.push_back(r);
1510 }
1511 st.gridLineCount = want;
1512}
1513
1514void BuildingFx::drawGrid3D(building::PlacementWorld *world, graphics::Graphics *gfx,
1515 float height) {
1516 if (!world || !gfx) return;
1517 auto it = states_.find(world);
1518 if (it == states_.end()) return;
1519 WorldState &st = it->second;
1520 rebuildGridLines(st, world, gfx, height);
1521 for (graphics::Renderable3D *line : st.gridLines) {
1522 if (line) line->meshRenderer()->visible = st.gridVisible;
1523 }
1524 if (st.heatCells3d.size() != st.heatCells.size()) {
1525 destroyHeatmap(st);
1526 for (const WorldState::HeatCell &cell : st.heatCells) {
1527 auto *r = graphics::Renderable3D::create();
1528 float wx = 0.f, wy = 0.f, wz = 0.f;
1529 world->cellToWorld3D(cell.x, cell.y, height + 0.015f, wx, wy, wz);
1530 auto tr = r->transform();
1531 tr->x = wx;
1532 tr->y = wy;
1533 tr->z = wz;
1534 tr->sx = world->getGrid().cellW * 0.92f;
1535 if (world->getGrid().plane == grid::GridPlane::XZ) {
1536 tr->sy = 0.02f;
1537 tr->sz = world->getGrid().cellH * 0.92f;
1538 } else {
1539 tr->sy = world->getGrid().cellH * 0.92f;
1540 tr->sz = 0.02f;
1541 }
1542 auto mr = r->meshRenderer();
1543 mr->mesh = cubeMesh(gfx);
1544 mr->r = cell.accepted ? 0.18f : 0.95f;
1545 mr->g = cell.accepted ? 0.9f : 0.16f;
1546 mr->b = cell.accepted ? 0.3f : 0.12f;
1547 mr->a = 0.35f;
1548 st.heatCells3d.push_back(r);
1549 }
1550 }
1551 for (graphics::Renderable3D *cell : st.heatCells3d)
1552 if (cell) cell->meshRenderer()->visible = st.gridVisible;
1553}
1554
1555} // namespace eve::buildingfx
double z
double y
double x
Duration start
float w
Definition AnimClip.cpp:738
const std::string & s
bool mirrorZ
bool mirrorX
int mask
float rotationDegrees
std::string variant
std::vector< V3 > centers
Definition BushMesh.cpp:100
bool changed
SQObjectPtr key
Definition ECS.cpp:155
int instanceId
std::string layout
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
Module-neutral grid topology and sizing (no building/map dependency).
double r
std::vector< std::uint32_t > indices
std::vector< float > normals
float v
float halfWidth
HexVec3 left
HexVec3 right
std::int32_t second
float elevation
int h
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
JointPoint2D axis
std::array< float, 3 > position
Poco::JSON::Object::Ptr result
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float tr
Texture * normal
int level
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:27
#define getModInst(N, T)
Definition Module.h:40
OceanMeshSettings mesh
std::shared_ptr< OceanRenderer > renderer
ssq::Object frame
glm::dvec2 center
Topology topology
Object::Ptr value
float t
Renderable3D::MeshRenderer * mr
RoadNetwork resolved
bool found
unsigned line
int count
AbilityActivationGroup group
Cell cell
bool placed
std::optional< InteractionSession > session
Definition Tactics.cpp:39
Heightmap curve
std::vector< TileLayer::Tileset::Visual > visuals
bool visible
float(ui::Theme::* member)[4]
std::uint32_t depth
uint32_t index
glm::vec3 point
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
const std::string & message() const noexcept
Human-readable explanation.
Definition Diagnostic.h:147
Move-only operation result carrying either a value or Status.
Definition Result.h:176
const Diagnostic * error() const noexcept
Inspect the first diagnostic, or null when none was supplied.
Definition Result.h:329
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:286
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static const BuildingDefinition * find(const std::string &id)
Finds .
Definition Building.cpp:557
EVENGINE_API_WORLD public API.
Definition Ghost.h:16
float getElevation() const
Returns the elevation.
Definition Ghost.h:45
int getCellY() const
Returns the cell y.
Definition Ghost.h:34
float getWorldY() const
Returns the world y.
Definition Ghost.h:41
int getCellX() const
Returns the cell x.
Definition Ghost.h:32
float getSurfaceTangentX() const
X component of the captured unit surface tangent.
Definition Ghost.h:81
std::string getSurfaceId() const
Surface identity captured by the last successful setFromSurface call.
Definition Ghost.h:71
bool isValid() const
True when valid.
Definition Ghost.h:67
std::string getPlacementKind() const
Current placement domain (cell, edge, corner, or free).
Definition Ghost.h:56
float getSurfaceTangentZ() const
Z component of the captured unit surface tangent.
Definition Ghost.h:85
float getWorldX() const
Returns the world x.
Definition Ghost.h:39
float getRotationDeg() const
Returns the rotation deg.
Definition Ghost.h:50
float getSurfaceTangentY() const
Y component of the captured unit surface tangent.
Definition Ghost.h:83
std::string getBuildingId() const
Returns the building id.
Definition Ghost.h:27
float getSurfaceNormalY() const
Y component of the captured unit surface normal.
Definition Ghost.h:77
float getSurfaceNormalZ() const
Z component of the captured unit surface normal.
Definition Ghost.h:79
float getSurfaceNormalX() const
X component of the captured unit surface normal.
Definition Ghost.h:75
EVENGINE_API_WORLD public API.
static eve::Result< EdgeCurveSurface > sampleEdgeCurveSurface(const PlacementWorld &world, const std::string &surfaceName, const std::vector< EdgeCurvePoint > &controlPoints, int subdivisions)
Sample one same-identity custom surface frame at every analytic curve segment.
static void effectiveFootprint(const BuildingDefinition &def, float rotationDeg, int *outW, int *outH)
旋转后的占地宽高(cardinal 90/270 交换)。
static std::string edgeVariant(const PlacementWorld &world, int instanceId)
Stable visual topology class: isolated/end/straight/corner/tee/cross.
static uint8_t edgeConnectionMask(const PlacementWorld &world, int instanceId)
Compute compatible neighbouring edge topology.
格子型建筑放置世界(脚本可直接操作)。
EVENGINE_API_WORLD public API.
Definition BuildingFx.h:41
static eve::Result< CurveMeshData > buildEdgeCurveMesh(const building::PlacementWorld &world, const std::vector< CurveControlPoint > &controlPoints, int subdivisions, float width, float height, float elevation=0.f)
Generate a continuous rectangular wall extrusion along a cubic Bezier curve.
static eve::Result< CurveMeshData > buildSurfaceCurveMesh(const std::vector< CurveSurfaceSample > &samples, float width, float height)
Extrude a wall from committed world-space surface frames.
static eve::Result< CurveMeshData > buildEdgeCurveGroupMeshForInstance(const building::PlacementWorld &world, int instanceId, float width, float height, float elevation=0.f)
Generate the continuous mesh owned by the curve group containing an edge instance.
Mesh * newMeshCube(float size=1.f)
Procedural cube (edge length size, centered at origin, outward CCW for RH Y-up), with per-face normal...
virtual void drawSolidRect(float x, float y, float w, float h, float r, float g, float b, float a=1.f)
RGBA-float overload matching the script-facing drawSolidRect name.
virtual bool updateMeshVertices(Mesh *mesh, const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
In-place update of a mesh's vertex/index data (CPU -> host-visible VBO). Only call when content chang...
Texture * newTextureFromFile(const std::string &filename) override
Load a path-cached file texture; cache hits return the same resource without I/O or upload....
virtual Mesh * newMeshFromArrays(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
Upload a triangle mesh from packed CPU arrays. Owned by Graphics. posXYZ required (vertexCount*3)....
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
EVENGINE_API_BACKENDS public API.
EdgeAxis
Canonical orientation of an edge in logical grid coordinates.
CurvePreviewUpdateStatus
Script-safe outcome of refreshing a custom-surface curve preview.
Definition BuildingFx.h:38
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
double sample(const Heightmap &map, double u, double v)
Sample.
glm::vec4 Color
Render-neutral RGBA color shared by graphics-facing modules.
Definition RenderTypes.h:8
建筑模板(进程级注册表中的定义)。
One cubic Bezier control point in logical grid-vertex space.
Strong world-local identity of one authoritative edge-curve group.
World-owned authoritative description and membership of one committed edge curve.
One committed 3D centerline frame sampled from a custom placement surface.
已放置的建筑实例。
std::string placementKind
Placement domain snapshot: cell, edge, corner, or free.
CornerAddress corner
Canonical grid vertex when placementKind is corner.
std::string surfaceId
Stable provider-local surface name used to create this placement; empty for unsurfaced placement.
float surfaceTangentX
Unit tangent captured with the normal to define the local placement frame.
float surfaceNormalX
Unit surface normal captured at placement time.
One cubic Bezier control point in logical grid-vertex coordinates.
Definition BuildingFx.h:44
Owning backend-neutral indexed extrusion generated along an edge curve.
Definition BuildingFx.h:51
One committed world-space centerline frame for terrain-conforming extrusion.
Definition BuildingFx.h:60
std::vector< CurveControlPoint > controlPoints
Definition BuildingFx.h:211