* 增加动画切换、动态加载模型

This commit is contained in:
2026-04-27 00:51:56 +08:00
parent 326c021f3d
commit 0569b07d58
12 changed files with 128 additions and 65 deletions
+3 -3
View File
@@ -87,18 +87,18 @@ std::string Actor::GetAniName(size_t i)
}
return model->_allAnimation[i]._name;
}
void Actor::SwitchAni(std::string_view name)
void Actor::SetAni(std::string_view name)
{
if (_no_bone_animation())
return;
//_modelAnimationInstance->Set(name, -1.0f);
_modelAnimationInstance->Set(name, 0);
}
std::string Actor::GetAniCurrent()
{
if (_no_bone_animation())
return {};
return "xx";// _modelAnimationInstance->_clipName;
return _modelAnimationInstance->Get();
}
void Actor::CreateRigidBody()
+1 -1
View File
@@ -54,7 +54,7 @@ public:
/**
* @brief 切换动画(从头播放)
*/
void SwitchAni(std::string_view name);
void SetAni(std::string_view name);
/**
* @brief 返回当前动画
* @retval 失败返回空(没有骨骼动画时)
+9 -1
View File
@@ -27,7 +27,7 @@ void Factory::LoadModel(std::string_view path_name, std::string_view name)
auto iter = _imp->_mapName2Model.find(res_name);
if (iter != _imp->_mapName2Model.end())
{
log_err("资源名字已经存在:{},id:{}", res_name, iter->second);
log_warn("资源名字已经存在:{},id:{}", res_name, iter->second);
return;
}
//-------------------------判断从文件还是从缓存读取-----------------------
@@ -154,6 +154,14 @@ void Factory::LoadModel(std::string_view path_name, std::string_view name)
res_name, mul_model.size(), b_read_cache, t1 - t0);
}
std::vector<std::string> Factory::GetAllModel()
{
std::vector<std::string> ret;
for (auto& iter : _imp->_mapName2Model)
ret.push_back(iter.first);
return ret;
}
gcActor Factory::CreateActor(std::string_view name)
{
gcActor act = get_gc<Actor>();
+9 -1
View File
@@ -97,6 +97,8 @@ public:
*/
void LoadModel(std::string_view path_name, std::string_view name = "");
std::vector<std::string> GetAllModel();
/**
* @brief 返回图像目录
*/
@@ -111,7 +113,13 @@ public:
{
return _strPathVoice;
}
/**
* @brief 返回模型目录
*/
std::string GetPathModel()
{
return _strPathModel;
}
/**
* @brief 返回资源缓存目录,例如"binary/"
*/
+30
View File
@@ -150,6 +150,36 @@ void ModelAnimationInstance::Update(FloatTime dt)
}
}
void ModelAnimationInstance::Set(size_t i, float t)
{
if (i >= _model->_allAnimation.size())
{
log_warn("动画下标越界:{}/{}", i, _model->_allAnimation.size());
return;
}
_indexAni = i;
_t = t;
}
void ModelAnimationInstance::Set(std::string_view name, float t)
{
for (size_t i = 0; i < _model->_allAnimation.size(); ++i)
{
if (_model->_allAnimation[i]._name == name)
{
_indexAni = i;
_t = t;
return;
}
}
log_warn("未找到动画名:{}", name);
}
std::string ModelAnimationInstance::Get()
{
return _model->_allAnimation[_indexAni]._name;
}
void ModelAnimationInstance::UpdateNode()
{
for (size_t i = 0; i < _treeNode.size(); ++i)
+4
View File
@@ -133,6 +133,10 @@ public:
//! 每帧调用,推进时间点进行插值
void Update(FloatTime dt);
void Set(size_t i, float t);
void Set(std::string_view name, float t);
std::string Get();
void UpdateNode();
ModelNodeRt* GetNode(int index)
{
-21
View File
@@ -14,14 +14,6 @@ void Test()
Tool::Manager* g_Tool;
void Init()
{
g_factory->LoadModel("AK-47.glb", "ak47");
g_factory->LoadModel("Cesium_Man.glb", "man");
g_factory->LoadModel("assault_drone.glb", "drone");
g_factory->LoadModel("axis.glb", "axis");
g_factory->LoadModel("DamagedHelmet.gltf", "helmet");
g_factory->LoadModel("dog.glb", "dog");
g_factory->LoadModel("Soldier.glb", "soldier");
g_Tool = Tool::Manager::Create();
g_Tool->SetVisible(false);
@@ -39,19 +31,6 @@ void Run()
{
g_Tool->Update();
g_Tool->Render();
//base->txt->Render();
/*base->spr->Render();
base->txt->Render();
base->spr_logo->Render();*/
//g_gfx->Dot({ 400, 300, 0.5f });
//g_gfx->Line({ 20, 20, 0 }, { 800, 600, 0 });
//g_gfx->Triangle({ 400, 10, 0 }, { 10, 500, 0 }, { 750, 550, 0 });
//g_gfx->TriangleSolid({ 0, 0, 0 }, { 100, 0, 0 }, { 10, 250, 0 });
//g_gfx->Love();
//base->spr9->Render();
}
void Release()
+64 -33
View File
@@ -10,14 +10,42 @@ public:
UIActor() :
_bControl{ false }
{
_vecAct.push_back(g_factory->CreateActor("ak47"));
_vecAct.push_back(g_factory->CreateActor("drone"));
_vecAct.push_back(g_factory->CreateActor("axis"));
_vecAct.push_back(g_factory->CreateActor("man"));
_vecAct.push_back(g_factory->CreateActor("helmet"));
_vecAct.push_back(g_factory->CreateActor("dog"));
_vecAct.push_back(g_factory->CreateActor("soldier"));
_cur = 0;
UI::Panel* panel = CreatePanel("root");
panel->SetPosScale({ 0.0f, 0.0f });
panel->SetPosPixel(POS_CONTENT_LB);
panel->SetLocator(createLocatorY(180.0f));
//
File::GetPathFileAll(g_factory->GetPathModel(), _allModel);
for (auto& iter : _allModel)
{
iter = File::GetRelative(iter, g_factory->GetPathModel());
}
{
UI::ListBox* lst = g_ui->CreateListBoxDefault(false);
lst->SetItemCallback([](UI::Panel* item, Value v) {
if (v)
item->ExSetString(v.ToString());
else
item->ExSetString({});
});
lst->SetItemData(_allModel);
panel->Add(lst);
_lst = lst;
}
{
UI::ListBox* lst = g_ui->CreateListBoxDefault(false);
lst->SetItemCallback([](UI::Panel* item, Value v) {
if (v)
item->ExSetString(v.ToString());
else
item->ExSetString({});
});
panel->Add(lst);
_lstAnimation = lst;
}
_vecAct.resize(1);
}
~UIActor()
{}
@@ -65,31 +93,30 @@ public:
else if (g_input->KeyDown(KeyCode::KP_6))
g_factory->SetDebugViewPbr(6);
//
if (g_input->KeyDown(KeyCode::KEY1))
_cur = 0;
else if (g_input->KeyDown(KeyCode::KEY2))
_cur = 1;
else if (g_input->KeyDown(KeyCode::KEY3))
_cur = 2;
else if (g_input->KeyDown(KeyCode::KEY4))
_cur = 3;
else if (g_input->KeyDown(KeyCode::KEY5))
_cur = 4;
else if (g_input->KeyDown(KeyCode::KEY6))
_cur = 5;
else if (g_input->KeyDown(KeyCode::KEY7))
_cur = 6;
else if (g_input->KeyDown(KeyCode::KEY8))
_cur = 7;
else if (g_input->KeyDown(KeyCode::KEY9))
_cur = 8;
if (_cur < _vecAct.size())
Value v = _lst->GetSelectClickDouble();
if (v)
{
if (_vecAct[_cur]->HasAni() && g_input->KeyState(KeyCode::SPACE))
_vecAct[_cur]->Update();
_vecAct[_cur]->Render();
g_factory->LoadModel(v.ToString(), v.ToString());
_vecAct[0] = g_factory->CreateActor(v.ToString());
_allAnmation.clear();
for (size_t i = 0; i < _vecAct[0]->GetAniSize(); ++i)
{
_allAnmation.push_back(_vecAct[0]->GetAniName(i));
}
_lstAnimation->SetItemData(_allAnmation);
}
if (_vecAct[0])
{
Value v = _lstAnimation->GetSelectClickDouble();
if (v)
{
_vecAct[0]->SetAni(v.ToString());
}
if (_vecAct[0]->HasAni() && !g_input->KeyState(KeyCode::SPACE))
_vecAct[0]->Update();
_vecAct[0]->Render();
}
g_gfx->SetColor(ColorDef::AXIS_X);
g_gfx->Line({}, { 100, 0, 0 });
@@ -106,7 +133,11 @@ public:
private:
std::vector<gcActor> _vecAct;
bool _bControl;
size_t _cur;
UI::ListBox* _lst;
std::vector<std::string> _allModel;
UI::ListBox* _lstAnimation;
std::vector<std::string> _allAnmation;
LightDir _light;
};
+3 -2
View File
@@ -36,11 +36,12 @@ public:
virtual void Render() override
{
if (g_input->ClickDouble())
Value v = _edt->GetSelectClickDouble();
if (v)
{
if (_snd)
_snd->Stop();
_snd = snd_play(_edt->GetSelectClickDouble().ToString());
_snd = snd_play(v.ToString());
}
}
~UIAudio()
+2 -1
View File
@@ -3,4 +3,5 @@
#include <dl_type.h>
using namespace dl;
constexpr Position POS_CONTENT = { 320, 140 };
constexpr Position POS_CONTENT = { 320, 140 };
constexpr Position POS_CONTENT_LB = { 95, 225 };
+1 -1
View File
@@ -117,7 +117,7 @@ void UIMain::_change_scene()
else if (_selectName == "actor")
{
_uiInfo._txtTitle->SetString("Actor 3d绘图");
_uiInfo._txtInfo->SetString("3d图形绘制 按tab操作相机\n1-9切换模型,空格键播放");
_uiInfo._txtInfo->SetString("3d图形绘制 按tab操作相机\n双击切换模型,空格键停止动画");
g_scene->Add("child", new UIActor);
_sprBg->SetVisible(false);
}
+2 -1
View File
@@ -29,7 +29,8 @@ foreach(dir ${PATH_SRC})
else()
message("其他代码路径: ${dir}")
endif()
aux_source_directory(${dir} SRC_LIST)
file(GLOB_RECURSE SRC_LIST ${dir}/*.cpp ${dir}/*.h
)
endforeach()
list(LENGTH SRC_LIST SRC_LIST_NUM)