sandbox
Loading...
Searching...
No Matches
animations_module.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_ANIMATIONS_ANIMATIONS_MODULE_HPP_
3#define LIBSBX_ANIMATIONS_ANIMATIONS_MODULE_HPP_
4
5#include <libsbx/core/module.hpp>
6#include <libsbx/core/engine.hpp>
7
8#include <libsbx/utility/logger.hpp>
9
10#include <libsbx/scenes/node.hpp>
11#include <libsbx/scenes/scenes_module.hpp>
12
13#include <libsbx/scenes/components/skinned_mesh.hpp>
14
15#include <libsbx/animations/animator.hpp>
16#include <libsbx/animations/animation.hpp>
17#include <libsbx/animations/mesh.hpp>
18
19namespace sbx::animations {
20
21class animations_module : public core::module<animations_module> {
22
23 inline static const auto is_registered = register_module(stage::post);
24
25public:
26
28
29 ~animations_module() override;
30
31 auto update() -> void override;
32
33 template<typename... Args>
34 auto add_animated_mesh(const scenes::node node, const math::uuid mesh_id, Args&&... args) -> scenes::skinned_mesh& {
35 auto& assets_module = core::engine::get_module<assets::assets_module>();
36 auto& scenes_module = sbx::core::engine::get_module<sbx::scenes::scenes_module>();
37
38 auto& scene = scenes_module.scene();
39
40 auto& skinned_mesh = scene.add_component<sbx::scenes::skinned_mesh>(node, mesh_id, std::forward<Args>(args)...);
41
42 const auto& mesh = assets_module.get_asset<animations::mesh>(mesh_id);
43
44 const auto& skeleton = mesh.skeleton();
45
46 const auto& bones = skeleton.bones();
47
48 auto nodes = std::vector<scenes::node>{};
49 nodes.reserve(bones.size());
50
51 for (auto i = 0u; i < bones.size(); ++i) {
52 const auto parent_id = bones[i].parent_id;
53 const auto parent = (parent_id == skeleton::bone::null) ? node : nodes.at(parent_id);
54
55 const auto [position, rotation, scale] = math::decompose(bones[i].local_bind_matrix);
56
57 nodes.push_back(scene.create_child_node(parent, skeleton.name_for_bone(i).str(), scenes::transform{position, rotation, scale}));
58 }
59
60 skinned_mesh.set_nodes(std::move(nodes));
61
62 return skinned_mesh;
63 }
64
65 auto find_skeleton_node(const scenes::node node, const utility::hashed_string& name) const -> scenes::node;
66
67}; // class assets_module
68
69} // namespace sbx::animations
70
71#endif // LIBSBX_ANIMATIONS_ANIMATIONS_MODULE_HPP_
Definition: tests.cpp:6
Definition: animations_module.hpp:21
Definition: mesh.hpp:21
Definition: skeleton.hpp:21
Definition: module.hpp:92
Definition: uuid.hpp:160
Definition: skinned_mesh.hpp:27
Definition: transform.hpp:16
Definition: hashed_string.hpp:17