Add sample_clamped method to animation curves, returning Boxed values #16395
+123
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Currently the evaluation of
AnimationCurve
s is coupled to the Bevy animation system. This PR provides an integration point for plugins that would like to reuse the animation loading (e.g. from GLTF) and sampling code but implement a custom animation composition system (the use case I have in mind is bevy_animation_graph).Solution
The
AnimationCurve
trait provides asample_clamped
method, which returns a boxed version of the result of sampling the underlying curve:Each of the provided implementations of
AnimationCurve
return a custom sampled type, e.g.:This fits with the "open polymorphism" approach that the rest of the rest of the animation curves code is using. Users can then check the
TypeId
of the returned type to determine what to do with the sampled value.Alternative solutions
Closed polymorphism
Provide an enum value as the result of
AnimationCurve::sample_clamped
:However this would be at odds with the current implementation of
AnimationCurve
, where users can implement their ownAnimationCurve
types.Testing
AnimatableCurve
sampling.WeightsCurve
sampling.bevy_animation_graph
.Showcase
bevy_animation_graph example
bevy_animation_graph evaluates animation curves in "Clip" nodes, and stores the
sampled values for all targets in a
Pose
struct which is made available toother nodes as an output.
Currently evaluating animation curves in
bevy_animation_graph
involves abusingthe
Reflect
API to access private fields in the curve evaluators:With this PR, we can instead do the following:
Furthermore, the API in this PR opens the door to adding support for
AnimatableCurve
s, whereas before this was not possible (or at least I couldnot find a way).