feat: automatically loading models used in skeleton

This commit is contained in:
MihailRis 2024-07-17 12:12:40 +03:00
parent 051dc6e05f
commit 739ef49d8f
11 changed files with 43 additions and 32 deletions

View File

@ -1,13 +0,0 @@
# Blender MTL File: 'player.blend'
# Material Count: 1
newmtl entities/player
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
map_Kd /home/ubuntu/Projects/Cpp/VoxelEngine-Cpp/res/content/base/textures/entities/player.png

View File

@ -5,10 +5,6 @@
"events/pickup" "events/pickup"
], ],
"models": [ "models": [
"block", "drop-item"
"drop-block",
"drop-item",
"player",
"player-head"
] ]
} }

View File

@ -12,6 +12,7 @@
#include "../content/Content.hpp" #include "../content/Content.hpp"
#include "../content/ContentPack.hpp" #include "../content/ContentPack.hpp"
#include "../voxels/Block.hpp" #include "../voxels/Block.hpp"
#include "../objects/rigging.hpp"
#include "../graphics/core/Texture.hpp" #include "../graphics/core/Texture.hpp"
#include "../logic/scripting/scripting.hpp" #include "../logic/scripting/scripting.hpp"
@ -205,6 +206,16 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
fs::path folder = info.folder / fs::path("layouts"); fs::path folder = info.folder / fs::path("layouts");
addLayouts(pack->getEnvironment(), info.id, folder, loader); addLayouts(pack->getEnvironment(), info.id, folder, loader);
} }
for (auto& entry : content->getSkeletons()) {
auto& skeleton = *entry.second;
for (auto& bone : skeleton.getBones()) {
auto& model = bone->model.name;
if (!model.empty()) {
loader.add(AssetType::MODEL, MODELS_FOLDER+"/"+model, model);
}
}
}
} }
loader.add(AssetType::ATLAS, TEXTURES_FOLDER+"/blocks", "blocks"); loader.add(AssetType::ATLAS, TEXTURES_FOLDER+"/blocks", "blocks");
loader.add(AssetType::ATLAS, TEXTURES_FOLDER+"/items", "items"); loader.add(AssetType::ATLAS, TEXTURES_FOLDER+"/items", "items");

View File

@ -49,7 +49,7 @@ Content::Content(
Content::~Content() { Content::~Content() {
} }
const rigging::SkeletonConfig* Content::getRig(const std::string& id) const { const rigging::SkeletonConfig* Content::getSkeleton(const std::string& id) const {
auto found = skeletons.find(id); auto found = skeletons.find(id);
if (found == skeletons.end()) { if (found == skeletons.end()) {
return nullptr; return nullptr;
@ -80,3 +80,7 @@ const UptrsMap<std::string, BlockMaterial>& Content::getBlockMaterials() const {
const UptrsMap<std::string, ContentPackRuntime>& Content::getPacks() const { const UptrsMap<std::string, ContentPackRuntime>& Content::getPacks() const {
return packs; return packs;
} }
const UptrsMap<std::string, rigging::SkeletonConfig>& Content::getSkeletons() const {
return skeletons;
}

View File

@ -200,12 +200,13 @@ public:
return resourceIndices[static_cast<size_t>(type)]; return resourceIndices[static_cast<size_t>(type)];
} }
const rigging::SkeletonConfig* getRig(const std::string& id) const; const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
const BlockMaterial* findBlockMaterial(const std::string& id) const; const BlockMaterial* findBlockMaterial(const std::string& id) const;
const ContentPackRuntime* getPackRuntime(const std::string& id) const; const ContentPackRuntime* getPackRuntime(const std::string& id) const;
const UptrsMap<std::string, BlockMaterial>& getBlockMaterials() const; const UptrsMap<std::string, BlockMaterial>& getBlockMaterials() const;
const UptrsMap<std::string, ContentPackRuntime>& getPacks() const; const UptrsMap<std::string, ContentPackRuntime>& getPacks() const;
const UptrsMap<std::string, rigging::SkeletonConfig>& getSkeletons() const;
}; };
#endif // CONTENT_CONTENT_HPP_ #endif // CONTENT_CONTENT_HPP_

View File

@ -20,7 +20,7 @@ static int l_get_model(lua::State* L) {
if (!modelOverride.model) { if (!modelOverride.model) {
return lua::pushstring(L, modelOverride.name); return lua::pushstring(L, modelOverride.name);
} }
return lua::pushstring(L, rigConfig->getNodes()[index]->model.name); return lua::pushstring(L, rigConfig->getBones()[index]->model.name);
} }
return 0; return 0;
} }

View File

