如何解析glTF的animations.samplers.input?对官方示例存疑
animations.samplers.input in glTF's "Animated Triangle" Example Hey there, let's unpack this confusion around animations.samplers.input using the Animated Triangle example— I remember struggling with this exact link between the spec and raw binary data when I first started with glTF!
First, let's recap the spec requirement clearly:
规范中对
animations.samplers.input属性的说明:包含关键帧输入值(如时间)的accessor的索引。该accessor的componentType必须为FLOAT,值以秒为单位表示时间,且满足time[0] ≥ 0.0、严格递增(即time[n+1] > time[n])。
Let's tie this to the Animated Triangle example
Here's how this translates to the actual files in that example:
Find the sampler in the glTF JSON
In the animation section of the JSON, you'll see asamplersarray entry where theinputfield is an integer— that's the index of the accessor holding the time values.Look up the accessor
The accessor with that index will have:componentType: 5126(this is the glTF code for 32-bit FLOAT)type: "SCALAR"(since each input is a single time value, not a vector)bufferViewpointing to a specific range in the.binfile, plus details likecount(how many keyframe times there are)
Parse the binary data
When you extract the bytes from the.binfile at the range specified by the buffer view, you'll need to read them as 32-bit little-endian floats (glTF uses little-endian for all binary data). For the Animated Triangle, this will be a short sequence like0.0, 1.0, 2.0— notice how each value is strictly larger than the last, and the first is ≥ 0.0, which checks all the spec boxes.
What this actually does for the animation
These time values are the "timestamps" that tell the glTF runtime when to trigger each keyframe change. For example, if the first input value is 0.0 seconds, that's when the triangle starts at its initial position; at 1.0 seconds, it moves to the next keyframe position, and so on. The strictly increasing rule ensures the animation plays smoothly in order, no backward jumps or broken timing.
Quick troubleshooting tips
- If you're parsing manually, don't mix up endianness— this is a super common mistake that leads to garbage time values.
- Double-check the accessor's
countmatches the number of floats you extract from the binary. If they don't line up, you're looking at the wrong byte range.
内容的提问来源于stack exchange,提问作者davidkomer

