3
$\begingroup$

Blender supports Bézier keyframe interpolation:
Blender Graph Editor showing a channel animated with three keyframes, where the values of the keyframes are along a sloped line, but the middle keyframe has a tangent with a perpendicular slope.

glTF supports "Cubic Spline Interpolation". The documentation on this is insufficient for me to understand exactly what they mean. (Are the in-tangent and out-tangents scalar distances in time? Rotation angles of the tangent?)

Currently (Blender 3.2.1) exporting the above animation to glTF with "Always Sample Animations" unchecked results in a runtime animation that is similar to what we see when we re-import the glTF back into Blender:
Blender's Graph editor showing a channel animated with three keyframes in a line, as above, but now the tangent on the middle keyframe is roughly inline with the slope connecting the three keyframes

Is this a bug in the glTF exporter? If so, I'd be happy to dive in and try to help fix it.

Or, is this a known limitation of the glTF representation of animation? If so, what tangent adjustments in Blender are "glTF-safe", and what tangent adjustments must be avoided?

$\endgroup$

1 Answer 1

3
$\begingroup$

tl;dr

You need to use Blender 3.3 and I believe the curve is "glTF-safe" if the handles are evenly spaced along the X-axis ie. they divide the interval between the keyframes into thirds.

Interval divided into thirds

Details follow.



About how glTF tangents work: The in-tangent and out-tangent in glTF are just the left-hand and right-hand derivatives with respect to time of the curve at the keyframe.

The glTF exporter does indeed export these wrong in 3.2, but a recent PR (merged two days ago) may have fixed this. So first of all you need to use 3.3 alpha from https://builder.blender.org. If all is well, both the value and the derivative should match at the keyframe points.

In glTF, these four data points, the value and the derivative at both keyframes, uniquely determine the curve at all times in an interval. But not so in Blender. For example, look at the below picture of two curves that differ only in the position of the selected handle.

Two curves in Blender

As you can see, despite the slope of the highlighted line being the same, the right one is more "humpy" in the middle. This means even if the values and the derivatives in the glTF are correct at the keyframes, it doesn't follow the rest of the curve is correct. Blender's curve depends on six parameters, not four, so in general conversion to glTF will be lossy. (Basically Blender does cubic interpolation in both X and Y directions, while glTF does only the Y direction.)

Which curves can be exactly converted? Based on a calculation I did a few years ago, a glTF curve exactly converts to a Blender curve where the X coordinates of the handles are 1/3 and 2/3 of the way along the interval, so this form (pictured in the tl;dr) should also convert exactly to a glTF curve. Be warned: this is a theoretical calculation, I have never tested it, it may be wrong :).

PS. This is only one of many, many reasons a Blender curve cannot be exactly converted to a glTF curve naively ie. without sampling. This is why "Always Sample Animations" is on by default. If possible, prefer to leave it on; you will avoid these kinds of problems.

PPS. The glTF importer does not support cubic spline curves at all; it just throws the tangents away, never setting the handles. So don't bother trying to use the importer to check if the exporter is correct.

$\endgroup$
3
  • $\begingroup$ Thank you for this, on multiple fronts. I had no idea the roundtripping didn't work; that's clutch information. I'm working to derive the reverse math now (which could also be used to fix the importer), and your point about 1/3, 2/3 totally lines up with what I'm experimentally seeing. There's likely an accept marker coming your way. $\endgroup$
    – Phrogz
    Jul 15, 2022 at 20:49
  • 1
    $\begingroup$ FWIW, my own experimentation (also not provably correct) confirms that the curves created by the glTF cubic spline interpolation formula can always be represented by a cubic Bézier with handles always placed 1/3 and 2/3 between the keyframes. The answer I link to there provides the formulae to derive the Bézier control points from the glTF tangent values. $\endgroup$
    – Phrogz
    Jul 15, 2022 at 22:08
  • $\begingroup$ Yeah, that's the same formula I got. $\endgroup$
    – scurest
    Jul 16, 2022 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .