rename rig to skeleton
This commit is contained in:
parent
91230ecbeb
commit
60f4f33180
@ -2,6 +2,6 @@
|
||||
"components": [
|
||||
"falling_block"
|
||||
],
|
||||
"rig-name": "base:block",
|
||||
"skeleton-name": "base:block",
|
||||
"hitbox": [0.8, 0.8, 0.8]
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
local tsf = entity.transform
|
||||
local body = entity.rigidbody
|
||||
local rig = entity.modeltree
|
||||
local rig = entity.skeleton
|
||||
|
||||
inair = true
|
||||
ready = false
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
local tsf = entity.transform
|
||||
local body = entity.rigidbody
|
||||
local rig = entity.modeltree
|
||||
local rig = entity.skeleton
|
||||
|
||||
ARGS = ARGS or {}
|
||||
local blockid = ARGS.block
|
||||
|
||||
@ -35,15 +35,15 @@ local function new_Rigidbody(eid)
|
||||
return setmetatable({eid=eid}, Rigidbody)
|
||||
end
|
||||
|
||||
local Modeltree = {__index={
|
||||
get_model=function(self, i) return __modeltree.get_model(self.eid, i) end,
|
||||
get_matrix=function(self, i) return __modeltree.get_matrix(self.eid, i) end,
|
||||
set_matrix=function(self, i, m) return __modeltree.set_matrix(self.eid, i, m) end,
|
||||
set_texture=function(self, s, s2) return __modeltree.set_texture(self.eid, s, s2) end,
|
||||
local Skeleton = {__index={
|
||||
get_model=function(self, i) return __skeleton.get_model(self.eid, i) end,
|
||||
get_matrix=function(self, i) return __skeleton.get_matrix(self.eid, i) end,
|
||||
set_matrix=function(self, i, m) return __skeleton.set_matrix(self.eid, i, m) end,
|
||||
set_texture=function(self, s, s2) return __skeleton.set_texture(self.eid, s, s2) end,
|
||||
}}
|
||||
|
||||
local function new_Modeltree(eid)
|
||||
return setmetatable({eid=eid}, Modeltree)
|
||||
local function new_Skeleton(eid)
|
||||
return setmetatable({eid=eid}, Skeleton)
|
||||
end
|
||||
|
||||
-- Entity class
|
||||
@ -63,7 +63,7 @@ return {
|
||||
local entity = setmetatable({eid=eid}, Entity)
|
||||
entity.transform = new_Transform(eid)
|
||||
entity.rigidbody = new_Rigidbody(eid)
|
||||
entity.modeltree = new_Modeltree(eid)
|
||||
entity.skeleton = new_Skeleton(eid)
|
||||
entity.components = {}
|
||||
entities[eid] = entity;
|
||||
return entity
|
||||
|
||||
@ -50,6 +50,6 @@ inline const std::string FONTS_FOLDER = "fonts";
|
||||
inline const std::string LAYOUTS_FOLDER = "layouts";
|
||||
inline const std::string SOUNDS_FOLDER = "sounds";
|
||||
inline const std::string MODELS_FOLDER = "models";
|
||||
inline const std::string RIGS_FOLDER = "rigs";
|
||||
inline const std::string SKELETONS_FOLDER = "skeletons";
|
||||
|
||||
#endif // CONSTANTS_HPP_
|
||||
|
||||
@ -30,11 +30,11 @@ Content::Content(
|
||||
ContentUnitDefs<EntityDef> entities,
|
||||
UptrsMap<std::string, ContentPackRuntime> packs,
|
||||
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
||||
UptrsMap<std::string, rigging::RigConfig> rigs
|
||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons
|
||||
) : indices(std::move(indices)),
|
||||
packs(std::move(packs)),
|
||||
blockMaterials(std::move(blockMaterials)),
|
||||
rigs(std::move(rigs)),
|
||||
skeletons(std::move(skeletons)),
|
||||
blocks(std::move(blocks)),
|
||||
items(std::move(items)),
|
||||
entities(std::move(entities)),
|
||||
@ -44,9 +44,9 @@ Content::Content(
|
||||
Content::~Content() {
|
||||
}
|
||||
|
||||
const rigging::RigConfig* Content::getRig(const std::string& id) const {
|
||||
auto found = rigs.find(id);
|
||||
if (found == rigs.end()) {
|
||||
const rigging::SkeletonConfig* Content::getRig(const std::string& id) const {
|
||||
auto found = skeletons.find(id);
|
||||
if (found == skeletons.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return found->second.get();
|
||||
|
||||
@ -22,7 +22,7 @@ class Content;
|
||||
class ContentPackRuntime;
|
||||
|
||||
namespace rigging {
|
||||
class RigConfig;
|
||||
class SkeletonConfig;
|
||||
}
|
||||
|
||||
enum class contenttype {
|
||||
@ -116,7 +116,7 @@ class Content {
|
||||
std::unique_ptr<ContentIndices> indices;
|
||||
UptrsMap<std::string, ContentPackRuntime> packs;
|
||||
UptrsMap<std::string, BlockMaterial> blockMaterials;
|
||||
UptrsMap<std::string, rigging::RigConfig> rigs;
|
||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
|
||||
public:
|
||||
ContentUnitDefs<Block> blocks;
|
||||
ContentUnitDefs<ItemDef> items;
|
||||
@ -131,7 +131,7 @@ public:
|
||||
ContentUnitDefs<EntityDef> entities,
|
||||
UptrsMap<std::string, ContentPackRuntime> packs,
|
||||
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
||||
UptrsMap<std::string, rigging::RigConfig> rigs
|
||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons
|
||||
);
|
||||
~Content();
|
||||
|
||||
@ -139,7 +139,7 @@ public:
|
||||
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 ContentPackRuntime* getPackRuntime(const std::string& id) const;
|
||||
|
||||
|
||||
@ -8,8 +8,8 @@ void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) {
|
||||
packs[pack->getId()] = std::move(pack);
|
||||
}
|
||||
|
||||
void ContentBuilder::add(std::unique_ptr<rigging::RigConfig> rig) {
|
||||
rigs[rig->getName()] = std::move(rig);
|
||||
void ContentBuilder::add(std::unique_ptr<rigging::SkeletonConfig> skeleton) {
|
||||
skeletons[skeleton->getName()] = std::move(skeleton);
|
||||
}
|
||||
|
||||
BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) {
|
||||
@ -75,7 +75,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
|
||||
entities.build(),
|
||||
std::move(packs),
|
||||
std::move(blockMaterials),
|
||||
std::move(rigs)
|
||||
std::move(skeletons)
|
||||
);
|
||||
|
||||
// Now, it's time to resolve foreign keys
|
||||
|
||||
@ -50,7 +50,7 @@ public:
|
||||
|
||||
class ContentBuilder {
|
||||
UptrsMap<std::string, BlockMaterial> blockMaterials;
|
||||
UptrsMap<std::string, rigging::RigConfig> rigs;
|
||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
|
||||
UptrsMap<std::string, ContentPackRuntime> packs;
|
||||
std::unordered_map<std::string, contenttype> allNames;
|
||||
public:
|
||||
@ -61,7 +61,7 @@ public:
|
||||
~ContentBuilder();
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@ -336,8 +336,8 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs
|
||||
}
|
||||
}
|
||||
root->flag("save", def.save.enabled);
|
||||
root->flag("save-rig-pose", def.save.rig.pose);
|
||||
root->flag("save-rig-textures", def.save.rig.textures);
|
||||
root->flag("save-skeleton-pose", def.save.skeleton.pose);
|
||||
root->flag("save-skeleton-textures", def.save.skeleton.textures);
|
||||
root->flag("save-body-velocity", def.save.body.velocity);
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -468,13 +468,13 @@ void ContentLoader::load() {
|
||||
}
|
||||
}
|
||||
|
||||
fs::path rigsDir = folder / fs::u8path("rigs");
|
||||
if (fs::is_directory(rigsDir)) {
|
||||
for (const auto& entry : fs::directory_iterator(rigsDir)) {
|
||||
fs::path skeletonsDir = folder / fs::u8path("skeletons");
|
||||
if (fs::is_directory(skeletonsDir)) {
|
||||
for (const auto& entry : fs::directory_iterator(skeletonsDir)) {
|
||||
const fs::path& file = entry.path();
|
||||
std::string name = pack->id+":"+file.stem().u8string();
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ extern const luaL_Reg vec4lib [];
|
||||
extern const luaL_Reg worldlib [];
|
||||
|
||||
// Components
|
||||
extern const luaL_Reg modeltreelib [];
|
||||
extern const luaL_Reg skeletonlib [];
|
||||
extern const luaL_Reg rigidbodylib [];
|
||||
extern const luaL_Reg transformlib [];
|
||||
|
||||
|
||||
@ -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}
|
||||
};
|
||||
56
src/logic/scripting/lua/lib__skeleton.cpp
Normal file
56
src/logic/scripting/lua/lib__skeleton.cpp
Normal 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}
|
||||
};
|
||||
@ -34,10 +34,10 @@ static int l_entity_despawn(lua::State* L) {
|
||||
|
||||
static int l_entity_set_rig(lua::State* L) {
|
||||
if (auto entity = get_entity(L, 1)) {
|
||||
std::string rigName = lua::require_string(L, 2);
|
||||
auto rigConfig = content->getRig(rigName);
|
||||
std::string skeletonName = lua::require_string(L, 2);
|
||||
auto rigConfig = content->getRig(skeletonName);
|
||||
if (rigConfig == nullptr) {
|
||||
throw std::runtime_error("rig not found '"+rigName+"'");
|
||||
throw std::runtime_error("skeleton not found '"+skeletonName+"'");
|
||||
}
|
||||
entity->setRig(rigConfig);
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ static void create_libs(lua::State* L) {
|
||||
openlib(L, "entities", entitylib);
|
||||
|
||||
// components
|
||||
openlib(L, "__modeltree", modeltreelib);
|
||||
openlib(L, "__skeleton", skeletonlib);
|
||||
openlib(L, "__rigidbody", rigidbodylib);
|
||||
openlib(L, "__transform", transformlib);
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ static debug::Logger logger("entities");
|
||||
|
||||
static inline std::string COMP_TRANSFORM = "transform";
|
||||
static inline std::string COMP_RIGIDBODY = "rigidbody";
|
||||
static inline std::string COMP_MODELTREE = "modeltree";
|
||||
static inline std::string COMP_SKELETON = "skeleton";
|
||||
|
||||
void Transform::refresh() {
|
||||
combined = glm::mat4(1.0f);
|
||||
@ -38,15 +38,15 @@ void Entity::destroy() {
|
||||
}
|
||||
}
|
||||
|
||||
rigging::Rig& Entity::getModeltree() const {
|
||||
return registry.get<rigging::Rig>(entity);
|
||||
rigging::Skeleton& Entity::getSkeleton() const {
|
||||
return registry.get<rigging::Skeleton>(entity);
|
||||
}
|
||||
|
||||
void Entity::setRig(const rigging::RigConfig* rigConfig) {
|
||||
auto& rig = registry.get<rigging::Rig>(entity);
|
||||
rig.config = rigConfig;
|
||||
rig.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
|
||||
rig.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
|
||||
void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
|
||||
auto& skeleton = registry.get<rigging::Skeleton>(entity);
|
||||
skeleton.config = rigConfig;
|
||||
skeleton.pose.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) {
|
||||
@ -70,9 +70,9 @@ entityid_t Entities::spawn(
|
||||
dynamic::Map_sptr saved,
|
||||
entityid_t uid)
|
||||
{
|
||||
auto rig = level->content->getRig(def.rigName);
|
||||
if (rig == nullptr) {
|
||||
throw std::runtime_error("rig "+def.rigName+" not found");
|
||||
auto skeleton = level->content->getRig(def.skeletonName);
|
||||
if (skeleton == nullptr) {
|
||||
throw std::runtime_error("skeleton "+def.skeletonName+" not found");
|
||||
}
|
||||
auto entity = registry.create();
|
||||
entityid_t id;
|
||||
@ -107,7 +107,7 @@ entityid_t Entities::spawn(
|
||||
auto& scripting = registry.emplace<ScriptComponents>(entity);
|
||||
entities[id] = entity;
|
||||
uids[entity] = id;
|
||||
registry.emplace<rigging::Rig>(entity, rig->instance());
|
||||
registry.emplace<rigging::Skeleton>(entity, skeleton->instance());
|
||||
for (auto& componentName : def.components) {
|
||||
auto component = std::make_unique<UserComponent>(
|
||||
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) {
|
||||
auto& transform = entity.getTransform();
|
||||
auto& body = entity.getRigidbody();
|
||||
auto& rig = entity.getModeltree();
|
||||
auto& skeleton = entity.getSkeleton();
|
||||
|
||||
if (auto bodymap = map->map(COMP_RIGIDBODY)) {
|
||||
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_mat(tsfmap, "rot", transform.rot);
|
||||
}
|
||||
std::string rigName = rig.config->getName();
|
||||
map->str("rig", rigName);
|
||||
if (rigName != rig.config->getName()) {
|
||||
rig.config = level->content->getRig(rigName);
|
||||
std::string skeletonName = skeleton.config->getName();
|
||||
map->str("skeleton", skeletonName);
|
||||
if (skeletonName != skeleton.config->getName()) {
|
||||
skeleton.config = level->content->getRig(skeletonName);
|
||||
}
|
||||
if (auto rigmap = map->map(COMP_MODELTREE)) {
|
||||
if (auto texturesmap = rigmap->map("textures")) {
|
||||
if (auto skeletonmap = map->map(COMP_SKELETON)) {
|
||||
if (auto texturesmap = skeletonmap->map("textures")) {
|
||||
for (auto& [slot, _] : texturesmap->values) {
|
||||
texturesmap->str(slot, rig.textures[slot]);
|
||||
texturesmap->str(slot, skeleton.textures[slot]);
|
||||
}
|
||||
}
|
||||
if (auto posearr = rigmap->list("pose")) {
|
||||
for (size_t i = 0; i < std::min(rig.pose.matrices.size(), posearr->size()); i++) {
|
||||
dynamic::get_mat(posearr, i, rig.pose.matrices[i]);
|
||||
if (auto posearr = skeletonmap->list("pose")) {
|
||||
for (size_t i = 0; i < std::min(skeleton.pose.matrices.size(), posearr->size()); 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();
|
||||
if (rig.config->getName() != def.rigName) {
|
||||
root->put("rig", rig.config->getName());
|
||||
auto& skeleton = entity.getSkeleton();
|
||||
if (skeleton.config->getName() != def.skeletonName) {
|
||||
root->put("skeleton", skeleton.config->getName());
|
||||
}
|
||||
if (def.save.rig.pose || def.save.rig.textures) {
|
||||
auto& rigmap = root->putMap(COMP_MODELTREE);
|
||||
if (def.save.rig.textures) {
|
||||
auto& map = rigmap.putMap("textures");
|
||||
for (auto& [slot, texture] : rig.textures) {
|
||||
if (def.save.skeleton.pose || def.save.skeleton.textures) {
|
||||
auto& skeletonmap = root->putMap(COMP_SKELETON);
|
||||
if (def.save.skeleton.textures) {
|
||||
auto& map = skeletonmap.putMap("textures");
|
||||
for (auto& [slot, texture] : skeleton.textures) {
|
||||
map.put(slot, texture);
|
||||
}
|
||||
}
|
||||
if (def.save.rig.pose) {
|
||||
auto& list = rigmap.putList("pose");
|
||||
for (auto& mat : rig.pose.matrices) {
|
||||
if (def.save.skeleton.pose) {
|
||||
auto& list = skeletonmap.putList("pose");
|
||||
for (auto& mat : skeleton.pose.matrices) {
|
||||
list.put(dynamic::to_value(mat));
|
||||
}
|
||||
}
|
||||
@ -390,16 +390,16 @@ void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum,
|
||||
scripting::on_entities_render();
|
||||
}
|
||||
|
||||
auto view = registry.view<Transform, rigging::Rig>();
|
||||
for (auto [entity, transform, rig] : view.each()) {
|
||||
auto view = registry.view<Transform, rigging::Skeleton>();
|
||||
for (auto [entity, transform, skeleton] : view.each()) {
|
||||
if (transform.dirty) {
|
||||
transform.refresh();
|
||||
}
|
||||
const auto& pos = transform.pos;
|
||||
const auto& size = transform.size;
|
||||
if (frustum.isBoxVisible(pos-size, pos+size)) {
|
||||
const auto* rigConfig = rig.config;
|
||||
rigConfig->render(assets, batch, rig, transform.combined);
|
||||
const auto* rigConfig = skeleton.config;
|
||||
rigConfig->render(assets, batch, skeleton, transform.combined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,8 +94,8 @@ class Frustum;
|
||||
class Entities;
|
||||
|
||||
namespace rigging {
|
||||
struct Rig;
|
||||
class RigConfig;
|
||||
struct Skeleton;
|
||||
class SkeletonConfig;
|
||||
}
|
||||
|
||||
class Entity {
|
||||
@ -136,9 +136,9 @@ public:
|
||||
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 {
|
||||
return registry.get<EntityId>(entity).uid;
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
#include "../physics/Hitbox.hpp"
|
||||
|
||||
namespace rigging {
|
||||
class RigConfig;
|
||||
class SkeletonConfig;
|
||||
}
|
||||
|
||||
struct EntityDef {
|
||||
@ -23,14 +23,14 @@ struct EntityDef {
|
||||
glm::vec3 hitbox {0.25f};
|
||||
std::vector<std::pair<size_t, AABB>> boxSensors {};
|
||||
std::vector<std::pair<size_t, float>> radialSensors {};
|
||||
std::string rigName = name;
|
||||
std::string skeletonName = name;
|
||||
|
||||
struct {
|
||||
bool enabled = true;
|
||||
struct {
|
||||
bool textures = false;
|
||||
bool pose = false;
|
||||
} rig;
|
||||
} skeleton;
|
||||
struct {
|
||||
bool velocity = true;
|
||||
bool settings = false;
|
||||
@ -39,7 +39,7 @@ struct EntityDef {
|
||||
|
||||
struct {
|
||||
entityid_t id;
|
||||
rigging::RigConfig* rig;
|
||||
rigging::SkeletonConfig* skeleton;
|
||||
} rt {};
|
||||
|
||||
EntityDef(const std::string& name) : name(name) {}
|
||||
|
||||
@ -7,18 +7,18 @@
|
||||
|
||||
using namespace rigging;
|
||||
|
||||
RigNode::RigNode(
|
||||
Bone::Bone(
|
||||
size_t index,
|
||||
std::string name,
|
||||
std::string model,
|
||||
std::vector<std::unique_ptr<RigNode>> subnodes)
|
||||
std::vector<std::unique_ptr<Bone>> bones)
|
||||
: index(index),
|
||||
name(std::move(name)),
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@ -26,83 +26,83 @@ void RigNode::setModel(const std::string& name) {
|
||||
modelUpdated = true;
|
||||
}
|
||||
|
||||
void RigNode::refreshModel(const Assets* assets) {
|
||||
void Bone::refreshModel(const Assets* assets) {
|
||||
if (modelUpdated) {
|
||||
model = assets->get<model::Model>(modelName);
|
||||
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;
|
||||
for (auto& subnode : node->getSubnodes()) {
|
||||
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) {
|
||||
get_all_nodes(nodes, this->root.get());
|
||||
}
|
||||
|
||||
size_t RigConfig::update(
|
||||
size_t SkeletonConfig::update(
|
||||
size_t index,
|
||||
Rig& rig,
|
||||
RigNode* node,
|
||||
Skeleton& skeleton,
|
||||
Bone* node,
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
void RigConfig::update(Rig& rig, glm::mat4 matrix) const {
|
||||
update(0, rig, root.get(), matrix);
|
||||
void SkeletonConfig::update(Skeleton& skeleton, glm::mat4 matrix) const {
|
||||
update(0, skeleton, root.get(), matrix);
|
||||
}
|
||||
|
||||
void RigConfig::render(
|
||||
void SkeletonConfig::render(
|
||||
Assets* assets,
|
||||
ModelBatch& batch,
|
||||
Rig& rig,
|
||||
Skeleton& skeleton,
|
||||
const glm::mat4& matrix) const
|
||||
{
|
||||
update(rig, matrix);
|
||||
update(skeleton, matrix);
|
||||
for (size_t i = 0; i < nodes.size(); i++) {
|
||||
auto* node = nodes[i];
|
||||
node->refreshModel(assets);
|
||||
if (auto model = node->getModel()) {
|
||||
batch.pushMatrix(rig.calculated.matrices[i]);
|
||||
batch.draw(model, &rig.textures);
|
||||
batch.pushMatrix(skeleton.calculated.matrices[i]);
|
||||
batch.draw(model, &skeleton.textures);
|
||||
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
|
||||
) {
|
||||
std::string name;
|
||||
std::string model;
|
||||
root->str("name", name);
|
||||
root->str("model", model);
|
||||
std::vector<std::unique_ptr<RigNode>> subnodes;
|
||||
std::vector<std::unique_ptr<Bone>> bones;
|
||||
size_t count = 1;
|
||||
if (auto nodesList = root->list("nodes")) {
|
||||
for (size_t i = 0; i < nodesList->size(); i++) {
|
||||
if (const auto& map = nodesList->map(i)) {
|
||||
auto [subcount, subNode] = read_node(map.get(), index+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 file,
|
||||
std::string_view name
|
||||
@ -113,5 +113,5 @@ std::unique_ptr<RigConfig> RigConfig::parse(
|
||||
throw std::runtime_error("missing 'root' element");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@ -17,8 +17,8 @@ namespace model {
|
||||
}
|
||||
|
||||
namespace rigging {
|
||||
struct Rig;
|
||||
class RigConfig;
|
||||
struct Skeleton;
|
||||
class SkeletonConfig;
|
||||
|
||||
struct Pose {
|
||||
std::vector<glm::mat4> matrices;
|
||||
@ -28,19 +28,19 @@ namespace rigging {
|
||||
}
|
||||
};
|
||||
|
||||
class RigNode {
|
||||
class Bone {
|
||||
size_t index;
|
||||
std::string name;
|
||||
std::string modelName;
|
||||
std::vector<std::unique_ptr<RigNode>> subnodes;
|
||||
std::vector<std::unique_ptr<Bone>> bones;
|
||||
model::Model* model = nullptr;
|
||||
bool modelUpdated = true;
|
||||
public:
|
||||
RigNode(
|
||||
Bone(
|
||||
size_t index,
|
||||
std::string name,
|
||||
std::string model,
|
||||
std::vector<std::unique_ptr<RigNode>> subnodes);
|
||||
std::vector<std::unique_ptr<Bone>> bones);
|
||||
|
||||
void setModel(const std::string& name);
|
||||
|
||||
@ -59,59 +59,59 @@ namespace rigging {
|
||||
}
|
||||
|
||||
const auto& getSubnodes() const {
|
||||
return subnodes;
|
||||
return bones;
|
||||
}
|
||||
};
|
||||
|
||||
struct Rig {
|
||||
const RigConfig* config;
|
||||
struct Skeleton {
|
||||
const SkeletonConfig* config;
|
||||
Pose pose;
|
||||
Pose calculated;
|
||||
std::unordered_map<std::string, std::string> textures;
|
||||
};
|
||||
|
||||
class RigConfig {
|
||||
class SkeletonConfig {
|
||||
std::string name;
|
||||
std::unique_ptr<RigNode> root;
|
||||
std::unique_ptr<Bone> root;
|
||||
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:
|
||||
/// 0 - root
|
||||
/// 1 --- sub1
|
||||
/// 2 ----- subsub1
|
||||
/// 3 --- sub2
|
||||
std::vector<RigNode*> nodes;
|
||||
std::vector<Bone*> nodes;
|
||||
|
||||
size_t update(
|
||||
size_t index,
|
||||
Rig& rig,
|
||||
RigNode* node,
|
||||
Skeleton& skeleton,
|
||||
Bone* node,
|
||||
glm::mat4 matrix) const;
|
||||
public:
|
||||
RigConfig(const std::string& name, std::unique_ptr<RigNode> root,
|
||||
SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root,
|
||||
size_t nodesCount);
|
||||
|
||||
void update(Rig& rig, glm::mat4 matrix) const;
|
||||
void update(Skeleton& skeleton, glm::mat4 matrix) const;
|
||||
void render(
|
||||
Assets* assets,
|
||||
ModelBatch& batch,
|
||||
Rig& rig,
|
||||
Skeleton& skeleton,
|
||||
const glm::mat4& matrix) const;
|
||||
|
||||
Rig instance() const {
|
||||
return Rig {
|
||||
Skeleton instance() const {
|
||||
return Skeleton {
|
||||
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 file,
|
||||
std::string_view name
|
||||
);
|
||||
|
||||
const std::vector<RigNode*>& getNodes() const {
|
||||
const std::vector<Bone*>& getNodes() const {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user