KybernetikGames / animancer

Documentation for the Animancer Unity Plugin.
65 stars 8 forks source link

BoneTransform.SetLocalPosition not actually applied #214

Closed TigerHix closed 2 years ago

TigerHix commented 2 years ago

I am modifying the Lean animation job example to allow passing bone position offsets into the job, like this:

public struct Job : IAnimationJob {

    public NativeArray<TransformStreamHandle> Bones;

    public NativeArray<Vector3> AdditionalBonePositionOffsets;

    public void ProcessAnimation(AnimationStream stream) {
        for (var i = 0; i < (int)HumanBodyBones.LastBone; i++) {
            var boneTransform = Bones[i];
            if (boneTransform.IsValid(stream)) {
                var offsetPosition = boneTransform.GetLocalPosition(stream) + AdditionalBonePositionOffsets[i];

                Debug.Log($"New local position: {offsetPosition}");

                boneTransform.SetLocalPosition(stream, offsetPosition);
            }
        }
    }

}

Then I update AdditionalBonePositionOffsets on every frame similar to the Lean example.

Then I am seeing from console that the local position of the bone transform is indeed updated:

New local position: (0.00, 50.04, 0.02)

However, the actual transform is not (I am not playing any animation, so I am not even sure why aren't those (0,0,0)):

image

Interestingly, I tried implementing bone rotation offsets using very similar code, and it worked! The bones were able to, well, rotate with an offset. But why positions don't work?

In fact, I tried replacing boneTransform.SetLocalPosition(stream, offsetPosition)

with

boneTransform.SetPosition(stream, new Vector3(30,40,50));

And only the Hips bone position was set to (30, 40, 50) - all other bone transforms were not.

Would this be an Animancer or Unity limitation, or am I doing something wrong here?

Thanks!

KybernetikGames commented 2 years ago

Humanoid Rigs evidently don't support position animations except on the root for Root Motion. I just tried it on the Generic Rig in the Two Bone IK example and it worked, but I got the same result as you on a Humanoid.

TigerHix commented 2 years ago

@KybernetikGames Thanks. It seems indeed that even if I manually set the position in the editor (by turning off Animancer first), immediately after I turn on Animancer (and the Playable graph starts flowing) the position was reset.

However, one thing I found extremely strange is that the bone position can be set to some values. For instance, if you set every bone position to (0, 0, 0), the character will turn into a weird pose. So boneTransform.SetLocalPosition/boneTransform.SetPosition did affect something, but it's not what we would expect.

I will experiment with it a little bit more.