Skip to content

Commit 631ad18

Browse files
committed
ESP (pushing but needs to be refactored)
1 parent 7c179d1 commit 631ad18

File tree

6 files changed

+342
-27
lines changed

6 files changed

+342
-27
lines changed

common/src/main/kotlin/com/lambda/event/events/RenderEvent.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import com.lambda.event.Event
55
import com.lambda.event.callback.Cancellable
66
import com.lambda.event.callback.ICancellable
77
import com.lambda.util.math.Vec2d
8+
import net.minecraft.entity.LivingEntity
9+
import net.minecraft.util.math.BlockPos
10+
import java.awt.Color
811

912
abstract class RenderEvent : Event {
1013
class World : RenderEvent()
@@ -15,5 +18,6 @@ abstract class RenderEvent : Event {
1518

1619
val screenSize = Vec2d(mc.window.framebufferWidth, mc.window.framebufferHeight) / scale
1720
}
21+
1822
class UpdateTarget : RenderEvent(), ICancellable by Cancellable()
1923
}

common/src/main/kotlin/com/lambda/graphics/gl/GlStateUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ object GlStateUtils {
3636
depthTest(false)
3737
}
3838

39+
fun withLineWidth(width: Double, block: () -> Unit) {
40+
glLineWidth(width.toFloat())
41+
block()
42+
glLineWidth(1f)
43+
}
44+
3945
@JvmStatic
4046
fun capSet(id: Int, flag: Boolean) {
4147
val field = when (id) {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.lambda.graphics.renderer.esp
2+
3+
import com.lambda.event.events.RenderEvent
4+
import com.lambda.event.listener.SafeListener.Companion.listener
5+
import com.lambda.graphics.buffer.vao.VAO
6+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
7+
import com.lambda.graphics.buffer.vao.vertex.VertexMode
8+
import com.lambda.graphics.gl.GlStateUtils.withLineWidth
9+
import com.lambda.graphics.renderer.esp.DirectionMask.DOWN
10+
import com.lambda.graphics.renderer.esp.DirectionMask.EAST
11+
import com.lambda.graphics.renderer.esp.DirectionMask.NORTH
12+
import com.lambda.graphics.renderer.esp.DirectionMask.SOUTH
13+
import com.lambda.graphics.renderer.esp.DirectionMask.UP
14+
import com.lambda.graphics.renderer.esp.DirectionMask.WEST
15+
import com.lambda.graphics.renderer.esp.DirectionMask.hasDirection
16+
import com.lambda.graphics.shader.Shader
17+
import com.lambda.module.Module
18+
import com.lambda.util.primitives.extension.max
19+
import com.lambda.util.primitives.extension.min
20+
import net.minecraft.util.math.Box
21+
import java.awt.Color
22+
23+
class CachedEspRenderer(val owner: Any) {
24+
private val filled = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.STATIC_RENDERER)
25+
private var updateFilled = false
26+
27+
private val outline = VAO(VertexMode.LINES, VertexAttrib.Group.STATIC_RENDERER)
28+
private var updateOutline = false
29+
30+
var outlineWidth = 1.0
31+
32+
fun build(box: Box, filledColor: Color, outlineColor: Color, sides: Int = DirectionMask.ALL, outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR) {
33+
buildFilled(box, filledColor, sides)
34+
buildOutline(box, outlineColor, sides, outlineMode)
35+
}
36+
37+
fun buildFilled(box: Box, color: Color, sides: Int = DirectionMask.ALL) = filled.use {
38+
updateFilled = true
39+
val pos1 = box.min
40+
val pos2 = box.max
41+
42+
grow(8)
43+
44+
val blb by lazy { vec3(pos1.x, pos1.y, pos1.z).color(color).end() }
45+
val blf by lazy { vec3(pos1.x, pos1.y, pos2.z).color(color).end() }
46+
val brb by lazy { vec3(pos2.x, pos1.y, pos1.z).color(color).end() }
47+
val brf by lazy { vec3(pos2.x, pos1.y, pos2.z).color(color).end() }
48+
val tlb by lazy { vec3(pos1.x, pos2.y, pos1.z).color(color).end() }
49+
val tlf by lazy { vec3(pos1.x, pos2.y, pos2.z).color(color).end() }
50+
val trb by lazy { vec3(pos2.x, pos2.y, pos1.z).color(color).end() }
51+
val trf by lazy { vec3(pos2.x, pos2.y, pos2.z).color(color).end() }
52+
53+
if (sides.hasDirection(EAST)) putQuad(brb, brf, trf, trb)
54+
if (sides.hasDirection(WEST)) putQuad(blb, blf, tlf, tlb)
55+
if (sides.hasDirection(UP)) putQuad(tlb, tlf, trf, trb)
56+
if (sides.hasDirection(DOWN)) putQuad(blb, brb, brf, blf)
57+
if (sides.hasDirection(SOUTH)) putQuad(blf, brf, trf, tlf)
58+
if (sides.hasDirection(NORTH)) putQuad(blb, brb, trb, tlb)
59+
}
60+
61+
fun buildOutline(box: Box, color: Color, sides: Int = DirectionMask.ALL, outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR) = outline.use {
62+
updateOutline = true
63+
val pos1 = box.min
64+
val pos2 = box.max
65+
66+
grow(8)
67+
68+
val blb by lazy { vec3(pos1.x, pos1.y, pos1.z).color(color).end() }
69+
val blf by lazy { vec3(pos1.x, pos1.y, pos2.z).color(color).end() }
70+
val brb by lazy { vec3(pos2.x, pos1.y, pos1.z).color(color).end() }
71+
val brf by lazy { vec3(pos2.x, pos1.y, pos2.z).color(color).end() }
72+
val tlb by lazy { vec3(pos1.x, pos2.y, pos1.z).color(color).end() }
73+
val tlf by lazy { vec3(pos1.x, pos2.y, pos2.z).color(color).end() }
74+
val trb by lazy { vec3(pos2.x, pos2.y, pos1.z).color(color).end() }
75+
val trf by lazy { vec3(pos2.x, pos2.y, pos2.z).color(color).end() }
76+
77+
val hasEast = sides.hasDirection(EAST)
78+
val hasWest = sides.hasDirection(WEST)
79+
val hasUp = sides.hasDirection(UP)
80+
val hasDown = sides.hasDirection(DOWN)
81+
val hasSouth = sides.hasDirection(SOUTH)
82+
val hasNorth = sides.hasDirection(NORTH)
83+
84+
if (outlineMode.check(hasUp, hasNorth)) putLine(tlb, trb)
85+
if (outlineMode.check(hasUp, hasSouth)) putLine(tlf, trf)
86+
if (outlineMode.check(hasUp, hasWest)) putLine(tlb, tlf)
87+
if (outlineMode.check(hasUp, hasEast)) putLine(trf, trb)
88+
89+
if (outlineMode.check(hasDown, hasNorth)) putLine(blb, brb)
90+
if (outlineMode.check(hasDown, hasSouth)) putLine(blf, brf)
91+
if (outlineMode.check(hasDown, hasWest)) putLine(blb, blf)
92+
if (outlineMode.check(hasDown, hasEast)) putLine(brb, brf)
93+
94+
if (outlineMode.check(hasWest, hasNorth)) putLine(tlb, blb)
95+
if (outlineMode.check(hasNorth, hasEast)) putLine(trb, brb)
96+
if (outlineMode.check(hasEast, hasSouth)) putLine(trf, brf)
97+
if (outlineMode.check(hasSouth, hasWest)) putLine(tlf, blf)
98+
}
99+
100+
fun clear() {
101+
filled.clear()
102+
outline.clear()
103+
}
104+
105+
init {
106+
owner.listener<RenderEvent.World> {
107+
if (updateFilled) {
108+
updateFilled = false
109+
filled.upload()
110+
}
111+
112+
if (updateOutline) {
113+
updateOutline = false
114+
outline.upload()
115+
}
116+
117+
shader.use()
118+
shader["u_CameraPosition"] = mc.gameRenderer.camera.pos
119+
120+
filled.render()
121+
withLineWidth(outlineWidth, outline::render)
122+
}
123+
}
124+
125+
companion object {
126+
private val shader = Shader("renderer/pos_color", "renderer/box_static")
127+
}
128+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.lambda.graphics.renderer.esp
2+
3+
import net.minecraft.util.math.Direction
4+
5+
object DirectionMask {
6+
const val EAST = 1 // X +
7+
const val WEST = 2 // X -
8+
9+
const val UP = 4 // Y +
10+
const val DOWN = 8 // Y -
11+
12+
const val SOUTH = 16 // Z +
13+
const val NORTH = 32 // Z -
14+
15+
const val ALL = EAST or WEST or UP or DOWN or SOUTH or NORTH
16+
const val NONE = 0
17+
18+
fun Int.exclude(dir: Int) = this xor dir
19+
fun Int.hasDirection(dir: Int) = (this and dir) != 0
20+
21+
val Direction.mask get() = when (this) {
22+
Direction.DOWN -> DOWN
23+
Direction.UP -> UP
24+
Direction.NORTH -> NORTH
25+
Direction.SOUTH -> SOUTH
26+
Direction.WEST -> WEST
27+
Direction.EAST -> EAST
28+
}
29+
30+
enum class OutlineMode(val check: (Boolean, Boolean) -> Boolean) {
31+
// Render engine will add a line if BOTH touching sides are included into the mask
32+
AND(Boolean::and),
33+
34+
// Render engine will add a line if ANY OF touching sides is included into the mask
35+
OR(Boolean::or)
36+
}
37+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.lambda.graphics.renderer.esp
2+
3+
import com.lambda.context.SafeContext
4+
import com.lambda.event.events.RenderEvent
5+
import com.lambda.event.events.TickEvent
6+
import com.lambda.event.listener.SafeListener.Companion.listener
7+
import com.lambda.graphics.buffer.vao.VAO
8+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
9+
import com.lambda.graphics.buffer.vao.vertex.VertexMode
10+
import com.lambda.graphics.renderer.esp.DirectionMask.DOWN
11+
import com.lambda.graphics.renderer.esp.DirectionMask.EAST
12+
import com.lambda.graphics.renderer.esp.DirectionMask.NORTH
13+
import com.lambda.graphics.renderer.esp.DirectionMask.SOUTH
14+
import com.lambda.graphics.renderer.esp.DirectionMask.UP
15+
import com.lambda.graphics.renderer.esp.DirectionMask.WEST
16+
import com.lambda.graphics.renderer.esp.DirectionMask.hasDirection
17+
import com.lambda.graphics.shader.Shader
18+
import com.lambda.util.primitives.extension.max
19+
import com.lambda.util.primitives.extension.min
20+
import com.lambda.util.primitives.extension.partialTicks
21+
import com.lambda.util.primitives.extension.prevPos
22+
import net.minecraft.entity.Entity
23+
import net.minecraft.util.math.Box
24+
import java.awt.Color
25+
26+
object EntityEspRegistry {
27+
private val filled = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.DYNAMIC_RENDERER)
28+
private val outline = VAO(VertexMode.LINES, VertexAttrib.Group.DYNAMIC_RENDERER)
29+
private val shader = Shader("renderer/pos_color", "renderer/box_dynamic")
30+
31+
fun renderEntityESP(owner: Any, block: SafeContext.(Renderer) -> Unit) {
32+
owner.listener<TickEvent.Post> {
33+
block(Renderer)
34+
}
35+
}
36+
37+
object Renderer {
38+
fun build(entity: Entity, filledColor: Color, outlineColor: Color, sides: Int = DirectionMask.ALL, outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR) {
39+
buildFilled(entity, filledColor, sides)
40+
buildOutline(entity, outlineColor, sides, outlineMode)
41+
}
42+
43+
fun buildFilled(entity: Entity, color: Color, sides: Int = DirectionMask.ALL) = filled.use {
44+
val box = entity.boundingBox
45+
46+
val delta = entity.prevPos.subtract(entity.pos)
47+
val prevBox = Box(box.min.add(delta), box.max.add(delta))
48+
49+
val pos11 = prevBox.min
50+
val pos12 = prevBox.max
51+
val pos21 = box.min
52+
val pos22 = box.max
53+
54+
grow(8)
55+
56+
val blb by lazy { vec3(pos11.x, pos11.y, pos11.z).vec3(pos21.x, pos21.y, pos21.z).color(color).end() }
57+
val blf by lazy { vec3(pos11.x, pos11.y, pos12.z).vec3(pos21.x, pos21.y, pos22.z).color(color).end() }
58+
val brb by lazy { vec3(pos12.x, pos11.y, pos11.z).vec3(pos22.x, pos21.y, pos21.z).color(color).end() }
59+
val brf by lazy { vec3(pos12.x, pos11.y, pos12.z).vec3(pos22.x, pos21.y, pos22.z).color(color).end() }
60+
val tlb by lazy { vec3(pos11.x, pos12.y, pos11.z).vec3(pos21.x, pos22.y, pos21.z).color(color).end() }
61+
val tlf by lazy { vec3(pos11.x, pos12.y, pos12.z).vec3(pos21.x, pos22.y, pos22.z).color(color).end() }
62+
val trb by lazy { vec3(pos12.x, pos12.y, pos11.z).vec3(pos22.x, pos22.y, pos21.z).color(color).end() }
63+
val trf by lazy { vec3(pos12.x, pos12.y, pos12.z).vec3(pos22.x, pos22.y, pos22.z).color(color).end() }
64+
65+
if (sides.hasDirection(EAST)) putQuad(brb, brf, trf, trb)
66+
if (sides.hasDirection(WEST)) putQuad(blb, blf, tlf, tlb)
67+
if (sides.hasDirection(UP)) putQuad(tlb, tlf, trf, trb)
68+
if (sides.hasDirection(DOWN)) putQuad(blb, brb, brf, blf)
69+
if (sides.hasDirection(SOUTH)) putQuad(blf, brf, trf, tlf)
70+
if (sides.hasDirection(NORTH)) putQuad(blb, brb, trb, tlb)
71+
}
72+
73+
fun buildOutline(entity: Entity, color: Color, sides: Int = DirectionMask.ALL, outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR) = outline.use {
74+
val box = entity.boundingBox
75+
76+
val delta = entity.prevPos.subtract(entity.pos)
77+
val prevBox = Box(box.min.add(delta), box.max.add(delta))
78+
79+
val pos11 = prevBox.min
80+
val pos12 = prevBox.max
81+
val pos21 = box.min
82+
val pos22 = box.max
83+
84+
grow(8)
85+
86+
val blb by lazy { vec3(pos11.x, pos11.y, pos11.z).vec3(pos21.x, pos21.y, pos21.z).color(color).end() }
87+
val blf by lazy { vec3(pos11.x, pos11.y, pos12.z).vec3(pos21.x, pos21.y, pos22.z).color(color).end() }
88+
val brb by lazy { vec3(pos12.x, pos11.y, pos11.z).vec3(pos22.x, pos21.y, pos21.z).color(color).end() }
89+
val brf by lazy { vec3(pos12.x, pos11.y, pos12.z).vec3(pos22.x, pos21.y, pos22.z).color(color).end() }
90+
val tlb by lazy { vec3(pos11.x, pos12.y, pos11.z).vec3(pos21.x, pos22.y, pos21.z).color(color).end() }
91+
val tlf by lazy { vec3(pos11.x, pos12.y, pos12.z).vec3(pos21.x, pos22.y, pos22.z).color(color).end() }
92+
val trb by lazy { vec3(pos12.x, pos12.y, pos11.z).vec3(pos22.x, pos22.y, pos21.z).color(color).end() }
93+
val trf by lazy { vec3(pos12.x, pos12.y, pos12.z).vec3(pos22.x, pos22.y, pos22.z).color(color).end() }
94+
95+
val hasEast = sides.hasDirection(EAST)
96+
val hasWest = sides.hasDirection(WEST)
97+
val hasUp = sides.hasDirection(UP)
98+
val hasDown = sides.hasDirection(DOWN)
99+
val hasSouth = sides.hasDirection(SOUTH)
100+
val hasNorth = sides.hasDirection(NORTH)
101+
102+
if (outlineMode.check(hasUp, hasNorth)) putLine(tlb, trb)
103+
if (outlineMode.check(hasUp, hasSouth)) putLine(tlf, trf)
104+
if (outlineMode.check(hasUp, hasWest)) putLine(tlb, tlf)
105+
if (outlineMode.check(hasUp, hasEast)) putLine(trf, trb)
106+
107+
if (outlineMode.check(hasDown, hasNorth)) putLine(blb, brb)
108+
if (outlineMode.check(hasDown, hasSouth)) putLine(blf, brf)
109+
if (outlineMode.check(hasDown, hasWest)) putLine(blb, blf)
110+
if (outlineMode.check(hasDown, hasEast)) putLine(brb, brf)
111+
112+
if (outlineMode.check(hasWest, hasNorth)) putLine(tlb, blb)
113+
if (outlineMode.check(hasNorth, hasEast)) putLine(trb, brb)
114+
if (outlineMode.check(hasEast, hasSouth)) putLine(trf, brf)
115+
if (outlineMode.check(hasSouth, hasWest)) putLine(tlf, blf)
116+
}
117+
}
118+
119+
init {
120+
listener<TickEvent.Post>(Int.MAX_VALUE) {
121+
filled.clear()
122+
outline.clear()
123+
}
124+
125+
listener<TickEvent.Post>(Int.MIN_VALUE) {
126+
filled.upload()
127+
outline.upload()
128+
}
129+
130+
listener<RenderEvent.World> {
131+
shader.use()
132+
shader["u_TickDelta"] = mc.partialTicks
133+
shader["u_CameraPosition"] = mc.gameRenderer.camera.pos
134+
135+
filled.render()
136+
outline.render()
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)