Skip to content

Commit

Permalink
gltfpack: Reuse time accessors between consecutive tracks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zeux committed Dec 10, 2024
1 parent 740a0a6 commit 1ab8735
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,9 @@ void writeAnimation(std::string& json, std::vector<BufferView>& 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];
Expand All @@ -1458,7 +1461,17 @@ void writeAnimation(std::string& json, std::vector<BufferView>& 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);
Expand Down

0 comments on commit 1ab8735

Please sign in to comment.