From 1ab873528790109ee64675e90669c956ce23b529 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 10 Dec 2024 13:26:14 -0800 Subject: [PATCH] gltfpack: Reuse time accessors between consecutive tracks When tracks have custom time inputs (and resampling is disabled), we can reuse inputs when they are equal between two tracks. This sharing is only performed between consecutive tracks as it allows us to still process tracks linearly. --- gltf/write.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gltf/write.cpp b/gltf/write.cpp index 938238b25..20e9bbc5b 100644 --- a/gltf/write.cpp +++ b/gltf/write.cpp @@ -1449,6 +1449,9 @@ void writeAnimation(std::string& json, std::vector& views, std::stri size_t track_offset = 0; + size_t last_track_time_accr = 0; + const Track* last_track_time = NULL; + for (size_t j = 0; j < tracks.size(); ++j) { const Track& track = *tracks[j]; @@ -1458,7 +1461,17 @@ void writeAnimation(std::string& json, std::vector& views, std::stri size_t track_time_accr = time_accr; if (!track.time.empty()) - track_time_accr = writeAnimationTime(views, json_accessors, accr_offset, track.time, settings); + { + // reuse time accessors between consecutive tracks if possible + if (last_track_time && track.time == last_track_time->time) + track_time_accr = last_track_time_accr; + else + { + track_time_accr = writeAnimationTime(views, json_accessors, accr_offset, track.time, settings); + last_track_time_accr = track_time_accr; + last_track_time = &track; + } + } std::string scratch; StreamFormat format = writeKeyframeStream(scratch, track.path, track.data, settings, track.interpolation == cgltf_interpolation_type_cubic_spline);