diff --git a/engine/src/toast/world/skeletal_node.cpp b/engine/src/toast/world/skeletal_node.cpp index 6b8243371..ded6767c7 100644 --- a/engine/src/toast/world/skeletal_node.cpp +++ b/engine/src/toast/world/skeletal_node.cpp @@ -31,6 +31,19 @@ auto SkeletalNode::play(std::string_view clip_name) -> bool { m_playback.start(*clip); // The previous clip may be freed and a new one allocated at its address m_track_clip = nullptr; + m_fade_duration = 0.0f; + return true; +} + +auto SkeletalNode::crossfade(std::string_view clip_name, float seconds) -> bool { + if (!play(clip_name)) { + return false; + } + if (seconds > 0.0f && !m_local.empty()) { + m_fade_from = m_local; + m_fade_time = 0.0f; + m_fade_duration = seconds; + } return true; } @@ -256,8 +269,23 @@ void SkeletalNode::tick() { m_local[i] = skeleton->bones()[i].rest; } m_playback.clip->sampleInto(m_playback.time, m_track_to_bone, m_local); + if (m_fade_duration > 0.0f && m_fade_from.size() == m_local.size()) { + m_fade_time += static_cast(Time::delta()); + const float t = std::min(m_fade_time / m_fade_duration, 1.0f); + for (size_t i = 0; i < m_local.size(); ++i) { + m_local[i].translation = glm::mix(m_fade_from[i].translation, m_local[i].translation, t); + m_local[i].rotation = glm::slerp(m_fade_from[i].rotation, m_local[i].rotation, t); + m_local[i].scale = glm::mix(m_fade_from[i].scale, m_local[i].scale, t); + } + if (t >= 1.0f) { + m_fade_duration = 0.0f; + } + } computeModel(*skeleton); applyNodeTracks(); + if (!m_playback.playing) { + clip_finished.fire(m_playback.clip->name); + } } applyAttachments(*skeleton); diff --git a/engine/src/toast/world/skeletal_node.hpp b/engine/src/toast/world/skeletal_node.hpp index bb6fa206c..39baaabf8 100644 --- a/engine/src/toast/world/skeletal_node.hpp +++ b/engine/src/toast/world/skeletal_node.hpp @@ -26,6 +26,10 @@ class [[ToastNode, Icon("Bone")]] TOAST_API SkeletalNode : public Node3D { [[Reflect]] auto play(std::string_view clip_name) -> bool; + /// Blends from the current pose into the clip over @p seconds + [[Reflect]] + auto crossfade(std::string_view clip_name, float seconds) -> bool; + [[Reflect]] void stop(); @@ -34,11 +38,40 @@ class [[ToastNode, Icon("Bone")]] TOAST_API SkeletalNode : public Node3D { m_playback.paused = value; } - [[nodiscard]] + [[nodiscard, Reflect]] auto isPlaying() const noexcept -> bool { return m_playback.playing; } + [[nodiscard, Reflect]] + auto isPaused() const noexcept -> bool { + return m_playback.paused; + } + + /// Empty when no clip was played yet + [[nodiscard, Reflect]] + auto clipName() const -> std::string { + return m_playback.clip != nullptr ? m_playback.clip->name : std::string {}; + } + + [[nodiscard, Reflect]] + auto clipTime() const noexcept -> float { + return m_playback.time; + } + + [[nodiscard, Reflect]] + auto clipDuration() const noexcept -> float { + return m_playback.clip != nullptr ? m_playback.clip->duration : 0.0f; + } + + [[Reflect]] + void seek(float seconds) noexcept { + m_playback.time = seconds; + } + + /// Fires once when a non looping clip reaches its end + signals::Signal clip_finished; + [[nodiscard]] auto time() const noexcept -> float { return m_playback.time; @@ -127,6 +160,9 @@ class [[ToastNode, Icon("Bone")]] TOAST_API SkeletalNode : public Node3D { assets::Handle m_bound_asset; const assets::Skeleton* m_bound = nullptr; std::vector m_local; + std::vector m_fade_from; + float m_fade_time = 0.0f; + float m_fade_duration = 0.0f; std::vector m_model; std::vector m_parents;