mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 23:17:56 +01:00
55c7050c49
A channel (CHNL component) is a named value of a model, kept within its range, which moves what it's bound to. The game now follows its bindings to joints and to clips. - A joint binding (a JOIN component of a node): the value of the target (channel * scale + offset) turns a hinge (radians) or a screw (turning and moving by its pitch), or moves a slider (metres) along the axis of the joint, from the transform of the node, within the limits of the joint when it has them; a fixed or a ball joint leaves the node at rest (animation::joint_transform()). - A clip binding: the value of the target is the share of the duration of the clip, 0 to 1, so the channel scrubs the clip without playing it. - Bindings to variants and light slots are read but not followed yet. - Format, before the release: the target of a clip binding is the index of the clip in ACLP (clips aren't components); the validator checks the target by the kind of the binding (a joint has to be a JOIN component). - TAnimModel::set_channel(name, value); a channel not set has its initial value. RaPrepare() sets the nodes from the channels after the clips playing, so a channel overrides a clip on the same node. The nodes of the joints are flagged animated at load, which keeps the model out of the instancing. - New form of the animation event: "animation <delay> <model> channel <name> <value>"; a channel the model hasn't gets "Bad event: ... can't find channel ..." in the log. The current release doesn't read the new form. - The editor self-test gets "channel <node> <name> <value>". Checked in the game (Vulkan, a skinned tube with a hinge on its tip and a clip on its base): "bend 1" from the self-test and "bend -1" from an event turn the tip both ways, "phase 0.5" from an event turns the whole tube through the clip, an unknown channel logs the error. Unit tests 1204/1204, 4 new ones for the hinge, slider, screw and limits, the range of a channel and the targets of the bindings in the file. Not built with GCC here.
160 lines
5.1 KiB
C++
160 lines
5.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 <numbers>
|
|
|
|
#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);
|
|
}
|
|
|
|
float channel_value(channel const &Channel, float const Value)
|
|
{
|
|
auto const low{std::min(Channel.minimum, Channel.maximum)};
|
|
auto const high{std::max(Channel.minimum, Channel.maximum)};
|
|
return std::clamp(Value, low, high);
|
|
}
|
|
|
|
glm::mat4 joint_transform(joint_binding const &Joint, float const Value)
|
|
{
|
|
auto value{Value};
|
|
if (Joint.limited)
|
|
{
|
|
value = std::clamp(value, std::min(Joint.minimum, Joint.maximum), std::max(Joint.minimum, Joint.maximum));
|
|
}
|
|
auto const length{glm::length(Joint.axis)};
|
|
if (length <= 0.f)
|
|
{
|
|
return Joint.rest;
|
|
}
|
|
auto const axis{Joint.axis / length};
|
|
switch (Joint.type)
|
|
{
|
|
case joint_type::hinge: return Joint.rest * glm::rotate(glm::mat4{1.f}, value, axis);
|
|
case joint_type::slider: return Joint.rest * glm::translate(glm::mat4{1.f}, axis * value);
|
|
case joint_type::screw:
|
|
return Joint.rest * glm::translate(glm::mat4{1.f}, axis * (value / (2.f * std::numbers::pi_v<float>) * Joint.pitch)) * glm::rotate(glm::mat4{1.f}, value, axis);
|
|
default: return Joint.rest;
|
|
}
|
|
}
|
|
|
|
} // namespace animation
|