dl
dl_model_data.h
浏览该文件的文档.
1
12#pragma once
13
14#include <vector>
15#include <limits>
16
19#include "math/dl_matrix.h"
20
21namespace dl
22{
23 // 模型节点变换
25 {
31 _t{},
32 _r{ 0,0,0,1 },
33 _s{ 1,1,1 }
34 {
36 }
37
39 {
43 return mat_t * mat_r * mat_s * _mat;
44 }
45 };
46 struct ModelMesh;
47 struct ModelSkin;
48 // 模型节点
49 struct ModelNode
50 {
51 int _index;
53 ModelNode* _parent; // 父节点
54 ModelMesh* _mesh; // 网格
55 ModelSkin* _skin; // 蒙皮
56 std::vector<ModelNode*> _allChild; // 子节点
57
58 struct Runtime
59 {
60 ModelNodeTransform _trans2Parent; // 到父节点的变换
65
66 // 返回局部矩阵(到父节点)
68
69 // 返回世界矩阵
71 };
73
74 void Update();
75
77 {
78 return _rt.GetMatrixWorld(this);
79 }
80 };
81
82 struct ModelSkin
83 {
84 std::string name;
86 std::vector<Matrix4> _allOffsetMatrix; // 偏移矩阵(世界坐标系->节点)
87 std::vector<ModelNode*> _allJoint;
88 };
89 //
99 {
100 uint32_t firstIndex;
101 uint32_t indexCount;
102 uint32_t vertexCount;
103 uint32_t material;
106 };
107
108 // 模型网格
110 {
111 struct Runtime
112 {
114 std::vector<Matrix4> _allMatJoint;
115 };
117 std::vector<ModelPrimitive*> primitives;
119 };
120
121
122 // 动画关键帧(不变量)
124 {
126 _t(-1.0f),//负数代表无效
127 _transform{}
128 {}
129
132 };
133 // 多个节点关键帧组成的一个动画通道
134 //struct ModelAnimationChannel
135 //{
136 // // 插值类型
137 // enum class PathType
138 // {
139 // TRANSLATION,
140 // ROATATION,
141 // SCALING,
142 // };
143
144 // // 关键帧插值
145 // void Interpolate(FloatTime t, Joint& joint)
146 // {
147 // // 没有关键帧直接返回(保持不变)
148 // if (_allKeyFrame.empty())
149 // return;
150
151 // ModelAnimationKeyFrame& kf_front = _allKeyFrame.front();
152 // ModelAnimationKeyFrame& kf_back = _allKeyFrame.back();
153 // int idx = -1;
154 // if (t < kf_front._t)
155 // {
156 // ModelAnimationKeyFrame& kf = kf_front;
157 // if (_type == PathType::TRANSLATION)
158 // joint._t = *(Position3*)&kf._transform;
159 // else if (_type == PathType::ROATATION)
160 // joint._r = kf._transform;
161 // else if (_type == PathType::SCALING)
162 // joint._s = *(Position3*)&kf._transform;
163 // return;
164 // }
165 // else if (t > kf_back._t)
166 // {
167 // ModelAnimationKeyFrame& kf = kf_back;
168 // if (_type == PathType::TRANSLATION)
169 // joint._t = *(Position3*)&kf._transform;
170 // else if (_type == PathType::ROATATION)
171 // joint._r = kf._transform;
172 // else if (_type == PathType::SCALING)
173 // joint._s = *(Position3*)&kf._transform;
174 // return;
175 // }
176 // else
177 // {
178 // for (size_t i = 0; i < _allKeyFrame.size() - 1; ++i)
179 // {
180 // // [0, n-1)
181 // ModelAnimationKeyFrame& kf_0 = _allKeyFrame[i];
182 // ModelAnimationKeyFrame& kf_1 = _allKeyFrame[i + 1];
183 // if (t >= kf_0._t && t <= kf_1._t)
184 // {
185 // idx = i;
186 // break;
187 // }
188 // }
189 // }
190 // if (idx == -1)
191 // return;
192
193 // ModelAnimationKeyFrame& kf_0 = _allKeyFrame[idx];
194 // ModelAnimationKeyFrame& kf_1 = _allKeyFrame[idx + 1];
195 // FloatTime percent = (t - kf_0._t) / (kf_1._t - kf_0._t);
196
197 // if (_type == PathType::TRANSLATION)
198 // {
199 // joint._t = Each::Lerp(
200 // *(Position3*)&kf_0._transform,
201 // *(Position3*)&kf_1._transform, percent);
202 // }
203 // else if (_type == PathType::ROATATION)
204 // {
205 // joint._r = Math::Slerp(kf_0._transform, kf_1._transform, percent);
206 // }
207 // else if (_type == PathType::SCALING)
208 // {
209 // joint._s = Each::Lerp(
210 // *(Position3*)&kf_0._transform,
211 // *(Position3*)&kf_1._transform, percent);
212 // }
213 // else
214 // {
215 // assert(0);
216 // }
217 // }
218 // PathType _type;
219 // int _boneId;
220 // std::vector<ModelAnimationKeyFrame> _allKeyFrame;
221 //};
223 //struct ModelAnimation
224 //{
225 // FloatTime _t0;
226 // FloatTime _t1;
227 // std::vector<ModelAnimationChannel> _allModelAnimationChannel;
228 //public:
229 // FloatTime GetClipEndTime()
230 // {
231 // return _t1;
232 // }
233 // //插值
234 // void InterpolateAll(FloatTime t, std::vector<Joint>& all_joint)
235 // {
236 // // 一个通道只会平移、旋转、缩放其中一种
237 // // 多个通道影响同一个bone,需要相矩阵乘起来
238 // for (size_t i = 0; i < _allModelAnimationChannel.size(); ++i)
239 // {
240 // ModelAnimationChannel& channel = _allModelAnimationChannel[i];
241 // if (channel._boneId != -1)
242 // channel.Interpolate(t, all_joint[channel._boneId]);
243 // }
244 // }
245 //};
246
247 // 动画通道
255 // 动画采样
257 {
260 std::vector<float> inputs;
261 std::vector<Position4> outputsVec4;
262 std::vector<float> outputs;
263 Position4 cubicSplineInterpolation(size_t index, float time, uint32_t stride);
264 void translate(size_t index, float time, ModelNode* node);
265 void scale(size_t index, float time, ModelNode* node);
266 void rotate(size_t index, float time, ModelNode* node);
267 };
268
270 {
271 std::string _name;
272 std::vector<ModelAnimationSampler> _allSampler;
273 std::vector<ModelAnimationChannel> _allChannel;
274 float start;// = std::numeric_limits<float>::max();
275 float end;// = std::numeric_limits<float>::min();
276 };
277
278 // 模型数据
280 {
281 std::vector<ModelNode*> _allNodeChild; // 子节点
282 std::vector<ModelNode*> _allNode; // 所有节点
283 std::vector<ModelAnimation> _allAnimation; // 所有动画
284 std::vector<ModelSkin*> _allSkin; // 所有蒙皮
285
286 struct Runtime
287 {
288 float _t;
289 size_t _curAni;
290 };
292
293 void Update();
294 };
295
296
297
298
299
300
302//class BoneData
303//{
304// friend class ModelLoader;
305// friend class FactoryImp;
306// friend class ModelAnimationInstance;
307// friend class Actor;
308//public:
309// FloatTime GetClipEndTime(std::string_view cilp_name)
310// {
311// auto clip = _allAnimation.find(std::string{ cilp_name });
312// return clip->second.GetClipEndTime();
313// }
314//
315// // 或许可以缓存常用时刻的变换
316// void GetFinalTrans(std::string_view clip_name, FloatTime t,
317// std::vector<Matrix4>& final_trans)
318// {
319// // 骨骼数量
320// size_t num_bone = _allBoneOffset.size();
321//
322// // 变换到父骨骼的矩阵
323// std::vector<Matrix4> trans_to_parent;
324// trans_to_parent.resize(num_bone);
325//
326// std::vector<Joint> all_joint = _allBoneJoint;
327//
328// // 根据动画进行插值
329// auto iter = _allAnimation.find(std::string{ clip_name });
330// ModelAnimation& clip = iter->second;
331// clip.InterpolateAll(t, all_joint);
332//
333// //
334// for (size_t i = 0; i < all_joint.size(); ++i)
335// {
336// Joint& joint = all_joint[i];
337// Matrix4 mat_t = Mat::CreateTranslation(joint._t);
338// Matrix4 mat_r = Mat::CreateRotation(joint._r);
339// Matrix4 mat_s = Mat::CreateScaling(joint._s);
340// trans_to_parent[i] = mat_t * mat_r * mat_s * joint._mat;
341// }
342//
343// // 计算trans_to_root
344// std::vector<Matrix4> trans_to_root;
345// trans_to_root.resize(num_bone);
346//
347// trans_to_root[0] = Mat::CreateIdentity();// trans_to_parent[0];
348// for (size_t i = 1; i < num_bone; ++i)
349// {
350// // 自己到父
351// Matrix4& mat_to_parent = trans_to_parent[i];
352// // 父骨骼 到 根 的矩阵
353// int index_parent = _allBoneParent[i];
354// Matrix4& mat_parent_to_root = trans_to_root[index_parent];
355// // 自己到根的矩阵 = 自己到父,再父到根
356// trans_to_root[i] = mat_parent_to_root * mat_to_parent;
357// }
358//
359// // 计算最终矩阵
360// for (size_t i = 0; i < num_bone; ++i)
361// {
362// Matrix4& mat_offset = _allBoneOffset[i];
363// Matrix4& mat_to_root = trans_to_root[i];
364//
365// // 顶点本身处于模型坐标系
366// // 模型坐标系 -> 对应骨骼坐标系
367// // 骨骼坐标系 -> 根坐标系(动画插值计算)
368// final_trans[i] = _root2Model * mat_to_root * mat_offset;
369// }
370// //_model2root *
371// //_root2Model *
372// }
373//
374// void Save(StreamOutput& s)
375// {
376// s.Write(&_allBoneParent);
377// s.Write(&_allBoneJoint);
378// s.Write(&_allBoneOffset);
379// s.Write(&_root2Model);
380// s.Write(&_model2root);
381//
382// size_t size = _allAnimation.size();
383// s.Write(&size);
384// for (auto& iter : _allAnimation)
385// {
386// const std::string& name = iter.first;
387// const ModelAnimation& clip = iter.second;
388// s.Write(&name);
389// s.Write(&clip._t0);
390// s.Write(&clip._t1);
391// size_t size1 = clip._allModelAnimationChannel.size();
392// s.Write(&size1);
393// for (size_t i = 0; i < size1; ++i)
394// {
395// const ModelAnimationChannel& kf_mul = clip._allModelAnimationChannel[i];
396// s.Write(&kf_mul._type);
397// s.Write(&kf_mul._boneId);
398// size_t size2 = kf_mul._allKeyFrame.size();
399// s.Write(&size2);
400// for (size_t j = 0; j < size2; ++j)
401// {
402// const ModelAnimationKeyFrame& kf = kf_mul._allKeyFrame[j];
403// s.Write(&kf);
404// }
405// }
406// }
407// }
408// void Load(StreamInput& s)
409// {
410// s.Read(&_allBoneParent);
411// s.Read(&_allBoneJoint);
412// s.Read(&_allBoneOffset);
413// s.Read(&_root2Model);
414// s.Read(&_model2root);
415//
416// size_t size;
417// s.Read(&size);
418// for (size_t k = 0; k < size; ++k)
419// {
420// std::string name;
421// s.Read(&name);
422// ModelAnimation& clip = _allAnimation[name];
423// s.Read(&clip._t0);
424// s.Read(&clip._t1);
425// size_t size1;
426// s.Read(&size1);
427// clip._allModelAnimationChannel.resize(size1);
428// for (size_t i = 0; i < size1; ++i)
429// {
430// ModelAnimationChannel& kf_mul = clip._allModelAnimationChannel[i];
431// s.Read(&kf_mul._type);
432// s.Read(&kf_mul._boneId);
433// size_t size2;
434// s.Read(&size2);
435// kf_mul._allKeyFrame.resize(size2);
436// for (size_t j = 0; j < size2; ++j)
437// {
438// s.Read(&kf_mul._allKeyFrame[j]);
439// }
440// }
441// }
442// }
443//private:
444// std::vector<int> _allBoneParent;// Bone的父Bone
445// std::vector<Joint> _allBoneJoint;// Bone到父Bone的变换
446// std::vector<Matrix4> _allBoneOffset;// Bone偏移矩阵
447// std::unordered_map<std::string, ModelAnimation> _allAnimation;// 动画片段
448// Matrix4 _root2Model;
449// Matrix4 _model2root;
450//};
451
454{
455public:
457 std::vector<Matrix4> _finalTrans;// 某时间点的最终变换(仅骨骼)
458 std::string _clipName;// 当前动画名称
459 FloatTime _t;// 时间点
460 size_t _id;// 骨骼实例下标(不是ub下标)
461
463 _skinndeData{ nullptr },
464 _t{},
465 _id{(size_t)-1}
466 {}
467
468 //t为-1代表 设为起始
469 //void Set(std::string_view name, FloatTime t)
470 //{
471 // auto iter = _skinndeData->_allAnimation.find(std::string{ name });
472 // if (iter == _skinndeData->_allAnimation.end())
473 // {
474 // log_err("指定动画不存在:{}", name);
475 // return;
476 // }
477 // _clipName = std::string{ name };
478 // ModelAnimation& clip = iter->second;
479 // if (t < 0)
480 // _t = clip._t0;
481 // else if (Math::IsOutRange(t, clip._t0, clip._t1))
482 // {
483 // log_err("设置动画时间越界:{},{}->{}", t, clip._t0, clip._t1);
484 // _t = clip._t0;
485 // }
486 // else
487 // _t = t;
488 //}
489
492 {
493 _skinndeData->_rt._t += dt;
494
495 _skinndeData->Update();
496
497 /*_t += dt;
498 if (_t > _skinndeData->GetClipEndTime(_clipName))
499 _t = 0;
500
501 _skinndeData->GetFinalTrans(_clipName, _t, _finalTrans);*/
502 }
503};
504
505}
std::vector< Matrix4 > _finalTrans
void Update(FloatTime dt)
每帧调用,推进时间点进行插值
矩阵(列存储)
Matrix4 CreateRotation(const Position3 &axis, Float angle)
创建旋转矩阵
Matrix4 CreateTranslation(const Position3 &t)
创建平移矩阵
Matrix4 CreateScaling(const Position3 &s)
创建缩放矩阵
Matrix4 CreateIdentity()
创建单位矩阵
float FloatTime
Matrix< Float, 4, 4 > Matrix4
Array< Float, 3 > Position3
Array< Float, 4 > Position4
BoundingBox getAABB(Matrix4 m)
BoundingBox(Position3 min, Position3 max)
动画,包含多个动画通道
std::vector< ModelAnimationSampler > _allSampler
std::vector< ModelAnimationChannel > _allChannel
void translate(size_t index, float time, ModelNode *node)
void rotate(size_t index, float time, ModelNode *node)
std::vector< Position4 > outputsVec4
Position4 cubicSplineInterpolation(size_t index, float time, uint32_t stride)
void scale(size_t index, float time, ModelNode *node)
std::vector< ModelNode * > _allNodeChild
std::vector< ModelNode * > _allNode
std::vector< ModelAnimation > _allAnimation
std::vector< ModelSkin * > _allSkin
std::vector< Matrix4 > _allMatJoint
std::vector< ModelPrimitive * > primitives
Matrix4 GetMatrixWorld(ModelNode *node)
ModelNodeTransform _trans2Parent
std::vector< ModelNode * > _allChild
ModelNode * skeletonRoot
std::vector< Matrix4 > _allOffsetMatrix
std::vector< ModelNode * > _allJoint