Reorder nodes to follow skin node order to eliminate one layer of indirection in indices

This commit is contained in:
Xiao Qi 2023-03-05 04:17:54 +08:00
parent 13fab61332
commit 810c1e4c08
1 changed files with 39 additions and 22 deletions

View File

@ -268,43 +268,60 @@ namespace SH_COMP
if (asset.anims.empty())
return;
auto& rig = asset.rig;
auto& header = rig.header;
for (auto const& node : data.nodes)
if (data.skins.size() == 0)
{
if (node.mesh > -1)
continue;
std::vector<IndexType> intermediate(node.children.begin(), node.children.end());
rig.nodes.emplace_back(
node.name,
static_cast<std::vector<IndexType> const&>(intermediate),
node.rotation,
node.translation,
node.matrix
//node.weights
);
header.charCounts.emplace_back(node.name.size());
std::cout << "[Model Compiler] Unable to load rigs without skin, aborting";
return;
}
for (auto const& skin : data.skins)
{
auto& rig = asset.rig;
auto& header = rig.header;
auto const& skin = data.skins[0];
auto const jointsCount {skin.joints.size()};
auto const& joints = skin.joints;
std::vector<SHMat4> inverseBindMatrices;
FetchData(skin.inverseBindMatrices, inverseBindMatrices);
std::vector<IndexType> joints(skin.joints.begin(), skin.joints.end());
auto& nodes{ rig.nodes };
auto matrix{ inverseBindMatrices.begin() };
for (auto const& joint : joints)
std::vector<NodeAsset> nodesOrdered;
nodesOrdered.reserve(jointsCount);
auto& nodes{ data.nodes };
for (auto i{0}; i < jointsCount; ++i)
{
nodes[joint].inverseBindMatrix = *(matrix++);
auto const& node{nodes[joints[i]]};
std::vector<IndexType> intermediate(node.children.size());
std::ranges::transform(
node.children,
intermediate.begin(),
[joints, jointsCount](auto const& index)->IndexType
{
for (IndexType i{0}; i < jointsCount; ++i)
{
if (joints[i] == index)
return i;
}
}
);
nodesOrdered.emplace_back(
node.name,
static_cast<std::vector<IndexType> const&>(intermediate),
node.rotation,
node.scale,
node.translation,
node.matrix,
inverseBindMatrices[i]
);
nodesOrdered[i].inverseBindMatrix = inverseBindMatrices[i];
header.charCounts.emplace_back(node.name.size());
}
rig.nodes = std::move(nodesOrdered);
//Build header
header.startNode = data.skins[0].joints[0];
header.startNode = 0;
header.nodeCount = rig.nodes.size();
}
}