Skip to content

Commit 2a1ac36

Browse files
Radient: update animation import for packed glTF outputs
1 parent 6621b17 commit 2a1ac36

2 files changed

Lines changed: 45 additions & 32 deletions

File tree

‎Radient/src/Import/RadientGLTFConverter.cpp‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ RADIENT_STATUS InitializeAnimationCurve(const GLTF::AnimationSampler& Sample
459459
Uint32 AnimationIndex,
460460
Uint32 SamplerIndex,
461461
const char* CurveName,
462+
Uint32 ComponentCount,
462463
AnimationCurveStorage<ValueType>& Curve,
463464
ConvertValueType&& ConvertValue)
464465
{
@@ -484,8 +485,17 @@ RADIENT_STATUS InitializeAnimationCurve(const GLTF::AnimationSampler& Sample
484485
}
485486

486487
const size_t ValuesPerKey = Interpolation == RADIENT_ANIMATION_INTERPOLATION_CUBIC_SPLINE ? 3u : 1u;
487-
if (Sampler.Inputs.size() > std::numeric_limits<size_t>::max() / ValuesPerKey ||
488-
Sampler.OutputsVec4.size() != Sampler.Inputs.size() * ValuesPerKey)
488+
if (Sampler.Inputs.size() > std::numeric_limits<size_t>::max() / ValuesPerKey)
489+
{
490+
LOG_ERROR_MESSAGE("GLTF animation ", AnimationIndex, " sampler ", SamplerIndex,
491+
" has too many ", CurveName, " values");
492+
return RADIENT_STATUS_INVALID_DATA;
493+
}
494+
495+
const size_t ValueCount = Sampler.Inputs.size() * ValuesPerKey;
496+
if (Sampler.OutputComponentCount != ComponentCount ||
497+
Sampler.Outputs.size() % ComponentCount != 0 ||
498+
Sampler.Outputs.size() / ComponentCount != ValueCount)
489499
{
490500
LOG_ERROR_MESSAGE("GLTF animation ", AnimationIndex, " sampler ", SamplerIndex,
491501
" has an invalid number of ", CurveName, " values");
@@ -497,9 +507,9 @@ RADIENT_STATUS InitializeAnimationCurve(const GLTF::AnimationSampler& Sample
497507
std::transform(Sampler.Inputs.begin(), Sampler.Inputs.end(), Curve.Times.begin(),
498508
[AnimationStart](Float32 Time) { return Time - AnimationStart; });
499509

500-
Curve.Values.reserve(Sampler.OutputsVec4.size());
501-
for (const float4& Value : Sampler.OutputsVec4)
502-
Curve.Values.push_back(ConvertValue(Value));
510+
Curve.Values.reserve(ValueCount);
511+
for (size_t ValueIndex = 0; ValueIndex < ValueCount; ++ValueIndex)
512+
Curve.Values.push_back(ConvertValue(Sampler.GetOutputElement(ValueIndex)));
503513

504514
return RADIENT_STATUS_OK;
505515
}
@@ -553,22 +563,25 @@ RADIENT_STATUS CreateImportedSkeletonAnimation(const GLTF::Model&
553563
case GLTF::AnimationChannel::PATH_TYPE::TRANSLATION:
554564
Status = InitializeAnimationCurve(
555565
Sampler, AnimationStart, AnimationIndex, Channel.SamplerIndex, "translation",
566+
3,
556567
Tracks[JointIndex].Translation,
557-
[](const float4& Value) { return RadientFloat3{Value.x, Value.y, Value.z}; });
568+
[](const float* pValue) { return RadientFloat3{pValue[0], pValue[1], pValue[2]}; });
558569
break;
559570

560571
case GLTF::AnimationChannel::PATH_TYPE::ROTATION:
561572
Status = InitializeAnimationCurve(
562573
Sampler, AnimationStart, AnimationIndex, Channel.SamplerIndex, "rotation",
574+
4,
563575
Tracks[JointIndex].Rotation,
564-
[](const float4& Value) { return RadientQuaternion{Value.x, Value.y, Value.z, Value.w}; });
576+
[](const float* pValue) { return RadientQuaternion{pValue[0], pValue[1], pValue[2], pValue[3]}; });
565577
break;
566578

567579
case GLTF::AnimationChannel::PATH_TYPE::SCALE:
568580
Status = InitializeAnimationCurve(
569581
Sampler, AnimationStart, AnimationIndex, Channel.SamplerIndex, "scale",
582+
3,
570583
Tracks[JointIndex].Scale,
571-
[](const float4& Value) { return RadientFloat3{Value.x, Value.y, Value.z}; });
584+
[](const float* pValue) { return RadientFloat3{pValue[0], pValue[1], pValue[2]}; });
572585
break;
573586

