Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor of Minimap #4238

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object ChunkRenderer {
}

for (posToUpdate in positionsToUpdate) {
val color = getColor(posToUpdate)
val color = getColor(posToUpdate.x, posToUpdate.z)

textureAtlasManager.editChunk(ChunkPos(posToUpdate)) { texture, atlasPosition ->
val (x, y) = atlasPosition.getPosOnAtlas(posToUpdate.x and 15, posToUpdate.z and 15)
Expand All @@ -87,10 +87,10 @@ object ChunkRenderer {
}
}

private fun getColor(pos: BlockPos): Int {
private fun getColor(x: Int, z: Int): Int {
val world = mc.world!!

val height = heightmapManager.getHeight(pos.x, pos.z)
val height = heightmapManager.getHeight(x, z)
val offsetsToCheck =
arrayOf(
Vec2i(-1, 0),
Expand All @@ -105,7 +105,7 @@ object ChunkRenderer {

val higherOffsets =
offsetsToCheck.filter { offset ->
heightmapManager.getHeight(pos.x + offset.x, pos.z + offset.y) > height
heightmapManager.getHeight(x + offset.x, z + offset.y) > height
}

val higherOffsetVec = higherOffsets.fold(Vec2i(0, 0)) { acc, vec -> acc.add(vec) }
Expand All @@ -117,13 +117,13 @@ object ChunkRenderer {
130.0 / 255.0
} else {
val similarityToSunDirection = higherOffsetVec.similarity(SUN_DIRECTION)
val eee = higherOffsetVec.dotProduct(Vec2i(pos.x, pos.z)).toDouble() / higherOffsetVec.length()
val eee = higherOffsetVec.dotProduct(Vec2i(x, z)).toDouble() / higherOffsetVec.length()
val sine = sin(eee * 0.5 * PI)

(190.0 + (similarityToSunDirection * 55.0) + sine * 10.0) / 255.0
}

val surfaceBlockPos = BlockPos(pos.x, height, pos.z)
val surfaceBlockPos = BlockPos(x, height, z)
val surfaceBlockState = world.getBlockState(surfaceBlockPos)

if (surfaceBlockState.isAir) {
Expand Down Expand Up @@ -162,7 +162,7 @@ object ChunkRenderer {
for (offZ in 0..15) {
val (texX, texY) = atlasPosition.getPosOnAtlas(offX, offZ)

val color = getColor(BlockPos(offX or (x shl 4), 0, offZ or (z shl 4)))
val color = getColor(offX or (x shl 4), offZ or (z shl 4))

texture.image!!.setColor(texX, texY, color)
}
Expand All @@ -175,7 +175,7 @@ object ChunkRenderer {
for (offZ in from.y..to.y) {
val (texX, texY) = atlasPosition.getPosOnAtlas(offX, offZ)

val color = getColor(BlockPos(offX + otherPos.startX, 0, offZ + otherPos.startZ))
val color = getColor(offX + otherPos.startX, offZ + otherPos.startZ)

texture.image!!.setColor(texX, texY, color)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MinimapHeightmapManager {
x: Int,
z: Int,
): Int {
val chunkPos = ChunkPos(BlockPos(x, 0, z))
val chunkPos = ChunkPos(x shr 4, z shr 4)
val heightmap = getHeightmap(chunkPos)

return heightmap.getHeight(x - chunkPos.startX, z - chunkPos.startZ)
Expand All @@ -54,7 +54,7 @@ class MinimapHeightmapManager {

for (x in 0..15) {
for (z in 0..15) {
heightmap.setHeight(x, z, calculateHeight(chunkPos.startX + x, chunkPos.startZ + z))
heightmap.setHeight(x, z, calculateHeight(chunkPos.startX or x, chunkPos.startZ or z))
}
}
}
Expand Down Expand Up @@ -116,20 +116,21 @@ class MinimapHeightmapManager {
z: Int,
maxY: Int? = null,
): Int {
val world = mc.world!!
val chunk = mc.world!!.getChunk(x shr 4, z shr 4)

val maxHeight = (maxY ?: world.height) - 1
val maxHeight = (maxY ?: chunk.height) - 1

val pos = BlockPos.Mutable(x, maxHeight, z)
while (pos.y > world.bottomY) {
val state = world.getBlockState(pos)

while (pos.y > chunk.bottomY) {
val state = chunk.getBlockState(pos)
if (isSurface(pos, state)) {
return pos.y
}
pos.y--
}

return world.bottomY
return chunk.bottomY
}

private fun isSurface(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import net.ccbluex.liquidbounce.utils.math.Vec2i
import net.minecraft.client.texture.NativeImage
import net.minecraft.client.texture.NativeImageBackedTexture
import net.minecraft.util.math.ChunkPos
import java.util.concurrent.ConcurrentSkipListSet

/**
* Size of the texture atlas in chunks (size x size)
Expand All @@ -41,8 +42,8 @@ private val NOT_LOADED_ATLAS_POSITION = MinimapTextureAtlasManager.AtlasPosition

class MinimapTextureAtlasManager {
private val texture = NativeImageBackedTexture(ATLAS_SIZE * 16, ATLAS_SIZE * 16, false)
private val availableAtlasPositions = mutableListOf<AtlasPosition>()
private val dirtyAtlasPositions = hashSetOf<AtlasPosition>()
private val availableAtlasPositions = ArrayList<AtlasPosition>(ATLAS_SIZE * ATLAS_SIZE - 1)
private val dirtyAtlasPositions = ConcurrentSkipListSet<AtlasPosition>()
private val chunkPosAtlasPosMap = hashMapOf<ChunkPos, AtlasPosition>()

private var allocated = false
Expand Down Expand Up @@ -105,14 +106,9 @@ class MinimapTextureAtlasManager {
chunkPos: ChunkPos,
editor: (NativeImageBackedTexture, AtlasPosition) -> Unit,
) {
val atlasPosition =
synchronized(this) {
val atlasPosition = getOrAllocate(chunkPos)
val atlasPosition = getOrAllocate(chunkPos)

dirtyAtlasPositions.add(atlasPosition)

atlasPosition
}
dirtyAtlasPositions.add(atlasPosition)

editor(texture, atlasPosition)
}
Expand All @@ -123,21 +119,21 @@ class MinimapTextureAtlasManager {
* @return the GLid of the texture
*/
fun prepareRendering(): Int {
this.texture.bindTexture()
if (this.dirtyAtlasPositions.isEmpty()) {
return this.texture.glId
}

synchronized(this) {
val dirtyChunks = this.dirtyAtlasPositions.size
this.texture.bindTexture()

when {
!this.allocated -> uploadFullTexture()
dirtyChunks == 0 -> {}
dirtyChunks < FULL_UPLOAD_THRESHOLD -> uploadOnlyDirtyPositions()
else -> uploadFullTexture()
}
val dirtyChunks = this.dirtyAtlasPositions.size

this.dirtyAtlasPositions.clear()
when {
!this.allocated || dirtyChunks >= FULL_UPLOAD_THRESHOLD -> uploadFullTexture()
else -> uploadOnlyDirtyPositions()
}

this.dirtyAtlasPositions.clear()

return this.texture.glId
}

Expand Down Expand Up @@ -167,16 +163,15 @@ class MinimapTextureAtlasManager {
dirtyAtlasPosition.baseXOnAtlas, dirtyAtlasPosition.baseYOnAtlas,
0, 0,
16, 16,
false, false)
false, false
)
}
}
}

data class AtlasPosition(private val x: Int, private val y: Int) {
val baseXOnAtlas: Int
get() = x * 16
val baseYOnAtlas: Int
get() = y * 16
data class AtlasPosition(private val x: Int, private val y: Int) : Comparable<AtlasPosition> {
val baseXOnAtlas: Int = x shl 4
val baseYOnAtlas: Int = y shl 4

val uv: BoundingBox2f
get() {
Expand All @@ -200,5 +195,8 @@ class MinimapTextureAtlasManager {
): Vec2i {
return Vec2i(baseXOnAtlas + chunkX, baseYOnAtlas + chunkY)
}

override fun compareTo(other: AtlasPosition): Int =
compareValuesBy(this, other, AtlasPosition::x, AtlasPosition::y)
}
}
Loading