-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add sample_clamped method to animation curves, returning Boxed values #16395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
This is fairly close to what I had imagined in this direction (see also #15664 ); another idea would be to yield a fn downcast_curve<T>(self) -> Option<Box<dyn Curve<T>>> {
// yield a boxed version of the underlying curve if `T` matches the curve type, otherwise None
} I think this is a bit more reusable, but the use-cases might be slightly different, since the dependency on the curve type is a bit different. The main upside with something like this would be that you can do other stuff with the curve API instead of only having the output value. (The main downside in my view is that implementing this requires unsafe code, since this is effectively a "custom |
I like that, but maybe there's value in having both. I think only exposing a
What do you think? |
That makes sense. I have been kind of wondering if the right direction for the animation system long-term would be to tie every animation curve to a specific I don't think having both is unreasonable :)
Yeah, this is another reason to want to tie things to
Every (For some background, the only reason that |
I don't know what the reasons were for keeping the Transform and weight curves separate tbh (performance maybe?), but I agree that having a more "natural" relationship between the (sorry for the long time between replies btw, hard to find time after day job with end of year deadlines approaching) |
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).