505 lines
12 KiB
C++
505 lines
12 KiB
C++
/**
|
||
* @file dl_model_bone.h
|
||
* @brief 模型之骨骼动画
|
||
*
|
||
*
|
||
* @version 1.0
|
||
* @author lveyou
|
||
* @date 22-08-17
|
||
*
|
||
* @info
|
||
*/
|
||
#pragma once
|
||
|
||
#include <vector>
|
||
#include <limits>
|
||
|
||
#include "memory/dl_stream_output.h"
|
||
#include "memory/dl_stream_input.h"
|
||
#include "math/dl_matrix.h"
|
||
|
||
namespace dl
|
||
{
|
||
// 模型节点变换
|
||
struct ModelNodeTransform
|
||
{
|
||
Position3 _t;
|
||
Position4 _r;
|
||
Position3 _s;
|
||
Matrix4 _mat;
|
||
ModelNodeTransform() :
|
||
_t{},
|
||
_r{ 0,0,0,1 },
|
||
_s{ 1,1,1 }
|
||
{
|
||
_mat = Mat::CreateIdentity();
|
||
}
|
||
|
||
Matrix4 GetMatrix()
|
||
{
|
||
Matrix4 mat_t = Mat::CreateTranslation(_t);
|
||
Matrix4 mat_r = Mat::CreateRotation(_r);
|
||
Matrix4 mat_s = Mat::CreateScaling(_s);
|
||
return mat_t * mat_r * mat_s * _mat;
|
||
}
|
||
};
|
||
struct ModelMesh;
|
||
struct ModelSkin;
|
||
// 模型节点
|
||
struct ModelNode
|
||
{
|
||
int _index;
|
||
int skinIndex;
|
||
ModelNode* _parent; // 父节点
|
||
ModelMesh* _mesh; // 网格
|
||
ModelSkin* _skin; // 蒙皮
|
||
std::vector<ModelNode*> _allChild; // 子节点
|
||
|
||
struct Runtime
|
||
{
|
||
ModelNodeTransform _trans2Parent; // 到父节点的变换
|
||
bool _bNeedUpdateMatrixWorld;
|
||
bool _bNeedUpdateMatrixLocal;
|
||
Matrix4 _matWorld;
|
||
Matrix4 _matLocal;
|
||
|
||
// 返回局部矩阵(到父节点)
|
||
Matrix4 GetMatrixLocal();
|
||
|
||
// 返回世界矩阵
|
||
Matrix4 GetMatrixWorld(ModelNode* node);
|
||
};
|
||
Runtime _rt;
|
||
|
||
void Update();
|
||
|
||
Matrix4 GetMatrixWorld()
|
||
{
|
||
return _rt.GetMatrixWorld(this);
|
||
}
|
||
};
|
||
|
||
struct ModelSkin
|
||
{
|
||
std::string name;
|
||
ModelNode* skeletonRoot = nullptr;
|
||
std::vector<Matrix4> _allOffsetMatrix; // 偏移矩阵(世界坐标系->节点)
|
||
std::vector<ModelNode*> _allJoint;
|
||
};
|
||
//
|
||
struct BoundingBox {
|
||
Position3 min;
|
||
Position3 max;
|
||
bool valid = false;
|
||
BoundingBox();
|
||
BoundingBox(Position3 min, Position3 max);
|
||
BoundingBox getAABB(Matrix4 m);
|
||
};
|
||
struct ModelPrimitive
|
||
{
|
||
uint32_t firstIndex;
|
||
uint32_t indexCount;
|
||
uint32_t vertexCount;
|
||
uint32_t material;
|
||
bool hasIndices;
|
||
BoundingBox bb;
|
||
};
|
||
|
||
// 模型网格
|
||
struct ModelMesh
|
||
{
|
||
struct Runtime
|
||
{
|
||
Matrix4 _mat;
|
||
std::vector<Matrix4> _allMatJoint;
|
||
};
|
||
Runtime _rt;
|
||
std::vector<ModelPrimitive*> primitives;
|
||
BoundingBox bb;
|
||
};
|
||
|
||
|
||
// 动画关键帧(不变量)
|
||
struct ModelAnimationKeyFrame
|
||
{
|
||
ModelAnimationKeyFrame() :
|
||
_t(-1.0f),//负数代表无效
|
||
_transform{}
|
||
{}
|
||
|
||
FloatTime _t;
|
||
Position4 _transform;
|
||
};
|
||
// 多个节点关键帧组成的一个动画通道
|
||
//struct ModelAnimationChannel
|
||
//{
|
||
// // 插值类型
|
||
// enum class PathType
|
||
// {
|
||
// TRANSLATION,
|
||
// ROATATION,
|
||
// SCALING,
|
||
// };
|
||
|
||
// // 关键帧插值
|
||
// void Interpolate(FloatTime t, Joint& joint)
|
||
// {
|
||
// // 没有关键帧直接返回(保持不变)
|
||
// if (_allKeyFrame.empty())
|
||
// return;
|
||
|
||
// ModelAnimationKeyFrame& kf_front = _allKeyFrame.front();
|
||
// ModelAnimationKeyFrame& kf_back = _allKeyFrame.back();
|
||
// int idx = -1;
|
||
// if (t < kf_front._t)
|
||
// {
|
||
// ModelAnimationKeyFrame& kf = kf_front;
|
||
// if (_type == PathType::TRANSLATION)
|
||
// joint._t = *(Position3*)&kf._transform;
|
||
// else if (_type == PathType::ROATATION)
|
||
// joint._r = kf._transform;
|
||
// else if (_type == PathType::SCALING)
|
||
// joint._s = *(Position3*)&kf._transform;
|
||
// return;
|
||
// }
|
||
// else if (t > kf_back._t)
|
||
// {
|
||
// ModelAnimationKeyFrame& kf = kf_back;
|
||
// if (_type == PathType::TRANSLATION)
|
||
// joint._t = *(Position3*)&kf._transform;
|
||
// else if (_type == PathType::ROATATION)
|
||
// joint._r = kf._transform;
|
||
// else if (_type == PathType::SCALING)
|
||
// joint._s = *(Position3*)&kf._transform;
|
||
// return;
|
||
// }
|
||
// else
|
||
// {
|
||
// for (size_t i = 0; i < _allKeyFrame.size() - 1; ++i)
|
||
// {
|
||
// // [0, n-1)
|
||
// ModelAnimationKeyFrame& kf_0 = _allKeyFrame[i];
|
||
// ModelAnimationKeyFrame& kf_1 = _allKeyFrame[i + 1];
|
||
// if (t >= kf_0._t && t <= kf_1._t)
|
||
// {
|
||
// idx = i;
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
// if (idx == -1)
|
||
// return;
|
||
|
||
// ModelAnimationKeyFrame& kf_0 = _allKeyFrame[idx];
|
||
// ModelAnimationKeyFrame& kf_1 = _allKeyFrame[idx + 1];
|
||
// FloatTime percent = (t - kf_0._t) / (kf_1._t - kf_0._t);
|
||
|
||
// if (_type == PathType::TRANSLATION)
|
||
// {
|
||
// joint._t = Each::Lerp(
|
||
// *(Position3*)&kf_0._transform,
|
||
// *(Position3*)&kf_1._transform, percent);
|
||
// }
|
||
// else if (_type == PathType::ROATATION)
|
||
// {
|
||
// joint._r = Math::Slerp(kf_0._transform, kf_1._transform, percent);
|
||
// }
|
||
// else if (_type == PathType::SCALING)
|
||
// {
|
||
// joint._s = Each::Lerp(
|
||
// *(Position3*)&kf_0._transform,
|
||
// *(Position3*)&kf_1._transform, percent);
|
||
// }
|
||
// else
|
||
// {
|
||
// assert(0);
|
||
// }
|
||
// }
|
||
// PathType _type;
|
||
// int _boneId;
|
||
// std::vector<ModelAnimationKeyFrame> _allKeyFrame;
|
||
//};
|
||
//! 动画,包含多个动画通道
|
||
//struct ModelAnimation
|
||
//{
|
||
// FloatTime _t0;
|
||
// FloatTime _t1;
|
||
// std::vector<ModelAnimationChannel> _allModelAnimationChannel;
|
||
//public:
|
||
// FloatTime GetClipEndTime()
|
||
// {
|
||
// return _t1;
|
||
// }
|
||
// //插值
|
||
// void InterpolateAll(FloatTime t, std::vector<Joint>& all_joint)
|
||
// {
|
||
// // 一个通道只会平移、旋转、缩放其中一种
|
||
// // 多个通道影响同一个bone,需要相矩阵乘起来
|
||
// for (size_t i = 0; i < _allModelAnimationChannel.size(); ++i)
|
||
// {
|
||
// ModelAnimationChannel& channel = _allModelAnimationChannel[i];
|
||
// if (channel._boneId != -1)
|
||
// channel.Interpolate(t, all_joint[channel._boneId]);
|
||
// }
|
||
// }
|
||
//};
|
||
|
||
// 动画通道
|
||
struct ModelAnimationChannel
|
||
{
|
||
enum PathType { TRANSLATION, ROTATION, SCALE };
|
||
PathType path;
|
||
ModelNode* node;
|
||
uint32_t samplerIndex;
|
||
};
|
||
// 动画采样
|
||
struct ModelAnimationSampler
|
||
{
|
||
enum InterpolationType { LINEAR, STEP, CUBICSPLINE };
|
||
InterpolationType interpolation;
|
||
std::vector<float> inputs;
|
||
std::vector<Position4> outputsVec4;
|
||
std::vector<float> outputs;
|
||
Position4 cubicSplineInterpolation(size_t index, float time, uint32_t stride);
|
||
void translate(size_t index, float time, ModelNode* node);
|
||
void scale(size_t index, float time, ModelNode* node);
|
||
void rotate(size_t index, float time, ModelNode* node);
|
||
};
|
||
|
||
struct ModelAnimation
|
||
{
|
||
std::string _name;
|
||
std::vector<ModelAnimationSampler> _allSampler;
|
||
std::vector<ModelAnimationChannel> _allChannel;
|
||
float start;// = std::numeric_limits<float>::max();
|
||
float end;// = std::numeric_limits<float>::min();
|
||
};
|
||
|
||
// 模型数据
|
||
struct ModelData
|
||
{
|
||
std::vector<ModelNode*> _allNodeChild; // 子节点
|
||
std::vector<ModelNode*> _allNode; // 所有节点
|
||
std::vector<ModelAnimation> _allAnimation; // 所有动画
|
||
std::vector<ModelSkin*> _allSkin; // 所有蒙皮
|
||
|
||
struct Runtime
|
||
{
|
||
float _t;
|
||
size_t _curAni;
|
||
};
|
||
Runtime _rt;
|
||
|
||
void Update();
|
||
};
|
||
|
||
|
||
|
||
|
||
|
||
|
||
//! 骨骼数据(多个骨骼)
|
||
//class BoneData
|
||
//{
|
||
// friend class ModelLoader;
|
||
// friend class FactoryImp;
|
||
// friend class ModelAnimationInstance;
|
||
// friend class Actor;
|
||
//public:
|
||
// FloatTime GetClipEndTime(std::string_view cilp_name)
|
||
// {
|
||
// auto clip = _allAnimation.find(std::string{ cilp_name });
|
||
// return clip->second.GetClipEndTime();
|
||
// }
|
||
//
|
||
// // 或许可以缓存常用时刻的变换
|
||
// void GetFinalTrans(std::string_view clip_name, FloatTime t,
|
||
// std::vector<Matrix4>& final_trans)
|
||
// {
|
||
// // 骨骼数量
|
||
// size_t num_bone = _allBoneOffset.size();
|
||
//
|
||
// // 变换到父骨骼的矩阵
|
||
// std::vector<Matrix4> trans_to_parent;
|
||
// trans_to_parent.resize(num_bone);
|
||
//
|
||
// std::vector<Joint> all_joint = _allBoneJoint;
|
||
//
|
||
// // 根据动画进行插值
|
||
// auto iter = _allAnimation.find(std::string{ clip_name });
|
||
// ModelAnimation& clip = iter->second;
|
||
// clip.InterpolateAll(t, all_joint);
|
||
//
|
||
// //
|
||
// for (size_t i = 0; i < all_joint.size(); ++i)
|
||
// {
|
||
// Joint& joint = all_joint[i];
|
||
// Matrix4 mat_t = Mat::CreateTranslation(joint._t);
|
||
// Matrix4 mat_r = Mat::CreateRotation(joint._r);
|
||
// Matrix4 mat_s = Mat::CreateScaling(joint._s);
|
||
// trans_to_parent[i] = mat_t * mat_r * mat_s * joint._mat;
|
||
// }
|
||
//
|
||
// // 计算trans_to_root
|
||
// std::vector<Matrix4> trans_to_root;
|
||
// trans_to_root.resize(num_bone);
|
||
//
|
||
// trans_to_root[0] = Mat::CreateIdentity();// trans_to_parent[0];
|
||
// for (size_t i = 1; i < num_bone; ++i)
|
||
// {
|
||
// // 自己到父
|
||
// Matrix4& mat_to_parent = trans_to_parent[i];
|
||
// // 父骨骼 到 根 的矩阵
|
||
// int index_parent = _allBoneParent[i];
|
||
// Matrix4& mat_parent_to_root = trans_to_root[index_parent];
|
||
// // 自己到根的矩阵 = 自己到父,再父到根
|
||
// trans_to_root[i] = mat_parent_to_root * mat_to_parent;
|
||
// }
|
||
//
|
||
// // 计算最终矩阵
|
||
// for (size_t i = 0; i < num_bone; ++i)
|
||
// {
|
||
// Matrix4& mat_offset = _allBoneOffset[i];
|
||
// Matrix4& mat_to_root = trans_to_root[i];
|
||
//
|
||
// // 顶点本身处于模型坐标系
|
||
// // 模型坐标系 -> 对应骨骼坐标系
|
||
// // 骨骼坐标系 -> 根坐标系(动画插值计算)
|
||
// final_trans[i] = _root2Model * mat_to_root * mat_offset;
|
||
// }
|
||
// //_model2root *
|
||
// //_root2Model *
|
||
// }
|
||
//
|
||
// void Save(StreamOutput& s)
|
||
// {
|
||
// s.Write(&_allBoneParent);
|
||
// s.Write(&_allBoneJoint);
|
||
// s.Write(&_allBoneOffset);
|
||
// s.Write(&_root2Model);
|
||
// s.Write(&_model2root);
|
||
//
|
||
// size_t size = _allAnimation.size();
|
||
// s.Write(&size);
|
||
// for (auto& iter : _allAnimation)
|
||
// {
|
||
// const std::string& name = iter.first;
|
||
// const ModelAnimation& clip = iter.second;
|
||
// s.Write(&name);
|
||
// s.Write(&clip._t0);
|
||
// s.Write(&clip._t1);
|
||
// size_t size1 = clip._allModelAnimationChannel.size();
|
||
// s.Write(&size1);
|
||
// for (size_t i = 0; i < size1; ++i)
|
||
// {
|
||
// const ModelAnimationChannel& kf_mul = clip._allModelAnimationChannel[i];
|
||
// s.Write(&kf_mul._type);
|
||
// s.Write(&kf_mul._boneId);
|
||
// size_t size2 = kf_mul._allKeyFrame.size();
|
||
// s.Write(&size2);
|
||
// for (size_t j = 0; j < size2; ++j)
|
||
// {
|
||
// const ModelAnimationKeyFrame& kf = kf_mul._allKeyFrame[j];
|
||
// s.Write(&kf);
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// void Load(StreamInput& s)
|
||
// {
|
||
// s.Read(&_allBoneParent);
|
||
// s.Read(&_allBoneJoint);
|
||
// s.Read(&_allBoneOffset);
|
||
// s.Read(&_root2Model);
|
||
// s.Read(&_model2root);
|
||
//
|
||
// size_t size;
|
||
// s.Read(&size);
|
||
// for (size_t k = 0; k < size; ++k)
|
||
// {
|
||
// std::string name;
|
||
// s.Read(&name);
|
||
// ModelAnimation& clip = _allAnimation[name];
|
||
// s.Read(&clip._t0);
|
||
// s.Read(&clip._t1);
|
||
// size_t size1;
|
||
// s.Read(&size1);
|
||
// clip._allModelAnimationChannel.resize(size1);
|
||
// for (size_t i = 0; i < size1; ++i)
|
||
// {
|
||
// ModelAnimationChannel& kf_mul = clip._allModelAnimationChannel[i];
|
||
// s.Read(&kf_mul._type);
|
||
// s.Read(&kf_mul._boneId);
|
||
// size_t size2;
|
||
// s.Read(&size2);
|
||
// kf_mul._allKeyFrame.resize(size2);
|
||
// for (size_t j = 0; j < size2; ++j)
|
||
// {
|
||
// s.Read(&kf_mul._allKeyFrame[j]);
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//private:
|
||
// std::vector<int> _allBoneParent;// Bone的父Bone
|
||
// std::vector<Joint> _allBoneJoint;// Bone到父Bone的变换
|
||
// std::vector<Matrix4> _allBoneOffset;// Bone偏移矩阵
|
||
// std::unordered_map<std::string, ModelAnimation> _allAnimation;// 动画片段
|
||
// Matrix4 _root2Model;
|
||
// Matrix4 _model2root;
|
||
//};
|
||
|
||
//! 骨骼动画实例
|
||
class ModelAnimationInstance
|
||
{
|
||
public:
|
||
ModelData* _skinndeData;// 弱引用
|
||
std::vector<Matrix4> _finalTrans;// 某时间点的最终变换(仅骨骼)
|
||
std::string _clipName;// 当前动画名称
|
||
FloatTime _t;// 时间点
|
||
size_t _id;// 骨骼实例下标(不是ub下标)
|
||
|
||
ModelAnimationInstance() :
|
||
_skinndeData{ nullptr },
|
||
_t{},
|
||
_id{(size_t)-1}
|
||
{}
|
||
|
||
//t为-1代表 设为起始
|
||
//void Set(std::string_view name, FloatTime t)
|
||
//{
|
||
// auto iter = _skinndeData->_allAnimation.find(std::string{ name });
|
||
// if (iter == _skinndeData->_allAnimation.end())
|
||
// {
|
||
// log_err("指定动画不存在:{}", name);
|
||
// return;
|
||
// }
|
||
// _clipName = std::string{ name };
|
||
// ModelAnimation& clip = iter->second;
|
||
// if (t < 0)
|
||
// _t = clip._t0;
|
||
// else if (Math::IsOutRange(t, clip._t0, clip._t1))
|
||
// {
|
||
// log_err("设置动画时间越界:{},{}->{}", t, clip._t0, clip._t1);
|
||
// _t = clip._t0;
|
||
// }
|
||
// else
|
||
// _t = t;
|
||
//}
|
||
|
||
//! 每帧调用,推进时间点进行插值
|
||
void Update(FloatTime dt)
|
||
{
|
||
_skinndeData->_rt._t += dt;
|
||
|
||
_skinndeData->Update();
|
||
|
||
/*_t += dt;
|
||
if (_t > _skinndeData->GetClipEndTime(_clipName))
|
||
_t = 0;
|
||
|
||
_skinndeData->GetFinalTrans(_clipName, _t, _finalTrans);*/
|
||
}
|
||
};
|
||
|
||
} |