Skip to content

Commit

Permalink
Radar: Improve FPS at high zooms (#448)
Browse files Browse the repository at this point in the history
* Radar: Improve FPS at high zooms

Reduces amount of GL calls to improve FPS.

Also added an option to disable rendering the chunk border lines. At high zooms these are not helpful visually and further reduce FPS.

* bring back color settings
  • Loading branch information
rfresh2 authored Jan 8, 2023
1 parent aede6c7 commit 7b85616
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import com.lambda.client.util.graphics.VertexHelper
class RenderRadarEvent(
val vertexHelper: VertexHelper,
val radius: Float,
val scale: Float
val scale: Float,
val chunkLines: Boolean
) : Event
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal object Radar : HudElement(
description = "Shows entities and new chunks"
) {
private val zoom by setting("Zoom", 3f, 1f..10f, 0.1f)
private val chunkLines by setting("Chunk Lines", true)

private val players = setting("Players", true)
private val passive = setting("Passive Mobs", false)
Expand All @@ -45,7 +46,7 @@ internal object Radar : HudElement(

runSafe {
drawBorder(vertexHelper)
post(RenderRadarEvent(vertexHelper, radius, zoom)) // Let other modules display radar elements
post(RenderRadarEvent(vertexHelper, radius, zoom, chunkLines)) // Let other modules display radar elements
drawEntities(vertexHelper)
drawLabels()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ object NewChunks : Module(
}

safeListener<RenderRadarEvent> {
if (renderMode == RenderMode.WORLD) return@safeListener

val playerOffset = Vec2d((player.posX - (player.chunkCoordX shl 4)), (player.posZ - (player.chunkCoordZ shl 4)))
val chunkDist = (it.radius * it.scale).toInt() shr 4

// at high zooms (further zoomed out) there will be thousands of rects being rendered
// buffering rects here to reduce GL calls and improve FPS
val distantChunkRects: MutableList<Pair<Vec2d, Vec2d>> = mutableListOf()
val chunkGridRects: MutableList<Pair<Vec2d, Vec2d>> = mutableListOf()
for (chunkX in -chunkDist..chunkDist) {
for (chunkZ in -chunkDist..chunkDist) {
val pos0 = getChunkPos(chunkX, chunkZ, playerOffset, it.scale)
Expand All @@ -136,21 +137,25 @@ object NewChunks : Module(
) ?: false

if (!chunk.isLoaded && !isCachedChunk) {
RenderUtils2D.drawRectFilled(it.vertexHelper, pos0, pos1, distantChunkColor)
distantChunkRects.add(Pair(pos0, pos1))
}
RenderUtils2D.drawRectOutline(it.vertexHelper, pos0, pos1, 0.3f, chunkGridColor)
chunkGridRects.add(Pair(pos0, pos1))
}
}
}
if (distantChunkRects.isNotEmpty()) RenderUtils2D.drawRectFilledList(it.vertexHelper, distantChunkRects, distantChunkColor)
if (it.chunkLines && chunkGridRects.isNotEmpty()) RenderUtils2D.drawRectOutlineList(it.vertexHelper, chunkGridRects, 0.3f, chunkGridColor)

val newChunkRects: MutableList<Pair<Vec2d, Vec2d>> = mutableListOf()
chunks.keys.forEach { chunk ->
val pos0 = getChunkPos(chunk.x - player.chunkCoordX, chunk.z - player.chunkCoordZ, playerOffset, it.scale)
val pos1 = getChunkPos(chunk.x - player.chunkCoordX + 1, chunk.z - player.chunkCoordZ + 1, playerOffset, it.scale)

if (isSquareInRadius(pos0, pos1, it.radius)) {
RenderUtils2D.drawRectFilled(it.vertexHelper, pos0, pos1, newChunkColor)
newChunkRects.add(Pair(pos0, pos1))
}
}
if (newChunkRects.isNotEmpty()) RenderUtils2D.drawRectFilledList(it.vertexHelper, newChunkRects, newChunkColor)
}

safeListener<PacketEvent.PostReceive> { event ->
Expand Down
47 changes: 47 additions & 0 deletions src/main/kotlin/com/lambda/client/util/graphics/RenderUtils2D.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,59 @@ object RenderUtils2D {
drawLineLoop(vertexHelper, vertices, lineWidth, color)
}

fun drawRectOutlineList(vertexHelper: VertexHelper,
rects: List<Pair<Vec2d, Vec2d>>,
lineWidth: Float = 1f,
color: ColorHolder) {
prepareGl()
glLineWidth(lineWidth)
vertexHelper.begin(GL_LINES)
rects.forEach {
val pos1 = it.first // Top left
val pos2 = Vec2d(it.second.x, it.first.y) // Top right
val pos3 = it.second // Bottom right
val pos4 = Vec2d(it.first.x, it.second.y) // Bottom left
vertexHelper.put(pos1, color)
vertexHelper.put(pos2, color)
vertexHelper.put(pos2, color)
vertexHelper.put(pos3, color)
vertexHelper.put(pos3, color)
vertexHelper.put(pos4, color)
vertexHelper.put(pos4, color)
vertexHelper.put(pos1, color)
}
vertexHelper.end()
releaseGl()
glLineWidth(1f)
}

fun drawRectFilled(vertexHelper: VertexHelper, posBegin: Vec2d = Vec2d(0.0, 0.0), posEnd: Vec2d, color: ColorHolder) {
val pos2 = Vec2d(posEnd.x, posBegin.y) // Top right
val pos4 = Vec2d(posBegin.x, posEnd.y) // Bottom left
drawQuad(vertexHelper, posBegin, pos2, posEnd, pos4, color)
}

fun drawRectFilledList(vertexHelper: VertexHelper,
rects: List<Pair<Vec2d, Vec2d>>,
color: ColorHolder) {
prepareGl()
vertexHelper.begin(GL_TRIANGLES)
rects.forEach {
val pos1 = it.first // Top left
val pos2 = Vec2d(it.second.x, it.first.y) // Top right
val pos3 = it.second // Bottom right
val pos4 = Vec2d(it.first.x, it.second.y) // Bottom left
vertexHelper.put(pos1, color)
vertexHelper.put(pos2, color)
vertexHelper.put(pos4, color)
vertexHelper.put(pos4, color)
vertexHelper.put(pos3, color)
vertexHelper.put(pos2, color)
}
vertexHelper.end()
releaseGl()
}

private fun drawQuad(vertexHelper: VertexHelper, pos1: Vec2d, pos2: Vec2d, pos3: Vec2d, pos4: Vec2d, color: ColorHolder) {
val vertices = arrayOf(pos1, pos2, pos4, pos3)
drawTriangleStrip(vertexHelper, vertices, color)
Expand Down

0 comments on commit 7b85616

Please sign in to comment.