载入中...
搜索中...
未找到
ParticleTrack.cpp
浏览该文件的文档.
1// eve-hot-path
2#include <algorithm>
3#include <cmath>
4#include <utility>
7namespace eve::physics {
8namespace {
9Diagnostic invalidParticle(const char* text) {
11}
12bool finitePose(const BakedParticlePose& p) {
13 for (float v : {p.x, p.y, p.w, p.h, p.rotation, p.r, p.g, p.b, p.a, p.u0, p.v0, p.u1, p.v1})
14 if (!std::isfinite(v)) return false;
15 return true;
16}
17} // namespace
18ParticleTrack::ParticleTrack(std::shared_ptr<const Data> data) : data_(std::move(data)) {}
19Result<ParticleTrack> ParticleTrack::prepare(float duration, std::vector<ParticleTrajectory> trajectories,
20 const BakeRecipe& recipe) {
21 auto encoded = recipe.encode();
22 if (!encoded) return Result<ParticleTrack>::failure(encoded.status());
23 if (!std::isfinite(duration) || duration < 0 || trajectories.size() > 65536 ||
24 recipe.coordinates() != BakeCoordinates::PlanarPixels)
25 return Result<ParticleTrack>::failure(invalidParticle("invalid duration, birth budget or coordinates"));
26 std::sort(trajectories.begin(), trajectories.end(), [](const auto& a, const auto& b) {
27 return a.emitter == b.emitter ? a.birthSerial < b.birthSerial : a.emitter < b.emitter;
28 });
29 std::size_t total = 0;
30 for (std::size_t i = 0; i < trajectories.size(); ++i) {
31 const auto& t = trajectories[i];
32 if (t.emitter.isNil() || t.birthSerial == 0 || t.keys.size() < 2 || t.keys.size() > 2000000 - total ||
33 (i && trajectories[i - 1].emitter == t.emitter && trajectories[i - 1].birthSerial == t.birthSerial))
35 invalidParticle("invalid or duplicate birth, or key budget exceeded"));
36 total += t.keys.size();
37 float previous = -1;
38 for (const auto& key : t.keys) {
39 if (!std::isfinite(key.seconds) || key.seconds < 0 || key.seconds <= previous || key.seconds > duration ||
40 !finitePose(key.pose))
41 return Result<ParticleTrack>::failure(invalidParticle("invalid particle event time or presentation"));
42 previous = key.seconds;
43 }
44 }
45 return Result<ParticleTrack>::success(ParticleTrack(std::make_shared<const Data>(
46 Data{duration, std::move(trajectories), std::make_shared<const BakeRecipe>(recipe)})));
47}
48Result<std::size_t> ParticleTrack::sample(float seconds, std::span<BakedParticleSample> output) const {
49 if (!std::isfinite(seconds) || output.size() < capacity())
50 return Result<std::size_t>::failure(invalidParticle("invalid sample time or insufficient output capacity"));
51 seconds = std::clamp(seconds, 0.f, data_->duration);
52 std::size_t count = 0;
53 for (const auto& track : data_->trajectories) {
54 if (seconds < track.keys.front().seconds || seconds >= track.keys.back().seconds) continue;
55 auto upper = std::upper_bound(track.keys.begin(), track.keys.end(), seconds,
56 [](float time, const ParticleTrackKey& key) { return time < key.seconds; });
57 const auto& a = *(upper - 1);
58 const auto& b = *upper;
59 double f = (double(seconds) - a.seconds) / (double(b.seconds) - a.seconds);
60 auto blend = [f](float x, float y) { return static_cast<float>((1 - f) * double(x) + f * double(y)); };
61 auto pose = a.pose;
62 pose.x = blend(a.pose.x, b.pose.x);
63 pose.y = blend(a.pose.y, b.pose.y);
64 pose.w = blend(a.pose.w, b.pose.w);
65 pose.h = blend(a.pose.h, b.pose.h);
66 pose.rotation = blend(a.pose.rotation, b.pose.rotation);
67 pose.r = blend(a.pose.r, b.pose.r);
68 pose.g = blend(a.pose.g, b.pose.g);
69 pose.b = blend(a.pose.b, b.pose.b);
70 pose.a = blend(a.pose.a, b.pose.a);
71 output[count++] = {track.emitter, track.birthSerial, pose};
72 }
74}
75std::size_t ParticleTrack::capacity() const noexcept { return data_->trajectories.size(); }
76float ParticleTrack::duration() const noexcept { return data_->duration; }
77std::shared_ptr<const BakeRecipe> ParticleTrack::recipe() const noexcept { return data_->recipe; }
78} // namespace eve::physics
double y
double x
float duration
std::string output
eve::EntitySpatialPose pose
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
std::uint32_t capacity
float v
float blend
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::shared_ptr< const OceanSprayFrame > previous
BakeRecipe recipe
float f
float t
std::string text
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
Bounded provenance builder for explicit-coordinate baked recordings. @ownership Owns names and conten...
Definition BakeRecipe.h:21
Immutable particle trajectories independent of live simulation, ECS and graphics resources....
eve::Diagnostic Diagnostic
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Explicit event/sample time and owning presentation value.