diff --git a/dl/render/dl_model.h b/dl/render/dl_model.h index 26774bb3..f51701bb 100644 --- a/dl/render/dl_model.h +++ b/dl/render/dl_model.h @@ -44,6 +44,7 @@ private: std::vector _treeNodeParent; std::vector _allAnimation; // 动画 std::vector _allSkin; // 骨骼 + std::vector _allSub; // 网格 std::vector _mulInstance; // 实例(每个actor有一个) }; diff --git a/dl/render/dl_model_data.cpp b/dl/render/dl_model_data.cpp index 581c899b..cc7e1e56 100644 --- a/dl/render/dl_model_data.cpp +++ b/dl/render/dl_model_data.cpp @@ -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); } diff --git a/dl/render/dl_model_data.h b/dl/render/dl_model_data.h index d877c147..82c3592d 100644 --- a/dl/render/dl_model_data.h +++ b/dl/render/dl_model_data.h @@ -116,7 +116,6 @@ struct ModelNodeRt int _indexSkin; // 整个骨骼是在一个节点上 int _indexParent; // 父节点索引 ModelNodeTransform _trans2Parent; // 到父节点的变换(具有初始值,动画只改变srt) - std::vector _allSub; bool _bNeedUpdateMatrixWorld; bool _bNeedUpdateMatrixLocal; @@ -230,6 +229,7 @@ struct ModelData std::vector _allAnimation; // 所有动画 std::vector _allSkin; // 所有蒙皮 std::vector _allMaterial; // 所有材质 + std::vector _allSub; // 所有网格 std::vector _allIndex; // 索引 std::vector _allVertex; // 顶点 std::vector _allVertexSkin; // 顶点 diff --git a/dl/render/dl_model_loader.cpp b/dl/render/dl_model_loader.cpp index f6cedb8f..b53a6b47 100644 --- a/dl/render/dl_model_loader.cpp +++ b/dl/render/dl_model_loader.cpp @@ -145,7 +145,7 @@ std::vector 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 ModelLoader::_load_gltf( _gltf_load_material(model, res_name, model_data._allMaterial); _gltf_load_scene(model, model_data); - //子网格合并 - //if (false) - //{//合并网格连续,且材质相同的项 - // std::vector 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 map_pri; + for (ModelNodeRtSub& iter : data._allSub) + { + map_pri[iter._range._offsetIndex] = &iter; + } + // 合并网格连续,且材质相同的项 + std::vector 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, diff --git a/dl/render/dl_model_object.cpp b/dl/render/dl_model_object.cpp index 1f514e4c..57de2de5 100644 --- a/dl/render/dl_model_object.cpp +++ b/dl/render/dl_model_object.cpp @@ -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); } diff --git a/dl/render/dl_model_object.h b/dl/render/dl_model_object.h index 2e49ef89..43252196 100644 --- a/dl/render/dl_model_object.h +++ b/dl/render/dl_model_object.h @@ -99,6 +99,7 @@ private: std::vector _allMaterial; std::vector _allAnimation; std::vector _allSkin; + std::vector _allSub; }; diff --git a/dl/vk/dl_vk.cpp b/dl/vk/dl_vk.cpp index 86242108..cc34eff0 100644 --- a/dl/vk/dl_vk.cpp +++ b/dl/vk/dl_vk.cpp @@ -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; }