@ -48,7 +48,7 @@ static int l_get_skeleton(lua::State* L) {
static int l_set_skeleton(lua::State* L) { static int l_set_skeleton(lua::State* L) {
if (auto entity = get_entity(L, 1)) { if (auto entity = get_entity(L, 1)) {
std::string skeletonName = lua::require_string(L, 2); std::string skeletonName = lua::require_string(L, 2);
auto rigConfig = content->getRig(skeletonName); auto rigConfig = content->getSkeleton(skeletonName);
if (rigConfig == nullptr) { if (rigConfig == nullptr) {
throw std::runtime_error("skeleton not found '"+skeletonName+"'"); throw std::runtime_error("skeleton not found '"+skeletonName+"'");
} }

View File

@ -49,10 +49,10 @@ void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
auto& skeleton = registry.get<rigging::Skeleton>(entity); auto& skeleton = registry.get<rigging::Skeleton>(entity);
skeleton.config = rigConfig; skeleton.config = rigConfig;
skeleton.pose.matrices.resize( skeleton.pose.matrices.resize(
rigConfig->getNodes().size(), glm::mat4(1.0f) rigConfig->getBones().size(), glm::mat4(1.0f)
); );
skeleton.calculated.matrices.resize( skeleton.calculated.matrices.resize(
rigConfig->getNodes().size(), glm::mat4(1.0f) rigConfig->getBones().size(), glm::mat4(1.0f)
); );
} }
@ -99,7 +99,7 @@ entityid_t Entities::spawn(
dynamic::Map_sptr saved, dynamic::Map_sptr saved,
entityid_t uid) entityid_t uid)
{ {
auto skeleton = level->content->getRig(def.skeletonName); auto skeleton = level->content->getSkeleton(def.skeletonName);
if (skeleton == nullptr) { if (skeleton == nullptr) {
throw std::runtime_error("skeleton "+def.skeletonName+" not found"); throw std::runtime_error("skeleton "+def.skeletonName+" not found");
} }
@ -205,7 +205,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
std::string skeletonName = skeleton.config->getName(); std::string skeletonName = skeleton.config->getName();
map->str("skeleton", skeletonName); map->str("skeleton", skeletonName);
if (skeletonName != skeleton.config->getName()) { if (skeletonName != skeleton.config->getName()) {
skeleton.config = level->content->getRig(skeletonName); skeleton.config = level->content->getSkeleton(skeletonName);
} }
if (auto skeletonmap = map->map(COMP_SKELETON)) { if (auto skeletonmap = map->map(COMP_SKELETON)) {
if (auto texturesmap = skeletonmap->map("textures")) { if (auto texturesmap = skeletonmap->map("textures")) {

View File

@ -17,15 +17,27 @@ struct EntityDef {
/// @brief Entity string id (with prefix included) /// @brief Entity string id (with prefix included)
std::string const name; std::string const name;
/// @brief Component IDs
std::vector<std::string> components; std::vector<std::string> components;
/// @brief Physic body type
BodyType bodyType = BodyType::DYNAMIC; BodyType bodyType = BodyType::DYNAMIC;
/// @brief Hitbox size
glm::vec3 hitbox {0.25f}; glm::vec3 hitbox {0.25f};
/// @brief 'aabb' sensors
std::vector<std::pair<size_t, AABB>> boxSensors {}; std::vector<std::pair<size_t, AABB>> boxSensors {};
/// @brief 'radius' sensors
std::vector<std::pair<size_t, float>> radialSensors {}; std::vector<std::pair<size_t, float>> radialSensors {};
/// @brief Skeleton ID
std::string skeletonName = name; std::string skeletonName = name;
/// @brief Does entity prevent blocks setup
bool blocking = true; bool blocking = true;
/// @brief save-** flags
struct { struct {
bool enabled = true; bool enabled = true;
struct { struct {

View File

@ -34,13 +34,13 @@ void Bone::setModel(const std::string& name) {
Skeleton::Skeleton(const SkeletonConfig* config) Skeleton::Skeleton(const SkeletonConfig* config)
: config(config), : config(config),
pose(config->getNodes().size()), pose(config->getBones().size()),
calculated(config->getNodes().size()), calculated(config->getBones().size()),
flags(config->getNodes().size()), flags(config->getBones().size()),
textures(), textures(),
modelOverrides(config->getNodes().size()), modelOverrides(config->getBones().size()),
visible(true) { visible(true) {
for (size_t i = 0; i < config->getNodes().size(); i++) { for (size_t i = 0; i < config->getBones().size(); i++) {
flags[i].visible = true; flags[i].visible = true;
} }
} }

View File

@ -120,7 +120,7 @@ namespace rigging {
std::string_view name std::string_view name
); );
const std::vector<Bone*>& getNodes() const { const std::vector<Bone*>& getBones() const {
return nodes; return nodes;
} }