载入中...
搜索中...
未找到
StaticPrefabRenderInstance.cpp
浏览该文件的文档.
3#include "common/ECS.h"
5
6#include <algorithm>
7#include <cmath>
8#include <cstdio>
9#include <exception>
10#include <glm/gtc/type_ptr.hpp>
11#include <vector>
12
13namespace eve::asset_graphics {
14namespace {
15bool validAffine(const glm::mat4& matrix) {
16 const auto* values = glm::value_ptr(matrix);
17 return std::all_of(values, values + 16, [](float value) { return std::isfinite(value); }) && matrix[0][3] == 0.f &&
18 matrix[1][3] == 0.f && matrix[2][3] == 0.f && matrix[3][3] == 1.f &&
19 std::isfinite(glm::length(glm::vec3(matrix[0]))) && std::isfinite(glm::length(glm::vec3(matrix[1]))) &&
20 std::isfinite(glm::length(glm::vec3(matrix[2])));
21}
22void setMatrix(graphics::Renderable3D& renderer, const glm::mat4& matrix) {
23 renderer.setPosition(matrix[3].x, matrix[3].y, matrix[3].z);
24 renderer.setScale(glm::length(glm::vec3(matrix[0])), glm::length(glm::vec3(matrix[1])),
25 glm::length(glm::vec3(matrix[2])));
26 renderer.transform()->linkedWorld = matrix;
27 renderer.transform()->hasLinkedWorld = true;
28}
29} // namespace
31 std::unique_ptr<PreparedStaticPrefabScene> preparation;
32 std::vector<ecs::EntityHandle> entities;
33};
34StaticPrefabRenderInstance::StaticPrefabRenderInstance(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
36 auto result = release();
37 if (!result.ok()) std::fprintf(stderr, "prefab renderer cleanup failed: %s\n", result.status().describe().c_str());
38}
40 std::unique_ptr<PreparedStaticPrefabScene> preparation, const std::array<float, 16>& world) {
42 const auto matrix = glm::make_mat4(world.data());
43 if (!preparation || preparation->parts().empty() || !validAffine(matrix) || ecs::current()->is_deferred())
44 return Output::failure(
46 "prefab renderer requires live nonempty preparation, affine pose and non-deferred ECS"));
47 for (const auto& part : preparation->parts()) {
48 if (!part.mesh || !part.material || !validAffine(matrix * glm::make_mat4(part.localTransform.data())))
50 "prefab renderer part or composed transform is invalid"));
51 }
52 std::unique_ptr<StaticPrefabRenderInstance> candidate;
53 try {
54 candidate.reset(new StaticPrefabRenderInstance(std::make_unique<Impl>(Impl{std::move(preparation), {}})));
55 candidate->impl_->entities.reserve(candidate->impl_->preparation->parts().size());
56 for (const auto& part : candidate->impl_->preparation->parts()) {
57 auto* entity = graphics::Renderable3D::create();
58 candidate->impl_->entities.push_back(ecs::handle_of(entity));
59 entity->setVisible(false);
60 entity->setMesh(part.mesh);
61 entity->setMaterial(part.material);
62 entity->setCastShadow(part.castShadows);
63 entity->setReceiveShadow(part.receiveShadows);
64 setMatrix(*entity, matrix * glm::make_mat4(part.localTransform.data()));
65 }
66 } catch (const std::exception& error) {
67 return Output::failure(Diagnostic::error(DiagnosticCode::Failed, error.what()));
68 }
69 for (const auto& handle : candidate->impl_->entities)
70 static_cast<graphics::Renderable3D*>(ecs::try_get(handle))->setVisible(true);
71 return Output::success(std::move(candidate));
72}
73std::size_t StaticPrefabRenderInstance::rendererCount() const noexcept { return impl_->entities.size(); }
75 if (index >= impl_->entities.size())
77 Diagnostic::error(DiagnosticCode::NotFound, "prefab renderer index is absent"));
78 auto* entity = ecs::try_get(impl_->entities[index]);
79 if (!entity)
81 Diagnostic::error(DiagnosticCode::StaleHandle, "prefab renderer entity was deleted or reused"));
83}
85 if (!impl_->preparation)
87 Diagnostic::error(DiagnosticCode::PreconditionViolation, "prefab renderer instance was released"));
88 const auto matrix = glm::make_mat4(world.data());
89 if (!validAffine(matrix))
90 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "invalid prefab world matrix"));
91 const auto& parts = impl_->preparation->parts();
92 for (std::size_t i = 0; i < parts.size(); ++i) {
93 auto target = renderer(i);
94 if (!target.ok()) return Result<void>::failure(target.status());
95 if (!validAffine(matrix * glm::make_mat4(parts[i].localTransform.data())))
97 Diagnostic::error(DiagnosticCode::InvalidArgument, "prefab composed matrix is not representable"));
98 }
99 for (std::size_t i = 0; i < parts.size(); ++i) {
100 auto* target = static_cast<graphics::Renderable3D*>(ecs::try_get(impl_->entities[i]));
101 setMatrix(*target, matrix * glm::make_mat4(parts[i].localTransform.data()));
102 }
103 return Result<void>::success();
104}
106 for (const auto& handle : impl_->entities) {
107 if (auto* entity = static_cast<graphics::Renderable3D*>(ecs::try_get(handle))) {
108 entity->setVisible(false);
109 entity->setMesh(nullptr);
110 entity->setMaterial(nullptr);
111 ecs::DestroyEntity(entity);
112 }
113 }
114 impl_->entities.clear();
115 if (impl_->preparation) {
116 auto result = impl_->preparation->release();
117 if (!result.ok()) return result;
118 impl_->preparation.reset();
119 }
120 return Result<void>::success();
121}
122} // namespace eve::asset_graphics
double z
double y
double x
LogicalId target
std::vector< eve::artifact::PartView > parts
void * impl
Poco::JSON::Object::Ptr result
std::shared_ptr< OceanRenderer > renderer
std::string error
Definition Package.cpp:174
PrimitiveHandle handle
std::set< std::string > values
Object::Ptr value
const EntityFact * entity
glm::dmat4 matrix
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
Own renderer entities and their prepared prefab resources, without owning a Scene node....
Result< void > release()
Destroy surviving entities, then release preparation; cleanup failure retains the lease for retry.
std::size_t rendererCount() const noexcept
Return the number of owned renderer handles; externally deleted entries remain counted until release.
Result< graphics::Renderable3D * > renderer(std::size_t index) const
Resolve a renderer for synchronous Scene linking or diagnostics.
~StaticPrefabRenderInstance()
Destroy surviving renderer entities before releasing their material/mesh preparation lease.
static Result< std::unique_ptr< StaticPrefabRenderInstance > > create(std::unique_ptr< PreparedStaticPrefabScene > preparation, const std::array< float, 16 > &world)
Consume preparation and publish all renderer parts after validating composed transforms.
StaticPrefabRenderInstance(const StaticPrefabRenderInstance &)=delete
Result< void > setWorldTransform(const std::array< float, 16 > &world)
Update all composed renderer matrices after validating every matrix and live entity handle.
EVENGINE_API_BACKENDS public API.
std::unique_ptr< PreparedStaticPrefabScene > preparation