rename rig to skeleton

This commit is contained in:
MihailRis 2024-07-10 04:28:12 +03:00
parent 91230ecbeb
commit 60f4f33180
24 changed files with 187 additions and 187 deletions

View File

@ -2,6 +2,6 @@
"components": [ "components": [
"falling_block" "falling_block"
], ],
"rig-name": "base:block", "skeleton-name": "base:block",
"hitbox": [0.8, 0.8, 0.8] "hitbox": [0.8, 0.8, 0.8]
} }

View File

@ -1,6 +1,6 @@
local tsf = entity.transform local tsf = entity.transform
local body = entity.rigidbody local body = entity.rigidbody
local rig = entity.modeltree local rig = entity.skeleton
inair = true inair = true
ready = false ready = false

View File

@ -1,6 +1,6 @@
local tsf = entity.transform local tsf = entity.transform
local body = entity.rigidbody local body = entity.rigidbody
local rig = entity.modeltree local rig = entity.skeleton
ARGS = ARGS or {} ARGS = ARGS or {}
local blockid = ARGS.block local blockid = ARGS.block

View File

@ -35,15 +35,15 @@ local function new_Rigidbody(eid)
return setmetatable({eid=eid}, Rigidbody) return setmetatable({eid=eid}, Rigidbody)
end end
local Modeltree = {__index={ local Skeleton = {__index={
get_model=function(self, i) return __modeltree.get_model(self.eid, i) end, get_model=function(self, i) return __skeleton.get_model(self.eid, i) end,
get_matrix=function(self, i) return __modeltree.get_matrix(self.eid, i) end, get_matrix=function(self, i) return __skeleton.get_matrix(self.eid, i) end,
set_matrix=function(self, i, m) return __modeltree.set_matrix(self.eid, i, m) end, set_matrix=function(self, i, m) return __skeleton.set_matrix(self.eid, i, m) end,
set_texture=function(self, s, s2) return __modeltree.set_texture(self.eid, s, s2) end, set_texture=function(self, s, s2) return __skeleton.set_texture(self.eid, s, s2) end,
}} }}
local function new_Modeltree(eid) local function new_Skeleton(eid)
return setmetatable({eid=eid}, Modeltree) return setmetatable({eid=eid}, Skeleton)
end end
-- Entity class -- Entity class
@ -63,7 +63,7 @@ return {
local entity = setmetatable({eid=eid}, Entity) local entity = setmetatable({eid=eid}, Entity)
entity.transform = new_Transform(eid) entity.transform = new_Transform(eid)
entity.rigidbody = new_Rigidbody(eid) entity.rigidbody = new_Rigidbody(eid)
entity.modeltree = new_Modeltree(eid) entity.skeleton = new_Skeleton(eid)
entity.components = {} entity.components = {}
entities[eid] = entity; entities[eid] = entity;
return entity return entity

View File

@ -50,6 +50,6 @@ inline const std::string FONTS_FOLDER = "fonts";
inline const std::string LAYOUTS_FOLDER = "layouts"; inline const std::string LAYOUTS_FOLDER = "layouts";
inline const std::string SOUNDS_FOLDER = "sounds"; inline const std::string SOUNDS_FOLDER = "sounds";
inline const std::string MODELS_FOLDER = "models"; inline const std::string MODELS_FOLDER = "models";
inline const std::string RIGS_FOLDER = "rigs"; inline const std::string SKELETONS_FOLDER = "skeletons";
#endif // CONSTANTS_HPP_ #endif // CONSTANTS_HPP_

View File

@ -30,11 +30,11 @@ Content::Content(
ContentUnitDefs<EntityDef> entities, ContentUnitDefs<EntityDef> entities,
UptrsMap<std::string, ContentPackRuntime> packs, UptrsMap<std::string, ContentPackRuntime> packs,
UptrsMap<std::string, BlockMaterial> blockMaterials, UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::RigConfig> rigs UptrsMap<std::string, rigging::SkeletonConfig> skeletons
) : indices(std::move(indices)), ) : indices(std::move(indices)),
packs(std::move(packs)), packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)), blockMaterials(std::move(blockMaterials)),
rigs(std::move(rigs)), skeletons(std::move(skeletons)),
blocks(std::move(blocks)), blocks(std::move(blocks)),
items(std::move(items)), items(std::move(items)),
entities(std::move(entities)), entities(std::move(entities)),
@ -44,9 +44,9 @@ Content::Content(
Content::~Content() { Content::~Content() {
} }
const rigging::RigConfig* Content::getRig(const std::string& id) const { const rigging::SkeletonConfig* Content::getRig(const std::string& id) const {
auto found = rigs.find(id); auto found = skeletons.find(id);
if (found == rigs.end()) { if (found == skeletons.end()) {
return nullptr; return nullptr;
} }
return found->second.get(); return found->second.get();

View File

@ -22,7 +22,7 @@ class Content;
class ContentPackRuntime; class ContentPackRuntime;
namespace rigging { namespace rigging {
class RigConfig; class SkeletonConfig;
} }
enum class contenttype { enum class contenttype {
@ -116,7 +116,7 @@ class Content {
std::unique_ptr<ContentIndices> indices; std::unique_ptr<ContentIndices> indices;
UptrsMap<std::string, ContentPackRuntime> packs; UptrsMap<std::string, ContentPackRuntime> packs;
UptrsMap<std::string, BlockMaterial> blockMaterials; UptrsMap<std::string, BlockMaterial> blockMaterials;
UptrsMap<std::string, rigging::RigConfig> rigs; UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
public: public:
ContentUnitDefs<Block> blocks; ContentUnitDefs<Block> blocks;
ContentUnitDefs<ItemDef> items; ContentUnitDefs<ItemDef> items;
@ -131,7 +131,7 @@ public:
ContentUnitDefs<EntityDef> entities, ContentUnitDefs<EntityDef> entities,
UptrsMap<std::string, ContentPackRuntime> packs, UptrsMap<std::string, ContentPackRuntime> packs,
UptrsMap<std::string, BlockMaterial> blockMaterials, UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::RigConfig> rigs UptrsMap<std::string, rigging::SkeletonConfig> skeletons
); );
~Content(); ~Content();
@ -139,7 +139,7 @@ public:
return indices.get(); return indices.get();
} }
const rigging::RigConfig* getRig(const std::string& id) const; const rigging::SkeletonConfig* getRig(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;

View File

@ -8,8 +8,8 @@ void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) {
packs[pack->getId()] = std::move(pack); packs[pack->getId()] = std::move(pack);
} }
void ContentBuilder::add(std::unique_ptr<rigging::RigConfig> rig) { void ContentBuilder::add(std::unique_ptr<rigging::SkeletonConfig> skeleton) {
rigs[rig->getName()] = std::move(rig); skeletons[skeleton->getName()] = std::move(skeleton);
} }
BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) { BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) {
@ -75,7 +75,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
entities.build(), entities.build(),
std::move(packs), std::move(packs),
std::move(blockMaterials), std::move(blockMaterials),
std::move(rigs) std::move(skeletons)
); );
// Now, it's time to resolve foreign keys // Now, it's time to resolve foreign keys

