88 lines
1.9 KiB
C++
88 lines
1.9 KiB
C++
#include "dl_model_loader.h" // Android dl_concept.h 宏名字冲突需要前置
|
|
|
|
#ifndef DL_DISABLE_RENDER
|
|
|
|
#include "io/dl_log.h"
|
|
#include "render/dl_model.h"
|
|
#include "render/dl_model_object.h"
|
|
|
|
namespace dl
|
|
{
|
|
|
|
std::vector<ModelObject*> ModelObject::Create(std::string_view path_name, std::string_view res_name)
|
|
{
|
|
std::vector<ModelObject*> ret = g_modelLoader->Load(path_name, res_name);
|
|
if (ret.empty())
|
|
{
|
|
log_err("模型加载失败:{}", path_name);
|
|
return {};
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void ModelAnimationInstance::Update(FloatTime dt)
|
|
{
|
|
if (_indexAni > _model->_allAnimation.size())
|
|
{
|
|
log_warn("动画下标越界!");
|
|
return;
|
|
}
|
|
_t += dt;
|
|
const ModelAnimation& ani = _model->_allAnimation[_indexAni];
|
|
if (_t > ani.end)
|
|
{
|
|
_t = 0;
|
|
}
|
|
|
|
// 动画插值(操作动画控制的节点)
|
|
bool updated = false;
|
|
for (const ModelAnimationChannel& channel : ani._allChannel)
|
|
{
|
|
ModelNodeRt* node = GetNode(channel.indexNode);
|
|
|
|
const ModelAnimationSampler& sampler = ani._allSampler[channel.samplerIndex];
|
|
if (sampler.inputs.size() > sampler.outputsVec4.size())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float time = _t;
|
|
for (size_t i = 0; i < sampler.inputs.size() - 1; i++)
|
|
{
|
|
if (time >= sampler.inputs[i]
|
|
&& time <= sampler.inputs[i + 1])
|
|
{
|
|
float u = std::max(0.0f, time - sampler.inputs[i])
|
|
/ (sampler.inputs[i + 1] - sampler.inputs[i]);
|
|
if (u <= 1.0f)
|
|
{
|
|
switch (channel.path)
|
|
{
|
|
case ModelAnimationChannel::PathType::TRANSLATION:
|
|
sampler.translate(i, time, node);
|
|
break;
|
|
case ModelAnimationChannel::PathType::SCALE:
|
|
sampler.scale(i, time, node);
|
|
break;
|
|
case ModelAnimationChannel::PathType::ROTATION:
|
|
sampler.rotate(i, time, node);
|
|
break;
|
|
}
|
|
updated = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 节点树更新
|
|
if (updated)
|
|
{
|
|
_treeNode.ForEach([&](ModelNodeRt* node) {
|
|
node->Update(_model->_allSkin);
|
|
_finalTrans[node->_index] = node->_mat;
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#endif |