refactor more code

This commit is contained in:
Moritz Zwerger 2023-08-01 00:33:55 +02:00
parent fae7d123f0
commit 14b0d11943
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 100 additions and 79 deletions

View File

@ -23,7 +23,7 @@ import de.bixilon.minosoft.gui.rendering.models.block.BlockModel
import de.bixilon.minosoft.gui.rendering.models.block.element.ModelElement
import de.bixilon.minosoft.gui.rendering.models.block.element.face.FaceUV
import de.bixilon.minosoft.gui.rendering.models.block.element.face.ModelFace
import de.bixilon.minosoft.gui.rendering.models.block.state.apply.SingleBlockStateApply.Companion.fallbackUV
import de.bixilon.minosoft.gui.rendering.models.block.element.face.ModelFace.Companion.fallbackUV
import de.bixilon.minosoft.gui.rendering.models.raw.display.DisplayPositions
import de.bixilon.minosoft.gui.rendering.models.raw.display.ModelDisplay
import de.bixilon.minosoft.gui.rendering.models.raw.light.GUILights

View File

@ -27,17 +27,21 @@ data class ElementRotation(
val angle: Float,
val rescale: Boolean = false,
) {
companion object {
private val ORIGIN = Vec3(0.5f)
fun deserialize(data: JsonObject): ElementRotation? {
val angle = data["angle"]?.toFloat() ?: 0.0f
val angle = data["angle"]?.toFloat() ?: return null
if (angle == 0.0f) return null
val rescale = data["rescale"]?.toBoolean() ?: false
if (angle == 0.0f && !rescale) return null
val origin = data["origin"]?.toVec3()?.apply { this /= BLOCK_SIZE } ?: ORIGIN
val axis = Axes[data["axis"].toString()]
return ElementRotation(origin, axis, angle, rescale)
}
}

View File

@ -20,6 +20,7 @@ import de.bixilon.kutil.json.JsonUtil.toJsonObject
import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
import de.bixilon.minosoft.data.direction.Directions
import de.bixilon.minosoft.gui.rendering.models.block.element.face.ModelFace
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakingUtil
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.toVec3
data class ModelElement(
@ -30,6 +31,14 @@ data class ModelElement(
val rotation: ElementRotation? = null,
) {
fun positions(direction: Directions): FloatArray {
val positions = BakingUtil.positions(direction, from, to)
if (rotation == null) return positions
TODO("Can not rotate positions yet!")
}
companion object {
const val BLOCK_SIZE = 16.0f

View File

@ -17,6 +17,7 @@ import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kutil.json.JsonObject
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.minosoft.data.Axes
import de.bixilon.minosoft.data.direction.Directions
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.gui.rendering.models.block.BlockModel
@ -56,6 +57,27 @@ data class ModelFace(
this.loadedTexture = createTexture(model, manager)
}
fun getUV(uvLock: Boolean, from: Vec3, to: Vec3, direction: Directions, rotatedDirection: Directions, positions: FloatArray, x: Int, y: Int): FaceUV {
if (!uvLock) {
return this.uv ?: fallbackUV(direction, from, to)
}
var rotated = this.uv ?: return fallbackUV(rotatedDirection, positions.start(), positions.end())
if (direction.axis == Axes.X && x > 0) {
for (i in 0 until x) {
rotated = rotated.rotateLeft()
}
}
if (direction.axis == Axes.Y && y > 0) {
for (i in 0 until y) {
rotated = rotated.rotateLeft()
}
}
return rotated
}
companion object {
fun deserialize(direction: Directions, from: Vec3, to: Vec3, data: JsonObject): ModelFace {
@ -89,5 +111,31 @@ data class ModelFace(
return map
}
fun fallbackUV(direction: Directions, from: Vec3, to: Vec3): FaceUV {
return when (direction) {
// @formatter:off
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
}
}
private fun FloatArray.start(): Vec3 {
return Vec3(this[0], this[1], this[2])
}
private fun FloatArray.end(): Vec3 {
return Vec3(this[6], this[7], this[8])
}
private fun FaceUV.rotateLeft(): FaceUV {
return FaceUV(Vec2(-start.y + 1.0f, end.x), Vec2(-end.y + 1.0f, start.x))
}
}
}

View File

@ -14,7 +14,6 @@
package de.bixilon.minosoft.gui.rendering.models.block.state.apply
import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kutil.exception.Broken
import de.bixilon.kutil.json.JsonObject
import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
@ -24,12 +23,10 @@ import de.bixilon.minosoft.data.direction.DirectionUtil.rotateX
import de.bixilon.minosoft.data.direction.DirectionUtil.rotateY
import de.bixilon.minosoft.data.direction.Directions
import de.bixilon.minosoft.gui.rendering.models.block.BlockModel
import de.bixilon.minosoft.gui.rendering.models.block.element.face.FaceUV
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakedFace
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakedModel
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakingUtil.compact
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakingUtil.compactProperties
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakingUtil.positions
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakingUtil.pushRight
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.cull.side.FaceProperties
import de.bixilon.minosoft.gui.rendering.models.loader.BlockLoader
@ -61,21 +58,24 @@ data class SingleBlockStateApply(
}
}
private fun Directions.xRotations(): Int {
if (axis == Axes.X) {
return if (negative) -x else x
}
if (axis == Axes.Y && (x == 2 || x == 3)) {
return if (negative) -1 else 1
} else if (axis == Axes.Z && (x == 1 || x == 2)) {
return if (negative) 1 else -1
}
return 0
}
private fun FloatArray.rotateX(direction: Directions): FloatArray {
if (x == 0) return this
rotateX(x)
if (direction.axis == Axes.X) {
return pushRight(3, if (direction.negative) -x else x)
}
if (direction.axis == Axes.Y && (x == 2 || x == 3)) {
return pushRight(3, if (direction.negative) -1 else 1)
} else if (direction.axis == Axes.Z && (x == 1 || x == 2)) {
return pushRight(3, if (direction.negative) 1 else -1)
}
return this
return pushRight(3, direction.xRotations())
}
@ -96,20 +96,23 @@ data class SingleBlockStateApply(
}
}
private fun Directions.yRotations(): Int {
if (axis == Axes.Y) {
return if (negative) -y else y
}
if ((axis == Axes.Z && (y == 2 || y == 3))) {
return if (negative) -1 else 1
} else if (axis == Axes.X && (y == 1 || y == 2)) {
return if (negative) 1 else -1
}
return 0
}
private fun FloatArray.rotateY(direction: Directions): FloatArray {
if (y == 0) return this
rotateY(y)
if (direction.axis == Axes.Y) {
return pushRight(3, if (direction.negative) -y else y)
}
if ((direction.axis == Axes.Z && (y == 2 || y == 3))) {
return pushRight(3, if (direction.negative) -1 else 1)
} else if (direction.axis == Axes.X && (y == 1 || y == 2)) {
return pushRight(3, if (direction.negative) 1 else -1)
}
return this
return pushRight(3, direction.yRotations())
}
@ -123,7 +126,7 @@ data class SingleBlockStateApply(
}
}
private fun rotatedY(direction: Directions, rotated: Directions): Int {
private fun rotatedY(direction: Directions): Int {
if (direction.axis != Axes.Y) return 0
return if (direction.negative) -y else y
}
@ -140,17 +143,12 @@ data class SingleBlockStateApply(
if (x == 0 && y == 0) return 0
if (x == 0) {
return rotatedY(direction, rotated)
return rotatedY(direction)
}
if (y == 0) {
return rotatedX(direction, rotated)
}
return rotatedX(direction, direction.rotateX(x)) + rotatedY(direction.rotateX(x), rotated)
}
private fun FaceUV.rotateLeft(): FaceUV {
return FaceUV(Vec2(-(start.y - 0.5f) + 0.5f, end.x), Vec2(-(end.y - 0.5f) + 0.5f, start.x))
return rotatedX(direction, direction.rotateX(x)) + rotatedY(direction.rotateX(x))
}
override fun bake(): BakedModel? {
@ -168,27 +166,12 @@ data class SingleBlockStateApply(
.rotateY(this.y)
val positions = positions(direction, element.from, element.to)
val positions = element.positions(direction)
.rotateX(direction)
.rotateY(direction.rotateX(this.x))
var abc = face.uv ?: if (uvLock) fallbackUV(rotatedDirection, positions.start(), positions.end()) else fallbackUV(direction, element.from, element.to)
if (uvLock && face.uv != null) {
if (direction.axis == Axes.X) {
for (x in 0 until x) {
abc = abc.rotateLeft()
}
}
if (direction.axis == Axes.Y) {
for (y in 0 until y) {
abc = abc.rotateLeft()
}
}
}
var uv = abc.toArray(rotatedDirection, face.rotation)
var uv = face.getUV(uvLock, element.from, element.to, direction, rotatedDirection, positions, x, y).toArray(rotatedDirection, face.rotation)
if (!uvLock) {
val rotation = getTextureRotation(direction, rotatedDirection)
@ -196,7 +179,7 @@ data class SingleBlockStateApply(
}
val shade = rotatedDirection.shade
val faceProperties = positions.properties(rotatedDirection, texture)
val faceProperties = if (element.rotation == null) positions.properties(rotatedDirection, texture) else null
val bakedFace = BakedFace(positions, uv, shade, face.tintIndex, if (faceProperties == null) null else rotatedDirection, texture, faceProperties)
bakedFaces[rotatedDirection.ordinal] += bakedFace
@ -207,14 +190,6 @@ data class SingleBlockStateApply(
return BakedModel(bakedFaces.compact(), properties.compactProperties(), null) // TODO
}
private fun FloatArray.start(): Vec3 {
return Vec3(this[0], this[1], this[2])
}
private fun FloatArray.end(): Vec3 {
return Vec3(this[6], this[7], this[8])
}
fun FloatArray.properties(direction: Directions, texture: Texture): FaceProperties? {
// TODO: Bad code?
@ -270,18 +245,5 @@ data class SingleBlockStateApply(
Directions.NORTH, Directions.SOUTH -> 0.8f
Directions.WEST, Directions.EAST -> 0.6f
}
fun fallbackUV(direction: Directions, from: Vec3, to: Vec3): FaceUV {
return when (direction) {
// @formatter:off
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
}
}
}
}

View File

@ -76,6 +76,7 @@ object BakingUtil {
fun FloatArray.pushRight(components: Int, steps: Int): FloatArray {
if (steps == 0 || components == 0) return this
if (this.size % components != 0) throw IllegalArgumentException("Size mismatch!")
var steps = steps % (size / components)
if (steps < 0) steps += size * components

View File

@ -17,7 +17,6 @@ import de.bixilon.kutil.cast.CastUtil.nullCast
import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.minosoft.assets.util.InputStreamUtil.readJsonObject
import de.bixilon.minosoft.data.registries.blocks.types.Block
import de.bixilon.minosoft.data.registries.blocks.types.building.WoolBlock
import de.bixilon.minosoft.data.registries.blocks.types.legacy.CustomBlockModel
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.gui.rendering.models.block.BlockModel
@ -58,8 +57,6 @@ class BlockLoader(private val loader: ModelLoader) {
fun bake(latch: AbstractLatch?) {
for (block in loader.context.connection.registries.block) {
if (block !is WoolBlock.Red) continue
val prototype = block.model.nullCast<BlockModelPrototype>() ?: continue
block.model = null