diff --git a/src/rmodels.c b/src/rmodels.c index fb975f7b7..4dbfb4beb 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -6390,6 +6390,7 @@ static bool GetPoseAtTimeGLTF(cgltf_interpolation_type interpolationType, cgltf_ float tstart = 0.0f; float tend = 0.0f; int keyframe = 0; // Defaults to first pose + bool found = false; for (int i = 0; i < (int)input->count - 1; i++) { @@ -6402,10 +6403,26 @@ static bool GetPoseAtTimeGLTF(cgltf_interpolation_type interpolationType, cgltf_ if ((tstart <= time) && (time < tend)) { keyframe = i; + found = true; break; } } + // No interval contains a time at (or past) the last keyframe, because the + // search above requires time < tend: clamp to the edge interval instead of + // falling back to keyframe 0, which returns a pose from the start + if (!found && ((int)input->count >= 2)) + { + keyframe = (int)input->count - 2; + + float tfirst = 0.0f; + if (!cgltf_accessor_read_float(input, 0, &tfirst, 1)) return false; + if (time < tfirst) keyframe = 0; + + if (!cgltf_accessor_read_float(input, keyframe, &tstart, 1)) return false; + if (!cgltf_accessor_read_float(input, keyframe + 1, &tend, 1)) return false; + } + // Constant animation, no need to interpolate if (FloatEquals(tend, tstart)) {