Skip to content

Commit

Permalink
Add animation channels and skins to LOD merging (microsoft#12)
Browse files Browse the repository at this point in the history
* Add animation channels and skins to merged object list. This assumes that each LOD document has the same number and order of animations that should be cumulative in the output document.
  • Loading branch information
najadojo authored and robertos committed Feb 8, 2018
1 parent 9cc0960 commit 2c1d159
Showing 1 changed file with 69 additions and 12 deletions.
81 changes: 69 additions & 12 deletions glTF-Toolkit/src/GLTFLODUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,16 @@ namespace
mesh.name += nodeLodLabel;
AddIndexOffset(mesh.id, meshOffset);

for (auto Itr = mesh.primitives.begin(); Itr != mesh.primitives.end(); Itr++)
for (auto &primitive : mesh.primitives)
{
AddIndexOffset(Itr->positionsAccessorId, accessorOffset);
AddIndexOffset(Itr->normalsAccessorId, accessorOffset);
AddIndexOffset(Itr->indicesAccessorId, accessorOffset);
AddIndexOffset(Itr->uv0AccessorId, accessorOffset);
AddIndexOffset(Itr->uv1AccessorId, accessorOffset);
AddIndexOffset(Itr->color0AccessorId, accessorOffset);

AddIndexOffset(Itr->materialId, materialOffset);
AddIndexOffset(primitive.positionsAccessorId, accessorOffset);
AddIndexOffset(primitive.normalsAccessorId, accessorOffset);
AddIndexOffset(primitive.indicesAccessorId, accessorOffset);
AddIndexOffset(primitive.uv0AccessorId, accessorOffset);
AddIndexOffset(primitive.uv1AccessorId, accessorOffset);
AddIndexOffset(primitive.color0AccessorId, accessorOffset);

AddIndexOffset(primitive.materialId, materialOffset);
}

gltfLod.meshes.Append(std::move(mesh));
Expand All @@ -326,6 +326,8 @@ namespace

// Nodes depend upon Nodes and Meshes
size_t nodeOffset = gltfLod.nodes.Size();
// Skins depend upon Nodes
size_t skinOffset = gltfLod.skins.Size();
{
auto nodes = lod.nodes.Elements();
for (auto node : nodes)
Expand All @@ -335,14 +337,69 @@ namespace
node.name += nodeLodLabel;
AddIndexOffset(node.id, nodeOffset);
AddIndexOffset(node.meshId, meshOffset);
if (!node.skinId.empty())
{
AddIndexOffset(node.skinId, skinOffset);
}

for (auto Itr = node.children.begin(); Itr != node.children.end(); Itr++)
for (auto &child : node.children)
{
AddIndexOffset(*Itr, nodeOffset);
AddIndexOffset(child, nodeOffset);
}

gltfLod.nodes.Append(std::move(node));
};
}
}

{
auto skins = lod.skins.Elements();
for (auto skin : skins)
{
// post-fix with lod level indication;
// no functional reason other than making it easier to natively read gltf files with lods
skin.name += nodeLodLabel;
AddIndexOffset(skin.id, skinOffset);
AddIndexOffset(skin.skeletonId, nodeOffset);
AddIndexOffset(skin.inverseBindMatricesAccessorId, accessorOffset);

for (auto &jointId : skin.jointIds)
{
AddIndexOffset(jointId, nodeOffset);
}

gltfLod.skins.Append(std::move(skin));
}
}

// Animation channels depend upon Nodes and Accessors
{
for (size_t animationIndex = 0; animationIndex < gltfLod.animations.Size(); animationIndex++)
{
const auto &baseAnimation = gltfLod.animations[animationIndex];
Animation newAnimation(baseAnimation);
const auto &lodAnimation = lod.animations[animationIndex];

size_t samplerOffset = baseAnimation.samplers.Size();
for (const auto &sampler : lodAnimation.samplers.Elements())
{
AnimationSampler newSampler(sampler);
AddIndexOffset(newSampler.id, samplerOffset);
AddIndexOffset(newSampler.inputAccessorId, accessorOffset);
AddIndexOffset(newSampler.outputAccessorId, accessorOffset);
newAnimation.samplers.Append(std::move(newSampler));
}

size_t channelsOffset = baseAnimation.channels.size();
for (auto channel : lodAnimation.channels)
{
AddIndexOffset(channel.id, channelsOffset);
AddIndexOffset(channel.target.nodeId, nodeOffset);
AddIndexOffset(channel.samplerId, samplerOffset);

newAnimation.channels.push_back(std::move(channel));
}
gltfLod.animations.Replace(newAnimation);
}
}

// update the primary GLTF root nodes lod extension to reference the new lod root node
Expand Down

0 comments on commit 2c1d159

Please sign in to comment.