Polyphase Game Engine
Loading...
Searching...
No Matches
SkeletalMesh.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "Assets/Material.h"
6#include "Asset.h"
7#include "Vertex.h"
8#include "Constants.h"
9
10#include "Graphics/Graphics.h"
13
14#include "Maths.h"
15
16#if EDITOR
17#include <assimp/scene.h>
18#endif
19
20struct Bone
21{
22 std::string mName;
23 int32_t mIndex = -1;
24 int32_t mParentIndex = -1;
25 glm::mat4 mOffsetMatrix = { };
26 glm::mat4 mInvOffsetMatrix = { };
27};
28
30{
31 float mTime = 0.0f;
32 glm::vec3 mValue = {};
33};
34
36{
37 float mTime = 0.0f;
38 glm::quat mValue = {};
39};
40
42{
43 float mTime = 0.0f;
44 glm::vec3 mValue = {};
45};
46
48{
49 float mTime = 0.0f;
50 glm::vec3 mValue = {};
51};
52
54{
55 std::string mName;
56 std::vector<AnimEventKey> mEventKeys;
57};
58
60{
62 std::string mName;
63 std::string mAnimation;
64 float mTime = 0.0f;
65 glm::vec3 mValue = {};
66};
67
68struct Channel
69{
70 int32_t mBoneIndex = -1;
71 std::vector<PositionKey> mPositionKeys;
72 std::vector<RotationKey> mRotationKeys;
73 std::vector<ScaleKey> mScaleKeys;
74};
75
77{
78 std::string mName;
79 float mDuration = 0.0f;
80 float mTicksPerSecond = 0.0f;
81 std::vector<Channel> mChannels;
82 std::vector<AnimEventTrack> mEventTracks;
83};
84
85// Contiguous range inside the SkeletalMesh's shared vertex/index buffers
86// that should be drawn with its own material. One section per source aiMesh
87// when the mesh was imported with "combineMeshes".
89{
90 std::string mName;
91 uint32_t mFirstIndex = 0;
92 uint32_t mIndexCount = 0;
93 uint32_t mBaseVertex = 0;
94 uint32_t mVertexCount = 0;
96};
97
99{
100public:
101
103
104 SkeletalMesh();
106
107 SkeletalMeshResource* GetResource();
108
109 // Asset Interface
110 virtual void LoadStream(Stream& stream, Platform platform) override;
111 virtual void SaveStream(Stream& stream, Platform platform) override;
112 virtual void Create() override;
113 virtual void Destroy() override;
114 virtual bool Import(const std::string& path, ImportOptions* options) override;
115 virtual void GatherProperties(std::vector<Property>& outProps) override;
116 virtual glm::vec4 GetTypeColor() override;
117 virtual const char* GetTypeName() override;
118 virtual const char* GetTypeImportExt() override;
119
120 class Material* GetMaterial();
121 void SetMaterial(class Material* newMaterial);
122
123 // Multi-section accessors. A legacy single-material mesh appears as one
124 // implicit section named "Default" covering the entire index range.
125 uint32_t GetNumSections() const;
126 const SkeletalMeshSection& GetSection(uint32_t index) const;
127 SkeletalMeshSection& GetSectionMutable(uint32_t index);
128 class Material* GetSectionMaterial(uint32_t index) const;
129 void SetSectionMaterial(uint32_t index, class Material* material);
130 int32_t FindSectionIndex(const std::string& name) const;
131 const std::vector<SkeletalMeshSection>& GetSections() const;
132
133 uint32_t GetNumIndices();
134 uint32_t GetNumFaces();
135 uint32_t GetNumVertices();
136
137 const IndexType* GetIndices() const;
138
139 int32_t FindBoneIndex(const std::string& name) const;
140 const std::vector<Bone>& GetBones() const;
141 const Bone& GetBone(int32_t index) const;
142 uint32_t GetNumBones() const;
143
144 glm::mat4 GetInvRootTransform() const;
145
146 const std::vector<Animation>& GetAnimations() const;
147 const Animation* GetAnimation(const char* name);
148
149 // Get length of animation in seconds
150 float GetAnimationDuration(const char* name);
151
152 const std::vector<VertexSkinned>& GetVertices() const;
153
154 void FinalizeBoneTransforms(std::vector<glm::mat4>& inoutTransforms);
155
156 void CopyBindPose(std::vector<glm::mat4>& outTransforms);
157
158 // Decomposed local-bind TRS, cached in InitBindPose. Used by
159 // SkeletalMesh3D's blend loop to seed bones from bind pose before
160 // accumulating animation layers, so masked-out bones in lower slots
161 // and unmasked bones in upper slots both blend against a known pose.
162 const glm::vec3& GetBindPosePos(int32_t boneIndex) const;
163 const glm::quat& GetBindPoseRot(int32_t boneIndex) const;
164 const glm::vec3& GetBindPoseScale(int32_t boneIndex) const;
165
166 // Mark every bone in [rootIndex, descendants...] in outBitset (size = numBones).
167 // Exploits the parent-before-child DFS order produced by SetupBoneHierarchy.
168 void GatherDescendants(int32_t rootIndex, std::vector<uint8_t>& outBitset, bool includeRoot = true) const;
169
170 // Resolve an include/exclude subtree spec into a per-bone bitset. Unknown
171 // bone names are skipped (caller logs). selfOnly = include only the named
172 // bones without recursing.
173 void GatherSubtreeBoneSet(
174 const std::vector<std::string>& includes,
175 const std::vector<std::string>& excludes,
176 bool selfOnly,
177 std::vector<uint8_t>& outBitset) const;
178
179 Bounds GetBounds() const;
180
181 const glm::mat4 GetBindPoseMatrix(int32_t boneIndex) const;
182
183 SkeletalMesh* GetAnimationLookupMesh();
184 void SetAnimationLookupMesh(SkeletalMesh* lookupMesh);
185
186 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
187
188private:
189
190 void InitBindPose();
191 void ComputeBounds();
192
193 MaterialRef mMaterial;
194 SkeletalMeshRef mAnimationLookupMesh;
195 uint32_t mNumVertices;
196 uint32_t mNumIndices;
197 uint32_t mNumUvMaps;
198
199 std::vector<Bone> mBones;
200 std::vector<Animation> mAnimations;
201 std::vector<VertexSkinned> mVertices;
202 std::vector<SkeletalMeshSection> mSections;
203
204 glm::mat4 mInvRootTransform;
205 std::vector<glm::mat4> mBindPoseMatrices;
206 // Decomposed local-bind TRS, parallel to mBindPoseMatrices. Populated by
207 // InitBindPose. Zero-sized while bones haven't been initialised.
208 std::vector<glm::vec3> mBindPoseDecompPos;
209 std::vector<glm::quat> mBindPoseDecompRot;
210 std::vector<glm::vec3> mBindPoseDecompScale;
211 std::vector<IndexType> mIndices;
212
213 Bounds mBounds;
214 float mBoundsScale = 1.1f;
215
216 // Graphics Resource
217 SkeletalMeshResource mResource;
218
219#if EDITOR
220public:
221 void Create(const aiScene& scene,
222 const aiMesh& meshData,
223 std::vector<Material>* materials = nullptr);
224
225 void SetupBoneHierarchy(
226 const aiNode& node,
227 const aiMesh& meshData,
228 std::vector<uint8_t>& boneIndices,
229 std::vector<float>& boneWeights,
230 int32_t parentBoneIndex);
231
232 void SetupAnimations(const aiScene& scene);
233 void SetupResource(const aiMesh& meshData,
234 const std::vector<float>& boneWeights,
235 const std::vector<uint8_t>& boneIndices);
236
237 void CreateCombined(const aiScene& scene,
238 const std::vector<const aiMesh*>& renderMeshes);
239#endif // EDITOR
240};
Platform
Definition EngineTypes.h:31
uint16_t IndexType
Definition GraphicsTypes.h:122
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Bounds ComputeBounds(const std::vector< T > &vertices)
Definition Utilities.h:153
Definition AssetRef.h:18
Definition Asset.h:119
virtual bool Import(const std::string &path, ImportOptions *options=nullptr)
Definition Asset.cpp:292
virtual void Create()
Definition Asset.cpp:77
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:284
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:302
virtual const char * GetTypeImportExt()
Definition Asset.cpp:312
virtual const char * GetTypeName()
Definition Asset.cpp:307
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:297
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:270
virtual void Destroy()
Definition Asset.cpp:92
Definition Datum.h:169
Definition Asset.h:108
Definition Material.h:48
Definition SkeletalMesh3d.h:85
Definition SkeletalMesh.h:99
DECLARE_ASSET(SkeletalMesh, Asset)
Definition Stream.h:21
Definition SkeletalMesh.h:48
float mTime
Definition SkeletalMesh.h:49
glm::vec3 mValue
Definition SkeletalMesh.h:50
Definition SkeletalMesh.h:54
std::string mName
Definition SkeletalMesh.h:55
std::vector< AnimEventKey > mEventKeys
Definition SkeletalMesh.h:56
Definition SkeletalMesh.h:60
SkeletalMesh3D * mNode
Definition SkeletalMesh.h:61
float mTime
Definition SkeletalMesh.h:64
std::string mAnimation
Definition SkeletalMesh.h:63
glm::vec3 mValue
Definition SkeletalMesh.h:65
std::string mName
Definition SkeletalMesh.h:62
Definition SkeletalMesh.h:77
std::string mName
Definition SkeletalMesh.h:78
std::vector< Channel > mChannels
Definition SkeletalMesh.h:81
float mTicksPerSecond
Definition SkeletalMesh.h:80
std::vector< AnimEventTrack > mEventTracks
Definition SkeletalMesh.h:82
float mDuration
Definition SkeletalMesh.h:79
Definition SkeletalMesh.h:21
glm::mat4 mInvOffsetMatrix
Definition SkeletalMesh.h:26
glm::mat4 mOffsetMatrix
Definition SkeletalMesh.h:25
std::string mName
Definition SkeletalMesh.h:22
int32_t mIndex
Definition SkeletalMesh.h:23
int32_t mParentIndex
Definition SkeletalMesh.h:24
Definition EngineTypes.h:200
Definition SkeletalMesh.h:69
std::vector< ScaleKey > mScaleKeys
Definition SkeletalMesh.h:73
int32_t mBoneIndex
Definition SkeletalMesh.h:70
std::vector< PositionKey > mPositionKeys
Definition SkeletalMesh.h:71
std::vector< RotationKey > mRotationKeys
Definition SkeletalMesh.h:72
Definition SkeletalMesh.h:30
glm::vec3 mValue
Definition SkeletalMesh.h:32
float mTime
Definition SkeletalMesh.h:31
Definition SkeletalMesh.h:36
glm::quat mValue
Definition SkeletalMesh.h:38
float mTime
Definition SkeletalMesh.h:37
Definition SkeletalMesh.h:42
float mTime
Definition SkeletalMesh.h:43
glm::vec3 mValue
Definition SkeletalMesh.h:44
Definition GraphicsTypes.h:198
Definition SkeletalMesh.h:89
std::string mName
Definition SkeletalMesh.h:90
MaterialRef mMaterial
Definition SkeletalMesh.h:95
uint32_t mBaseVertex
Definition SkeletalMesh.h:93
uint32_t mVertexCount
Definition SkeletalMesh.h:94
uint32_t mIndexCount
Definition SkeletalMesh.h:92
uint32_t mFirstIndex
Definition SkeletalMesh.h:91