fix: 'box' primitive uv & update stairs model

This commit is contained in:
MihailRis 2025-05-29 20:53:39 +03:00
parent 407184250c
commit 45f845eb10
6 changed files with 21 additions and 11 deletions

View File

@ -1,6 +1,6 @@
<model> <model>
<box from="0,0,0" to="1,0.5,1" delete="top,south"/> <box from="0,0,0" to="1,0.5,1" delete="top,south"/>
<box from="0,0.5,0" to="1,1,0.5" delete="bottom,south"/> <box from="0,0.5,0" to="1,1,0.5" delete="bottom,south"/>
<rect from="0,0.5,0.5" right="1,0,0" up="0,0,0.5"/> <rect from="0,0.5,0.5" right="1,0,0" up="0,0,0.5" texture="$2"/>
<rect from="0,0,0" right="1,0,0" up="0,1,0"/> <rect from="0,0,0" right="1,0,0" up="0,1,0" texture="$1"/>
</model> </model>

View File

@ -14,8 +14,8 @@ static const std::unordered_map<std::string, int> side_indices {
{"south", 1}, {"south", 1},
{"top", 2}, {"top", 2},
{"bottom", 3}, {"bottom", 3},
{"east", 4}, {"west", 4},
{"west", 5}, {"east", 5},
}; };
static void perform_rect(const xmlelement& root, model::Model& model) { static void perform_rect(const xmlelement& root, model::Model& model) {

View File

@ -77,13 +77,13 @@ void Mesh::addBox(
if (enabledSides[1]) // south if (enabledSides[1]) // south
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]); addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
if (enabledSides[2]) // top if (enabledSides[2]) // top
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]); addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1));
if (enabledSides[3]) // bottom if (enabledSides[3]) // bottom
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]); addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3] * glm::vec2(-1, 1));
if (enabledSides[4]) // west if (enabledSides[4]) // west
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]); addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
if (enabledSides[5]) // east if (enabledSides[5]) // east
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]); addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1));
} }
void Mesh::scale(const glm::vec3& size) { void Mesh::scale(const glm::vec3& size) {

View File

@ -98,7 +98,7 @@ model::Model ModelsGenerator::fromCustom(
for (size_t i = 0; i < modelBoxes.size(); i++) { for (size_t i = 0; i < modelBoxes.size(); i++) {
auto& mesh = model.addMesh("blocks:"); auto& mesh = model.addMesh("blocks:");
mesh.lighting = lighting; mesh.lighting = lighting;
const UVRegion boxtexfaces[6] = { UVRegion boxtexfaces[6] = {
get_region_for(modelTextures[i * 6 + 5], assets), get_region_for(modelTextures[i * 6 + 5], assets),
get_region_for(modelTextures[i * 6 + 4], assets), get_region_for(modelTextures[i * 6 + 4], assets),
get_region_for(modelTextures[i * 6 + 3], assets), get_region_for(modelTextures[i * 6 + 3], assets),
@ -106,6 +106,9 @@ model::Model ModelsGenerator::fromCustom(
get_region_for(modelTextures[i * 6 + 1], assets), get_region_for(modelTextures[i * 6 + 1], assets),
get_region_for(modelTextures[i * 6 + 0], assets) get_region_for(modelTextures[i * 6 + 0], assets)
}; };
boxtexfaces[2].scale(glm::vec2(-1));
boxtexfaces[5].scale(glm::vec2(-1, 1));
bool enabled[6] {1,1,1,1,1,1}; bool enabled[6] {1,1,1,1,1,1};
mesh.addBox( mesh.addBox(
modelBoxes[i].center(), modelBoxes[i].center(),

View File

@ -95,6 +95,7 @@ void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) {
ctx.setFramebuffer(fbo.get()); ctx.setFramebuffer(fbo.get());
ctx.setViewport({size.x, size.y}); ctx.setViewport({size.x, size.y});
ctx.setDepthTest(true); ctx.setDepthTest(true);
ctx.setCullFace(true);
display::clear(); display::clear();
auto& ui3dShader = assets.require<Shader>("ui3d"); auto& ui3dShader = assets.require<Shader>("ui3d");
@ -111,8 +112,8 @@ void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) {
"blocks:dbg_south", "blocks:dbg_south",
"blocks:dbg_top", "blocks:dbg_top",
"blocks:dbg_bottom", "blocks:dbg_bottom",
"blocks:dbg_east",
"blocks:dbg_west", "blocks:dbg_west",
"blocks:dbg_east",
}; };
region = util::get_texture_region( region = util::get_texture_region(
assets, assets,

View File

@ -43,8 +43,8 @@ struct UVRegion {
} }
void scale(float x, float y) { void scale(float x, float y) {
float w = getWidth(); float w = u2 - u1;
float h = getHeight(); float h = v2 - v1;
float cx = (u1 + u2) * 0.5f; float cx = (u1 + u2) * 0.5f;
float cy = (v1 + v2) * 0.5f; float cy = (v1 + v2) * 0.5f;
u1 = cx - w * 0.5f * x; u1 = cx - w * 0.5f * x;
@ -63,4 +63,10 @@ struct UVRegion {
u2 = vec.z; u2 = vec.z;
v2 = vec.w; v2 = vec.w;
} }
UVRegion operator*(const glm::vec2& scale) const {
auto copy = UVRegion(*this);
copy.scale(scale);
return copy;
}
}; };