chore: make gltf dumping and loading work for models without bone weights

This commit is contained in:
Jan 2024-09-03 22:58:12 +02:00
parent 2699de86b6
commit 8d2f5541d3
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
2 changed files with 11 additions and 9 deletions

View File

@ -233,7 +233,7 @@ namespace
static void CalculateBoneBounds(XBoneInfo& info, const unsigned boneIndex, const XModelCommon& common) static void CalculateBoneBounds(XBoneInfo& info, const unsigned boneIndex, const XModelCommon& common)
{ {
if (common.m_vertex_bone_weights.empty()) if (common.m_bone_weight_data.weights.empty())
return; return;
info.bounds[0].x = 0.0f; info.bounds[0].x = 0.0f;
@ -524,7 +524,7 @@ namespace
static void ReorderVerticesByWeightCount(std::vector<size_t>& vertexIndices, XSurface& surface, const XModelCommon& common) static void ReorderVerticesByWeightCount(std::vector<size_t>& vertexIndices, XSurface& surface, const XModelCommon& common)
{ {
if (common.m_vertex_bone_weights.empty()) if (common.m_bone_weight_data.weights.empty())
return; return;
const auto vertexCount = vertexIndices.size(); const auto vertexCount = vertexIndices.size();
@ -609,7 +609,7 @@ namespace
CreateVertex(surface.verts0[vertexIndex], commonVertex); CreateVertex(surface.verts0[vertexIndex], commonVertex);
} }
if (!common.m_vertex_bone_weights.empty()) if (!common.m_bone_weight_data.weights.empty())
{ {
// Since bone weights are sorted by weight count, the last must have the highest weight count // Since bone weights are sorted by weight count, the last must have the highest weight count
const auto hasVertsBlend = common.m_vertex_bone_weights[vertexIndices[vertexIndices.size() - 1]].weightCount > 1; const auto hasVertsBlend = common.m_vertex_bone_weights[vertexIndices[vertexIndices.size() - 1]].weightCount > 1;

View File

@ -119,7 +119,7 @@ namespace
JsonMesh mesh; JsonMesh mesh;
const auto hasBoneWeightData = !xmodel.m_vertex_bone_weights.empty(); const auto hasBoneWeightData = !xmodel.m_bone_weight_data.weights.empty();
auto objectIndex = 0u; auto objectIndex = 0u;
for (const auto& object : xmodel.m_objects) for (const auto& object : xmodel.m_objects)
@ -275,7 +275,9 @@ namespace
for (auto boneIndex = 0u; boneIndex < boneCount; boneIndex++) for (auto boneIndex = 0u; boneIndex < boneCount; boneIndex++)
skin.joints.emplace_back(boneIndex + m_first_bone_node); skin.joints.emplace_back(boneIndex + m_first_bone_node);
if (!xmodel.m_bone_weight_data.weights.empty())
skin.inverseBindMatrices = m_inverse_bind_matrices_accessor; skin.inverseBindMatrices = m_inverse_bind_matrices_accessor;
skin.skeleton = m_first_bone_node; skin.skeleton = m_first_bone_node;
gltf.skins->emplace_back(std::move(skin)); gltf.skins->emplace_back(std::move(skin));
@ -313,7 +315,7 @@ namespace
m_vertex_buffer_view = gltf.bufferViews->size(); m_vertex_buffer_view = gltf.bufferViews->size();
gltf.bufferViews->emplace_back(vertexBufferView); gltf.bufferViews->emplace_back(vertexBufferView);
if (!xmodel.m_vertex_bone_weights.empty()) if (!xmodel.m_bone_weight_data.weights.empty())
{ {
JsonBufferView jointsBufferView; JsonBufferView jointsBufferView;
jointsBufferView.buffer = 0u; jointsBufferView.buffer = 0u;
@ -391,7 +393,7 @@ namespace
m_uv_accessor = gltf.accessors->size(); m_uv_accessor = gltf.accessors->size();
gltf.accessors->emplace_back(uvAccessor); gltf.accessors->emplace_back(uvAccessor);
if (!xmodel.m_vertex_bone_weights.empty()) if (!xmodel.m_bone_weight_data.weights.empty())
{ {
JsonAccessor jointsAccessor; JsonAccessor jointsAccessor;
jointsAccessor.bufferView = m_joints_buffer_view; jointsAccessor.bufferView = m_joints_buffer_view;
@ -488,7 +490,7 @@ namespace
gltf.accessors.value()[m_position_accessor].max = std::vector({maxPosition[0], maxPosition[1], maxPosition[2]}); gltf.accessors.value()[m_position_accessor].max = std::vector({maxPosition[0], maxPosition[1], maxPosition[2]});
} }
if (!xmodel.m_vertex_bone_weights.empty()) if (!xmodel.m_bone_weight_data.weights.empty())
{ {
assert(xmodel.m_vertex_bone_weights.size() == xmodel.m_vertices.size()); assert(xmodel.m_vertex_bone_weights.size() == xmodel.m_vertices.size());
@ -572,7 +574,7 @@ namespace
result += xmodel.m_vertices.size() * sizeof(GltfVertex); result += xmodel.m_vertices.size() * sizeof(GltfVertex);
if (!xmodel.m_vertex_bone_weights.empty()) if (!xmodel.m_bone_weight_data.weights.empty())
{ {
// Joints and weights // Joints and weights
result += xmodel.m_vertices.size() * 4u * (sizeof(uint8_t) + sizeof(float)); result += xmodel.m_vertices.size() * 4u * (sizeof(uint8_t) + sizeof(float));