mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 08:17:09 +01:00
c8068d8f3d
Stage 8 of the EU07M model format. A model can carry animation clips; each track of a clip sets the translation, the rotation or the scale of a node at a time of the clip, and the parts of a node without a track keep its TRS. - Format, five optional sections: ACLP (clips: name, duration, flags 0x1 loop and 0x2 autoplay, range of tracks), ATRK (tracks: node, part, interpolation step / linear / cubic, ranges of keys), ATIM (key times), AVEC (vectors; with cubic the in-tangent, value and out-tangent of glTF) and AROT (rotations in 8 bytes, "smallest three": the three smaller components as snorm16 scaled by sqrt(2) and the index of the largest; eu07m::encode_rotation / decode_rotation). The validator refuses a node out of range, keys out of their arrays, times which aren't numbers from 0 up, a cubic rotation and tracks out of the range of the clip. -eu07m dump lists the clips and their tracks. The current release doesn't read these sections; a file without them loads as before. - Sampling (model/clip.cpp): step, lerp / slerp the shorter way, the Hermite spline of glTF with tangents per second; the first and the last key held outside of them; a looped clip goes around its duration, another stops at its end. - In the game: the clips play by the simulation time, from TAnimModel::play_clip(name, speed) or, flagged autoplay, from the first time the instance is drawn. RaPrepare() samples them for the instance drawn and sets the transforms of the nodes (TSubModel::SetClipTransform(), the new animation type at_Clip, gated by the instance like the others), so a clip moving the joints of a skin bends the skinned parts. The nodes a clip moves are flagged animated at load, which keeps the model out of the instancing. stop_clips() stops them. - The editor self-test gets "clip <node> <name> [speed]". Not yet: a scenery event starting a clip, and the CHNL channels bound to the time of a clip. Checked: a skinned tube with an autoplay clip (the tip to 60 degrees and back in 2 s, looped) beside a still one, six screenshots in Vulkan and in GL 4.6 go from straight to fully bent while the other stays (time let run in the editor for the test only). Unit tests 1200/1200, 6 new ones for the packing, the interpolations, the time and the file. Not built with GCC here.
129 lines
4.1 KiB
C++
129 lines
4.1 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "model/clip.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
namespace animation
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
// the key at or before Time and the share of the way to the next one
|
|
struct key_span
|
|
{
|
|
std::size_t key{0};
|
|
float share{0.f};
|
|
float length{0.f}; // seconds between the two keys
|
|
bool last{true}; // Time at or past the last key, or a single key
|
|
};
|
|
|
|
key_span find_span(std::span<float const> const Times, float const Time)
|
|
{
|
|
key_span result;
|
|
if (Times.size() < 2 || Time <= Times.front())
|
|
{
|
|
result.last = Times.size() < 2;
|
|
return result;
|
|
}
|
|
if (Time >= Times.back())
|
|
{
|
|
result.key = Times.size() - 1;
|
|
return result;
|
|
}
|
|
auto const next{static_cast<std::size_t>(std::distance(Times.begin(), std::ranges::upper_bound(Times, Time)))};
|
|
result.key = next - 1;
|
|
result.length = Times[next] - Times[result.key];
|
|
result.share = result.length > 0.f ? (Time - Times[result.key]) / result.length : 0.f;
|
|
result.last = false;
|
|
return result;
|
|
}
|
|
|
|
glm::vec3 hermite(glm::vec3 const &From, glm::vec3 const &FromTangent, glm::vec3 const &To, glm::vec3 const &ToTangent, float const Share)
|
|
{
|
|
auto const t2{Share * Share};
|
|
auto const t3{t2 * Share};
|
|
return (2.f * t3 - 3.f * t2 + 1.f) * From + (t3 - 2.f * t2 + Share) * FromTangent + (-2.f * t3 + 3.f * t2) * To + (t3 - t2) * ToTangent;
|
|
}
|
|
|
|
glm::vec3 vector_value(track const &Track, std::size_t const Key)
|
|
{
|
|
// with cubic the keys come as in-tangent, value, out-tangent
|
|
auto const index{Track.interpolation == curve::cubic ? Key * 3 + 1 : Key};
|
|
return index < Track.vectors.size() ? Track.vectors[index] : glm::vec3{0.f};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
float clip_time(clip const &Clip, double const Elapsed)
|
|
{
|
|
if (Clip.duration <= 0.f || Elapsed <= 0.0)
|
|
{
|
|
return 0.f;
|
|
}
|
|
auto const duration{static_cast<double>(Clip.duration)};
|
|
if (Clip.loop)
|
|
{
|
|
return static_cast<float>(std::fmod(Elapsed, duration));
|
|
}
|
|
return static_cast<float>(std::min(Elapsed, duration));
|
|
}
|
|
|
|
glm::vec3 sample_vector(track const &Track, float const Time)
|
|
{
|
|
auto const span{find_span(Track.times, Time)};
|
|
if (span.last || Track.interpolation == curve::step || span.share <= 0.f)
|
|
{
|
|
return vector_value(Track, span.key);
|
|
}
|
|
auto const from{vector_value(Track, span.key)};
|
|
auto const to{vector_value(Track, span.key + 1)};
|
|
if (Track.interpolation == curve::linear)
|
|
{
|
|
return glm::mix(from, to, span.share);
|
|
}
|
|
auto const outtangent{Track.vectors.size() > span.key * 3 + 2 ? Track.vectors[span.key * 3 + 2] : glm::vec3{0.f}};
|
|
auto const intangent{Track.vectors.size() > (span.key + 1) * 3 ? Track.vectors[(span.key + 1) * 3] : glm::vec3{0.f}};
|
|
return hermite(from, outtangent * span.length, to, intangent * span.length, span.share);
|
|
}
|
|
|
|
glm::quat sample_rotation(track const &Track, float const Time)
|
|
{
|
|
if (Track.rotations.empty())
|
|
{
|
|
return glm::quat{1.f, 0.f, 0.f, 0.f};
|
|
}
|
|
auto const span{find_span(Track.times, Time)};
|
|
auto const from{Track.rotations[std::min(span.key, Track.rotations.size() - 1)]};
|
|
if (span.last || Track.interpolation == curve::step || span.share <= 0.f || span.key + 1 >= Track.rotations.size())
|
|
{
|
|
return from;
|
|
}
|
|
// the shorter way round
|
|
return glm::normalize(glm::slerp(from, Track.rotations[span.key + 1], span.share));
|
|
}
|
|
|
|
glm::mat4 local_transform(clip const &Clip, node_tracks const &Node, float const Time)
|
|
{
|
|
auto const tracked = [&Clip](int const Track) { return (Track >= 0) && (static_cast<std::size_t>(Track) < Clip.tracks.size()); };
|
|
auto const translation{tracked(Node.translation) ? sample_vector(Clip.tracks[Node.translation], Time) : Node.rest_translation};
|
|
auto const rotation{tracked(Node.rotation) ? sample_rotation(Clip.tracks[Node.rotation], Time) : Node.rest_rotation};
|
|
auto const scale{tracked(Node.scale) ? sample_vector(Clip.tracks[Node.scale], Time) : Node.rest_scale};
|
|
return glm::translate(glm::mat4{1.f}, translation) * glm::mat4_cast(rotation) * glm::scale(glm::mat4{1.f}, scale);
|
|
}
|
|
|
|
} // namespace animation
|