dl
dl_model_object.h
浏览该文件的文档.
1
13#pragma once
14
15#ifndef DL_DISABLE_RENDER
16#include <string>
17#include <unordered_map>
18
19#include "io/dl_file.h"
20#include "vk/dl_vertex.h"
25
26namespace dl
27{
28
29//一个文件会有多个ModelObject,一个ModelObject根据材质分为多个ModelSub
30//注意ModelSub为一个材质,需要读取时合并网格
32{
33 friend class VK;
34 friend class ModelLoader;
35 friend class FactoryImp;
36 friend class Actor;
37public:
38 //从源文件加载模型
39 static std::vector<ModelObject*> Create(std::string_view path_name, std::string_view res_name);
40
41 void Load(std::string_view path_name);
42
43 //保存到文件
44 void SaveToFile(std::string_view path_name);
45
47 {
48 for (auto& iter : _allMaterial)
49 {
50 if (!iter._pathTexDiffuse.empty()
51 || !iter._pathTexPhysical.empty()
52 || !iter._pathTexNormal.empty()
53 || !iter._pathTexAo.empty()
54 || !iter._pathTexEmissvie.empty())
55 return true;
56 }
57 return false;
58 }
59
60 // 转换贴图路径
61 void ConvertTexturePath(std::string_view path)
62 {
63 // 避免重复导出同一张图像
64 std::unordered_map<std::string, std::string> map_out;
65
66 auto fn_output = [&map_out, path](std::string& path_name, size_t i, std::string_view tag)
67 {
68 if (path_name.empty())
69 return;
70 auto iter = map_out.find(path_name);
71 if (iter == map_out.end())
72 {
73 std::string fmt = File::GetExtension(path_name);
74 std::string name = fmt::format("{}{}{}.{}", path, tag, i, fmt);
75 File::Copy(path_name, name, File::CopyMode::OVERRIDE);
76 map_out[path_name] = name;
77 //返回path_name前,先记录到表
78 path_name = name;
79 }
80 else
81 path_name = iter->second;
82 };
83
84 size_t i = 0;
85 for (auto& iter : _allMaterial)
86 {
87 fn_output(iter._pathTexDiffuse, i, "diffuse#");
88 fn_output(iter._pathTexPhysical, i, "physical#");
89 fn_output(iter._pathTexNormal, i, "normal#");
90 fn_output(iter._pathTexAo, i, "ao#");
91 fn_output(iter._pathTexEmissvie, i, "emissvie#");
92 ++i;
93 }
94 }
96private:
97 std::vector<ModelNodeRt> _treeNode; // 节点树(骨骼动画使用)
98 std::vector<int> _treeNodeParent;
99
100 std::vector<Vertex> _allVertex; // 顶点缓存
101 std::vector<VertexBone> _allVertexBone; // 顶点缓存(有骨骼动画时使用)
102 std::vector<uint32_t> _allIndex; // 索引缓存
103
104 // 所有材质
105 std::vector<ModelMaterial> _allMaterial;
106 std::vector<ModelAnimation> _allAnimation;
107 std::vector<ModelSkin> _allSkin;
108 std::vector<ModelDrawCall> _allSub;
109};
110
111
113class Model;
115{
116public:
117 const Model* _model;// 弱引用
118 std::vector<Matrix4> _finalTrans;// 某时间点的最终变换(仅骨骼)
119 size_t _indexAni;
120 FloatTime _t;// 时间点
121 size_t _id;// 骨骼实例下标(不是ub下标)
122 std::vector<ModelNodeRt> _treeNode;
123 std::vector<int> _treeNodeParent;
124
126 _model{ nullptr },
127 _t{},
128 _id{ (size_t)-1 },
129 _indexAni{ 0 }
130 {
131 }
132
135
136 void Set(size_t i, float t);
137 void Set(std::string_view name, float t);
138 std::string Get();
139
142 {
143 if (index != -1)
144 return &_treeNode[index];
145 return nullptr;
146 }
147};
148}
149
150#endif
std::vector< Matrix4 > _finalTrans
void Set(size_t i, float t)
ModelNodeRt * GetNode(int index)
void Update(FloatTime dt)
每帧调用,推进时间点进行插值
void Set(std::string_view name, float t)
std::vector< ModelNodeRt > _treeNode
void Load(std::string_view path_name)
void ConvertTexturePath(std::string_view path)
static std::vector< ModelObject * > Create(std::string_view path_name, std::string_view res_name)
void SaveToFile(std::string_view path_name)
文件系统
读取到的原生数据
bool Copy(std::string_view source, std::string_view target, CopyMode mode=CopyMode::NONE, bool recursion=false)
复制文件或文件夹
std::string GetExtension(std::string_view path_name, bool has_dot=false)
返回文件后缀
float FloatTime
运行时节点树