Skip to content

Commit

Permalink
Merge pull request #152 from blaztinn/additive_rotation_fix
Browse files Browse the repository at this point in the history
Additive rotation fix
  • Loading branch information
guillaumeblanc authored Feb 17, 2024
2 parents 0213522 + ef6654f commit dd2c662
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/animation/offline/additive_animation_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ math::Float3 MakeDeltaTranslation(const math::Float3& _reference,

math::Quaternion MakeDeltaRotation(const math::Quaternion& _reference,
const math::Quaternion& _value) {
return _value * Conjugate(_reference);
return Conjugate(_reference) * _value;
}

math::Float3 MakeDeltaScale(const math::Float3& _reference,
Expand Down
4 changes: 2 additions & 2 deletions src/animation/runtime/blending_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace {
const math::SoaQuaternion interp_quat = { \
rotation.x * _simd_weight, rotation.y * _simd_weight, \
rotation.z * _simd_weight, (rotation.w - one) * _simd_weight + one}; \
_out.rotation = NormalizeEst(interp_quat) * _out.rotation; \
_out.rotation = _out.rotation * NormalizeEst(interp_quat); \
_out.scale = \
_out.scale * (one_minus_weight_f3 + (_in.scale * _simd_weight)); \
} while (void(0), 0)
Expand All @@ -147,7 +147,7 @@ namespace {
const math::SoaQuaternion interp_quat = { \
rotation.x * _simd_weight, rotation.y * _simd_weight, \
rotation.z * _simd_weight, (rotation.w - one) * _simd_weight + one}; \
_out.rotation = Conjugate(NormalizeEst(interp_quat)) * _out.rotation; \
_out.rotation = _out.rotation * Conjugate(NormalizeEst(interp_quat)); \
const math::SoaFloat3 rcp_scale = { \
math::RcpEst(math::MAdd(_in.scale.x, _simd_weight, one_minus_weight)), \
math::RcpEst(math::MAdd(_in.scale.y, _simd_weight, one_minus_weight)), \
Expand Down
53 changes: 53 additions & 0 deletions test/animation/runtime/blending_job_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,59 @@ TEST(AdditiveWeight, BlendingJob) {
}
}

TEST(AdditiveRotationFromNonIdentity, BlendingJob) {
using ozz::math::GetW;
using ozz::math::GetX;
using ozz::math::GetY;
using ozz::math::GetZ;

const ozz::math::SoaTransform identity = ozz::math::SoaTransform::identity();

// Initialize rest pose.
ozz::math::SoaTransform rest_poses[1] = {identity};
rest_poses[0].rotation = ozz::math::SoaQuaternion::Load(
ozz::math::simd_float4::Load(0.f, .70710677f, .382683432f, 0.f),
ozz::math::simd_float4::Load(0.f, 0.f, 0.f, .70710677f),
ozz::math::simd_float4::Load(0.f, 0.f, 0.f, 0.f),
ozz::math::simd_float4::Load(1.f, .70710677f, .9238795f, -.70710677f));

// Initialize inputs.
ozz::math::SoaQuaternion input_half_rotation = ozz::math::SoaQuaternion::Load(
ozz::math::simd_float4::Load(.70710677f, 0.f, 0.f, .382683432f),
ozz::math::simd_float4::Load(0.f, 0.f, .70710677f, 0.f),
ozz::math::simd_float4::Load(0.f, 0.f, 0.f, 0.f),
ozz::math::simd_float4::Load(.70710677f, 1.f, -.70710677f, .9238795f));
ozz::math::SoaTransform input_transforms[1] = {identity};
input_transforms[0].rotation = input_half_rotation * input_half_rotation;

ozz::math::SoaQuaternion expected_rotation =
rest_poses[0].rotation *
Conjugate(input_half_rotation * input_half_rotation);
constexpr float weights[]{-1.0f, -0.5f, 0.0f, 0.5f, 1.0f};
for (float weight : weights) {
BlendingJob::Layer layers[1];
layers[0].transform = input_transforms;

ozz::math::SoaTransform output_transforms[1];

BlendingJob job;
job.additive_layers = layers;
job.rest_pose = rest_poses;
job.output = output_transforms;

layers[0].weight = weight;
EXPECT_TRUE(job.Run());

ozz::math::SimdFloat4 dot =
ozz::math::Dot(expected_rotation, output_transforms[0].rotation);
ozz::math::SimdFloat4 distance =
ozz::math::simd_float4::one() - (dot * dot);
EXPECT_SIMDFLOAT_EQ_EST(distance, 0.0f, 0.0f, 0.0f, 0.0f);

expected_rotation = expected_rotation * input_half_rotation;
}
}

TEST(AdditiveJointWeight, BlendingJob) {
const ozz::math::SoaTransform identity = ozz::math::SoaTransform::identity();

Expand Down

0 comments on commit dd2c662

Please sign in to comment.