Skip to content

Commit 99055b0

Browse files
committed
feat: mesh builder
1 parent c11dd2f commit 99055b0

File tree

3 files changed

+114
-58
lines changed

3 files changed

+114
-58
lines changed

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

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,38 @@ import net.minecraft.util.math.Box
3030
import net.minecraft.util.shape.VoxelShape
3131
import java.awt.Color
3232

33-
fun StaticESPRenderer.buildVoxel(
33+
fun StaticESPRenderer.buildMesh(
3434
pos: BlockPos,
3535
state: BlockState,
36-
color: Color,
36+
filledColor: Color,
37+
outlineColor: Color,
3738
sides: Int = DirectionMask.ALL,
39+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
3840
) = runSafe {
3941
val shape = state.getOutlineShape(world, pos)
40-
buildVoxel(shape, color, sides)
42+
buildMesh(shape, filledColor, outlineColor, sides, outlineMode)
4143
}
4244

43-
fun StaticESPRenderer.buildVoxel(
45+
fun StaticESPRenderer.buildMesh(
4446
pos: BlockPos,
45-
color: Color,
47+
filledColor: Color,
48+
outlineColor: Color,
4649
sides: Int = DirectionMask.ALL,
50+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
4751
) = runSafe {
4852
val shape = pos.blockState(world).getOutlineShape(world, pos)
49-
buildVoxel(shape, color, sides)
53+
buildMesh(shape, filledColor, outlineColor, sides, outlineMode)
5054
}
5155

52-
fun StaticESPRenderer.buildVoxel(
56+
fun StaticESPRenderer.buildMesh(
5357
shape: VoxelShape,
54-
color: Color,
58+
filledColor: Color,
59+
outlineColor: Color,
5560
sides: Int = DirectionMask.ALL,
61+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
5662
) {
5763
shape.boundingBoxes
58-
.forEach { buildFilled(it, color, sides) }
64+
.forEach { build(it,filledColor, outlineColor, sides, outlineMode) }
5965
}
6066

6167

@@ -70,12 +76,72 @@ fun StaticESPRenderer.build(
7076
buildOutline(box, outlineColor, sides, outlineMode)
7177
}
7278

79+
fun StaticESPRenderer.buildFilledMesh(
80+
pos: BlockPos,
81+
state: BlockState,
82+
color: Color,
83+
sides: Int = DirectionMask.ALL,
84+
) = runSafe {
85+
val shape = state.getOutlineShape(world, pos)
86+
buildFilledMesh(shape, color, sides)
87+
}
88+
89+
fun StaticESPRenderer.buildFilledMesh(
90+
pos: BlockPos,
91+
color: Color,
92+
sides: Int = DirectionMask.ALL,
93+
) = runSafe {
94+
val shape = pos.blockState(world).getOutlineShape(world, pos)
95+
buildFilledMesh(shape, color, sides)
96+
}
97+
98+
fun StaticESPRenderer.buildFilledMesh(
99+
shape: VoxelShape,
100+
color: Color,
101+
sides: Int = DirectionMask.ALL,
102+
) {
103+
shape.boundingBoxes
104+
.forEach { buildFilled(it, color, sides) }
105+
}
106+
107+
73108
fun StaticESPRenderer.buildFilled(
74109
box: Box,
75110
color: Color,
76111
sides: Int = DirectionMask.ALL
77112
) = buildFilled(box, color, color, sides)
78113

114+
fun StaticESPRenderer.buildOutlineMesh(
115+
pos: BlockPos,
116+
state: BlockState,
117+
color: Color,
118+
sides: Int = DirectionMask.ALL,
119+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
120+
) = runSafe {
121+
val shape = state.getOutlineShape(world, pos)
122+
buildOutlineMesh(shape, color, sides)
123+
}
124+
125+
fun StaticESPRenderer.buildOutlineMesh(
126+
pos: BlockPos,
127+
color: Color,
128+
sides: Int = DirectionMask.ALL,
129+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
130+
) = runSafe {
131+
val shape = pos.blockState(world).getOutlineShape(world, pos)
132+
buildOutlineMesh(shape, color, sides)
133+
}
134+
135+
fun StaticESPRenderer.buildOutlineMesh(
136+
shape: VoxelShape,
137+
color: Color,
138+
sides: Int = DirectionMask.ALL,
139+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
140+
) {
141+
shape.boundingBoxes
142+
.forEach { buildOutline(it, color, sides) }
143+
}
144+
79145
fun StaticESPRenderer.buildOutline(
80146
box: Box,
81147
color: Color,

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

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,42 @@ import com.lambda.Lambda.mc
2121
import com.lambda.graphics.renderer.esp.ChunkedESP.Companion.newChunkedESP
2222
import com.lambda.graphics.renderer.esp.DirectionMask
2323
import com.lambda.graphics.renderer.esp.DirectionMask.buildSideMesh
24-
import com.lambda.graphics.renderer.esp.builders.buildFilled
25-
import com.lambda.graphics.renderer.esp.builders.buildOutline
24+
import com.lambda.graphics.renderer.esp.builders.buildFilledMesh
25+
import com.lambda.graphics.renderer.esp.builders.buildOutlineMesh
2626
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
2727
import com.lambda.module.Module
2828
import com.lambda.module.tag.ModuleTag
29+
import com.lambda.threading.runSafe
30+
import com.lambda.util.extension.blockColor
31+
import com.lambda.util.extension.blockFilledMesh
32+
import com.lambda.util.extension.blockOutlineMesh
2933
import com.lambda.util.extension.getBlockState
3034
import com.lambda.util.world.fastVectorOf
35+
import com.lambda.util.world.toBlockPos
3136
import net.minecraft.block.Block
37+
import net.minecraft.block.BlockState
3238
import net.minecraft.block.Blocks
3339
import net.minecraft.client.render.model.BakedModel
34-
import net.minecraft.util.math.Box
40+
import net.minecraft.util.math.BlockPos
3541
import java.awt.Color
3642

3743
object BlockESP : Module(
3844
name = "BlockESP",
3945
description = "Render block ESP",
4046
defaultTags = setOf(ModuleTag.RENDER)
4147
) {
42-
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks").apply {
43-
onValueSet { _, to ->
44-
esp.rebuild()
45-
if (!to) drawOutlines = true
46-
}
47-
}
48-
49-
private val faceColor: Color by setting("Face Color", Color(100, 150, 255, 51), "Color of the surfaces") {
50-
drawFaces
51-
}.apply {
52-
onValueSet { _, _ -> esp.rebuild() }
53-
}
54-
55-
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks").apply {
56-
onValueSet { _, to ->
57-
esp.rebuild()
58-
if (!to) drawFaces = true
59-
}
60-
}
48+
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks").apply { onValueSet { _, to -> esp.rebuild(); if (!to) drawOutlines = true } }
49+
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks").apply { onValueSet { _, to -> esp.rebuild(); if (!to) drawFaces = true } }
6150

62-
private val outlineColor: Color by setting("Outline Color", Color(100, 150, 255, 128), "Color of the outlines") {
63-
drawOutlines
64-
}.apply {
65-
onValueSet { _, _ -> esp.rebuild() }
66-
}
51+
private val useBlockColor: Boolean by setting("Use Block Color", false, "Use the color of the block instead").apply { onValueSet { _, _ -> esp.rebuild() } }
52+
private val faceColor: Color by setting("Face Color", Color(100, 150, 255, 51), "Color of the surfaces") { drawFaces && !useBlockColor }.apply { onValueSet { _, _ -> esp.rebuild() } }
53+
private val outlineColor: Color by setting("Outline Color", Color(100, 150, 255, 128), "Color of the outlines") { drawOutlines && !useBlockColor }.apply { onValueSet { _, _ -> esp.rebuild() } }
6754

68-
private val outlineMode: DirectionMask.OutlineMode by setting("Outline Mode", DirectionMask.OutlineMode.AND, "Outline mode").apply {
69-
onValueSet { _, _ -> esp.rebuild() }
70-
}
55+
private val outlineMode: DirectionMask.OutlineMode by setting("Outline Mode", DirectionMask.OutlineMode.AND, "Outline mode").apply { onValueSet { _, _ -> esp.rebuild() } }
7156

72-
private val mesh: Boolean by setting("Mesh", true, "Connect similar adjacent blocks").apply {
73-
onValueSet { _, _ -> esp.rebuild() }
74-
}
57+
private val mesh: Boolean by setting("Mesh", true, "Connect similar adjacent blocks").apply { onValueSet { _, _ -> esp.rebuild() } }
7558

76-
private val blocks: Set<Block> by setting("Blocks", setOf(Blocks.BEDROCK), "Render blocks").apply {
77-
onValueSet { _, _ -> esp.rebuild() }
78-
}
59+
private val blocks: Set<Block> by setting("Blocks", setOf(Blocks.BEDROCK), "Render blocks").apply { onValueSet { _, _ -> esp.rebuild() } }
7960

8061
@JvmStatic
8162
val barrier by setting("Solid Barrier Block", true, "Render barrier blocks")
@@ -104,22 +85,19 @@ object BlockESP : Module(
10485
}
10586
} else DirectionMask.ALL
10687

107-
build(
108-
// big hack
109-
Box(x.toDouble(), y.toDouble(), z.toDouble(), x.toDouble() + 1, y.toDouble() + 1, z.toDouble() + 1),
110-
sides
111-
)
88+
build(state, position.toBlockPos(), sides)
11289
}
11390

11491
private fun StaticESPRenderer.build(
115-
box: Box,
92+
state: BlockState,
93+
pos: BlockPos,
11694
sides: Int,
117-
) {
118-
if (drawFaces) {
119-
buildFilled(box, faceColor, sides)
120-
}
121-
if (drawOutlines) {
122-
buildOutline(box, outlineColor, sides, outlineMode)
123-
}
95+
) = runSafe {
96+
val filledMesh = blockFilledMesh(state, pos)
97+
val outlineMesh = blockOutlineMesh(state, pos)
98+
val blockColor = blockColor(state, pos)
99+
100+
if (drawFaces) buildFilledMesh(filledMesh, if (useBlockColor) blockColor else faceColor, sides)
101+
if (drawOutlines) buildOutlineMesh(outlineMesh, if (useBlockColor) blockColor else outlineColor, sides, outlineMode)
124102
}
125103
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.util.extension
1919

2020
import com.lambda.Lambda.mc
21+
import com.lambda.context.SafeContext
2122
import com.lambda.util.VarIntIterator
2223
import com.lambda.util.world.*
2324
import net.minecraft.block.Block
@@ -30,9 +31,20 @@ import net.minecraft.nbt.NbtCompound
3031
import net.minecraft.nbt.NbtList
3132
import net.minecraft.registry.RegistryEntryLookup
3233
import net.minecraft.structure.StructureTemplate
34+
import net.minecraft.util.math.BlockPos
3335
import net.minecraft.world.World
36+
import java.awt.Color
3437
import kotlin.experimental.and
3538

39+
fun SafeContext.blockFilledMesh(state: BlockState, pos: BlockPos) =
40+
state.getCollisionShape(world, pos).offset(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble())
41+
42+
fun SafeContext.blockOutlineMesh(state: BlockState, pos: BlockPos) =
43+
state.getOutlineShape(world, pos).offset(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble())
44+
45+
fun SafeContext.blockColor(state: BlockState, pos: BlockPos) =
46+
Color(state.getMapColor(world, pos).color)
47+
3648
fun World.getBlockState(x: Int, y: Int, z: Int): BlockState {
3749
if (isOutOfHeightLimit(y)) return Blocks.VOID_AIR.defaultState
3850

0 commit comments

Comments
 (0)