View File

@ -50,7 +50,7 @@ public:
class ContentBuilder { class ContentBuilder {
UptrsMap<std::string, BlockMaterial> blockMaterials; UptrsMap<std::string, BlockMaterial> blockMaterials;
UptrsMap<std::string, rigging::RigConfig> rigs; UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
UptrsMap<std::string, ContentPackRuntime> packs; UptrsMap<std::string, ContentPackRuntime> packs;
std::unordered_map<std::string, contenttype> allNames; std::unordered_map<std::string, contenttype> allNames;
public: public:
@ -61,7 +61,7 @@ public:
~ContentBuilder(); ~ContentBuilder();
void add(std::unique_ptr<ContentPackRuntime> pack); void add(std::unique_ptr<ContentPackRuntime> pack);
void add(std::unique_ptr<rigging::RigConfig> rig); void add(std::unique_ptr<rigging::SkeletonConfig> skeleton);
BlockMaterial& createBlockMaterial(const std::string& id); BlockMaterial& createBlockMaterial(const std::string& id);

View File

@ -336,8 +336,8 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs
} }
} }
root->flag("save", def.save.enabled); root->flag("save", def.save.enabled);
root->flag("save-rig-pose", def.save.rig.pose); root->flag("save-skeleton-pose", def.save.skeleton.pose);
root->flag("save-rig-textures", def.save.rig.textures); root->flag("save-skeleton-textures", def.save.skeleton.textures);
root->flag("save-body-velocity", def.save.body.velocity); root->flag("save-body-velocity", def.save.body.velocity);
root->flag("save-body-settings", def.save.body.settings); root->flag("save-body-settings", def.save.body.settings);
@ -347,7 +347,7 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs
def.bodyType = *bodyType; def.bodyType = *bodyType;
} }
root->str("rig-name", def.rigName); root->str("skeleton-name", def.skeletonName);
} }
void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) { void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) {
@ -468,13 +468,13 @@ void ContentLoader::load() {
} }
} }
fs::path rigsDir = folder / fs::u8path("rigs"); fs::path skeletonsDir = folder / fs::u8path("skeletons");
if (fs::is_directory(rigsDir)) { if (fs::is_directory(skeletonsDir)) {
for (const auto& entry : fs::directory_iterator(rigsDir)) { for (const auto& entry : fs::directory_iterator(skeletonsDir)) {
const fs::path& file = entry.path(); const fs::path& file = entry.path();
std::string name = pack->id+":"+file.stem().u8string(); std::string name = pack->id+":"+file.stem().u8string();
std::string text = files::read_string(file); std::string text = files::read_string(file);
builder.add(rigging::RigConfig::parse(text, file.u8string(), name)); builder.add(rigging::SkeletonConfig::parse(text, file.u8string(), name));
} }
} }
} }

