载入中...
搜索中...
未找到
AnimClip.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
6#include "animation/AnimSmr.h"
7#include "common/Result.h"
8
9#include <memory>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace eve::animation {
15
16class AnimPose;
17class AnimSkeleton;
18
28public:
30 void addBoneMapping(const std::string& sourceBone, const std::string& targetBone);
32 void clearBoneMappings();
34 void setNormalizedNameMatching(bool enabled) { normalizedNameMatching_ = enabled; }
36 bool getNormalizedNameMatching() const { return normalizedNameMatching_; }
37
39 void setRootBones(const std::string& sourceBone, const std::string& targetBone);
41 void setAutoRootScale(bool enabled) { autoRootScale_ = enabled; }
43 bool getAutoRootScale() const { return autoRootScale_; }
45 void setRootTranslationScale(float horizontal, float vertical);
47 float getRootHorizontalScale() const { return rootHorizontalScale_; }
49 float getRootVerticalScale() const { return rootVerticalScale_; }
51 void setUseSkeletonSpaceRotation(bool enabled) { skeletonSpaceRotation_ = enabled; }
53 bool getUseSkeletonSpaceRotation() const { return skeletonSpaceRotation_; }
54
61 void setSkinnedInteractionPreserve(bool enabled) { skinnedInteractionPreserve_ = enabled; }
63 bool getSkinnedInteractionPreserve() const { return skinnedInteractionPreserve_; }
68 void setInteractionContactThreshold(float distance);
70 float getInteractionContactThreshold() const { return interactionContactThreshold_; }
72 void setInteractionCorrectionWeight(float weight);
74 float getInteractionCorrectionWeight() const { return interactionCorrectionWeight_; }
76 void addInteractionIkChain(const std::string& rootBone, const std::string& midBone, const std::string& tipBone);
78 void clearInteractionIkChains();
80 int getInteractionCorrectionCount() const { return interactionCorrectionCount_; }
81
86 void setNeuralModelPath(const std::string& path) { neuralModelPath_ = path; }
88 const std::string& getNeuralModelPath() const { return neuralModelPath_; }
93 void setNeuralRetargetEnabled(bool enabled) { neuralRetargetEnabled_ = enabled; }
95 bool getNeuralRetargetEnabled() const { return neuralRetargetEnabled_; }
100 void setNeuralBackend(const std::string& backend) { neuralBackend_ = backend; }
102 const std::string& getNeuralBackend() const { return neuralBackend_; }
104 int getNeuralInferenceCount() const { return neuralInferenceCount_; }
105
107 int getMatchedBoneCount() const { return matchedBoneCount_; }
109 int getUnmatchedBoneCount() const { return static_cast<int>(unmatchedTargetBones_.size()); }
111 std::string getUnmatchedTargetBone(int index) const;
112
113private:
114 friend class AnimClip;
115 friend int smrRefineRetargetedClip(const AnimClip&, AnimClip&, const AnimSkeleton*, const AnimSkeleton*,
117 struct Mapping {
118 std::string source, target;
119 };
120 std::vector<Mapping> mappings_;
121 std::string sourceRoot_;
122 std::string targetRoot_;
123 bool normalizedNameMatching_ = true;
124 bool autoRootScale_ = true;
125 bool skeletonSpaceRotation_ = true;
126 bool skinnedInteractionPreserve_ = false;
127 bool neuralRetargetEnabled_ = false;
128 float rootHorizontalScale_ = 1.f;
129 float rootVerticalScale_ = 1.f;
130 float interactionContactThreshold_ = 0.f;
131 float interactionCorrectionWeight_ = 1.f;
132 int matchedBoneCount_ = 0;
133 int interactionCorrectionCount_ = 0;
134 int neuralInferenceCount_ = 0;
135 std::string neuralModelPath_;
136 std::string neuralBackend_ = "auto";
137 std::vector<std::string> unmatchedTargetBones_;
138 std::vector<AnimSmrIkChain> interactionIkChains_;
139};
140
146public:
148 explicit AnimClip(std::string name = "");
150 ~AnimClip();
151
152 AnimClip(const AnimClip&) = delete;
153 AnimClip& operator=(const AnimClip&) = delete;
154
156 void setName(const std::string& name) { name_ = name; }
158 std::string getName() const { return name_; }
159
161 void setDuration(float seconds);
163 float getDuration() const { return duration_; }
164
166 void setLoop(bool loop) { loop_ = loop; }
168 bool getLoop() const { return loop_; }
169
171 void setSampleRate(float hz);
173 float getSampleRate() const { return sampleRate_; }
174
176 void addPositionKey(int boneIndex, float time, float x, float y, float z);
178 void addRotationKey(int boneIndex, float time, float x, float y, float z, float w);
180 void addScaleKey(int boneIndex, float time, float x, float y, float z);
182 bool setPositionKey(int boneIndex, int keyIndex, float time, float x, float y, float z);
184 bool setRotationKey(int boneIndex, int keyIndex, float time, float x, float y, float z, float w);
186 bool setScaleKey(int boneIndex, int keyIndex, float time, float x, float y, float z);
188 bool removePositionKey(int boneIndex, int keyIndex);
190 bool removeRotationKey(int boneIndex, int keyIndex);
192 bool removeScaleKey(int boneIndex, int keyIndex);
194 bool clearTrack(int boneIndex);
196 int getTrackCount() const { return static_cast<int>(tracks_.size()); }
198 void addEvent(float time, const std::string& name, const std::string& payload = "");
200 bool setEvent(int index, float time, const std::string& name, const std::string& payload = "");
202 bool removeEvent(int index);
204 int getEventCount() const { return static_cast<int>(events_.size()); }
206 float getEventTime(int index) const;
208 std::string getEventName(int index) const;
210 std::string getEventPayload(int index) const;
212 [[nodiscard]] bool hasEvent(std::string_view name) const noexcept;
223 [[nodiscard]] eve::Result<void> validateNotifyContract(const std::vector<std::string>& requiredNames) const;
224
226 void addSyncMarker(float time, const std::string& name);
228 bool setSyncMarker(int index, float time, const std::string& name);
230 bool removeSyncMarker(int index);
232 int getSyncMarkerCount() const { return static_cast<int>(syncMarkers_.size()); }
234 float getSyncMarkerTime(int index) const;
236 std::string getSyncMarkerName(int index) const;
238 bool hasCompatibleSyncMarkers(const AnimClip* target) const;
240 float mapSyncTimeTo(float time, const AnimClip* target) const;
241
243 int getPositionKeyCount(int boneIndex) const;
245 int getRotationKeyCount(int boneIndex) const;
247 int getScaleKeyCount(int boneIndex) const;
248
250 float getPositionKeyTime(int boneIndex, int keyIndex) const;
252 float getPositionKeyX(int boneIndex, int keyIndex) const;
254 float getPositionKeyY(int boneIndex, int keyIndex) const;
256 float getPositionKeyZ(int boneIndex, int keyIndex) const;
257
259 float getRotationKeyTime(int boneIndex, int keyIndex) const;
261 float getRotationKeyX(int boneIndex, int keyIndex) const;
263 float getRotationKeyY(int boneIndex, int keyIndex) const;
265 float getRotationKeyZ(int boneIndex, int keyIndex) const;
267 float getRotationKeyW(int boneIndex, int keyIndex) const;
268
270 float getScaleKeyTime(int boneIndex, int keyIndex) const;
272 float getScaleKeyX(int boneIndex, int keyIndex) const;
274 float getScaleKeyY(int boneIndex, int keyIndex) const;
276 float getScaleKeyZ(int boneIndex, int keyIndex) const;
277
283 void applyPlanarRootMotion(int boneIndex, float speedX, float speedZ);
284
292 int compress(float positionError = 0.001f, float rotationErrorDegrees = 0.1f, float scaleError = 0.001f);
293
299 AnimClip* retarget(const AnimSkeleton* sourceSkeleton, const AnimSkeleton* targetSkeleton) const;
300
307 AnimClip* retargetWithProfile(const AnimSkeleton* sourceSkeleton, const AnimSkeleton* targetSkeleton,
308 AnimRetargetProfile* profile) const;
309
314 void sample(float time, AnimPose* out, const AnimSkeleton* skeleton = nullptr) const;
316 void sampleLod(float time, AnimPose* out, const AnimSkeleton* skeleton, int lodLevel) const;
317
319 float wrapTime(float time) const;
320
322 [[nodiscard]] std::unique_ptr<AnimClip> clone() const;
323
330 void adopt(AnimClip& other);
332 void collectEvents(float previousTime, float currentTime, bool loop, std::vector<std::string>& out) const;
333
334private:
335 friend class AnimPlayer;
336 friend class MotionDatabase;
338 struct Vec3Key {
339 float t = 0.f, x = 0.f, y = 0.f, z = 0.f;
340 };
341 struct QuatKey {
342 float t = 0.f, x = 0.f, y = 0.f, z = 0.f, w = 1.f;
343 };
344 struct BoneTrack {
345 std::vector<Vec3Key> positions;
346 std::vector<QuatKey> rotations;
347 std::vector<Vec3Key> scales;
348 };
349 struct EventMarker {
350 float t = 0.f;
351 std::string name;
352 std::string payload;
353 };
354 struct SyncMarker {
355 float t = 0.f;
356 std::string name;
357 };
358
359 void ensureBone(int boneIndex);
360 TransformTRS sampleBone(int boneIndex, float time, const TransformTRS& fallback) const;
361 void sampleClamped(float time, AnimPose* out, const AnimSkeleton* skeleton) const;
362
363 static void sampleVec3(const std::vector<Vec3Key>& keys, float time, float& x, float& y, float& z, bool& ok);
364 static void sampleQuat(const std::vector<QuatKey>& keys, float time, float& x, float& y, float& z, float& w,
365 bool& ok);
366
367 std::string name_;
368 float duration_ = 0.f;
369 bool loop_ = true;
370 float sampleRate_ = 30.f;
371 std::vector<BoneTrack> tracks_;
372 std::vector<EventMarker> events_;
373 std::vector<SyncMarker> syncMarkers_;
374};
375
376} // namespace eve::animation
LogicalId target
Value::Object payload
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
#define EVENGINE_API_WORLD
Definition Export.h:109
std::vector< float > positions
std::string name
float distance
std::vector< float > scales
Definition OnnxLstm.cpp:27
std::string path
Definition PlayHost.cpp:110
float t
Move-only, checked operation results for the common layer.
uint32_t index
const UnitySourceAsset & source
Move-only operation result carrying either a value or Status.
Definition Result.h:155
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 getLoop() const
Returns the loop.
Definition AnimClip.h:168
int getEventCount() const
Return the number of event markers.
Definition AnimClip.h:204
float getSampleRate() const
Returns the sample rate.
Definition AnimClip.h:173
std::string getName() const
Returns the name.
Definition AnimClip.h:158
int getSyncMarkerCount() const
Return the number of locomotion sync markers.
Definition AnimClip.h:232
void setName(const std::string &name)
Sets the name.
Definition AnimClip.h:156
int getTrackCount() const
Return the number of allocated bone tracks, including empty tracks.
Definition AnimClip.h:196
void setLoop(bool loop)
Sets the loop.
Definition AnimClip.h:166
AnimClip & operator=(const AnimClip &)=delete
AnimClip(const AnimClip &)=delete
Single-clip (or cross-fading) 3D animation player. Script type: AnimPlayer.
Definition AnimPlayer.h:30
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
Definition AnimPose.h:17
Settings and diagnostics for offline skeletal animation retargeting.
Definition AnimClip.h:27
const std::string & getNeuralBackend() const
Return the neural backend hint.
Definition AnimClip.h:102
float getInteractionContactThreshold() const
Return the configured interaction contact threshold (0 = auto).
Definition AnimClip.h:70
int getNeuralInferenceCount() const
Non-zero when the last retarget used the neural MeshRet path.
Definition AnimClip.h:104
void setNeuralRetargetEnabled(bool enabled)
Prefer neural MeshRet inference when animation_tensor provides ISmrNeuralRetarget....
Definition AnimClip.h:93
void setUseSkeletonSpaceRotation(bool enabled)
Choose skeleton-space Bind correction instead of legacy local-space correction.
Definition AnimClip.h:51
const std::string & getNeuralModelPath() const
Return the configured neural model path (may be empty).
Definition AnimClip.h:88
int getInteractionCorrectionCount() const
Number of frames that received at least one interaction IK correction last retarget.
Definition AnimClip.h:80
float getRootVerticalScale() const
Return the additional vertical root translation multiplier.
Definition AnimClip.h:49
int getUnmatchedBoneCount() const
Number of target bones left at bind pose by the most recent operation.
Definition AnimClip.h:109
void setSkinnedInteractionPreserve(bool enabled)
Enable MeshRet-inspired skinned interaction preservation after FK retarget.
Definition AnimClip.h:61
bool getUseSkeletonSpaceRotation() const
Return whether skeleton-space rotation correction is enabled.
Definition AnimClip.h:53
bool getAutoRootScale() const
Return whether automatic root scaling is enabled.
Definition AnimClip.h:43
float getRootHorizontalScale() const
Return the additional horizontal root translation multiplier.
Definition AnimClip.h:47
bool getNeuralRetargetEnabled() const
Return whether neural retarget is preferred when a provider is available.
Definition AnimClip.h:95
void setAutoRootScale(bool enabled)
Enable automatic root translation scaling from skeleton extents.
Definition AnimClip.h:41
int getMatchedBoneCount() const
Number of target bones mapped by the most recent retarget operation.
Definition AnimClip.h:107
void setNeuralModelPath(const std::string &path)
Filesystem path to a MeshRet-compatible ONNX model for neural skinned retarget. Empty path selects th...
Definition AnimClip.h:86
void setNormalizedNameMatching(bool enabled)
Enable exact-then-normalized automatic bone-name matching.
Definition AnimClip.h:34
bool getSkinnedInteractionPreserve() const
Return whether skinned interaction preservation is enabled.
Definition AnimClip.h:63
float getInteractionCorrectionWeight() const
Return the interaction IK blend weight.
Definition AnimClip.h:74
bool getNormalizedNameMatching() const
Return whether normalized automatic bone-name matching is enabled.
Definition AnimClip.h:36
void setNeuralBackend(const std::string &backend)
Backend hint: "auto" (default), "onnx", or "tensor". Unknown values are treated as "auto".
Definition AnimClip.h:100
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...
Baked motion-matching feature database from one or more AnimClips. Feature layout per frame: [0....
int smrRefineRetargetedClip(const AnimClip &sourceClip, AnimClip &targetClip, const AnimSkeleton *sourceSkeleton, const AnimSkeleton *targetSkeleton, AnimRetargetProfile &profile)
Refine an FK-retargeted clip so source body-part proximity/contact is preserved on the target.
Definition AnimSmr.cpp:344
bool enabled