载入中...
搜索中...
未找到
ParticleBakePresentation.cpp
浏览该文件的文档.
1// eve-hot-path
3#include <algorithm>
4#include <new>
5#include <utility>
7#include "graphics/Graphics.h"
10namespace eve::physics {
13 std::vector<BakedParticleSample> samples;
14 std::vector<graphics::DrawItem2D> items;
16 items.reserve(samples.size());
17 }
18};
19ParticleBakePresentation::ParticleBakePresentation(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
22 try {
23 return Result<std::unique_ptr<ParticleBakePresentation>>::success(std::unique_ptr<ParticleBakePresentation>(
24 new ParticleBakePresentation(std::make_unique<Impl>(std::move(track)))));
25 } catch (const std::bad_alloc&) {
27 DiagnosticCode::Failed, "particle presentation allocation failed", "particleBakePresentation"));
28 }
29}
35 std::span<const ParticleBakeMaterial> materials) {
36 if (materials.size() > 65536)
38 DiagnosticCode::InvalidArgument, "particle material budget exceeded", "particleBakePresentation"));
39 for (std::size_t i = 0; i < materials.size(); ++i)
40 if (materials[i].emitterSession.isNil() ||
41 (i && !(materials[i - 1].emitterSession < materials[i].emitterSession)))
43 DiagnosticCode::InvalidArgument, "particle materials must have sorted unique session identities",
44 "particleBakePresentation"));
45 return drawImpl(player, graphics, nullptr, materials);
46}
47Result<std::size_t> ParticleBakePresentation::drawImpl(BakedParticlePlayback& player, graphics::Graphics& graphics,
48 const graphics::DrawItem2D* uniformMaterial,
49 std::span<const ParticleBakeMaterial> materials) {
50 if (!impl_->track.isSameAsset(player.track()))
52 Diagnostic::error(DiagnosticCode::InvalidArgument, "foreign particle player", "particleBakePresentation"));
53 auto sampled = player.sample(impl_->samples);
54 if (!sampled) return Result<std::size_t>::failure(sampled.status());
55 const auto count = sampled.value();
56 // Clear borrowed resource references even if the renderer throws; no material survives this call.
57 struct Clear {
58 std::vector<graphics::DrawItem2D>& items;
59 ~Clear() { items.clear(); }
60 } clear{impl_->items};
61 bool usedCanvas = false;
62 for (std::size_t i = 0; i < count; ++i) {
63 const auto& pose = impl_->samples[i].pose;
64 const graphics::DrawItem2D* selected = uniformMaterial;
65 if (!selected) {
66 const auto id = impl_->samples[i].emitter;
67 auto found = std::lower_bound(
68 materials.begin(), materials.end(), id,
69 [](const ParticleBakeMaterial& binding, PersistentId value) { return binding.emitterSession < value; });
70 if (found == materials.end() || found->emitterSession != id)
72 Diagnostic::error(DiagnosticCode::NotFound, "visible particle emitter has no material binding",
73 "particleBakePresentation"));
74 selected = &found->material;
75 }
76 auto item = *selected;
77 usedCanvas = usedCanvas || item.canvas != nullptr;
78 item.x = pose.x;
79 item.y = pose.y;
80 item.w = pose.w;
81 item.h = pose.h;
82 item.rotation = pose.rotation;
83 item.color = {pose.r, pose.g, pose.b, pose.a};
84 item.u0 = pose.u0;
85 item.v0 = pose.v0;
86 item.u1 = pose.u1;
87 item.v1 = pose.v1;
88 item.hasUV = true;
89 item.rotatedUV = false;
90 item.anchorX = item.anchorY = 0.5f;
91 item.flipX = item.flipY = false;
92 item.order = static_cast<int>(i);
93 item.hasOrder = true;
94 item.quad = nullptr;
95 impl_->items.push_back(item);
96 }
97 if (!impl_->items.empty()) graphics::RenderSystem::drawItems(graphics, impl_->items, false);
98 if (usedCanvas) graphics.setCanvas();
100}
101} // namespace eve::physics
std::string selected
eve::EntitySpatialPose pose
void * impl
std::uint32_t capacity
eve::action::ActionVfxBinding binding
std::array< uint32_t, 16 > material
Object::Ptr value
bool found
Graphics * graphics
int count
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static void drawItems(Graphics &gfx, std::vector< DrawItem2D > &items, bool present)
Sort and draw items. If present=true, calls gfx.present() at the end. Map::render uses present=false ...
Independent clock and placement over a shared immutable particle clip. @ownership Owns reusable scrat...
Result< std::size_t > sample(std::span< BakedParticleSample > output)
Sample and transform into capacity-sized output; all failures preserve the caller's entire buffer....
ParticleTrack track() const
Return a cheap owning immutable clip copy, independent of player lifetime.
Prepared sprite-batch consumer of recorded particles, independent of source emitters....
ParticleBakePresentation(const ParticleBakePresentation &)=delete
static Result< std::unique_ptr< ParticleBakePresentation > > prepare(ParticleTrack track)
Allocate reusable presentation buffers for a frozen asset. @cost Linear in recorded births for buffer...
Result< std::size_t > draw(BakedParticlePlayback &player, graphics::Graphics &graphics, const graphics::DrawItem2D &material)
Sample and submit sprites through the ordinary 2D batch renderer. @ownership Borrows Graphics and mat...
Immutable particle trajectories independent of live simulation, ECS and graphics resources....
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
StrongUuid< detail::PersistentIdTag > PersistentId
Stable instance identity for persistence, networking and process boundaries.
Definition Identity.h:253
Shared 2D draw queue item (sprites + tiles).
Definition DrawItem2D.h:19