diff --git a/src/graphics/ui/elements/ModelViewer.cpp b/src/graphics/ui/elements/ModelViewer.cpp index acc8170c..c1d736d7 100644 --- a/src/graphics/ui/elements/ModelViewer.cpp +++ b/src/graphics/ui/elements/ModelViewer.cpp @@ -79,6 +79,33 @@ void ModelViewer::act(float delta) { camera.position = center - camera.front * distance; } +static util::TextureRegion determine_texture_region( + const Assets& assets, const std::string& texture +) { + static std::array faces { + "blocks:dbg_north", + "blocks:dbg_south", + "blocks:dbg_top", + "blocks:dbg_bottom", + "blocks:dbg_west", + "blocks:dbg_east", + }; + + if (texture.length() < 2 || texture.at(0) != '$') { + return util::get_texture_region(assets, texture, "blocks:notfound"); + } + + unsigned char sideIndex = texture.at(1) - '0'; + if (sideIndex < faces.size()) { + return util::get_texture_region( + assets, + faces.at(texture.at(1) - '0'), + "blocks:notfound" + ); + } + return util::get_texture_region(assets, "blocks:notfound", ""); +} + void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) { camera.setAspectRatio(size.x / size.y); camera.updateVectors(); @@ -104,26 +131,10 @@ void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) { ui3dShader.uniformMatrix("u_apply", glm::mat4(1.0f)); ui3dShader.uniformMatrix("u_projview", camera.getProjView()); batch->begin(); + for (const auto& mesh : model->meshes) { - util::TextureRegion region; - if (!mesh.texture.empty() && mesh.texture[0] == '$') { - // todo: refactor - static std::array faces { - "blocks:dbg_north", - "blocks:dbg_south", - "blocks:dbg_top", - "blocks:dbg_bottom", - "blocks:dbg_west", - "blocks:dbg_east", - }; - region = util::get_texture_region( - assets, - faces.at(mesh.texture.at(1) - '0'), - "blocks:notfound" - ); - } else { - region = util::get_texture_region(assets, mesh.texture, "blocks:notfound"); - } + auto region = determine_texture_region(assets, mesh.texture); + batch->texture(region.texture); batch->setRegion(region.region); for (const auto& vertex : mesh.vertices) {