View File

@ -29,7 +29,7 @@ extern const luaL_Reg vec4lib [];
extern const luaL_Reg worldlib []; extern const luaL_Reg worldlib [];
// Components // Components
extern const luaL_Reg modeltreelib []; extern const luaL_Reg skeletonlib [];
extern const luaL_Reg rigidbodylib []; extern const luaL_Reg rigidbodylib [];
extern const luaL_Reg transformlib []; extern const luaL_Reg transformlib [];

View File

@ -1,56 +0,0 @@
#include "libentity.hpp"
#include "../../../objects/rigging.hpp"
static int index_range_check(const rigging::Rig& rig, lua::Integer index) {
if (static_cast<size_t>(index) >= rig.pose.matrices.size()) {
throw std::runtime_error("index out of range [0, " +
std::to_string(rig.pose.matrices.size()) +
"]");
}
return static_cast<int>(index);
}
static int l_modeltree_get_model(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& rig = entity->getModeltree();
auto* rigConfig = rig.config;
auto index = index_range_check(rig, lua::tointeger(L, 2));
return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName());
}
return 0;
}
static int l_modeltree_get_matrix(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& rig = entity->getModeltree();
auto index = index_range_check(rig, lua::tointeger(L, 2));
return lua::pushmat4(L, rig.pose.matrices[index]);
}
return 0;
}
static int l_modeltree_set_matrix(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& rig = entity->getModeltree();
auto index = index_range_check(rig, lua::tointeger(L, 2));
rig.pose.matrices[index] = lua::tomat4(L, 3);
}
return 0;
}
static int l_modeltree_set_texture(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& rig = entity->getModeltree();
rig.textures[lua::require_string(L, 2)] = lua::require_string(L, 3);
}
return 0;
}
const luaL_Reg modeltreelib [] = {
{"get_model", lua::wrap<l_modeltree_get_model>},
{"get_matrix", lua::wrap<l_modeltree_get_matrix>},
{"set_matrix", lua::wrap<l_modeltree_set_matrix>},
{"set_texture", lua::wrap<l_modeltree_set_texture>},
{NULL, NULL}
};

View File

