Skip to content

Commit

Permalink
gltfpack: Refactor constant track support
Browse files Browse the repository at this point in the history
Instead of determining whether the track is constant when writing it, we
now have an explicit bit per track that says if it is. We still validate
the track length to make sure time or pose input is going to match the
output in terms of keyframe count.
  • Loading branch information
zeux committed Oct 21, 2020
1 parent 0abaccb commit 873fd90
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions gltf/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ void processAnimation(Animation& animation, const Settings& settings)
if (isTrackEqual(track.data, track.path, frames, &track.data[0], track.components))
{
// track is constant (equal to first keyframe), we only need the first keyframe
track.constant = true;
track.data.resize(track.components);

// track.dummy is true iff track redundantly sets up the value to be equal to default node transform
Expand Down
1 change: 1 addition & 0 deletions gltf/gltfpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct Track
cgltf_node* node;
cgltf_animation_path_type path;

bool constant;
bool dummy;

size_t components; // 1 unless path is cgltf_animation_path_type_weights
Expand Down
10 changes: 4 additions & 6 deletions gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,10 @@ void writeAnimation(std::string& json, std::vector<BufferView>& views, std::stri
{
const Track& track = *tracks[j];

bool tc = track.data.size() == track.components;
assert(track.data.size() == track.components * (track.constant ? 1 : animation.frames));

needs_time = needs_time || !tc;
needs_pose = needs_pose || tc;
needs_time = needs_time || !track.constant;
needs_pose = needs_pose || track.constant;
}

size_t time_accr = needs_time ? writeAnimationTime(views, json_accessors, accr_offset, animation.start, animation.frames, settings) : 0;
Expand All @@ -1243,8 +1243,6 @@ void writeAnimation(std::string& json, std::vector<BufferView>& views, std::stri
{
const Track& track = *tracks[j];

bool tc = track.data.size() == track.components;

std::string scratch;
StreamFormat format = writeKeyframeStream(scratch, track.path, track.data, settings);
BufferView::Compression compression = settings.compress && track.path != cgltf_animation_path_type_weights ? BufferView::Compression_Attribute : BufferView::Compression_None;
Expand All @@ -1260,7 +1258,7 @@ void writeAnimation(std::string& json, std::vector<BufferView>& views, std::stri

comma(json_samplers);
append(json_samplers, "{\"input\":");
append(json_samplers, tc ? pose_accr : time_accr);
append(json_samplers, track.constant ? pose_accr : time_accr);
append(json_samplers, ",\"output\":");
append(json_samplers, data_accr);
append(json_samplers, "}");
Expand Down

0 comments on commit 873fd90

Please sign in to comment.