Polyphase Game Engine
Loading...
Searching...
No Matches
SkeletalMesh3d.h
Go to the documentation of this file.
1#pragma once
2
3#include "Nodes/3D/Mesh3d.h"
4#include "AssetRef.h"
5#include "Vertex.h"
6// std::vector<Animation> mBoundExternalAnims below needs the full Animation
7// type, so we can't fall back on the forward declarations the file used to
8// rely on for AnimEvent / Channel / Animation.
10
11class BoneMaskAsset;
12
14{
15 One,
16 Four,
17 Num
18};
19
28
29// How a slot's animation contributes to the per-bone blend.
30// Replace : standard weighted lerp into the accumulator (default).
31// Additive: the slot's pose is interpreted as a delta from bind pose;
32// that delta is scaled by mWeight and composed on top of the
33// accumulator. Used for hit reactions, recoil, breathing,
34// lean-into-turn — anything you want to layer on a base anim.
35enum class AnimLayerMode
36{
37 Replace,
39
40 Count
41};
42
43class SkeletalMesh;
44struct AnimEvent;
45struct Channel;
46struct Animation;
47
49{
50 std::string mName;
51 float mTime = 0.0f;
52 float mSpeed = 1.0f;
53 float mWeight = 0.0f;
54 int32_t mSlot = 0;
55 bool mLoop = false;
56
57 // Optional bone mask. When valid, only bones marked in the mask's
58 // resolved bitset receive this slot's contribution; the rest stay at
59 // whatever the lower slots (or bind pose) provided.
61
62 // Weight fade. Engaged when mWeightFadeRate != 0; on each tick mWeight
63 // advances toward mWeightTarget by mWeightFadeRate * dt and clamps. When
64 // the target is 0 and reached, the slot self-removes.
65 float mWeightTarget = -1.0f;
66 float mWeightFadeRate = 0.0f;
67
69};
70
72{
73 std::string mName;
74 std::string mDependentAnim;
75 float mTime = 0.0f;
76 float mSpeed = 1.0f;;
77 float mWeight = 0.0f;
78 bool mLoop = false;
79 int32_t mSlot = -1;
80};
81
82typedef void(*AnimEventHandlerFP)(const AnimEvent& animEvent);
83
85{
86public:
87
89
92
93 virtual const char* GetTypeName() const override;
94 virtual void GatherProperties(std::vector<Property>& outProps) override;
95
96 virtual void Create() override;
97 virtual void Destroy() override;
98 SkeletalMeshCompResource* GetResource();
99
100 virtual void Tick(float deltaTime) override;
101 virtual void EditorTick(float deltaTime) override;
102
103 virtual bool IsStaticMesh3D() const override;
104 virtual bool IsSkeletalMesh3D() const override;
105
106 void SetSkeletalMesh(SkeletalMesh* skeletalMesh);
107 SkeletalMesh* GetSkeletalMesh();
108
109 void PlayAnimation(const char* animName, bool loop, float speed = 1.0f, float weight = 1.0f, int32_t priority = -1);
110 // Start `animName` on `slot`, restricted to the bones marked by `mask`.
111 // mask=nullptr is equivalent to PlayAnimation (full body). The slot's
112 // weight is the maximum contribution against lower slots; weight=1 and
113 // mask=UpperBody means upper-body bones are 100% driven by this clip
114 // while lower bones stay on whatever slot 0 (or bind pose) provided.
115 void PlayAnimationMasked(const char* animName, int32_t slot, BoneMaskAsset* mask,
116 bool loop, float speed = 1.0f, float weight = 1.0f);
117 // Start `animName` on `slot` as an additive layer — the slot's pose is
118 // interpreted as a delta from bind pose and composed on top of lower
119 // slots. Mask gates which bones receive the delta. Use for hit reactions,
120 // recoil, breathing, lean-into-turn over a base run/idle.
121 void PlayAnimationAdditive(const char* animName, int32_t slot, BoneMaskAsset* mask,
122 bool loop, float speed = 1.0f, float weight = 1.0f);
123 // Replace the bone mask on an already-playing slot. Pass nullptr to clear.
124 void SetSlotMask(int32_t slot, BoneMaskAsset* mask);
125 void SetSlotLayerMode(int32_t slot, AnimLayerMode mode);
126 // Snap a slot's weight without easing. Clamped to [0, 1].
127 void SetSlotWeight(int32_t slot, float weight);
128 // Fade a slot's weight from its current value to `targetWeight` over
129 // `seconds` (linear). If targetWeight == 0 and the fade completes, the
130 // slot self-removes.
131 void FadeSlotWeight(int32_t slot, float targetWeight, float seconds);
132 void QueueAnimation(const char* animName, bool loop, const char* targetAnim = nullptr, float speed = 1.0f, float weight = 1.0f, int32_t priority = -1);
133 void StopAnimation(const char* animName, bool cancelQueued = false);
134 void StopAllAnimations(bool cancelQueued = false);
135 void CancelQueuedAnimation(const char* animName);
136 void CancelAllQueuedAnimations();
137 bool IsAnimationPlaying(const char* animName);
138 void ResetAnimation();
139 float GetAnimationSpeed() const;
140 void SetAnimationSpeed(float speed);
141
142 void SetAnimationPaused(bool paused);
143 bool IsAnimationPaused() const;
144
145 void SetInheritPose(bool inherit);
146 bool IsInheritPoseEnabled() const;
147
148 void SetBoundsRadiusOverride(float radius);
149 float GetBoundsRadiusOverride() const;
150
151 bool HasAnimatedThisFrame() const;
152
153 ActiveAnimation* FindActiveAnimation(const char* animName);
154 std::vector<ActiveAnimation>& GetActiveAnimations();
155
156 QueuedAnimation* FindQueuedAnimation(const char* animName, const char* dependName = nullptr);
157 std::vector<QueuedAnimation>& GetQueuedAnimations();
158
159 glm::mat4 GetBoneTransform(const std::string& name) const;
160 glm::vec3 GetBonePosition(const std::string& name) const;
161 glm::quat GetBoneRotationQuat(const std::string& name) const;
162 glm::vec3 GetBoneRotationEuler(const std::string& name) const;
163 glm::vec3 GetBoneScale(const std::string& name) const;
164
165 glm::mat4 GetBoneTransform(int32_t index) const;
166 glm::vec3 GetBonePosition(int32_t boneIndex) const;
167 glm::quat GetBoneRotationQuat(int32_t boneIndex) const;
168 glm::vec3 GetBoneRotationEuler(int32_t boneIndex) const;
169 glm::vec3 GetBoneScale(int32_t boneIndex) const;
170
171 void SetBoneTransform(int32_t boneIndex, const glm::mat4& transform);
172 void SetBonePosition(int32_t boneIndex, glm::vec3 position);
173 void SetBoneRotation(int32_t boneIndex, glm::vec3 rotation);
174 void SetBoneScale(int32_t boneIndex, glm::vec2 scale);
175
176 uint32_t GetNumBones() const;
177 BoneInfluenceMode GetBoneInfluenceMode() const;
178
179 AnimationUpdateMode GetAnimationUpdateMode() const;
180 void SetAnimationUpdateMode(AnimationUpdateMode mode);
181
182 Vertex* GetSkinnedVertices();
183 uint32_t GetNumSkinnedVertices();
184
185 virtual Material* GetMaterial() override;
186 virtual void Render() override;
187
188 // Per-section material slot accessors. A "slot" maps 1:1 onto
189 // SkeletalMesh::GetSection(i). Resolution order for a slot:
190 // 1. component override (mSectionMaterialOverrides[slot])
191 // 2. section's own material (SkeletalMesh::GetSection(slot).mMaterial)
192 // 3. legacy mMaterialOverride (whole-mesh override)
193 // 4. legacy mesh-default mMaterial
194 // 5. renderer default material
195 uint32_t GetNumMaterialSlots() const;
196 Material* GetMaterialSlot(uint32_t slot) const;
197 void SetMaterialSlot(uint32_t slot, Material* material);
198 int32_t FindMaterialSlot(const std::string& sectionName) const;
199
200 void UpdateAnimation(float deltaTime, bool updateBones);
201
202 virtual Bounds GetLocalBounds() const override;
203
204 int32_t FindBoneIndex(const std::string& name) const;
205
206 void SetAnimEventHandler(AnimEventHandlerFP handler);
207 AnimEventHandlerFP GetAnimEventHandler();
208 void SetScriptAnimEventHandler(const ScriptFunc& func);
209
210protected:
211
212 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
213
214 void TickCommon(float deltaTime);
215
216 glm::vec3 InterpolateScale(float time, const Channel& channel);
217 glm::quat InterpolateRotation(float time, const Channel& channel);
218 glm::vec3 InterpolatePosition(float time, const Channel& channel);
219 void DetectTriggeredAnimEvents(
220 const Animation& animation,
221 float prevTickTime,
222 float tickTime,
223 float animationSpeed,
224 std::vector<AnimEvent>& outEvents);
225
226 uint32_t FindScaleIndex(float time, const Channel& channel);
227 uint32_t FindRotationIndex(float time, const Channel& channel);
228 uint32_t FindPositionIndex(float time, const Channel& channel);
229
230 void UpdateAttachedChildren(float deltaTime);
231 void CpuSkinVertices();
232
233 // External SkeletalAnimationAsset references. Resolution order in
234 // FindAnimation: embedded mesh animations first (preserves existing
235 // PlayAnimation behaviour), then animation-lookup-mesh chain, then
236 // bound external assets. Channels in external assets are keyed by bone
237 // NAME and get resolved into target-mesh bone indices lazily into the
238 // mBoundExternalAnims cache below.
239 const std::vector<SkeletalAnimationRef>& GetAnimationAssets() const { return mAnimationAssets; }
240 std::vector<SkeletalAnimationRef>& GetAnimationAssetsMutable();
241 void AddAnimationAsset(class SkeletalAnimationAsset* asset);
242 void RemoveAnimationAsset(class SkeletalAnimationAsset* asset);
243
244 const Animation* FindAnimation(const char* animName);
245 void InvalidateAnimationBindings();
246
248 std::vector<glm::mat4> mBoneMatrices;
249 std::vector<Vertex> mSkinnedVertices; // Used by CPU skinning only.
250 std::vector<MaterialRef> mSectionMaterialOverrides;
251 std::vector<SkeletalAnimationRef> mAnimationAssets;
252
253 // Lazy cache of external animation clips with channel bone indices
254 // resolved against the currently assigned target mesh. Rebuilt on
255 // first FindAnimation after the mesh or asset list changes.
256 std::vector<Animation> mBoundExternalAnims;
257 bool mAnimBindingsValid = false;
258
260 std::string mDefaultAnimation;
261 float mAnimationSpeed = 1.0f;
262 std::vector<ActiveAnimation> mActiveAnimations;
263 std::vector<QueuedAnimation> mQueuedAnimations;
264 float mBoundsRadiusOverride = 0.0f;
269
272
273 // Graphics Resource
275};
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
AnimationUpdateMode
Definition SkeletalMesh3d.h:21
void(* AnimEventHandlerFP)(const AnimEvent &animEvent)
Definition SkeletalMesh3d.h:82
BoneInfluenceMode
Definition SkeletalMesh3d.h:14
AnimLayerMode
Definition SkeletalMesh3d.h:36
Definition AssetRef.h:18
Definition BoneMaskAsset.h:19
Definition Datum.h:169
Definition Material.h:48
Definition Mesh3d.h:13
static bool HandlePropChange(Datum *datum, uint32_t index, const void *newValue)
Definition Mesh3d.cpp:12
virtual bool IsSkeletalMesh3D() const
Definition Mesh3d.cpp:132
virtual const char * GetTypeName() const override
Definition Mesh3d.cpp:39
virtual Material * GetMaterial()=0
virtual bool IsStaticMesh3D() const
Definition Mesh3d.cpp:127
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Mesh3d.cpp:44
void TickCommon(float deltaTime)
Definition Node.cpp:588
virtual void EditorTick(float deltaTime)
Definition Node.cpp:578
virtual void Destroy() override
Definition Primitive3d.cpp:129
virtual Bounds GetLocalBounds() const
Definition Primitive3d.cpp:829
virtual void Tick(float deltaTime) override
Definition Primitive3d.cpp:163
virtual void Render() override
Definition Primitive3d.cpp:259
virtual void Create() override
Definition Primitive3d.cpp:117
Definition ScriptFunc.h:10
Definition SkeletalAnimationAsset.h:26
Definition SkeletalMesh3d.h:85
std::vector< ActiveAnimation > mActiveAnimations
Definition SkeletalMesh3d.h:262
bool mInheritPose
Definition SkeletalMesh3d.h:267
std::vector< Vertex > mSkinnedVertices
Definition SkeletalMesh3d.h:249
ScriptableFP< AnimEventHandlerFP > mAnimEventHandler
Definition SkeletalMesh3d.h:259
std::vector< glm::mat4 > mBoneMatrices
Definition SkeletalMesh3d.h:248
BoneInfluenceMode mBoneInfluenceMode
Definition SkeletalMesh3d.h:270
std::vector< Animation > mBoundExternalAnims
Definition SkeletalMesh3d.h:256
std::string mDefaultAnimation
Definition SkeletalMesh3d.h:260
bool mHasAnimatedThisFrame
Definition SkeletalMesh3d.h:268
bool mRevertToBindPose
Definition SkeletalMesh3d.h:266
std::vector< QueuedAnimation > mQueuedAnimations
Definition SkeletalMesh3d.h:263
SkeletalMeshCompResource mResource
Definition SkeletalMesh3d.h:274
std::vector< SkeletalAnimationRef > mAnimationAssets
Definition SkeletalMesh3d.h:251
AnimationUpdateMode mAnimationUpdateMode
Definition SkeletalMesh3d.h:271
SkeletalMeshRef mSkeletalMesh
Definition SkeletalMesh3d.h:247
std::vector< MaterialRef > mSectionMaterialOverrides
Definition SkeletalMesh3d.h:250
const std::vector< SkeletalAnimationRef > & GetAnimationAssets() const
Definition SkeletalMesh3d.h:239
DECLARE_NODE(SkeletalMesh3D, Mesh3D)
bool mAnimationPaused
Definition SkeletalMesh3d.h:265
Definition SkeletalMesh.h:99
Definition SkeletalMesh3d.h:49
AssetRef mBoneMask
Definition SkeletalMesh3d.h:60
bool mLoop
Definition SkeletalMesh3d.h:55
float mWeightFadeRate
Definition SkeletalMesh3d.h:66
float mSpeed
Definition SkeletalMesh3d.h:52
int32_t mSlot
Definition SkeletalMesh3d.h:54
std::string mName
Definition SkeletalMesh3d.h:50
float mWeightTarget
Definition SkeletalMesh3d.h:65
float mTime
Definition SkeletalMesh3d.h:51
float mWeight
Definition SkeletalMesh3d.h:53
AnimLayerMode mLayerMode
Definition SkeletalMesh3d.h:68
Definition SkeletalMesh.h:60
Definition SkeletalMesh.h:77
Definition EngineTypes.h:200
Definition SkeletalMesh.h:69
Definition SkeletalMesh3d.h:72
bool mLoop
Definition SkeletalMesh3d.h:78
float mTime
Definition SkeletalMesh3d.h:75
std::string mName
Definition SkeletalMesh3d.h:73
std::string mDependentAnim
Definition SkeletalMesh3d.h:74
float mWeight
Definition SkeletalMesh3d.h:77
float mSpeed
Definition SkeletalMesh3d.h:76
int32_t mSlot
Definition SkeletalMesh3d.h:79
Definition ScriptFunc.h:43
Definition GraphicsTypes.h:238
Definition Vertex.h:20