Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required animation names #105

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/base/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void Units::create() {
}

i.mesh = get_mesh(i.id);
i.skeleton = SkeletalModelInstance(i.mesh->model);
i.skeleton = SkeletalModelInstance(i.mesh->model, get_required_animation_names(i.id));
i.update();
}

Expand All @@ -265,7 +265,7 @@ Unit& Units::add_unit(std::string id, glm::vec3 position) {
unit.angle = 0.f;
unit.random = { 1, 0, 0, 0 };
unit.creation_number = ++Unit::auto_increment;
unit.skeleton = SkeletalModelInstance(unit.mesh->model);
unit.skeleton = SkeletalModelInstance(unit.mesh->model, get_required_animation_names(id));
unit.update();

return units.back();
Expand Down Expand Up @@ -300,12 +300,12 @@ void Units::remove_units(const std::unordered_set<Unit*>& list) {
}

void Units::process_unit_field_change(const std::string& id, const std::string& field) {
if (field == "file") {
if (field == "file" || field == " attachmentlinkprops") {
id_to_mesh.erase(id);
for (auto& i : units) {
if (i.id == id) {
i.mesh = get_mesh(id);
i.skeleton = SkeletalModelInstance(i.mesh->model);
i.skeleton = SkeletalModelInstance(i.mesh->model, get_required_animation_names(id));
i.update();
}
}
Expand Down Expand Up @@ -362,6 +362,17 @@ std::shared_ptr<SkinnedMesh> Units::get_mesh(const std::string& id) {
return id_to_mesh[id];
}

std::vector<std::string> Units::get_required_animation_names(const std::string& id) {
std::vector<std::string> required_animation_names;
std::string animation_names = units_slk.data("attachmentlinkprops", id);
std::stringstream names_stream(animation_names);
std::string animation_name;
while (std::getline(names_stream, animation_name, ',')) {
required_animation_names.push_back(animation_name);
}
return required_animation_names;
}

void UnitAddAction::undo() {
map->units.units.resize(map->units.units.size() - units.size());
}
Expand Down
1 change: 1 addition & 0 deletions src/base/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Units {
void process_item_field_change(const std::string& id, const std::string& field);

std::shared_ptr<SkinnedMesh> get_mesh(const std::string& id);
static std::vector<std::string> get_required_animation_names(const std::string& id);
};

// Undo/redo structures
Expand Down
2 changes: 1 addition & 1 deletion src/brush/unit_brush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void UnitBrush::set_unit(const std::string& id) {
context->makeCurrent();
this->id = id;
mesh = map->units.get_mesh(id);
skeleton = SkeletalModelInstance(mesh->model);
skeleton = SkeletalModelInstance(mesh->model, Units::get_required_animation_names(id));
}

void UnitBrush::unselect_id(std::string_view id) {
Expand Down
23 changes: 19 additions & 4 deletions src/resources/skinned_mesh/skeletal_model_instance.ixx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module;

#include <algorithm>
#include <chrono>
#include <vector>
#include <memory>
Expand Down Expand Up @@ -45,8 +46,11 @@ export class SkeletalModelInstance {
std::vector<RenderNode> render_nodes;
std::vector<glm::mat4> world_matrices;

std::vector<std::string> required_animation_names; // for support for setting an animation by name later

SkeletalModelInstance() = default;
explicit SkeletalModelInstance(std::shared_ptr<mdx::MDX> model) : model(model) {
explicit SkeletalModelInstance(std::shared_ptr<mdx::MDX> model, std::vector<std::string> required_animation_names = {})
: model(model), required_animation_names(required_animation_names) {
size_t node_count = model->bones.size() +
model->lights.size() +
model->help_bones.size() +
Expand Down Expand Up @@ -81,10 +85,21 @@ export class SkeletalModelInstance {
current_keyframes.resize(model->unique_tracks);

for (size_t i = 0; i < model->sequences.size(); i++) {
if (model->sequences[i].name.find("Stand") != std::string::npos || model->sequences[i].name.find("stand") != std::string::npos) {
set_sequence(static_cast<int>(i));
break;
std::string anim_name = model->sequences[i].name;
std::transform(anim_name.begin(), anim_name.end(), anim_name.begin(),
[](unsigned char c) { return std::tolower(c); });
if (anim_name.find("stand") == std::string::npos)
continue;

bool good = true;
for (auto& req_anim_name : required_animation_names) {
if (anim_name.find(req_anim_name) == std::string::npos)
good = false;
}
if (!good)
continue;
set_sequence(static_cast<int>(i));
break;
}
}

Expand Down