On performace : Do constant [stepping] interpolation getting evaluated or no-op'd?

For a moving 3D object, suppose an “empty” type game object.

If i have X amount of keyframes, all with constant interpolation [marked as flat and both handles as constant], do they still get evaluated per frame as if they were linear interpolation or any other type of interpolation, or they just get a no-operation and you get to save performance on it?

On a technical level : Does unity run some magical mathematical function when it see constant interpolation ? or does it just return (ends) the function without touching the keyframe values?

This is a question in theory, not pre-mature optimization, something that i could tell if i had unity source code, but i don’t.

Well, we simply don’t know the exact inner workings of the engine since it’s proprietary close source code. However the Animator or the Animation component will set the values each frame of the animation. You can simply check that yourself by manually moving the object during a constant period of an animation.

So Unity most likely does not do any unreasonable optimisations it actually can’t justify. Also note that animation curves actually are bezier splines. It may be possible that with constant or linear keyframe that there are some early exits in the calculation. However this is even below micro optimisation since calculating a point on a bezier spline is pretty trivial.

In Unity you can update a Mesh with 1 million vertices once every frame (at least on a modern device) and it would still run fine. How many curves does your animation have? Even human skeleton animations usually only have a couple hundred.

Where do you think we as the community would get that information from? Do you think we have somehow access to the native code part? Maybe some Unity Staff could answer that. However it’s an implementation detail and Unity usually doesn’t make any concessions about certain internal details, mainly because they want to keep the freedom to change it in the future.

You can run some tests and benchmarks and draw conclusions from them. Though this only gives you probabilities and no 100% proof.

I don’t quite get what you’re asking here and I don’t think you know what you’re asking either ^^. How would Unity “see” that a keyframe represents a constant value without “touching” the value? All those special handling you think about would probably add more overhead than it would save. When the weight of a tangent is 0 the curve will become linear. When the tangent is infinity it’s a constant keyframe. A weight of 1/3 is the default weight for the usual curve but a keyframe could have any weight nowadays (in the past this was kinda fix).

About 10 years ago I answered a question about how animation curves actually work behind the scenes. It’s not entirely up-to-date since KeyFrames now have a weight for the tangents. But the overall calculation of the bezier curve is still the same. So the Evaluate method of the AnimationCurve figures out which segment the current time falls into and then simply samples the bezier curve of that segment at that given time. All just a bunch of multiplications and additions.

What exactly is the difference? Why do you want to know how it works internally if not for optimisation reasons?

I will allow myself to bump this after 2 months as i REFUSE TO BELIEVE that nobody here knows how one of the most popular engines work.

as for how it can see a keyframe without touching the value…

I assume each keyframe has a flag that tells if what interpolation type is expected, i mean after all, we can set it in the curve editor so it must be stored in some keyframe structure, That can allow special handling if we know the type of interpolation that is set.

If it is a b.curve, No, we can’t tell what the value will be without evaluating it first.

as for how many curves we would use on average : let’s say 700 bones rig, animation frame rate at 24 frames a second , animation is baked so each bone gets a keyframe every frame
without cleanup:

that’s 3 translation channels
4 rotation channels for quats
3 scaling channels

so that’s 7700 curves.
and A LOT OF KEYFRAMES to handle per second.