From 0a2f2cef0853e4b3f11e8e3be2a5738a74ef8387 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 21 May 2023 22:20:16 +0200 Subject: [PATCH] model bakery: flip uv y coordinate minosoft uv starts from top left, minecraft/opengl from bottom left. LOTS of tests need to be fixed. --- .../models/block/element/face/ModelFace.kt | 20 ++++++++++++------- .../rendering/world/mesh/SingleWorldMesh.kt | 20 +++++++++---------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/models/block/element/face/ModelFace.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/models/block/element/face/ModelFace.kt index 0480ced88..130a6e584 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/models/block/element/face/ModelFace.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/models/block/element/face/ModelFace.kt @@ -57,12 +57,12 @@ data class ModelFace( fun fallbackUV(direction: Directions, from: Vec3, to: Vec3): FaceUV { return when (direction) { // @formatter:off - Directions.DOWN -> FaceUV(from.x, 1.0f - to.z, to.x, 1.0f - from.z ) - Directions.UP -> FaceUV(from.x, from.z, to.x, to.z ) - Directions.NORTH -> FaceUV(1.0f - to.x, 1.0f - to.y, 1.0f - from.x, 1.0f - from.y ) - Directions.SOUTH -> FaceUV(from.x, 1.0f - to.y, to.x, 1.0f - from.y ) - Directions.WEST -> FaceUV(from.z, 1.0f - to.y, to.z, 1.0f - from.y ) - Directions.EAST -> FaceUV(1.0f - to.z, 1.0f - to.y, 1.0f - from.z, 1.0f - from.y ) + Directions.DOWN -> FaceUV(from.x, 1.0f - from.z, to.x, 1.0f - to.z) + Directions.UP -> FaceUV(from.x, to.z, to.x, from.z ) + Directions.NORTH -> FaceUV(1.0f - to.x, 1.0f - from.y, 1.0f - from.x, 1.0f - to.y) + Directions.SOUTH -> FaceUV(from.x, 1.0f - from.y, to.x, 1.0f - to.y) + Directions.WEST -> FaceUV(from.z, 1.0f - from.y, to.z, 1.0f - to.y) + Directions.EAST -> FaceUV(1.0f - to.z, 1.0f - from.y, 1.0f - from.z, 1.0f - to.y) // @formatter:on } } @@ -70,7 +70,13 @@ data class ModelFace( fun deserialize(direction: Directions, from: Vec3, to: Vec3, data: JsonObject): ModelFace { val texture = data["texture"].toString() - val uv = data["uv"]?.listCast()?.let { FaceUV(start = Vec2(it[0], it[1]) / BLOCK_SIZE, end = Vec2(it[2], it[3]) / BLOCK_SIZE) } ?: fallbackUV(direction, from, to) + val uv = data["uv"]?.listCast()?.let { + // auto transform (flip) y coordinate (in minosoft 0|0 is left up, not like in minecraft/opengl where it is left down) + FaceUV( + start = Vec2(it[0], it[3].toFloat()) / BLOCK_SIZE, + end = Vec2(it[2], it[1].toFloat()) / BLOCK_SIZE, + ) + } ?: fallbackUV(direction, from, to) val rotation = data["rotation"]?.toInt()?.rotation() ?: 0 val cull = data["cullface"]?.toString()?.let { if (it == "none") null else Directions[it] } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/world/mesh/SingleWorldMesh.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/world/mesh/SingleWorldMesh.kt index c08ed4990..8b65a1c5d 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/world/mesh/SingleWorldMesh.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/world/mesh/SingleWorldMesh.kt @@ -62,18 +62,18 @@ class SingleWorldMesh(context: RenderContext, initialCacheSize: Int, onDemand: B companion object { // TODO: uv coordinates should start in the upper left corner, then a 0=>0 mapping is possible val TRIANGLE_ORDER = intArrayOf( - 0, 3, - 3, 0, - 2, 1, - 2, 1, - 1, 2, - 0, 3, + 0, 0, + 3, 3, + 2, 2, + 2, 2, + 1, 1, + 0, 0, ) val QUAD_ORDER = intArrayOf( - 0, 3, - 3, 0, - 2, 1, - 1, 2, + 0, 0, + 3, 3, + 2, 2, + 1, 1, ) } }