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 AABB GetAABB() const;
181
182 const glm::mat4 GetBindPoseMatrix(int32_t boneIndex) const;
183
184 SkeletalMesh* GetAnimationLookupMesh();
185 void SetAnimationLookupMesh(SkeletalMesh* lookupMesh);
186
187 static bool HandlePropChange(Datum* datum, uint32_t index, const void* newValue);
188
189private:
190
191 void InitBindPose();
192 void ComputeBounds();
193
194 MaterialRef mMaterial;
195 SkeletalMeshRef mAnimationLookupMesh;
196 uint32_t mNumVertices;
197 uint32_t mNumIndices;
198 uint32_t mNumUvMaps;
199
200 std::vector<Bone> mBones;
201 std::vector<Animation> mAnimations;
202 std::vector<VertexSkinned> mVertices;
203 std::vector<SkeletalMeshSection> mSections;
204
205 glm::mat4 mInvRootTransform;
206 std::vector<glm::mat4> mBindPoseMatrices;
207 // Decomposed local-bind TRS, parallel to mBindPoseMatrices. Populated by
208 // InitBindPose. Zero-sized while bones haven't been initialised.
209 std::vector<glm::vec3> mBindPoseDecompPos;
210 std::vector<glm::quat> mBindPoseDecompRot;
211 std::vector<glm::vec3> mBindPoseDecompScale;
212 std::vector<IndexType> mIndices;
213
214 Bounds mBounds;
215
216 // Bind-pose AABB derived from vertex positions in ComputeBounds().
217 // Intentionally NOT serialized -- Create() recomputes bounds on every load.
218 AABB mAABB;
219
220 float mBoundsScale = 1.1f;
221
222 // Graphics Resource
223 SkeletalMeshResource mResource;
224
225#if EDITOR
226public:
227 void Create(const aiScene& scene,
228 const aiMesh& meshData,
229 std::vector<Material>* materials = nullptr);
230
231 void SetupBoneHierarchy(
232 const aiNode& node,
233 const aiMesh& meshData,
234 std::vector<uint8_t>& boneIndices,
235 std::vector<float>& boneWeights,
236 int32_t parentBoneIndex);
237
238 void SetupAnimations(const aiScene& scene);
239 void SetupResource(const aiMesh& meshData,
240 const std::vector<float>& boneWeights,
241 const std::vector<uint8_t>& boneIndices);
242
243 void CreateCombined(const aiScene& scene,
244 const std::vector<const aiMesh*>& renderMeshes);
245#endif // EDITOR
246};
Platform
Definition EngineTypes.h:32
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:120
virtual bool Import(const std::string &path, ImportOptions *options=nullptr)
Definition Asset.cpp:342
virtual void Create()
Definition Asset.cpp:78
virtual void SaveStream(Stream &stream, Platform platform)
Definition Asset.cpp:334
virtual glm::vec4 GetTypeColor()
Definition Asset.cpp:352
virtual const char * GetTypeImportExt()
Definition Asset.cpp:362
virtual const char * GetTypeName()
Definition Asset.cpp:357
virtual void GatherProperties(std::vector< Property > &outProps) override
Definition Asset.cpp:347
virtual void LoadStream(Stream &stream, Platform platform)
Definition Asset.cpp:320
virtual void Destroy()
Definition Asset.cpp:93
Definition Datum.h:169
Definition Asset.h:109
Definition Material.h:48
Definition SkeletalMesh3d.h:116
Definition SkeletalMesh.h:99
DECLARE_ASSET(SkeletalMesh, Asset)
Definition Stream.h:21
Definition EngineTypes.h:210
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:201
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