fix exception if model having invalid variable texture passed to modelviewer

This commit is contained in:
MihailRis 2025-12-03 20:40:50 +03:00 committed by ShiftyX1
parent 417521e94f
commit 98a3971e13

View File

@ -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<std::string, 6> 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<std::string, 6> 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) {