@ -0,0 +1,56 @@
#include "libentity.hpp"
#include "../../../objects/rigging.hpp"
static int index_range_check(const rigging::Skeleton& skeleton, lua::Integer index) {
if (static_cast<size_t>(index) >= skeleton.pose.matrices.size()) {
throw std::runtime_error("index out of range [0, " +
std::to_string(skeleton.pose.matrices.size()) +
"]");
}
return static_cast<int>(index);
}
static int l_get_model(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
auto* rigConfig = skeleton.config;
auto index = index_range_check(skeleton, lua::tointeger(L, 2));
return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName());
}
return 0;
}
static int l_get_matrix(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
auto index = index_range_check(skeleton, lua::tointeger(L, 2));
return lua::pushmat4(L, skeleton.pose.matrices[index]);
}
return 0;
}
static int l_set_matrix(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
auto index = index_range_check(skeleton, lua::tointeger(L, 2));
skeleton.pose.matrices[index] = lua::tomat4(L, 3);
}
return 0;
}
static int l_set_texture(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto& skeleton = entity->getSkeleton();
skeleton.textures[lua::require_string(L, 2)] = lua::require_string(L, 3);
}
return 0;
}
const luaL_Reg skeletonlib [] = {
{"get_model", lua::wrap<l_get_model>},
{"get_matrix", lua::wrap<l_get_matrix>},
{"set_matrix", lua::wrap<l_set_matrix>},
{"set_texture", lua::wrap<l_set_texture>},
{NULL, NULL}
};

View File

