150 lines
3.7 KiB
C++
150 lines
3.7 KiB
C++
/**
|
|
* @file dl_model_object.h
|
|
* @brief 中转模型数据
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 21-03-28
|
|
*
|
|
* @note 一个模型文件加载后变为多个子模型(它们拥有不同的材质、贴图,但顶点都放在一起,没有变换矩阵)
|
|
* @note 子模型必定为单一drawcall,名字为读取到的前加冒号。例如a:b:c,c为b的子节点,b为a的子节点
|
|
*/
|
|
#pragma once
|
|
|
|
#ifndef DL_DISABLE_RENDER
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#include "io/dl_file.h"
|
|
#include "vk/dl_vertex.h"
|
|
#include "memory/dl_stream_input.h"
|
|
#include "memory/dl_stream_output.h"
|
|
#include "render/dl_model_common.h"
|
|
#include "render/dl_model_data.h"
|
|
|
|
namespace dl
|
|
{
|
|
|
|
//一个文件会有多个ModelObject,一个ModelObject根据材质分为多个ModelSub
|
|
//注意ModelSub为一个材质,需要读取时合并网格
|
|
class ModelObject
|
|
{
|
|
friend class VK;
|
|
friend class ModelLoader;
|
|
friend class FactoryImp;
|
|
friend class Actor;
|
|
public:
|
|
//从源文件加载模型
|
|
static std::vector<ModelObject*> Create(std::string_view path_name, std::string_view res_name);
|
|
|
|
void Load(std::string_view path_name);
|
|
|
|
//保存到文件
|
|
void SaveToFile(std::string_view path_name);
|
|
|
|
bool IsHaveTexture()
|
|
{
|
|
for (auto& iter : _allMaterial)
|
|
{
|
|
if (!iter._pathTexDiffuse.empty()
|
|
|| !iter._pathTexPhysical.empty()
|
|
|| !iter._pathTexNormal.empty()
|
|
|| !iter._pathTexAo.empty()
|
|
|| !iter._pathTexEmissvie.empty())
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 转换贴图路径
|
|
void ConvertTexturePath(std::string_view path)
|
|
{
|
|
// 避免重复导出同一张图像
|
|
std::unordered_map<std::string, std::string> map_out;
|
|
|
|
auto fn_output = [&map_out, path](std::string& path_name, size_t i, std::string_view tag)
|
|
{
|
|
if (path_name.empty())
|
|
return;
|
|
auto iter = map_out.find(path_name);
|
|
if (iter == map_out.end())
|
|
{
|
|
std::string fmt = File::GetExtension(path_name);
|
|
std::string name = fmt::format("{}{}{}.{}", path, tag, i, fmt);
|
|
File::Copy(path_name, name, File::CopyMode::OVERRIDE);
|
|
map_out[path_name] = name;
|
|
//返回path_name前,先记录到表
|
|
path_name = name;
|
|
}
|
|
else
|
|
path_name = iter->second;
|
|
};
|
|
|
|
size_t i = 0;
|
|
for (auto& iter : _allMaterial)
|
|
{
|
|
fn_output(iter._pathTexDiffuse, i, "diffuse#");
|
|
fn_output(iter._pathTexPhysical, i, "physical#");
|
|
fn_output(iter._pathTexNormal, i, "normal#");
|
|
fn_output(iter._pathTexAo, i, "ao#");
|
|
fn_output(iter._pathTexEmissvie, i, "emissvie#");
|
|
++i;
|
|
}
|
|
}
|
|
ModelObject() {}
|
|
private:
|
|
std::vector<ModelNodeRt> _treeNode; // 节点树(骨骼动画使用)
|
|
std::vector<int> _treeNodeParent;
|
|
|
|
std::vector<Vertex> _allVertex; // 顶点缓存
|
|
std::vector<VertexBone> _allVertexBone; // 顶点缓存(有骨骼动画时使用)
|
|
std::vector<uint32_t> _allIndex; // 索引缓存
|
|
|
|
// 所有材质
|
|
std::vector<ModelMaterial> _allMaterial;
|
|
std::vector<ModelAnimation> _allAnimation;
|
|
std::vector<ModelSkin> _allSkin;
|
|
std::vector<ModelDrawCall> _allSub;
|
|
};
|
|
|
|
|
|
//! 骨骼动画实例
|
|
class Model;
|
|
class ModelAnimationInstance
|
|
{
|
|
public:
|
|
const Model* _model;// 弱引用
|
|
std::vector<Matrix4> _finalTrans;// 某时间点的最终变换(仅骨骼)
|
|
size_t _indexAni;
|
|
FloatTime _t;// 时间点
|
|
size_t _id;// 骨骼实例下标(不是ub下标)
|
|
std::vector<ModelNodeRt> _treeNode;
|
|
std::vector<int> _treeNodeParent;
|
|
|
|
ModelAnimationInstance() :
|
|
_model{ nullptr },
|
|
_t{},
|
|
_id{ (size_t)-1 },
|
|
_indexAni{ 0 }
|
|
{
|
|
}
|
|
|
|
//! 每帧调用,推进时间点进行插值
|
|
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)
|
|
{
|
|
if (index != -1)
|
|
return &_treeNode[index];
|
|
return nullptr;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif |