载入中...
搜索中...
未找到
AnimSyncGroup.cpp
浏览该文件的文档.
2
6#include "common/Exception.h"
7
8#include <cmath>
9
10namespace eve::animation {
11
12void AnimSyncGroup::addPlayer(AnimPlayer* player, float phaseOffset) {
13 if (!player) throw Exception("AnimSyncGroup.addPlayer: player is null");
14 entries_.push_back({player, phaseOffset});
15}
16
18 if (index < 0 || index >= getCount()) throw Exception("AnimSyncGroup.setLeader: invalid index %d", index);
19 leader_ = index;
20}
21
23 auto seconds = detail::secondsForStep(step, hasLastTick_, lastTick_, "AnimSyncGroup");
24 if (!seconds) return eve::Result<void>::failure(seconds.status());
25 (void)std::move(seconds).takeValue();
26 usedMarkerSync_ = false;
27 if (entries_.empty()) {
28 lastTick_ = step.tick;
29 hasLastTick_ = true;
31 }
32 for (const Entry& entry : entries_) {
33 if (entry.player->hasCurrentTick() && step.tick <= entry.player->currentTick())
35 eve::DiagnosticCode::Conflict, "animation sync-group player already consumed this tick"));
36 }
37 Entry& leader = entries_[static_cast<size_t>(leader_)];
38 auto leaderResult = leader.player->advance(step);
39 if (!leaderResult) return eve::Result<void>::failure(leaderResult.status());
40 AnimClip* leaderClip = leader.player->getClip();
41 if (!leaderClip || leaderClip->getDuration() <= 1e-8f) {
42 lastTick_ = step.tick;
43 hasLastTick_ = true;
45 }
46 phase_ = leaderClip->wrapTime(leader.player->getTime()) / leaderClip->getDuration();
47 for (int i = 0; i < getCount(); ++i) {
48 if (i == leader_) continue;
49 AnimPlayer* player = entries_[static_cast<size_t>(i)].player;
50 auto playerResult = player->advance(step);
51 if (!playerResult) return eve::Result<void>::failure(playerResult.status());
52 AnimClip* clip = player->getClip();
53 if (!clip || clip->getDuration() <= 1e-8f) continue;
54 float mappedTime;
55 if (leaderClip->hasCompatibleSyncMarkers(clip)) {
56 mappedTime = leaderClip->mapSyncTimeTo(leader.player->getTime(), clip);
57 usedMarkerSync_ = true;
58 } else {
59 mappedTime = phase_ * clip->getDuration();
60 }
61 mappedTime += entries_[static_cast<size_t>(i)].phaseOffset * clip->getDuration();
62 player->setTime(clip->wrapTime(mappedTime));
63 }
64 lastTick_ = step.tick;
65 hasLastTick_ = true;
67}
68
69void AnimSyncGroup::update(float dt) {
70 auto step = detail::legacyStep(dt, hasLastTick_, lastTick_, "AnimSyncGroup");
71 if (!step) {
72 step.ignore("legacy AnimSyncGroup update");
73 return;
74 }
75 advance(std::move(step).takeValue()).ignore("legacy AnimSyncGroup update");
76}
77
78} // namespace eve::animation
glm::vec4 clip
float step
Definition TreeMesh.cpp:314
uint32_t index
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
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:537
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
Keyframed skeletal animation clip (local TRS tracks per bone). Script type: AnimClip.
Definition AnimClip.h:145
float getDuration() const
Returns the duration.
Definition AnimClip.h:163
bool hasCompatibleSyncMarkers(const AnimClip *target) const
Whether this clip and target share a usable ordered marker interval.
Definition AnimClip.cpp:252
float mapSyncTimeTo(float time, const AnimClip *target) const
Map local time into target marker space, falling back to normalized duration.
Definition AnimClip.cpp:280
float wrapTime(float time) const
Wrap or clamp time according to loop flag.
Definition AnimClip.cpp:471
Single-clip (or cross-fading) 3D animation player. Script type: AnimPlayer.
Definition AnimPlayer.h:30
eve::Result< void > advance(const eve::SimulationStep &step) override
Advance playback and sample into internal pose.
void setTime(float seconds)
Sets the time.
AnimClip * getClip() const
Borrow the selected clip, or null if none is selected. The caller owns the clip and must keep it aliv...
Definition AnimPlayer.h:86
eve::Result< void > advance(const eve::SimulationStep &step)
Advance and synchronize all players using one scheduler step.
void update(float dt)
Legacy seconds facade; explicitly forwards to advance().
void setLeader(int index)
Sets the leader.
int getCount() const
Returns the count.
void addPlayer(AnimPlayer *player, float phaseOffset=0.f)
Adds player.
eve::Result< eve::SimulationStep > legacyStep(float seconds, bool hasLastTick, eve::SimulationTick lastTick, const char *owner)
Convert a legacy seconds call to the next local scheduler step.
eve::Result< float > secondsForStep(const eve::SimulationStep &step, bool hasLastTick, eve::SimulationTick lastTick, const char *owner)
Validate a scheduler step before an animation object mutates state.
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158