Skip to content

Commit

Permalink
Animation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
edunad committed May 11, 2024
1 parent 76b2817 commit 84e44c3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
13 changes: 3 additions & 10 deletions rawrbox.render/include/rawrbox/render/models/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ namespace rawrbox {

void readAnims(rawrbox::Matrix4x4& nodeTransform, const std::string& nodeName) const {
for (auto& playingAnim : this->_playingAnimations) {

auto& anim = playingAnim.second;
auto animChannel = std::find_if(anim.data->frames.begin(), anim.data->frames.end(), [&](AnimationFrame& x) {
return x.nodeName == nodeName;
Expand Down Expand Up @@ -139,23 +138,17 @@ namespace rawrbox {

if (animationEnded) {
if (it->second.loop) {
// If looping, wrap the time around
newTime = std::fmod(newTime, totalDur);
if (newTime < 0) newTime += totalDur;
if (newTime <= 0) newTime += totalDur;
} else {
// If not looping, clamp the time to the duration or 0
newTime = it->second.speed >= 0 ? totalDur : 0;
}

this->onAnimationComplete(it->second.name);

if (!it->second.loop) {
auto onCompleteCallback = it->second.onComplete;
it = this->_playingAnimations.erase(it);

if (onCompleteCallback != nullptr) onCompleteCallback();
continue;
}

this->onAnimationComplete(it->second.name);
}

it->second.time = newTime;
Expand Down
3 changes: 3 additions & 0 deletions samples/009-assimp/include/assimp/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace assimp {
std::unique_ptr<rawrbox::AssimpModel<rawrbox::MaterialSkinned>> _model6 = std::make_unique<rawrbox::AssimpModel<rawrbox::MaterialSkinned>>();

std::unique_ptr<rawrbox::AssimpModel<>> _model7 = std::make_unique<rawrbox::AssimpModel<>>();
std::unique_ptr<rawrbox::AssimpModel<rawrbox::MaterialSkinned>> _model8 = std::make_unique<rawrbox::AssimpModel<rawrbox::MaterialSkinned>>();

std::unique_ptr<rawrbox::Model<>> _modelGrid = std::make_unique<rawrbox::Model<>>();
std::unique_ptr<rawrbox::Text3D<>> _text = std::make_unique<rawrbox::Text3D<>>();
Expand All @@ -34,6 +35,8 @@ namespace assimp {
void update() override;
void draw() override;

void testANIM();

public:
Game() = default;
Game(const Game&) = delete;
Expand Down
38 changes: 32 additions & 6 deletions samples/009-assimp/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace assimp {
std::vector<std::pair<std::string, uint32_t>> initialContentFiles = {
{"./assets/models/shape_keys/shape_keys.glb", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_BLEND_SHAPES | rawrbox::ModelLoadFlags::Debug::PRINT_BLENDSHAPES},
{"./assets/models/ps1_phasmophobia/Phasmaphobia_Semi.fbx", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_LIGHT},
{"./assets/models/anim_test.glb", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS | rawrbox::ModelLoadFlags::Debug::PRINT_ANIMATIONS},
{"./assets/models/wolf/wolfman_animated.fbx", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS | rawrbox::ModelLoadFlags::Debug::PRINT_METADATA | rawrbox::ModelLoadFlags::Debug::PRINT_ANIMATIONS},
{"./assets/models/multiple_skeleton/twocubestest.gltf", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS | rawrbox::ModelLoadFlags::Debug::PRINT_BONE_STRUCTURE},
{"./assets/models/grandma_tv/scene.gltf", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS | rawrbox::ModelLoadFlags::Debug::PRINT_MATERIALS}};
Expand All @@ -73,6 +74,17 @@ namespace assimp {
});
}

void Game::testANIM() {
this->_model8->playAnimation("TEST.UP", false, 1.F, true, [this]() {
this->_model8->playAnimation("TEST.LEFT", false, 1.F, true, [this]() {
this->_model8->playAnimation("TEST.BACK", false, 1.F, true, [this]() {
fmt::print("loop anim test \n");
this->testANIM();
});
});
});
}

void Game::contentLoaded() {
if (this->_ready) return;
// Assimp test ---
Expand All @@ -99,29 +111,37 @@ namespace assimp {
this->_model4->setPos({-1, 0, 0});
this->_model4->upload();

auto* mdl4 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/multiple_skeleton/twocubestest.gltf")->get();
this->_model5->load(*mdl4);
auto* mdl3 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/multiple_skeleton/twocubestest.gltf")->get();
this->_model5->load(*mdl3);
this->_model5->playAnimation("MewAction", true, 0.8F);
this->_model5->playAnimation("MewAction.001", true, 0.5F);
this->_model5->setPos({0, 0, 2.5F});
this->_model5->setScale({0.25F, 0.25F, 0.25F});
this->_model5->upload();

auto* mdl5 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/grandma_tv/scene.gltf")->get();
this->_model6->load(*mdl5);
auto* mdl4 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/grandma_tv/scene.gltf")->get();
this->_model6->load(*mdl4);
this->_model6->playAnimation("Scene", true, 1.F);
this->_model6->setPos({-1, 0, -3.5F});
this->_model6->setScale({0.35F, 0.35F, 0.35F});
this->_model6->setEulerAngle({0, rawrbox::MathUtils::toRad(180.F), 0});
this->_model6->upload();

auto* mdl6 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/shape_keys/shape_keys.glb")->get();
this->_model7->load(*mdl6);
auto* mdl5 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/shape_keys/shape_keys.glb")->get();
this->_model7->load(*mdl5);
this->_model7->setScale({0.4F, 0.4F, 0.4F});
this->_model7->setPos({1.F, 0.4F, -3.5F});
this->_model7->upload(rawrbox::UploadType::FIXED_DYNAMIC);
// -----

// ANIM TEST ---
auto* mdl6 = rawrbox::RESOURCES::getFile<rawrbox::ResourceAssimp>("./assets/models/anim_test.glb")->get();

this->_model8->setPos({7, 1.1F, 2.F});
this->_model8->load(*mdl6);
this->_model8->upload();
// ---------

// Text test ----
this->_text->addText(*rawrbox::DEBUG_FONT_REGULAR, "TEXTURES + LIGHT", {-6.F, 3.0F, 0});
this->_text->addText(*rawrbox::DEBUG_FONT_REGULAR, "TEXTURES", {6.F, 3.0F, 0});
Expand All @@ -143,6 +163,10 @@ namespace assimp {
rawrbox::LIGHTS::add<rawrbox::PointLight>(rawrbox::Vector3f{-1, 0.6F, -1.4F}, rawrbox::Colors::White() * 100, 1.6F);
// -----------

// ANIM TEST ---
this->testANIM();
// -------------

this->_ready = true;
}

Expand All @@ -157,6 +181,7 @@ namespace assimp {
this->_model5.reset();
this->_model6.reset();
this->_model7.reset();
this->_model8.reset();
this->_modelGrid.reset();

this->_text.reset();
Expand Down Expand Up @@ -192,6 +217,7 @@ namespace assimp {
this->_model7->draw();
//

this->_model8->draw();
this->_text->draw();
}

Expand Down
Binary file added samples/assets/models/anim_test.glb
Binary file not shown.

0 comments on commit 84e44c3

Please sign in to comment.