| 982 | } |
| 983 | |
| 984 | std::vector<entt::entity> load_all_nodes(entt::registry & registry, const std::vector<std::shared_ptr<renderer::mesh>> & meshes) |
| 985 | { |
| 986 | std::vector<entt::entity> entities{gltf.nodes.size()}; |
| 987 | registry.create(entities.begin(), entities.end()); |
| 988 | |
| 989 | // Create all node components first |
| 990 | registry.insert<components::node>(entities.begin(), entities.end()); |
| 991 | |
| 992 | for (const auto & [entity, gltf_node]: std::ranges::zip_view(entities, gltf.nodes)) |
| 993 | { |
| 994 | components::node & node = registry.get<components::node>(entity); |
| 995 | node.name = gltf_node.name; |
| 996 | |
| 997 | if (gltf_node.meshIndex) |
| 998 | node.mesh = meshes[*gltf_node.meshIndex]; |
| 999 | |
| 1000 | if (gltf_node.skinIndex) |
| 1001 | { |
| 1002 | auto & skin = gltf.skins.at(*gltf_node.skinIndex); |
| 1003 | |
| 1004 | node.joints.resize(skin.joints.size()); |
| 1005 | |
| 1006 | for (auto [i, joint_index]: utils::enumerate(skin.joints)) |
| 1007 | node.joints.at(i).first = entities[joint_index]; |
| 1008 | |
| 1009 | if (skin.inverseBindMatrices) |
| 1010 | { |
| 1011 | const fastgltf::Accessor & accessor = gltf.accessors.at(*skin.inverseBindMatrices); |
| 1012 | |
| 1013 | fastgltf::iterateAccessorWithIndex<glm::mat4>(gltf, accessor, [&](const glm::mat4 & value, std::size_t idx) { |
| 1014 | node.joints.at(idx).second = value; |
| 1015 | }); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | for (size_t child: gltf_node.children) |
| 1020 | { |
| 1021 | registry.get<components::node>(entities[child]).parent = entity; |
| 1022 | } |
| 1023 | |
| 1024 | auto TRS = std::get<fastgltf::TRS>(gltf_node.transform); |
| 1025 | |
| 1026 | node.position = glm::make_vec3(TRS.translation.data()); |
| 1027 | node.orientation = glm::make_quat(TRS.rotation.data()); |
| 1028 | node.scale = glm::make_vec3(TRS.scale.data()); |
| 1029 | node.visible = true; |
| 1030 | } |
| 1031 | |
| 1032 | return entities; |
| 1033 | } |
| 1034 | |
| 1035 | void load_all_animations(entt::registry & registry, const std::vector<entt::entity> & node_entities) |
| 1036 | { |