@ -34,10 +34,10 @@ static int l_entity_despawn(lua::State* L) {
static int l_entity_set_rig(lua::State* L) { static int l_entity_set_rig(lua::State* L) {
if (auto entity = get_entity(L, 1)) { if (auto entity = get_entity(L, 1)) {
std::string rigName = lua::require_string(L, 2); std::string skeletonName = lua::require_string(L, 2);
auto rigConfig = content->getRig(rigName); auto rigConfig = content->getRig(skeletonName);
if (rigConfig == nullptr) { if (rigConfig == nullptr) {
throw std::runtime_error("rig not found '"+rigName+"'"); throw std::runtime_error("skeleton not found '"+skeletonName+"'");
} }
entity->setRig(rigConfig); entity->setRig(rigConfig);
} }

View File

@ -50,7 +50,7 @@ static void create_libs(lua::State* L) {
openlib(L, "entities", entitylib); openlib(L, "entities", entitylib);
// components // components
openlib(L, "__modeltree", modeltreelib); openlib(L, "__skeleton", skeletonlib);
openlib(L, "__rigidbody", rigidbodylib); openlib(L, "__rigidbody", rigidbodylib);
openlib(L, "__transform", transformlib); openlib(L, "__transform", transformlib);

View File

@ -22,7 +22,7 @@ static debug::Logger logger("entities");
static inline std::string COMP_TRANSFORM = "transform"; static inline std::string COMP_TRANSFORM = "transform";
static inline std::string COMP_RIGIDBODY = "rigidbody"; static inline std::string COMP_RIGIDBODY = "rigidbody";
static inline std::string COMP_MODELTREE = "modeltree"; static inline std::string COMP_SKELETON = "skeleton";
void Transform::refresh() { void Transform::refresh() {
combined = glm::mat4(1.0f); combined = glm::mat4(1.0f);
@ -38,15 +38,15 @@ void Entity::destroy() {
} }
} }
rigging::Rig& Entity::getModeltree() const { rigging::Skeleton& Entity::getSkeleton() const {
return registry.get<rigging::Rig>(entity); return registry.get<rigging::Skeleton>(entity);
} }
void Entity::setRig(const rigging::RigConfig* rigConfig) { void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
auto& rig = registry.get<rigging::Rig>(entity); auto& skeleton = registry.get<rigging::Skeleton>(entity);
rig.config = rigConfig; skeleton.config = rigConfig;
rig.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f)); skeleton.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
rig.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f)); skeleton.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
} }
Entities::Entities(Level* level) : level(level) { Entities::Entities(Level* level) : level(level) {
@ -70,9 +70,9 @@ entityid_t Entities::spawn(
dynamic::Map_sptr saved, dynamic::Map_sptr saved,
entityid_t uid) entityid_t uid)
{ {
auto rig = level->content->getRig(def.rigName); auto skeleton = level->content->getRig(def.skeletonName);
if (rig == nullptr) { if (skeleton == nullptr) {
throw std::runtime_error("rig "+def.rigName+" not found"); throw std::runtime_error("skeleton "+def.skeletonName+" not found");
} }
auto entity = registry.create(); auto entity = registry.create();
entityid_t id; entityid_t id;
@ -107,7 +107,7 @@ entityid_t Entities::spawn(
auto& scripting = registry.emplace<ScriptComponents>(entity); auto& scripting = registry.emplace<ScriptComponents>(entity);
entities[id] = entity; entities[id] = entity;
uids[entity] = id; uids[entity] = id;
registry.emplace<rigging::Rig>(entity, rig->instance()); registry.emplace<rigging::Skeleton>(entity, skeleton->instance());
for (auto& componentName : def.components) { for (auto& componentName : def.components) {
auto component = std::make_unique<UserComponent>( auto component = std::make_unique<UserComponent>(
componentName, entity_funcs_set {}, nullptr); componentName, entity_funcs_set {}, nullptr);
@ -149,7 +149,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) {
void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) { void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
auto& transform = entity.getTransform(); auto& transform = entity.getTransform();
auto& body = entity.getRigidbody(); auto& body = entity.getRigidbody();
auto& rig = entity.getModeltree(); auto& skeleton = entity.getSkeleton();
if (auto bodymap = map->map(COMP_RIGIDBODY)) { if (auto bodymap = map->map(COMP_RIGIDBODY)) {
dynamic::get_vec(bodymap, "vel", body.hitbox.velocity); dynamic::get_vec(bodymap, "vel", body.hitbox.velocity);
@ -166,20 +166,20 @@ void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
dynamic::get_vec(tsfmap, "size", transform.size); dynamic::get_vec(tsfmap, "size", transform.size);
dynamic::get_mat(tsfmap, "rot", transform.rot); dynamic::get_mat(tsfmap, "rot", transform.rot);
} }
std::string rigName = rig.config->getName(); std::string skeletonName = skeleton.config->getName();
map->str("rig", rigName); map->str("skeleton", skeletonName);
if (rigName != rig.config->getName()) { if (skeletonName != skeleton.config->getName()) {
rig.config = level->content->getRig(rigName); skeleton.config = level->content->getRig(skeletonName);
} }
if (auto rigmap = map->map(COMP_MODELTREE)) { if (auto skeletonmap = map->map(COMP_SKELETON)) {
if (auto texturesmap = rigmap->map("textures")) { if (auto texturesmap = skeletonmap->map("textures")) {
for (auto& [slot, _] : texturesmap->values) { for (auto& [slot, _] : texturesmap->values) {
texturesmap->str(slot, rig.textures[slot]); texturesmap->str(slot, skeleton.textures[slot]);
} }
} }
if (auto posearr = rigmap->list("pose")) { if (auto posearr = skeletonmap->list("pose")) {
for (size_t i = 0; i < std::min(rig.pose.matrices.size(), posearr->size()); i++) { for (size_t i = 0; i < std::min(skeleton.pose.matrices.size(), posearr->size()); i++) {
dynamic::get_mat(posearr, i, rig.pose.matrices[i]); dynamic::get_mat(posearr, i, skeleton.pose.matrices[i]);
} }
} }
} }
@ -237,21 +237,21 @@ dynamic::Value Entities::serialize(const Entity& entity) {
} }
} }
} }
auto& rig = entity.getModeltree(); auto& skeleton = entity.getSkeleton();
if (rig.config->getName() != def.rigName) { if (skeleton.config->getName() != def.skeletonName) {
root->put("rig", rig.config->getName()); root->put("skeleton", skeleton.config->getName());
} }
if (def.save.rig.pose || def.save.rig.textures) { if (def.save.skeleton.pose || def.save.skeleton.textures) {
auto& rigmap = root->putMap(COMP_MODELTREE); auto& skeletonmap = root->putMap(COMP_SKELETON);
if (def.save.rig.textures) { if (def.save.skeleton.textures) {
auto& map = rigmap.putMap("textures"); auto& map = skeletonmap.putMap("textures");
for (auto& [slot, texture] : rig.textures) { for (auto& [slot, texture] : skeleton.textures) {
map.put(slot, texture); map.put(slot, texture);
} }
} }
if (def.save.rig.pose) { if (def.save.skeleton.pose) {
auto& list = rigmap.putList("pose"); auto& list = skeletonmap.putList("pose");
for (auto& mat : rig.pose.matrices) { for (auto& mat : skeleton.pose.matrices) {
list.put(dynamic::to_value(mat)); list.put(dynamic::to_value(mat));
} }
} }
@ -390,16 +390,16 @@ void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum,
scripting::on_entities_render(); scripting::on_entities_render();
} }
auto view = registry.view<Transform, rigging::Rig>(); auto view = registry.view<Transform, rigging::Skeleton>();
for (auto [entity, transform, rig] : view.each()) { for (auto [entity, transform, skeleton] : view.each()) {
if (transform.dirty) { if (transform.dirty) {
transform.refresh(); transform.refresh();
} }
const auto& pos = transform.pos; const auto& pos = transform.pos;
const auto& size = transform.size; const auto& size = transform.size;
if (frustum.isBoxVisible(pos-size, pos+size)) { if (frustum.isBoxVisible(pos-size, pos+size)) {
const auto* rigConfig = rig.config; const auto* rigConfig = skeleton.config;
rigConfig->render(assets, batch, rig, transform.combined); rigConfig->render(assets, batch, skeleton, transform.combined);
} }
} }
} }

