mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 05:51:51 +00:00
fix: destroyed skeleton config access on skeleton reload
This commit is contained in:
parent
33db2633a2
commit
9893c09fd5
9 changed files with 35 additions and 22 deletions
|
|
@ -7,9 +7,9 @@ using namespace rigging;
|
||||||
NamedSkeletons::NamedSkeletons() = default;
|
NamedSkeletons::NamedSkeletons() = default;
|
||||||
|
|
||||||
std::shared_ptr<rigging::Skeleton> NamedSkeletons::createSkeleton(
|
std::shared_ptr<rigging::Skeleton> NamedSkeletons::createSkeleton(
|
||||||
const std::string& name, const SkeletonConfig* config
|
const std::string& name, std::shared_ptr<const SkeletonConfig> config
|
||||||
) {
|
) {
|
||||||
auto skeleton = std::make_shared<Skeleton>(config);
|
auto skeleton = std::make_shared<Skeleton>(std::move(config));
|
||||||
skeletons[name] = skeleton;
|
skeletons[name] = skeleton;
|
||||||
return skeleton;
|
return skeleton;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ public:
|
||||||
NamedSkeletons();
|
NamedSkeletons();
|
||||||
|
|
||||||
std::shared_ptr<rigging::Skeleton> createSkeleton(
|
std::shared_ptr<rigging::Skeleton> createSkeleton(
|
||||||
const std::string& name, const rigging::SkeletonConfig* config
|
const std::string& name,
|
||||||
|
std::shared_ptr<const rigging::SkeletonConfig> config
|
||||||
);
|
);
|
||||||
|
|
||||||
rigging::Skeleton* getSkeleton(const std::string& name);
|
rigging::Skeleton* getSkeleton(const std::string& name);
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,9 @@ WorldRenderer::WorldRenderer(
|
||||||
content.getDefaults()["hand-skeleton"].asString()
|
content.getDefaults()["hand-skeleton"].asString()
|
||||||
);
|
);
|
||||||
hands = std::make_unique<HandsRenderer>(
|
hands = std::make_unique<HandsRenderer>(
|
||||||
assets, *modelBatch, skeletons->createSkeleton("hand", &skeletonConfig)
|
assets,
|
||||||
|
*modelBatch,
|
||||||
|
skeletons->createSkeleton("hand", skeletonConfig.shared_from_this())
|
||||||
);
|
);
|
||||||
shadowMapping = std::make_unique<Shadows>(level);
|
shadowMapping = std::make_unique<Shadows>(level);
|
||||||
debugLines = std::make_unique<DebugLinesRenderer>(level);
|
debugLines = std::make_unique<DebugLinesRenderer>(level);
|
||||||
|
|
|
||||||
|
|
@ -108,13 +108,13 @@ 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 = assets->get<rigging::SkeletonConfig>(skeletonName);
|
auto rigConfig = assets->getShared<rigging::SkeletonConfig>(skeletonName);
|
||||||
if (rigConfig == nullptr) {
|
if (rigConfig == nullptr) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"skeleton not found '" + skeletonName + "'"
|
"skeleton not found '" + skeletonName + "'"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
entity->setRig(rigConfig);
|
entity->setRig(std::move(rigConfig));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -155,7 +155,7 @@ void Entities::loadEntity(const dv::value& map, Entity entity) {
|
||||||
std::string skeletonName = skeleton->config->getName();
|
std::string skeletonName = skeleton->config->getName();
|
||||||
map.at("skeleton-name").get(skeletonName);
|
map.at("skeleton-name").get(skeletonName);
|
||||||
if (skeletonName != skeleton->config->getName()) {
|
if (skeletonName != skeleton->config->getName()) {
|
||||||
skeleton->config = assets->get<rigging::SkeletonConfig>(skeletonName);
|
skeleton->config = assets->getShared<rigging::SkeletonConfig>(skeletonName);
|
||||||
}
|
}
|
||||||
if (auto foundSkeleton = map.at(COMP_SKELETON)) {
|
if (auto foundSkeleton = map.at(COMP_SKELETON)) {
|
||||||
skeleton->deserialize(*foundSkeleton);
|
skeleton->deserialize(*foundSkeleton);
|
||||||
|
|
@ -470,7 +470,7 @@ void Entities::render(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto* rigConfig = skeleton.config;
|
const auto& rigConfig = skeleton.config;
|
||||||
if (rigConfig) {
|
if (rigConfig) {
|
||||||
rigConfig->render(
|
rigConfig->render(
|
||||||
assets, batch, skeleton, transform.rot, pos, size
|
assets, batch, skeleton, transform.rot, pos, size
|
||||||
|
|
|
||||||
|
|
@ -37,15 +37,9 @@ rigging::Skeleton* Entity::getSkeleton() const {
|
||||||
return registry.try_get<rigging::Skeleton>(entity);
|
return registry.try_get<rigging::Skeleton>(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
|
void Entity::setRig(std::shared_ptr<const rigging::SkeletonConfig> rigConfig) {
|
||||||
auto& skeleton = registry.get<rigging::Skeleton>(entity);
|
auto& skeleton = registry.get<rigging::Skeleton>(entity);
|
||||||
skeleton.config = rigConfig;
|
skeleton.setConfig(std::move(rigConfig));
|
||||||
skeleton.pose.matrices.resize(
|
|
||||||
rigConfig->getBones().size(), glm::mat4(1.0f)
|
|
||||||
);
|
|
||||||
skeleton.calculated.matrices.resize(
|
|
||||||
rigConfig->getBones().size(), glm::mat4(1.0f)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dv::value Entity::serialize() const {
|
dv::value Entity::serialize() const {
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ public:
|
||||||
|
|
||||||
rigging::Skeleton* getSkeleton() const;
|
rigging::Skeleton* getSkeleton() const;
|
||||||
|
|
||||||
void setRig(const rigging::SkeletonConfig* rigConfig);
|
void setRig(std::shared_ptr<const rigging::SkeletonConfig> rigConfig);
|
||||||
|
|
||||||
entityid_t getUID() const;
|
entityid_t getUID() const;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ void Bone::setModel(const std::string& name) {
|
||||||
model = {name, nullptr, true};
|
model = {name, nullptr, true};
|
||||||
}
|
}
|
||||||
|
|
||||||
Skeleton::Skeleton(const SkeletonConfig* config)
|
Skeleton::Skeleton(std::shared_ptr<const SkeletonConfig> config)
|
||||||
: config(config),
|
: config(config),
|
||||||
pose(config->getBones().size()),
|
pose(config->getBones().size()),
|
||||||
calculated(config->getBones().size()),
|
calculated(config->getBones().size()),
|
||||||
|
|
@ -86,6 +86,16 @@ void Skeleton::deserialize(const dv::value& root) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Skeleton::setConfig(std::shared_ptr<const SkeletonConfig> rigConfig) {
|
||||||
|
config = std::move(rigConfig);
|
||||||
|
pose.matrices.resize(
|
||||||
|
config->getBones().size(), glm::mat4(1.0f)
|
||||||
|
);
|
||||||
|
calculated.matrices.resize(
|
||||||
|
config->getBones().size(), glm::mat4(1.0f)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
static void get_all_nodes(std::vector<Bone*>& nodes, Bone* node) {
|
static void get_all_nodes(std::vector<Bone*>& nodes, Bone* node) {
|
||||||
nodes[node->getIndex()] = node;
|
nodes[node->getIndex()] = node;
|
||||||
for (auto& subnode : node->getBones()) {
|
for (auto& subnode : node->getBones()) {
|
||||||
|
|
@ -97,6 +107,7 @@ SkeletonConfig::SkeletonConfig(
|
||||||
const std::string& name, std::unique_ptr<Bone> root, size_t nodesCount
|
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) {
|
||||||
|
assert(this->root.get() != nullptr);
|
||||||
get_all_nodes(nodes, this->root.get());
|
get_all_nodes(nodes, this->root.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -159,6 +170,9 @@ void SkeletonConfig::render(
|
||||||
const glm::vec3& position,
|
const glm::vec3& position,
|
||||||
const glm::vec3& scale
|
const glm::vec3& scale
|
||||||
) const {
|
) const {
|
||||||
|
if (skeleton.config->root == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
update(skeleton, rotation, position, scale);
|
update(skeleton, rotation, position, scale);
|
||||||
|
|
||||||
if (!skeleton.visible) {
|
if (!skeleton.visible) {
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ namespace rigging {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Skeleton {
|
struct Skeleton {
|
||||||
const SkeletonConfig* config;
|
std::shared_ptr<const SkeletonConfig> config;
|
||||||
Pose pose;
|
Pose pose;
|
||||||
Pose calculated;
|
Pose calculated;
|
||||||
std::vector<BoneFlags> flags;
|
std::vector<BoneFlags> flags;
|
||||||
|
|
@ -89,13 +89,15 @@ namespace rigging {
|
||||||
|
|
||||||
util::VecInterpolation<3, float> interpolation {false};
|
util::VecInterpolation<3, float> interpolation {false};
|
||||||
|
|
||||||
Skeleton(const SkeletonConfig* config);
|
Skeleton(std::shared_ptr<const SkeletonConfig> config);
|
||||||
|
|
||||||
dv::value serialize(bool saveTextures, bool savePose) const;
|
dv::value serialize(bool saveTextures, bool savePose) const;
|
||||||
void deserialize(const dv::value& root);
|
void deserialize(const dv::value& root);
|
||||||
|
|
||||||
|
void setConfig(std::shared_ptr<const SkeletonConfig> config);
|
||||||
};
|
};
|
||||||
|
|
||||||
class SkeletonConfig {
|
class SkeletonConfig : public std::enable_shared_from_this<SkeletonConfig> {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::unique_ptr<Bone> root;
|
std::unique_ptr<Bone> root;
|
||||||
std::unordered_map<std::string, size_t> indices;
|
std::unordered_map<std::string, size_t> indices;
|
||||||
|
|
@ -135,7 +137,7 @@ namespace rigging {
|
||||||
) const;
|
) const;
|
||||||
|
|
||||||
Skeleton instance() const {
|
Skeleton instance() const {
|
||||||
return Skeleton(this);
|
return Skeleton(shared_from_this());
|
||||||
}
|
}
|
||||||
|
|
||||||
const Bone* find(std::string_view str) const;
|
const Bone* find(std::string_view str) const;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue