Skip to content

Commit 7043f43

Browse files
committed
Rework mesh building and shape handling
1 parent 7204023 commit 7043f43

File tree

5 files changed

+64
-39
lines changed

5 files changed

+64
-39
lines changed

common/src/main/kotlin/com/lambda/graphics/renderer/esp/DirectionMask.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package com.lambda.graphics.renderer.esp
1919

2020
import com.lambda.util.world.FastVector
2121
import com.lambda.util.world.offset
22-
import com.lambda.util.world.toBlockPos
23-
import com.lambda.util.world.toFastVec
2422
import net.minecraft.util.math.BlockPos
2523
import net.minecraft.util.math.Direction
2624

@@ -43,8 +41,15 @@ object DirectionMask {
4341
fun Int.exclude(direction: Direction) = exclude(direction.mask)
4442
fun Int.hasDirection(dir: Int) = (this and dir) != 0
4543

46-
fun buildSideMesh(position: BlockPos, filter: (BlockPos) -> Boolean) =
47-
buildSideMesh(position.toFastVec()) { filter(it.toBlockPos()) }
44+
fun buildSideMesh(position: BlockPos, filter: (BlockPos) -> Boolean): Int {
45+
var sides = ALL
46+
47+
Direction.entries
48+
.filter { filter(position.offset(it)) }
49+
.forEach { sides = sides.exclude(it.mask) }
50+
51+
return sides
52+
}
4853

4954
fun buildSideMesh(position: FastVector, filter: (FastVector) -> Boolean): Int {
5055
var sides = ALL

common/src/main/kotlin/com/lambda/module/modules/render/BlockESP.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ import com.lambda.module.Module
2828
import com.lambda.module.tag.ModuleTag
2929
import com.lambda.threading.runSafe
3030
import com.lambda.util.extension.blockColor
31-
import com.lambda.util.extension.blockFilledMesh
32-
import com.lambda.util.extension.blockOutlineMesh
31+
import com.lambda.util.extension.outlineShape
3332
import com.lambda.util.extension.getBlockState
3433
import com.lambda.util.world.fastVectorOf
3534
import com.lambda.util.world.toBlockPos
@@ -91,12 +90,11 @@ object BlockESP : Module(
9190
pos: BlockPos,
9291
sides: Int,
9392
) = runSafe {
94-
val filledMesh = blockFilledMesh(state, pos)
95-
val outlineMesh = blockOutlineMesh(state, pos)
93+
val shape = outlineShape(state, pos)
9694
val blockColor = blockColor(state, pos)
9795

98-
if (drawFaces) buildFilledMesh(filledMesh, if (useBlockColor) blockColor else faceColor, sides)
99-
if (drawOutlines) buildOutlineMesh(outlineMesh, if (useBlockColor) blockColor else outlineColor, sides, outlineMode)
96+
if (drawFaces) buildFilledMesh(shape, if (useBlockColor) blockColor else faceColor, sides)
97+
if (drawOutlines) buildOutlineMesh(shape, if (useBlockColor) blockColor else outlineColor, sides, outlineMode)
10098
}
10199

102100
private fun rebuildMesh(from: Any, to: Any): Unit = esp.rebuild()

common/src/main/kotlin/com/lambda/module/modules/render/StorageESP.kt

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import com.lambda.module.Module
3131
import com.lambda.module.tag.ModuleTag
3232
import com.lambda.threading.runSafe
3333
import com.lambda.util.extension.blockColor
34-
import com.lambda.util.extension.blockFilledMesh
35-
import com.lambda.util.extension.blockOutlineMesh
34+
import com.lambda.util.extension.outlineShape
3635
import com.lambda.util.math.setAlpha
3736
import com.lambda.util.world.blockEntitySearch
3837
import com.lambda.util.world.entitySearch
@@ -50,6 +49,7 @@ import net.minecraft.block.entity.SmokerBlockEntity
5049
import net.minecraft.entity.Entity
5150
import net.minecraft.entity.decoration.ItemFrameEntity
5251
import net.minecraft.entity.vehicle.AbstractMinecartEntity
52+
import net.minecraft.entity.vehicle.MinecartEntity
5353
import net.minecraft.util.math.BlockPos
5454
import java.awt.Color
5555

@@ -67,14 +67,13 @@ object StorageESP : Module(
6767
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks") { page == Page.Render }.apply { onValueSet { _, to -> if (!to) drawOutlines = true } }
6868
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks") { page == Page.Render }.apply { onValueSet { _, to -> if (!to) drawFaces = true } }
6969
private val outlineMode by setting("Outline Mode", DirectionMask.OutlineMode.AND, "Outline mode") { page == Page.Render }
70-
private val mesh by setting("Mesh", true, "Connect similar adjacent blocks")
70+
private val mesh by setting("Mesh", true, "Connect similar adjacent blocks") { page == Page.Render }
7171

7272
/* Color settings */
7373
private val useBlockColor by setting("Use Block Color", true, "Use the color of the block instead") { page == Page.Color }
7474
private val alpha by setting("Alpha", 0.3, 0.1..1.0, 0.05) { page == Page.Color }
7575

7676
// TODO:
77-
// Once we have map setting we can do this:
7877
// val blockColors by setting("Block Colors", mapOf<String, Color>()) { page == Page.Color && !useBlockColor }
7978
// val renders by setting("Render Blocks", mapOf<String, Boolean>()) { page == Page.General }
8079
//
@@ -98,39 +97,63 @@ object StorageESP : Module(
9897
private val smokerColor by setting("Smoker Color", Color(112, 112, 112)) { page == Page.Color && !useBlockColor }
9998
private val shulkerColor by setting("Shulker Color", Color(178, 76, 216)) { page == Page.Color && !useBlockColor }
10099
private val itemFrameColor by setting("Item Frame Color", Color(216, 127, 51)) { page == Page.Color && !useBlockColor }
101-
private val cartColor by setting("Cart Color", Color(102, 127, 51)) { page == Page.Color && !useBlockColor }
100+
private val cartColor by setting("Minecart Color", Color(102, 127, 51)) { page == Page.Color && !useBlockColor }
101+
102+
private val entities = setOf(
103+
BarrelBlockEntity::class,
104+
BlastFurnaceBlockEntity::class,
105+
BrewingStandBlockEntity::class,
106+
ChestBlockEntity::class,
107+
DispenserBlockEntity::class,
108+
EnderChestBlockEntity::class,
109+
FurnaceBlockEntity::class,
110+
HopperBlockEntity::class,
111+
SmokerBlockEntity::class,
112+
ShulkerBoxBlockEntity::class,
113+
AbstractMinecartEntity::class,
114+
ItemFrameEntity::class,
115+
MinecartEntity::class,
116+
)
102117

103118
init {
104119
listen<RenderEvent.StaticESP> { event ->
105-
blockEntitySearch<BlockEntity>(range = distance)
106-
.forEach { event.renderer.build(it, it.pos, buildMesh(it.pos)) }
107-
108-
(entitySearch<AbstractMinecartEntity>(range = distance) +
109-
entitySearch<ItemFrameEntity>(range = distance))
110-
.forEach { event.renderer.build(it, DirectionMask.ALL) } // I didn't add block entity meshing because I'm not sure how to handle blocks that aren't full
120+
blockEntitySearch<BlockEntity>(distance)
121+
.filter { it::class in entities }
122+
.forEach { event.renderer.build(it, it.pos, excludedSides(it)) }
123+
124+
val mineCarts = entitySearch<AbstractMinecartEntity>(distance)
125+
val itemFrames = entitySearch<ItemFrameEntity>(distance)
126+
(mineCarts + itemFrames)
127+
.forEach { event.renderer.build(it, DirectionMask.ALL) }
111128
}
112129
}
113130

114-
private fun SafeContext.buildMesh(position: BlockPos) =
115-
if (mesh) buildSideMesh(position) {
116-
val block = world.getBlockEntity(it) ?: return@buildSideMesh false
117-
118-
getBlockEntityColor(block) != null &&
119-
block.cachedState.isFullCube(world, it)
120-
}
121-
else DirectionMask.ALL
131+
private fun SafeContext.excludedSides(blockEntity: BlockEntity): Int {
132+
val isFullCube = blockEntity.cachedState.isFullCube(world, blockEntity.pos)
133+
return if (mesh && isFullCube) {
134+
buildSideMesh(blockEntity.pos) { neighbor ->
135+
val other = world.getBlockEntity(neighbor) ?: return@buildSideMesh false
136+
val otherFullCube = other.cachedState.isFullCube(world, other.pos)
137+
val sameType = blockEntity.cachedState.block == other.cachedState.block
138+
val searchedFor = other::class in entities
139+
140+
searchedFor && otherFullCube && sameType
141+
}
142+
} else DirectionMask.ALL
143+
}
122144

123145
private fun StaticESPRenderer.build(
124146
block: BlockEntity,
125147
pos: BlockPos,
126148
sides: Int,
127149
) = runSafe {
128-
val color = if (useBlockColor) blockColor(block.cachedState, pos) else getBlockEntityColor(block) ?: return@runSafe
129-
val filledMesh = blockFilledMesh(block.cachedState, pos)
130-
val outlineMesh = blockOutlineMesh(block.cachedState, pos)
150+
val color = if (useBlockColor) {
151+
blockColor(block.cachedState, pos)
152+
} else getBlockEntityColor(block) ?: return@runSafe
153+
val shape = outlineShape(block.cachedState, pos)
131154

132-
if (drawFaces) buildFilledMesh(filledMesh, color.setAlpha(alpha), sides)
133-
if (drawOutlines) buildOutlineMesh(outlineMesh, color, sides, outlineMode)
155+
if (drawFaces) buildFilledMesh(shape, color.setAlpha(alpha), sides)
156+
if (drawOutlines) buildOutlineMesh(shape, color, sides, outlineMode)
134157
}
135158

136159
private fun StaticESPRenderer.build(

common/src/main/kotlin/com/lambda/util/extension/World.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.lambda.util.world.*
2424
import net.minecraft.block.Block
2525
import net.minecraft.block.BlockState
2626
import net.minecraft.block.Blocks
27-
import net.minecraft.block.entity.BlockEntity
2827
import net.minecraft.datafixer.DataFixTypes
2928
import net.minecraft.fluid.FluidState
3029
import net.minecraft.fluid.Fluids
@@ -37,10 +36,10 @@ import net.minecraft.world.World
3736
import java.awt.Color
3837
import kotlin.experimental.and
3938

40-
fun SafeContext.blockFilledMesh(state: BlockState, pos: BlockPos) =
39+
fun SafeContext.collisionShape(state: BlockState, pos: BlockPos) =
4140
state.getCollisionShape(world, pos).offset(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble())
4241

43-
fun SafeContext.blockOutlineMesh(state: BlockState, pos: BlockPos) =
42+
fun SafeContext.outlineShape(state: BlockState, pos: BlockPos) =
4443
state.getOutlineShape(world, pos).offset(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble())
4544

4645
fun SafeContext.blockColor(state: BlockState, pos: BlockPos) =

common/src/main/kotlin/com/lambda/util/world/WorldDsl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ inline fun <reified T : BlockEntity> SafeContext.blockEntitySearch(
124124
range: Double = 64.0,
125125
pos: BlockPos = player.blockPos,
126126
noinline filter: (T) -> Boolean = { true },
127-
) = internalGetBlockEntities<T>(pos.toFastVec(), range, predicate = filter)
127+
) = internalGetBlockEntities<T>(pos.toFastVec(), range, predicate = filter).toSet()
128128

129129
@DslMarker
130130
annotation class EntityMarker
@@ -201,7 +201,7 @@ inline fun <reified T : Entity> SafeContext.fastEntitySearch(
201201
range: Double,
202202
pos: BlockPos = player.blockPos,
203203
noinline filter: (T) -> Boolean = { true },
204-
) = internalGetFastEntities<T>(pos.toFastVec(), range, predicate = filter)
204+
) = internalGetFastEntities<T>(pos.toFastVec(), range, predicate = filter).toSet()
205205

206206
@DslMarker
207207
annotation class FluidMarker

0 commit comments

Comments
 (0)