Skip to content

Commit

Permalink
fix fallback rgb reading, fix tests
Browse files Browse the repository at this point in the history
That fixes weird looking grass (and a couple of other textures) in resource packs and 1.20.2
  • Loading branch information
Bixilon committed Oct 10, 2023
1 parent 8838e48 commit c3bf0f1
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package de.bixilon.minosoft.data.world.chunk.manager

import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.minosoft.data.registries.biomes.Biome
import de.bixilon.minosoft.data.registries.biomes.BiomePrecipitation
import de.bixilon.minosoft.data.registries.blocks.types.stone.StoneTest0
import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft
import de.bixilon.minosoft.data.world.biome.accessor.NoiseBiomeAccessor
Expand Down Expand Up @@ -438,7 +437,7 @@ class ChunkManagerTest {

fun noiseBiomeCache() {
val manager = create()
val biome = Biome(minosoft("test"), 0.0f, 0.0f, null, null, null, null, BiomePrecipitation.NONE)
val biome = Biome(minosoft("test"), 0.0f, 0.0f, null, null, null, null, null)
manager.world.cacheBiomeAccessor = NoiseBiomeAccessor(manager.world.connection, 0L)
val source = SpatialBiomeArray(Array(1024) { biome })
val matrix = manager.createMatrix(source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
import de.bixilon.minosoft.gui.rendering.chunk.mesh.ChunkMesh
import de.bixilon.minosoft.gui.rendering.chunk.mesh.SingleChunkMesh
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakedFace
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.Shades
import de.bixilon.minosoft.gui.rendering.system.base.MeshUtil.buffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.Texture
Expand Down Expand Up @@ -56,7 +57,7 @@ class BakedFaceTest {
}

fun mixed() {
val face = BakedFace(floatArrayOf(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f), floatArrayOf(-1f, -2f, -3f, -4f, -5f, -6f, -7f, -8f), 1.0f, -1, null, texture())
val face = BakedFace(floatArrayOf(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f), floatArrayOf(-1f, -2f, -3f, -4f, -5f, -6f, -7f, -8f), Shades.UP, -1, null, texture())

val mesh = mesh()

Expand All @@ -78,7 +79,7 @@ class BakedFaceTest {
}

fun blockSouth() {
val face = BakedFace(floatArrayOf(0f, 0f, 0f, 0f, 1f, 0f, 0f, 1f, 1f, 0f, 0f, 1f), floatArrayOf(0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f), 1.0f, -1, null, texture())
val face = BakedFace(floatArrayOf(0f, 0f, 0f, 0f, 1f, 0f, 0f, 1f, 1f, 0f, 0f, 1f), floatArrayOf(0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f), Shades.UP, -1, null, texture())

val mesh = mesh()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object BakedModelTestUtil {

vertices?.let { assertMatches(face.positions, it, "Vertices mismatch") }
uv?.let { if (!face.uv.contentEquals(it)) throw AssertionError("UV mismatch, expected [${uv[0]}|${uv[1]}], but got [${face.uv[0]}|${face.uv[1]}]") } // printing the first element is fine, it is always clockwise
shade?.let { Assert.assertEquals(face.shade, it, "Shade mismatch") }
shade?.let { Assert.assertEquals(face.shade.shade, it, "Shade mismatch") }
texture?.toResourceLocation()?.texture()?.let { Assert.assertEquals(face.texture, it, "Texture mismatch") }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/

package de.bixilon.minosoft.gui.rendering.textures

import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.minosoft.gui.rendering.system.base.texture.data.TextureData
import org.testng.Assert.assertEquals
import org.testng.annotations.Test
import java.io.ByteArrayInputStream
import java.io.InputStream

@Test(groups = ["texture", "assets"])
class TextureReadingTest {
private val GRAY_GRAY = TextureReadingTest::class.java.getResourceAsStream("/texture_reading/gray_gray.png")!!.readAllBytes()
private val GRAY_RGB = TextureReadingTest::class.java.getResourceAsStream("/texture_reading/gray_rgb.png")!!.readAllBytes()

private val READ_1 = TextureUtil::class.java.getDeclaredMethod("readTexture1", InputStream::class.java).apply { isAccessible = true }
private val READ_2 = TextureUtil::class.java.getDeclaredMethod("readTexture2", InputStream::class.java).apply { isAccessible = true }

private fun TextureData.assertGray() {
assertEquals(size, Vec2i(16, 16))

assertEquals(buffer.get(0), 0x94.toByte())
assertEquals(buffer.get(1), 0x94.toByte())
assertEquals(buffer.get(2), 0x94.toByte())
assertEquals(buffer.get(3), 0xFF.toByte())
assertEquals(buffer.get(4), 0xC3.toByte())
assertEquals(buffer.get(128), 0xA3.toByte())
assertEquals(buffer.get(256), 0xA2.toByte())
}

fun `read rgb 1`() {
val texture = READ_1.invoke(TextureUtil, ByteArrayInputStream(GRAY_RGB)) as TextureData
texture.assertGray()
}

fun `read rgb 2`() {
val texture = READ_2.invoke(TextureUtil, ByteArrayInputStream(GRAY_RGB)) as TextureData
texture.assertGray()
}

@Test(enabled = false)
fun `read gray 1`() {
val texture = READ_1.invoke(TextureUtil, ByteArrayInputStream(GRAY_GRAY)) as TextureData
texture.assertGray()
}

fun `read gray 2`() {
val texture = READ_2.invoke(TextureUtil, ByteArrayInputStream(GRAY_GRAY)) as TextureData
texture.assertGray()
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ data class Biome(
fogColor = fogColor,
waterColor = waterColor,
waterFogColor = waterFogColor,
precipitation = data["precipitation"]?.let { BiomePrecipitation[it] },
precipitation = data["precipitation"]?.let { if (it is Int) BiomePrecipitation.getOrNull(it - 1) else BiomePrecipitation[it] },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,30 @@ object TextureUtil {
}!!
}

private fun InputStream.readTexture1(): TextureData {
val decoder = PNGDecoder(this)
val data = BufferUtils.createByteBuffer(decoder.width * decoder.height * PNGDecoder.Format.RGBA.numComponents)
decoder.decode(data, decoder.width * PNGDecoder.Format.RGBA.numComponents, PNGDecoder.Format.RGBA)

return TextureData(Vec2i(decoder.width, decoder.height), data)
}

/**
* Other way of reading a texture if the first (preferred one) method does not work
* Only happens to some weird textures (seen in PureBDCraft)
* Ignores the alpha channel
*/
private fun InputStream.readTexture2(): TextureData {
val image: BufferedImage = ImageIO.read(this)
val rgb = image.getRGB(0, 0, image.width, image.height, null, 0, image.width)

val byteOutput = ByteArrayOutputStream()
val dataOutput = DataOutputStream(byteOutput)

for (color in rgb) {
dataOutput.writeInt((color shl 8) or 0xFF)
val samples = if (image.raster.numBands == 3) intArrayOf(0, 1, 2) else intArrayOf(0, 0, 0)

for (y in 0 until image.height) {
for (x in 0 until image.width) {
dataOutput.writeByte(image.raster.getSample(x, y, samples[0]))
dataOutput.writeByte(image.raster.getSample(x, y, samples[1]))
dataOutput.writeByte(image.raster.getSample(x, y, samples[2]))
val alpha = image.alphaRaster?.getSample(x, y, 0) ?: 0xFF
dataOutput.writeByte(alpha)
}
}

val buffer = MemoryUtil.memAlloc(byteOutput.size())
Expand All @@ -68,11 +77,7 @@ object TextureUtil {

fun InputStream.readTexture(): TextureData {
return try {
val decoder = PNGDecoder(this)
val data = BufferUtils.createByteBuffer(decoder.width * decoder.height * PNGDecoder.Format.RGBA.numComponents)
decoder.decode(data, decoder.width * PNGDecoder.Format.RGBA.numComponents, PNGDecoder.Format.RGBA)

TextureData(Vec2i(decoder.width, decoder.height), data)
readTexture1()
} catch (exception: Throwable) {
this.reset()
readTexture2()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class GrassTintCalculator : TintProvider {

fun getBlockColor(biome: Biome?): Int {
if (biome == null) {
return getColor(127, 127)
return getColor(127, 255)
}
val color = getColor(biome.colorMapPixelIndex)

Expand Down

0 comments on commit c3bf0f1

Please sign in to comment.