载入中...
搜索中...
未找到
GtsTerrainLodRuntime.cpp
浏览该文件的文档.
2
3#include "common/ECS.h"
6#include "procgen/Procgen.h"
7
8#include <vector>
9
10namespace eve::procgen {
12 std::vector<ecs::EntityHandle> tiles;
13 ecs::EntityHandle sourceTerrain{};
14 bool sourceWasVisible = false;
15 bool hasSource = false;
16 std::uint64_t revision = 0;
17};
18namespace {
19graphics::Renderable3D* resolve(const ecs::EntityHandle& handle) {
20 return dynamic_cast<graphics::Renderable3D*>(ecs::try_get(handle));
21}
22void destroy(std::vector<ecs::EntityHandle>& handles) {
23 for (auto& h : handles)
24 if (auto* entity = resolve(h)) ecs::DestroyEntity(entity);
25 handles.clear();
26}
27void restoreSource(ecs::EntityHandle& handle, bool wasVisible, bool& hasSource) {
28 if (hasSource)
29 if (auto* source = resolve(handle)) source->setVisible(wasVisible);
30 handle = {};
31 hasSource = false;
32}
33} // namespace
36 destroy(impl_->tiles);
37 restoreSource(impl_->sourceTerrain, impl_->sourceWasVisible, impl_->hasSource);
38}
40 graphics::Graphics& gfx, float diameter, float fov, float ox,
41 float oy, float oz) {
42 std::vector<ecs::EntityHandle> candidate(size_t(lods.getTileCount()));
43 for (int i = 0; i < lods.getTileCount(); ++i) {
44 auto* tile = lods.tileAt(i);
45 if (!tile || tile->levels.empty() || tile->levels.front().empty()) continue;
46 auto* entity = graphics::Renderable3D::create();
47 if (!entity) {
48 destroy(candidate);
50 "failed to create GTS terrain tile renderable",
51 "procgen.gtsTerrainLodRuntime"));
52 }
53 entity->setVisible(false);
54 candidate[size_t(i)] = ecs::handle_of(entity);
55 auto configured = procgen.configureGtsTerrainTileLods(lods, i, *entity, gfx, diameter, fov, ox, oy, oz);
56 if (!configured) {
57 destroy(candidate);
58 return Result<std::uint64_t>::failure(*configured.error());
59 }
60 }
61 for (auto& h : impl_->tiles)
62 if (auto* entity = resolve(h)) entity->setVisible(false);
63 for (auto& h : candidate)
64 if (auto* entity = resolve(h)) entity->setVisible(true);
65 destroy(impl_->tiles);
66 impl_->tiles = std::move(candidate);
67 ++impl_->revision;
68 if (impl_->revision == 0) ++impl_->revision;
69 return Result<std::uint64_t>::success(impl_->revision);
70}
73 graphics::Renderable3D& source, float diameter,
74 float fov, float ox, float oy, float oz) {
75 for (const auto& handle : impl_->tiles)
76 if (resolve(handle) == &source)
79 "GTS source terrain cannot be one of the managed LOD tiles", "sourceTerrain"));
80 const auto sourceHandle = ecs::handle_of(&source);
81 const bool sameSource = impl_->hasSource && resolve(impl_->sourceTerrain) == &source;
82 const bool enteringVisibility = sameSource ? impl_->sourceWasVisible : source.getVisible();
83 auto replaced = replace(lods, procgen, gfx, diameter, fov, ox, oy, oz);
84 if (!replaced) return replaced;
85 if (!sameSource) restoreSource(impl_->sourceTerrain, impl_->sourceWasVisible, impl_->hasSource);
86 impl_->sourceTerrain = sourceHandle;
87 impl_->sourceWasVisible = enteringVisibility;
88 impl_->hasSource = true;
89 source.setVisible(false);
90 return replaced;
91}
93 return impl_->hasSource ? resolve(impl_->sourceTerrain) : nullptr;
94}
96 int count = 0;
97 for (auto& h : impl_->tiles)
98 if (resolve(h)) ++count;
99 destroy(impl_->tiles);
100 restoreSource(impl_->sourceTerrain, impl_->sourceWasVisible, impl_->hasSource);
101 ++impl_->revision;
102 if (impl_->revision == 0) ++impl_->revision;
104}
106 int count = 0;
107 for (auto& h : impl_->tiles)
108 if (auto* entity = resolve(h)) {
109 entity->setMaterial(&material);
110 ++count;
111 }
113}
114int GtsTerrainLodRuntime::getTileCount() const { return int(impl_->tiles.size()); }
115std::uint64_t GtsTerrainLodRuntime::getRevision() const { return impl_->revision; }
117 return index >= 0 && index < int(impl_->tiles.size()) ? resolve(impl_->tiles[size_t(index)]) : nullptr;
118}
119} // namespace eve::procgen
int h
void(* destroy)(void *)
PrimitiveHandle handle
Material * material
std::uint32_t count
uint32_t index
const UnitySourceAsset & source
double oy
double ox
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:35
EVENGINE_API_BACKENDS public API.
void setVisible(bool visible)
GtsTerrainLodRuntime()
Gts terrain lod runtime.
int getTileCount() const
Return the number of row-major tile slots, including empty cells.
Result< int > clear()
Destroy every live tile entity and advance the revision.
Result< std::uint64_t > replaceAndHideSource(const GtsTerrainLodSet &lods, Procgen &procgen, graphics::Graphics &gfx, graphics::Renderable3D &sourceTerrain, float worldDiameter, float verticalFovDegrees, float originX=0.f, float originY=0.f, float originZ=0.f)
Replace the mesh batch and hide one source terrain only after successful publication.
graphics::Renderable3D * getRenderable(int tileIndex) const
Borrow a live tile renderable or return nullptr for empty, stale or invalid slots....
std::uint64_t getRevision() const
Return the current committed revision.
Result< std::uint64_t > replace(const GtsTerrainLodSet &lods, Procgen &procgen, graphics::Graphics &gfx, float worldDiameter, float verticalFovDegrees, float originX=0.f, float originY=0.f, float originZ=0.f)
Replace the complete tile batch after configuring every non-empty candidate.
graphics::Renderable3D * getSourceTerrain() const
Resolve the currently hidden source terrain, or null after source destruction.
~GtsTerrainLodRuntime()
Gts terrain lod runtime.
Result< int > applyMaterial(graphics::Material &material)
Assign one Graphics-owned material to every live tile renderable.
Owned result of the GTS split-and-sequential-simplify pipeline.
int getTileCount() const
Return the number of tiles, including empty source cells.
const GtsTerrainLodTile * tileAt(int index) const
Return one tile or nullptr for an invalid index. @ownership The LOD set retains ownership; the caller...
Procedural generation module. Phase A: runtime maps (semantic Grid2D → TileLayer)....
Definition Procgen.h:146
eve::Result< void > configureGtsTerrainTileLods(const GtsTerrainLodSet &lods, int tileIndex, graphics::Renderable3D &renderable, graphics::Graphics &gfx, float worldDiameter, float verticalFovDegrees, float originX=0.f, float originY=0.f, float originZ=0.f)
Upload and atomically attach one GTS tile's LOD chain to a renderable.
Definition Procgen.cpp:2369