| 1305 | } |
| 1306 | |
| 1307 | static void SortSkinBones(Skin* skin) |
| 1308 | { |
| 1309 | uint32_t bones_count = skin->m_Bones.Size(); |
| 1310 | BoneSortInfo* infos = new BoneSortInfo[bones_count]; |
| 1311 | |
| 1312 | uint32_t index_iter = 0; |
| 1313 | for (uint32_t i = 0; i < bones_count; ++i) |
| 1314 | { |
| 1315 | Bone* bone = &skin->m_Bones[i]; |
| 1316 | if (bone->m_ParentIndex == INVALID_INDEX) |
| 1317 | { |
| 1318 | CalcIndicesDepthFirst(infos, bone, &index_iter); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | std::sort(infos, infos + bones_count, BoneInfoSortPred()); |
| 1323 | |
| 1324 | // build the remap array |
| 1325 | InitSize(skin->m_BoneRemap, bones_count, bones_count); |
| 1326 | |
| 1327 | bool indices_differ = false; |
| 1328 | for (uint32_t i = 0; i < bones_count; ++i) |
| 1329 | { |
| 1330 | uint32_t index_old = infos[i].m_OldIndex; |
| 1331 | uint32_t index_new = infos[i].m_Index; |
| 1332 | skin->m_BoneRemap[index_old] = index_new; |
| 1333 | |
| 1334 | indices_differ |= index_old != index_new; |
| 1335 | } |
| 1336 | // If the indices don't differ, then we don't need to update the meshes bone indices either |
| 1337 | if (!indices_differ) |
| 1338 | { |
| 1339 | skin->m_BoneRemap.SetCapacity(0); |
| 1340 | } |
| 1341 | |
| 1342 | // do the remapping, i.e. flattern the hierarchy |
| 1343 | if (!skin->m_BoneRemap.Empty()) |
| 1344 | { |
| 1345 | dmArray<uint32_t> bone_order; |
| 1346 | dmArray<Bone> sorted_bones; |
| 1347 | InitSize(bone_order, bones_count, bones_count); |
| 1348 | InitSize(sorted_bones, bones_count, bones_count); |
| 1349 | |
| 1350 | for (uint32_t i = 0; i < bones_count; ++i) |
| 1351 | { |
| 1352 | bone_order[i] = i; |
| 1353 | Bone& bone = skin->m_Bones[i]; |
| 1354 | bone.m_Index = skin->m_BoneRemap[bone.m_Index]; |
| 1355 | bone.m_ParentIndex = bone.m_ParentIndex != INVALID_INDEX ? skin->m_BoneRemap[bone.m_ParentIndex] : INVALID_INDEX; |
| 1356 | } |
| 1357 | |
| 1358 | for (uint32_t i = 0; i < bones_count; ++i) |
| 1359 | { |
| 1360 | uint32_t new_index = skin->m_BoneRemap[i]; |
| 1361 | CopyBone(&sorted_bones[new_index], &skin->m_Bones[i]); |
| 1362 | } |
| 1363 | |
| 1364 | skin->m_Bones.Swap(sorted_bones); |
no test coverage detected