View File

@ -94,8 +94,8 @@ class Frustum;
class Entities; class Entities;
namespace rigging { namespace rigging {
struct Rig; struct Skeleton;
class RigConfig; class SkeletonConfig;
} }
class Entity { class Entity {
@ -136,9 +136,9 @@ public:
return registry.get<ScriptComponents>(entity); return registry.get<ScriptComponents>(entity);
} }
rigging::Rig& getModeltree() const; rigging::Skeleton& getSkeleton() const;
void setRig(const rigging::RigConfig* rigConfig); void setRig(const rigging::SkeletonConfig* rigConfig);
entityid_t getUID() const { entityid_t getUID() const {
return registry.get<EntityId>(entity).uid; return registry.get<EntityId>(entity).uid;

View File

@ -10,7 +10,7 @@
#include "../physics/Hitbox.hpp" #include "../physics/Hitbox.hpp"
namespace rigging { namespace rigging {
class RigConfig; class SkeletonConfig;
} }
struct EntityDef { struct EntityDef {
@ -23,14 +23,14 @@ struct EntityDef {
glm::vec3 hitbox {0.25f}; glm::vec3 hitbox {0.25f};
std::vector<std::pair<size_t, AABB>> boxSensors {}; std::vector<std::pair<size_t, AABB>> boxSensors {};
std::vector<std::pair<size_t, float>> radialSensors {}; std::vector<std::pair<size_t, float>> radialSensors {};
std::string rigName = name; std::string skeletonName = name;
struct { struct {
bool enabled = true; bool enabled = true;
struct { struct {
bool textures = false; bool textures = false;
bool pose = false; bool pose = false;
} rig; } skeleton;
struct { struct {
bool velocity = true; bool velocity = true;
bool settings = false; bool settings = false;
@ -39,7 +39,7 @@ struct EntityDef {
struct { struct {
entityid_t id; entityid_t id;
rigging::RigConfig* rig; rigging::SkeletonConfig* skeleton;
} rt {}; } rt {};
EntityDef(const std::string& name) : name(name) {} EntityDef(const std::string& name) : name(name) {}

View File

@ -7,18 +7,18 @@
using namespace rigging; using namespace rigging;
RigNode::RigNode( Bone::Bone(
size_t index, size_t index,
std::string name, std::string name,
std::string model, std::string model,
std::vector<std::unique_ptr<RigNode>> subnodes) std::vector<std::unique_ptr<Bone>> bones)
: index(index), : index(index),
name(std::move(name)), name(std::move(name)),
modelName(std::move(model)), modelName(std::move(model)),
subnodes(std::move(subnodes)) bones(std::move(bones))
{} {}
void RigNode::setModel(const std::string& name) { void Bone::setModel(const std::string& name) {
if (modelName == name) { if (modelName == name) {
return; return;
} }
@ -26,83 +26,83 @@ void RigNode::setModel(const std::string& name) {
modelUpdated = true; modelUpdated = true;
} }
void RigNode::refreshModel(const Assets* assets) { void Bone::refreshModel(const Assets* assets) {
if (modelUpdated) { if (modelUpdated) {
model = assets->get<model::Model>(modelName); model = assets->get<model::Model>(modelName);
modelUpdated = false; modelUpdated = false;
} }
} }
static void get_all_nodes(std::vector<RigNode*>& nodes, RigNode* node) { static void get_all_nodes(std::vector<Bone*>& nodes, Bone* node) {
nodes[node->getIndex()] = node; nodes[node->getIndex()] = node;
for (auto& subnode : node->getSubnodes()) { for (auto& subnode : node->getSubnodes()) {
get_all_nodes(nodes, subnode.get()); get_all_nodes(nodes, subnode.get());
} }
} }
RigConfig::RigConfig(const std::string& name, std::unique_ptr<RigNode> root, size_t nodesCount) SkeletonConfig::SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root, size_t nodesCount)
: name(name), root(std::move(root)), nodes(nodesCount) { : name(name), root(std::move(root)), nodes(nodesCount) {
get_all_nodes(nodes, this->root.get()); get_all_nodes(nodes, this->root.get());
} }
size_t RigConfig::update( size_t SkeletonConfig::update(
size_t index, size_t index,
Rig& rig, Skeleton& skeleton,
RigNode* node, Bone* node,
glm::mat4 matrix) const glm::mat4 matrix) const
{ {
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index]; skeleton.calculated.matrices[index] = matrix * skeleton.pose.matrices[index];
size_t count = 1; size_t count = 1;
for (auto& subnode : node->getSubnodes()) { for (auto& subnode : node->getSubnodes()) {
count += update(index+count, rig, subnode.get(), rig.calculated.matrices[index]); count += update(index+count, skeleton, subnode.get(), skeleton.calculated.matrices[index]);
} }
return count; return count;
} }
void RigConfig::update(Rig& rig, glm::mat4 matrix) const { void SkeletonConfig::update(Skeleton& skeleton, glm::mat4 matrix) const {
update(0, rig, root.get(), matrix); update(0, skeleton, root.get(), matrix);
} }
void RigConfig::render( void SkeletonConfig::render(
Assets* assets, Assets* assets,
ModelBatch& batch, ModelBatch& batch,
Rig& rig, Skeleton& skeleton,
const glm::mat4& matrix) const const glm::mat4& matrix) const
{ {
update(rig, matrix); update(skeleton, matrix);
for (size_t i = 0; i < nodes.size(); i++) { for (size_t i = 0; i < nodes.size(); i++) {
auto* node = nodes[i]; auto* node = nodes[i];
node->refreshModel(assets); node->refreshModel(assets);
if (auto model = node->getModel()) { if (auto model = node->getModel()) {
batch.pushMatrix(rig.calculated.matrices[i]); batch.pushMatrix(skeleton.calculated.matrices[i]);
batch.draw(model, &rig.textures); batch.draw(model, &skeleton.textures);
batch.popMatrix(); batch.popMatrix();
} }
} }
} }
static std::tuple<size_t, std::unique_ptr<RigNode>> read_node( static std::tuple<size_t, std::unique_ptr<Bone>> read_node(
dynamic::Map* root, size_t index dynamic::Map* root, size_t index
) { ) {
std::string name; std::string name;
std::string model; std::string model;
root->str("name", name); root->str("name", name);
root->str("model", model); root->str("model", model);
std::vector<std::unique_ptr<RigNode>> subnodes; std::vector<std::unique_ptr<Bone>> bones;
size_t count = 1; size_t count = 1;
if (auto nodesList = root->list("nodes")) { if (auto nodesList = root->list("nodes")) {
for (size_t i = 0; i < nodesList->size(); i++) { for (size_t i = 0; i < nodesList->size(); i++) {
if (const auto& map = nodesList->map(i)) { if (const auto& map = nodesList->map(i)) {
auto [subcount, subNode] = read_node(map.get(), index+count); auto [subcount, subNode] = read_node(map.get(), index+count);
subcount += count; subcount += count;
subnodes.push_back(std::move(subNode)); bones.push_back(std::move(subNode));
} }
} }
} }
return {index + count, std::make_unique<RigNode>(index, name, model, std::move(subnodes))}; return {index + count, std::make_unique<Bone>(index, name, model, std::move(bones))};
} }
std::unique_ptr<RigConfig> RigConfig::parse( std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
std::string_view src, std::string_view src,
std::string_view file, std::string_view file,
std::string_view name std::string_view name
@ -113,5 +113,5 @@ std::unique_ptr<RigConfig> RigConfig::parse(
throw std::runtime_error("missing 'root' element"); throw std::runtime_error("missing 'root' element");
} }
auto [count, rootNode] = read_node(rootNodeMap.get(), 0); auto [count, rootNode] = read_node(rootNodeMap.get(), 0);
return std::make_unique<RigConfig>(std::string(name), std::move(rootNode), count); return std::make_unique<SkeletonConfig>(std::string(name), std::move(rootNode), count);
} }

View File

@ -17,8 +17,8 @@ namespace model {
} }
namespace rigging { namespace rigging {
struct Rig; struct Skeleton;
class RigConfig; class SkeletonConfig;
struct Pose { struct Pose {
std::vector<glm::mat4> matrices; std::vector<glm::mat4> matrices;
@ -28,19 +28,19 @@ namespace rigging {
} }
}; };
class RigNode { class Bone {
size_t index; size_t index;
std::string name; std::string name;
std::string modelName; std::string modelName;
std::vector<std::unique_ptr<RigNode>> subnodes; std::vector<std::unique_ptr<Bone>> bones;
model::Model* model = nullptr; model::Model* model = nullptr;
bool modelUpdated = true; bool modelUpdated = true;
public: public:
RigNode( Bone(
size_t index, size_t index,
std::string name, std::string name,
std::string model, std::string model,
std::vector<std::unique_ptr<RigNode>> subnodes); std::vector<std::unique_ptr<Bone>> bones);
void setModel(const std::string& name); void setModel(const std::string& name);
@ -59,59 +59,59 @@ namespace rigging {
} }
const auto& getSubnodes() const { const auto& getSubnodes() const {
return subnodes; return bones;
} }
}; };
struct Rig { struct Skeleton {
const RigConfig* config; const SkeletonConfig* config;
Pose pose; Pose pose;
Pose calculated; Pose calculated;
std::unordered_map<std::string, std::string> textures; std::unordered_map<std::string, std::string> textures;
}; };
class RigConfig { class SkeletonConfig {
std::string name; std::string name;
std::unique_ptr<RigNode> root; std::unique_ptr<Bone> root;
std::unordered_map<std::string, size_t> indices; std::unordered_map<std::string, size_t> indices;
/// Nodes and indices are ordered from root to subnodes. /// Nodes and indices are ordered from root to bones.
/// Example: /// Example:
/// 0 - root /// 0 - root
/// 1 --- sub1 /// 1 --- sub1
/// 2 ----- subsub1 /// 2 ----- subsub1
/// 3 --- sub2 /// 3 --- sub2
std::vector<RigNode*> nodes; std::vector<Bone*> nodes;
size_t update( size_t update(
size_t index, size_t index,
Rig& rig, Skeleton& skeleton,
RigNode* node, Bone* node,
glm::mat4 matrix) const; glm::mat4 matrix) const;
public: public:
RigConfig(const std::string& name, std::unique_ptr<RigNode> root, SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root,
size_t nodesCount); size_t nodesCount);
void update(Rig& rig, glm::mat4 matrix) const; void update(Skeleton& skeleton, glm::mat4 matrix) const;
void render( void render(
Assets* assets, Assets* assets,
ModelBatch& batch, ModelBatch& batch,
Rig& rig, Skeleton& skeleton,
const glm::mat4& matrix) const; const glm::mat4& matrix) const;
Rig instance() const { Skeleton instance() const {
return Rig { return Skeleton {
this, Pose(nodes.size()), Pose(nodes.size()), {} this, Pose(nodes.size()), Pose(nodes.size()), {}
}; };
} }
static std::unique_ptr<RigConfig> parse( static std::unique_ptr<SkeletonConfig> parse(
std::string_view src, std::string_view src,
std::string_view file, std::string_view file,
std::string_view name std::string_view name
); );
const std::vector<RigNode*>& getNodes() const { const std::vector<Bone*>& getNodes() const {
return nodes; return nodes;
} }