- Modifié
[Libgdx] Scaling a skeleton with 'dont inherit' bones
Hello.
Let me just describe the situation real quick:
My animator disables scale inheritance on some bones to make it easier to animate, for example, the wings of a bat.
However, in my game I need many bats of different sizes for variety, so I scale the root bone accordingly from code. However, because my animator has disabled inheritance, those particular bones dont get scaled.
I need a way to force those bones to accept the root bone scale, I guess.
I tried deliberately changing the transform mode of those bones, but the result is super weird.
Is there anything we can do here?
I guess one solution is to load different instances of the skeleton using the different scales but that will take up memory (and limit the range of scales I can use)
Thanks!
You could set the scale on the root Bone, then also set the scale on any bone which has BoneData transformMode
set to noScale
or noScaleOrReflection
. You should multiple the bone's current scale with your scale, in case the animator has changed or keyed that bone's local scale.
You would need to make these changes to the bones every frame, else resetting to the setup pose would lose your changes. This is because you are changing a Skeleton instance's bones (the current pose) and not the SkeletonData bones (the setup pose). Also if a bone's local scale is keyed in animations, you'll want to apply your scale after the animation is applied (which again is every frame). The only alternative is to load multiple SkeletonDatas and make the changes there (or use the loader scale, as you mentioned), however setting a few bone scales each frame is not a big deal.
In Unity, all renders go through a linear transformation (based on its parent Transform) so that the resulting "skeleton space" render is converted to world space, which also allows it to be positioned, scaled and rotated.
Based on that idea, an option is:
You could put an extra operation to the render. The transformation can determine the position, rotation and scale of the skeleton in your game world. This post-render transformation would be agnostic to bone settings.
If you only needed to apply position and scale, this is as simple as multiplying all the positions by a scale factor, then adding the position.
If you are going this route, you would make sure the skeleton position is not set through skeleton.x and .y, or rootBone.x and .y but through that transformation.
And if you needed logical things like setting or getting bone positions, they would also go through transform and inverse transform operations.
I think this would require a slightly modified version of the draw method though.
Tried Nate's method first and it worked great! Thanks both of you.