574587
default:

‎Tests/RadientTest/src/RadientGLTFConverterTest.cpp‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,29 +1410,29 @@ TEST(RadientGLTFConverterTest, ExtractSceneGraphCreatesSkinWithCompleteJointHier
14101410
Animation.Name = "Joint motion";
14111411

14121412
Animation.Samplers.emplace_back(GLTF::AnimationSampler::INTERPOLATION_TYPE::LINEAR);
1413-
Animation.Samplers.back().Inputs = {2.f, 4.f};
1414-
Animation.Samplers.back().OutputsVec4 = {
1415-
{0.f, 0.f, 3.f, 0.f},
1416-
{2.f, 0.f, 3.f, 0.f},
1417-
};
1413+
Animation.Samplers.back().Inputs = {2.f, 4.f};
1414+
Animation.Samplers.back().OutputComponentCount = 3;
1415+
Animation.Samplers.back().Outputs = {
1416+
0.f, 0.f, 3.f,
1417+
2.f, 0.f, 3.f};
14181418

14191419
Animation.Samplers.emplace_back(GLTF::AnimationSampler::INTERPOLATION_TYPE::CUBICSPLINE);
1420-
Animation.Samplers.back().Inputs = {2.f, 4.f};
1421-
Animation.Samplers.back().OutputsVec4 = {
1422-
{0.f, 0.f, 0.f, 0.f},
1423-
{0.f, 0.f, 0.f, 1.f},
1424-
{0.f, 0.f, 0.f, 0.f},
1425-
{0.f, 0.f, 0.f, 0.f},
1426-
{0.f, 0.f, 1.f, 0.f},
1427-
{0.f, 0.f, 0.f, 0.f},
1428-
};
1420+
Animation.Samplers.back().Inputs = {2.f, 4.f};
1421+
Animation.Samplers.back().OutputComponentCount = 4;
1422+
Animation.Samplers.back().Outputs = {
1423+
0.f, 0.f, 0.f, 0.f,
1424+
0.f, 0.f, 0.f, 1.f,
1425+
0.f, 0.f, 0.f, 0.f,
1426+
0.f, 0.f, 0.f, 0.f,
1427+
0.f, 0.f, 1.f, 0.f,
1428+
0.f, 0.f, 0.f, 0.f};
14291429

14301430
Animation.Samplers.emplace_back(GLTF::AnimationSampler::INTERPOLATION_TYPE::STEP);
1431-
Animation.Samplers.back().Inputs = {2.f, 4.f};
1432-
Animation.Samplers.back().OutputsVec4 = {
1433-
{2.f, 3.f, 4.f, 0.f},
1434-
{5.f, 6.f, 7.f, 0.f},
1435-
};
1431+
Animation.Samplers.back().Inputs = {2.f, 4.f};
1432+
Animation.Samplers.back().OutputComponentCount = 3;
1433+
Animation.Samplers.back().Outputs = {
1434+
2.f, 3.f, 4.f,
1435+
5.f, 6.f, 7.f};
14361436

14371437
Animation.Channels.emplace_back(GLTF::AnimationChannel::PATH_TYPE::TRANSLATION, &Model.Nodes[2], 0);
14381438
Animation.Channels.emplace_back(GLTF::AnimationChannel::PATH_TYPE::ROTATION, &Model.Nodes[3], 1);
@@ -1524,11 +1524,11 @@ TEST(RadientGLTFConverterTest, OneSourceAnimationTargetsEveryAffectedSkeleton)
15241524
GLTF::Animation& Animation = Model.Animations[0];
15251525
Animation.Name = "Shared motion";
15261526
Animation.Samplers.emplace_back(GLTF::AnimationSampler::INTERPOLATION_TYPE::LINEAR);
1527-
Animation.Samplers[0].Inputs = {0.f, 1.f};
1528-
Animation.Samplers[0].OutputsVec4 = {
1529-
{0.f, 0.f, 0.f, 0.f},
1530-
{1.f, 0.f, 0.f, 0.f},
1531-
};
1527+
Animation.Samplers[0].Inputs = {0.f, 1.f};
1528+
Animation.Samplers[0].OutputComponentCount = 3;
1529+
Animation.Samplers[0].Outputs = {
1530+
0.f, 0.f, 0.f,
1531+
1.f, 0.f, 0.f};
15321532
Animation.Channels.emplace_back(GLTF::AnimationChannel::PATH_TYPE::TRANSLATION, &Model.Nodes[1], 0);
15331533

15341534
RadientImport::ImportedDocument Scene;

0 commit comments

Comments
 (0)