* 子网格合并
This commit is contained in:
@@ -44,6 +44,7 @@ private:
|
||||
std::vector<int> _treeNodeParent;
|
||||
std::vector<ModelAnimation> _allAnimation; // 动画
|
||||
std::vector<ModelSkin> _allSkin; // 骨骼
|
||||
std::vector<ModelNodeRtSub> _allSub; // 网格
|
||||
|
||||
std::vector<Instance> _mulInstance; // 实例(每个actor有一个)
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "dl_model_data.h"
|
||||
#include "dl_model_data.h"
|
||||
|
||||
#ifndef DL_DISABLE_RENDER
|
||||
#include "vk/dl_vk.h"
|
||||
@@ -12,7 +12,6 @@ namespace dl
|
||||
s.Write(&_index);
|
||||
s.Write(&_indexSkin);
|
||||
s.Write(&_indexParent);
|
||||
s.Write(&_allSub);
|
||||
s.Write(&_trans2Parent);
|
||||
}
|
||||
|
||||
@@ -21,7 +20,6 @@ namespace dl
|
||||
s.Read(&_index);
|
||||
s.Read(&_indexSkin);
|
||||
s.Read(&_indexParent);
|
||||
s.Read(&_allSub);
|
||||
s.Read(&_trans2Parent);
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +116,6 @@ struct ModelNodeRt
|
||||
int _indexSkin; // 整个骨骼是在一个节点上
|
||||
int _indexParent; // 父节点索引
|
||||
ModelNodeTransform _trans2Parent; // 到父节点的变换(具有初始值,动画只改变srt)
|
||||
std::vector<ModelNodeRtSub> _allSub;
|
||||
|
||||
bool _bNeedUpdateMatrixWorld;
|
||||
bool _bNeedUpdateMatrixLocal;
|
||||
@@ -230,6 +229,7 @@ struct ModelData
|
||||
std::vector<ModelAnimation> _allAnimation; // 所有动画
|
||||
std::vector<ModelSkin> _allSkin; // 所有蒙皮
|
||||
std::vector<ModelMaterial> _allMaterial; // 所有材质
|
||||
std::vector<ModelNodeRtSub> _allSub; // 所有网格
|
||||
std::vector<uint32_t> _allIndex; // 索引
|
||||
std::vector<Vertex> _allVertex; // 顶点
|
||||
std::vector<VertexBone> _allVertexSkin; // 顶点
|
||||
|
||||
@@ -145,7 +145,7 @@ std::vector<ModelObject*> ModelLoader::_load_obj(std::string_view path_name)
|
||||
sub._range._countTriangle = sub_idx.size() / 3;
|
||||
sub._indexMaterial = j - 1;
|
||||
++j;
|
||||
node._allSub.push_back(sub);
|
||||
// todo node._allSub.push_back(sub);
|
||||
|
||||
for (tinyobj::index_t& idx : sub_idx)
|
||||
{
|
||||
@@ -194,6 +194,7 @@ ModelObject* ModelLoader::_create_model_object(ModelData& data)
|
||||
obj->_allAnimation = data._allAnimation;
|
||||
obj->_allSkin = data._allSkin;
|
||||
obj->_treeNodeParent.resize(data._allNode.GetSize());
|
||||
obj->_allSub = data._allSub;
|
||||
obj->_allIndex = data._allIndex;
|
||||
if (obj->_allSkin.empty())
|
||||
obj->_allVertex = data._allVertex;
|
||||
@@ -208,15 +209,6 @@ ModelObject* ModelLoader::_create_model_object(ModelData& data)
|
||||
node_rt._index = node->_index;
|
||||
node_rt._indexSkin = node->_indexSkin;
|
||||
node_rt._trans2Parent = node->_trans2Parent;
|
||||
//
|
||||
for (ModelPrimitive& pri : node->_mesh.primitives)
|
||||
{
|
||||
ModelNodeRtSub sub;
|
||||
sub._indexMaterial = pri._indexMaterial;
|
||||
sub._range._offsetIndex = pri._indexBeg;
|
||||
sub._range._countTriangle = pri._indexCount / 3;
|
||||
node_rt._allSub.push_back(sub);
|
||||
}
|
||||
// parent_id 重映射
|
||||
if (!node->_parent)
|
||||
node_rt._indexParent = -1;
|
||||
@@ -283,40 +275,7 @@ std::vector<ModelObject*> ModelLoader::_load_gltf(
|
||||
_gltf_load_material(model, res_name, model_data._allMaterial);
|
||||
_gltf_load_scene(model, model_data);
|
||||
|
||||
//子网格合并
|
||||
//if (false)
|
||||
//{//合并网格连续,且材质相同的项
|
||||
// std::vector<ModelObjectSub> vec_sub;
|
||||
// ModelObjectSub* pre = nullptr;
|
||||
// //
|
||||
// for (auto iter = model_object->_allSub.begin();
|
||||
// iter != model_object->_allSub.end(); ++iter)
|
||||
// {
|
||||
// ModelObjectSub& sub = *iter;
|
||||
|
||||
// if (pre
|
||||
// && sub._indexMaterial == pre->_indexMaterial
|
||||
// && (sub._range._offsetIndex
|
||||
// == pre->_range._offsetIndex + pre->_range._countTriangle * 3))
|
||||
// {//是连续的
|
||||
// pre->_range._countTriangle += sub._range._countTriangle;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// vec_sub.push_back(sub);
|
||||
// pre = &vec_sub.back();
|
||||
// }
|
||||
// }
|
||||
// log_info("子网格合并:{}->{}", model_object->_allSub.size(), vec_sub.size());
|
||||
// for (ModelObjectSub& sub : vec_sub)
|
||||
// {
|
||||
// log_info("合并后网格,起始:{},三角数:{},材质:{}",
|
||||
// sub._range._offsetIndex, sub._range._countTriangle, sub._indexMaterial);
|
||||
// }
|
||||
|
||||
// std::swap(model_object->_allSub, vec_sub);
|
||||
// int pause = 3;
|
||||
//}
|
||||
|
||||
|
||||
/*if (!has_bone)
|
||||
{
|
||||
@@ -547,8 +506,6 @@ void ModelLoader::_gltf_load_scene(const tinygltf::Model& model, ModelData& data
|
||||
// 读取蒙皮
|
||||
_gltf_load_skin(model, data);
|
||||
|
||||
|
||||
|
||||
// 若是静态模型,则转为世界坐标系
|
||||
if (data._allSkin.empty())
|
||||
{
|
||||
@@ -577,6 +534,58 @@ void ModelLoader::_gltf_load_scene(const tinygltf::Model& model, ModelData& data
|
||||
}
|
||||
}
|
||||
}
|
||||
// pri -> all_sub
|
||||
for (auto& iter : data._allNode)
|
||||
{
|
||||
for (ModelPrimitive& pri : iter->GetValue()->_mesh.primitives)
|
||||
{
|
||||
ModelNodeRtSub sub;
|
||||
sub._indexMaterial = pri._indexMaterial;
|
||||
sub._range._offsetIndex = pri._indexBeg;
|
||||
sub._range._countTriangle = pri._indexCount / 3;
|
||||
data._allSub.push_back(sub);
|
||||
}
|
||||
}
|
||||
// 子网格合并
|
||||
if (true)
|
||||
{
|
||||
// 排序
|
||||
std::map<size_t, ModelNodeRtSub*> map_pri;
|
||||
for (ModelNodeRtSub& iter : data._allSub)
|
||||
{
|
||||
map_pri[iter._range._offsetIndex] = &iter;
|
||||
}
|
||||
// 合并网格连续,且材质相同的项
|
||||
std::vector<ModelNodeRtSub> vec_sub;
|
||||
ModelNodeRtSub* pre = nullptr;
|
||||
//
|
||||
for (auto& iter : map_pri)
|
||||
{
|
||||
ModelNodeRtSub* sub = iter.second;
|
||||
|
||||
if (pre
|
||||
&& sub->_indexMaterial == pre->_indexMaterial
|
||||
&& (sub->_range._offsetIndex
|
||||
== pre->_range._offsetIndex + pre->_range._countTriangle * 3))
|
||||
{// 是连续的
|
||||
pre->_range._countTriangle += sub->_range._countTriangle;
|
||||
}
|
||||
else
|
||||
{
|
||||
vec_sub.push_back(*sub);
|
||||
pre = &vec_sub.back();
|
||||
}
|
||||
}
|
||||
log_info("子网格合并:{}->{}", map_pri.size(), vec_sub.size());
|
||||
for (ModelNodeRtSub& sub : vec_sub)
|
||||
{
|
||||
log_info("合并后网格,起始:{},三角数:{},材质:{}",
|
||||
sub._range._offsetIndex, sub._range._countTriangle, sub._indexMaterial);
|
||||
}
|
||||
|
||||
std::swap(data._allSub, vec_sub);
|
||||
int pause = 3;
|
||||
}
|
||||
}
|
||||
|
||||
void ModelLoader::_gltf_load_node(const tinygltf::Model& model, int node,
|
||||
|
||||
@@ -53,6 +53,7 @@ void ModelObject::Load(std::string_view path_name)
|
||||
for (size_t i = 0; i < _allSkin.size(); ++i)
|
||||
_allSkin[i].Load(s);
|
||||
}
|
||||
s.Read(&_allSub);
|
||||
}
|
||||
|
||||
void ModelObject::SaveToFile(std::string_view path_name)
|
||||
@@ -84,6 +85,7 @@ void ModelObject::SaveToFile(std::string_view path_name)
|
||||
for (size_t i = 0; i < _allSkin.size(); ++i)
|
||||
_allSkin[i].Save(s);
|
||||
}
|
||||
s.Write(&_allSub);
|
||||
|
||||
s.Save(path_name, true);
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ private:
|
||||
std::vector<ModelMaterial> _allMaterial;
|
||||
std::vector<ModelAnimation> _allAnimation;
|
||||
std::vector<ModelSkin> _allSkin;
|
||||
std::vector<ModelNodeRtSub> _allSub;
|
||||
};
|
||||
|
||||
|
||||
|
||||
+21
-27
@@ -2539,6 +2539,8 @@ Model* VK::CreateModel(ModelObject* object)
|
||||
Model* model = new Model;
|
||||
model->_allAnimation = object->_allAnimation;
|
||||
model->_allSkin = object->_allSkin;
|
||||
model->_allSub = object->_allSub;
|
||||
model->_treeNode = object->_treeNode;
|
||||
//顶点缓冲
|
||||
if (!object->_allSkin.empty())
|
||||
{
|
||||
@@ -2575,16 +2577,11 @@ Model* VK::CreateModel(ModelObject* object)
|
||||
}
|
||||
|
||||
// 映射材质id
|
||||
model->_treeNode = object->_treeNode;
|
||||
|
||||
for (ModelNodeRt& node : model->_treeNode)
|
||||
for (ModelNodeRtSub& sub : model->_allSub)
|
||||
{
|
||||
for (ModelNodeRtSub& sub : node._allSub)
|
||||
if (sub._indexMaterial != -1)
|
||||
{
|
||||
if (sub._indexMaterial != -1)
|
||||
{
|
||||
sub._indexMaterial = vec_material_id[sub._indexMaterial];
|
||||
}
|
||||
sub._indexMaterial = vec_material_id[sub._indexMaterial];
|
||||
}
|
||||
}
|
||||
return model;
|
||||
@@ -2629,28 +2626,25 @@ void VK::RenderModel(VkCommandBuffer command_buffer, Model* model)
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
pipe_layout, 2, 1, &_descriptorSetsBone[offset], 0, nullptr);
|
||||
}
|
||||
for (ModelNodeRt& node : model->_treeNode)
|
||||
for (ModelNodeRtSub& sub : model->_allSub)
|
||||
{
|
||||
for (ModelNodeRtSub& sub : node._allSub)
|
||||
{
|
||||
// 绑定描述符表(ubo材质)
|
||||
uint32_t offset = (uint32_t)(sub._indexMaterial * NUM_MAX_FRAME_IN_FLIGHT
|
||||
+ _currentFrame);
|
||||
// 绑定材质
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
pipe_layout, 0, 1, &_descriptorSets[offset], 0, nullptr);
|
||||
// 绑定纹理
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
pipe_layout, 1, 1, &_descriptorSetsTex[sub._indexMaterial], 0, nullptr);
|
||||
// 绑定描述符表(ubo材质)
|
||||
uint32_t offset = (uint32_t)(sub._indexMaterial * NUM_MAX_FRAME_IN_FLIGHT
|
||||
+ _currentFrame);
|
||||
// 绑定材质
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
pipe_layout, 0, 1, &_descriptorSets[offset], 0, nullptr);
|
||||
// 绑定纹理
|
||||
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
pipe_layout, 1, 1, &_descriptorSetsTex[sub._indexMaterial], 0, nullptr);
|
||||
|
||||
//绘制
|
||||
vkCmdDrawIndexed(command_buffer,
|
||||
(uint32_t)(sub._range._countTriangle * 3), 1,
|
||||
(uint32_t)(sub._range._offsetIndex), 0, 0);
|
||||
//绘制
|
||||
vkCmdDrawIndexed(command_buffer,
|
||||
(uint32_t)(sub._range._countTriangle * 3), 1,
|
||||
(uint32_t)(sub._range._offsetIndex), 0, 0);
|
||||
|
||||
++_renderInfo._numDrawCall;
|
||||
int pause = 3;
|
||||
}
|
||||
++_renderInfo._numDrawCall;
|
||||
int pause = 3;
|
||||
}
|
||||
++_renderInfo._numActor;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user