载入中...
搜索中...
未找到
SceneVisibilityProvider.cpp
浏览该文件的文档.
1#include <map>
2#include <set>
3#include "common/Capability.h"
5#include "graphics/Graphics.h"
6#include "graphics/Mesh.h"
9#include "scene/SceneHost.h"
10
11namespace eve::graphics {
12namespace {
13const Value& member(const Value& v, const char* name) { return v.getIf<Value::Object>()->at(name); }
14Result<Value> rejected(DiagnosticCode code, const char* message) {
16}
17class VisibilityProvider final : public ISceneVisibilityEvidence {
18public:
19 Graphics* graphics = nullptr;
20 Result<Value> inspect(const std::string& name, const Value& request) override {
21 if (!graphics) return rejected(DiagnosticCode::Unsupported, "Visibility graphics provider detached");
23 if (!parsed.ok()) return Result<Value>::failure(parsed.status());
24 scene::SceneHost* host = nullptr;
25 if (ecs::current()->getManager<scene::SceneHost>()) {
26 auto view = ecs::View<scene::SceneHost, scene::SceneHost::Meta>();
27 for (auto it = view.begin(); it != view.end(); ++it) {
28 auto [meta] = *it;
29 if (meta->entity && meta->name == name) {
30 if (host) return rejected(DiagnosticCode::Conflict, "Projection host name is ambiguous");
31 host = meta->entity;
32 }
33 }
34 }
35 if (!host) return rejected(DiagnosticCode::StaleHandle, "Projection host missing");
36 std::map<std::uint32_t, Value> actual;
37 bool invalid = false;
38 const auto* ops = scene::linkOps(scene::findLinkKind("renderable3d"));
39 host->walkDepthFirst([&](scene::SceneHost*, int, scene::SceneNode& node) {
40 bool visible = host->meta()->visible && node.visible;
41 for (int parent = node.parent; parent >= 0; parent = host->tree()->nodes[parent].parent)
42 visible = visible && host->tree()->nodes[parent].visible;
43 if (!visible) return;
44 const auto* link = host->findLink(&node, scene::findLinkKind("renderable3d"));
45 if (!link || !link->target) return;
46 if (!ops || !ops->alive || !ops->alive(link->target)) {
47 invalid = true;
48 return;
49 }
50 auto* entity = static_cast<Renderable3D*>(link->target);
51 const auto* mesh = entity->meshRenderer()->mesh;
52 constexpr std::string_view prefix = "@projection/";
53 const auto split = node.id.rfind('/');
54 if (!node.id.starts_with(prefix) || split <= prefix.size() || !mesh || !mesh->geometryGeneration()) {
55 invalid = true;
56 return;
57 }
59 for (int c = 0; c < 4; ++c)
60 for (int r = 0; r < 4; ++r) model.emplace_back(double(node.world[c][r]));
61 Value proof = Value::Object{{"object", node.id.substr(prefix.size(), split - prefix.size())},
62 {"generation", std::int64_t(entity->generation)},
63 {"geometryGeneration", std::int64_t(mesh->geometryGeneration())},
64 {"vertexCount", mesh->gpuVertexCount},
65 {"indexCount", mesh->indexCount},
66 {"liveModel", std::move(model)}};
67 if (!actual.emplace(entity->id, std::move(proof)).second) invalid = true;
68 });
69 if (invalid) return rejected(DiagnosticCode::Conflict, "Projection links or geometry cannot be proven");
70 const auto& draws = *member(request, "draws").getIf<Value::Array>();
71 if (draws.size() != actual.size())
72 return rejected(DiagnosticCode::Conflict, "Visibility requires the complete visible native projection");
73 Value::Object proofs;
74 std::set<std::uint32_t> ids;
75 for (const auto& draw : draws) {
76 const auto& value = member(draw, "entityId");
77 const auto id = value.getIf<std::int64_t>() ? std::uint32_t(*value.getIf<std::int64_t>())
78 : std::uint32_t(*value.getIf<double>());
79 const auto it = actual.find(id);
80 if (it == actual.end() || !ids.insert(id).second ||
81 member(draw, "generation") != member(it->second, "generation") ||
82 member(draw, "object") != member(it->second, "object"))
83 return rejected(DiagnosticCode::Conflict, "Visibility draw is not the native object's projection");
84 proofs[std::to_string(id)] = it->second;
85 }
86 return Result<Value>::success(Value::Object{{"hostId", std::int64_t(host->id)},
87 {"hostGeneration", std::int64_t(host->generation)},
88 {"draws", std::move(proofs)}});
89 }
90 Result<Value> measure(const std::string& host, const Value& request, const Value& proof) override {
91 auto current = inspect(host, request);
92 if (!current.ok()) return current;
93 if (current.value() != proof)
94 return rejected(DiagnosticCode::Conflict, "Projection or geometry changed during preview");
96 if (!prepared.ok()) return Result<Value>::failure(prepared.status());
97 return prepared.value().measure(*graphics);
98 }
99};
100VisibilityProvider& provider() {
101 static VisibilityProvider instance;
102 return instance;
103}
104} // namespace
106 provider().graphics = graphics;
107 cap::provide<ISceneVisibilityEvidence>(&provider());
108}
109void detachSceneVisibilityProvider(Graphics* graphics) noexcept {
110 if (provider().graphics == graphics) {
111 cap::revoke<ISceneVisibilityEvidence>(&provider());
112 provider().graphics = nullptr;
113 }
114}
115} // namespace eve::graphics
std::string name
Definition ECS.cpp:154
std::string message
DiagnosticCode code
const GltfImportRequest & request
double r
float v
std::int32_t c
PhysicsJointLink link
const ColourScope * parent
MeshVfxBatchedDraw draw
OceanMeshSettings mesh
OceanSprayView view
Object::Ptr value
const EntityFact * entity
glm::mat4 model
const RoadNode * node
LinkOps ops
Graphics * graphics
double current
bool visible
UIHostHandle host
float(ui::Theme::* member)[4]
std::size_t at
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
static Result< SceneVisibilityQuery > prepare(const Value &request)
Validate schema v1/v2 and copy the bounded request without drawing.
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
Result< T > rejected(RuleId rule, std::string message, eve::DiagnosticCode code=eve::DiagnosticCode::PreconditionViolation)
Construct a Rejected result for invalid editing input or preconditions.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
void detachSceneVisibilityProvider(Graphics *graphics) noexcept
Revoke the matching provider before Graphics destruction.
void registerSceneVisibilityProvider(Graphics *graphics)
Bind and register the owner-thread native visibility provider during Graphics startup.
int findLinkKind(const char *kind)
Finds link kind.
const LinkOps * linkOps(int kindId)
Link ops.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47