Skip to content

Commit 46b9eab

Browse files
committed
EntityESP refactor
1 parent 04b8a1d commit 46b9eab

File tree

5 files changed

+162
-145
lines changed

5 files changed

+162
-145
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,41 @@ import com.lambda.Lambda.mc
44
import com.lambda.event.Event
55
import com.lambda.event.callback.Cancellable
66
import com.lambda.event.callback.ICancellable
7+
import com.lambda.graphics.renderer.esp.DirectionMask
8+
import com.lambda.graphics.renderer.esp.EntityEspRenderer
79
import com.lambda.util.math.Vec2d
8-
import net.minecraft.entity.LivingEntity
9-
import net.minecraft.util.math.BlockPos
10+
import net.minecraft.entity.Entity
1011
import java.awt.Color
1112

1213
abstract class RenderEvent : Event {
1314
class World : RenderEvent()
1415

16+
class EntityESP : RenderEvent() {
17+
fun build(
18+
entity: Entity,
19+
filledColor: Color,
20+
outlineColor: Color,
21+
sides: Int = DirectionMask.ALL,
22+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
23+
) {
24+
buildFilled(entity, filledColor, sides)
25+
buildOutline(entity, outlineColor, sides, outlineMode)
26+
}
27+
28+
fun buildFilled(
29+
entity: Entity,
30+
color: Color,
31+
sides: Int = DirectionMask.ALL
32+
) = EntityEspRenderer.buildFilled(entity, color, sides)
33+
34+
fun buildOutline(
35+
entity: Entity,
36+
color: Color,
37+
sides: Int = DirectionMask.ALL,
38+
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
39+
) = EntityEspRenderer.buildOutline(entity, color, sides, outlineMode)
40+
}
41+
1542
abstract class GUI(val scale: Double) : RenderEvent() {
1643
class Scaled(scaleFactor: Double) : GUI(scaleFactor)
1744
class Fixed : GUI(1.0)

common/src/main/kotlin/com/lambda/graphics/RenderMain.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.lambda.graphics.gl.GlStateUtils.setupGL
77
import com.lambda.graphics.gl.Matrices
88
import com.lambda.graphics.gl.Matrices.resetMatrix
99
import com.lambda.graphics.gl.Matrices.translate
10+
import com.lambda.graphics.renderer.esp.EntityEspRenderer
1011
import com.lambda.module.modules.client.GuiSettings
1112
import com.lambda.util.math.Vec2d
1213
import com.mojang.blaze3d.systems.RenderSystem.getProjectionMatrix
@@ -39,6 +40,7 @@ object RenderMain {
3940

4041
setupGL {
4142
RenderEvent.World().post()
43+
EntityEspRenderer.render()
4244
}
4345
}
4446

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

Lines changed: 0 additions & 139 deletions
This file was deleted.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.lambda.graphics.renderer.esp
2+
3+
import com.lambda.Lambda.mc
4+
import com.lambda.event.EventFlow.post
5+
import com.lambda.event.events.RenderEvent
6+
import com.lambda.event.events.TickEvent
7+
import com.lambda.event.listener.SafeListener.Companion.listener
8+
import com.lambda.graphics.buffer.vao.VAO
9+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
10+
import com.lambda.graphics.buffer.vao.vertex.VertexMode
11+
import com.lambda.graphics.renderer.esp.DirectionMask.DOWN
12+
import com.lambda.graphics.renderer.esp.DirectionMask.EAST
13+
import com.lambda.graphics.renderer.esp.DirectionMask.NORTH
14+
import com.lambda.graphics.renderer.esp.DirectionMask.SOUTH
15+
import com.lambda.graphics.renderer.esp.DirectionMask.UP
16+
import com.lambda.graphics.renderer.esp.DirectionMask.WEST
17+
import com.lambda.graphics.renderer.esp.DirectionMask.hasDirection
18+
import com.lambda.graphics.shader.Shader
19+
import com.lambda.util.primitives.extension.max
20+
import com.lambda.util.primitives.extension.min
21+
import com.lambda.util.primitives.extension.partialTicks
22+
import com.lambda.util.primitives.extension.prevPos
23+
import net.minecraft.entity.Entity
24+
import net.minecraft.util.math.Box
25+
import java.awt.Color
26+
27+
object EntityEspRenderer {
28+
private val filled = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.DYNAMIC_RENDERER)
29+
private val outline = VAO(VertexMode.LINES, VertexAttrib.Group.DYNAMIC_RENDERER)
30+
private val shader = Shader("renderer/pos_color", "renderer/box_dynamic")
31+
32+
fun buildFilled(entity: Entity, color: Color, sides: Int) = filled.use {
33+
val box = entity.boundingBox
34+
35+
val delta = entity.prevPos.subtract(entity.pos)
36+
val prevBox = Box(box.min.add(delta), box.max.add(delta))
37+
38+
val pos11 = prevBox.min
39+
val pos12 = prevBox.max
40+
val pos21 = box.min
41+
val pos22 = box.max
42+
43+
grow(8)
44+
45+
val blb by lazy { vec3(pos11.x, pos11.y, pos11.z).vec3(pos21.x, pos21.y, pos21.z).color(color).end() }
46+
val blf by lazy { vec3(pos11.x, pos11.y, pos12.z).vec3(pos21.x, pos21.y, pos22.z).color(color).end() }
47+
val brb by lazy { vec3(pos12.x, pos11.y, pos11.z).vec3(pos22.x, pos21.y, pos21.z).color(color).end() }
48+
val brf by lazy { vec3(pos12.x, pos11.y, pos12.z).vec3(pos22.x, pos21.y, pos22.z).color(color).end() }
49+
val tlb by lazy { vec3(pos11.x, pos12.y, pos11.z).vec3(pos21.x, pos22.y, pos21.z).color(color).end() }
50+
val tlf by lazy { vec3(pos11.x, pos12.y, pos12.z).vec3(pos21.x, pos22.y, pos22.z).color(color).end() }
51+
val trb by lazy { vec3(pos12.x, pos12.y, pos11.z).vec3(pos22.x, pos22.y, pos21.z).color(color).end() }
52+
val trf by lazy { vec3(pos12.x, pos12.y, pos12.z).vec3(pos22.x, pos22.y, pos22.z).color(color).end() }
53+
54+
if (sides.hasDirection(EAST)) putQuad(brb, brf, trf, trb)
55+
if (sides.hasDirection(WEST)) putQuad(blb, blf, tlf, tlb)
56+
if (sides.hasDirection(UP)) putQuad(tlb, tlf, trf, trb)
57+
if (sides.hasDirection(DOWN)) putQuad(blb, brb, brf, blf)
58+
if (sides.hasDirection(SOUTH)) putQuad(blf, brf, trf, tlf)
59+
if (sides.hasDirection(NORTH)) putQuad(blb, brb, trb, tlb)
60+
}
61+
62+
fun buildOutline(entity: Entity, color: Color, sides: Int, outlineMode: DirectionMask.OutlineMode) = outline.use {
63+
val box = entity.boundingBox
64+
65+
val delta = entity.prevPos.subtract(entity.pos)
66+
val prevBox = Box(box.min.add(delta), box.max.add(delta))
67+
68+
val pos11 = prevBox.min
69+
val pos12 = prevBox.max
70+
val pos21 = box.min
71+
val pos22 = box.max
72+
73+
grow(8)
74+
75+
val blb by lazy { vec3(pos11.x, pos11.y, pos11.z).vec3(pos21.x, pos21.y, pos21.z).color(color).end() }
76+
val blf by lazy { vec3(pos11.x, pos11.y, pos12.z).vec3(pos21.x, pos21.y, pos22.z).color(color).end() }
77+
val brb by lazy { vec3(pos12.x, pos11.y, pos11.z).vec3(pos22.x, pos21.y, pos21.z).color(color).end() }
78+
val brf by lazy { vec3(pos12.x, pos11.y, pos12.z).vec3(pos22.x, pos21.y, pos22.z).color(color).end() }
79+
val tlb by lazy { vec3(pos11.x, pos12.y, pos11.z).vec3(pos21.x, pos22.y, pos21.z).color(color).end() }
80+
val tlf by lazy { vec3(pos11.x, pos12.y, pos12.z).vec3(pos21.x, pos22.y, pos22.z).color(color).end() }
81+
val trb by lazy { vec3(pos12.x, pos12.y, pos11.z).vec3(pos22.x, pos22.y, pos21.z).color(color).end() }
82+
val trf by lazy { vec3(pos12.x, pos12.y, pos12.z).vec3(pos22.x, pos22.y, pos22.z).color(color).end() }
83+
84+
val hasEast = sides.hasDirection(EAST)
85+
val hasWest = sides.hasDirection(WEST)
86+
val hasUp = sides.hasDirection(UP)
87+
val hasDown = sides.hasDirection(DOWN)
88+
val hasSouth = sides.hasDirection(SOUTH)
89+
val hasNorth = sides.hasDirection(NORTH)
90+
91+
if (outlineMode.check(hasUp, hasNorth)) putLine(tlb, trb)
92+
if (outlineMode.check(hasUp, hasSouth)) putLine(tlf, trf)
93+
if (outlineMode.check(hasUp, hasWest)) putLine(tlb, tlf)
94+
if (outlineMode.check(hasUp, hasEast)) putLine(trf, trb)
95+
96+
if (outlineMode.check(hasDown, hasNorth)) putLine(blb, brb)
97+
if (outlineMode.check(hasDown, hasSouth)) putLine(blf, brf)
98+
if (outlineMode.check(hasDown, hasWest)) putLine(blb, blf)
99+
if (outlineMode.check(hasDown, hasEast)) putLine(brb, brf)
100+
101+
if (outlineMode.check(hasWest, hasNorth)) putLine(tlb, blb)
102+
if (outlineMode.check(hasNorth, hasEast)) putLine(trb, brb)
103+
if (outlineMode.check(hasEast, hasSouth)) putLine(trf, brf)
104+
if (outlineMode.check(hasSouth, hasWest)) putLine(tlf, blf)
105+
}
106+
107+
fun render() {
108+
shader.use()
109+
shader["u_TickDelta"] = mc.partialTicks
110+
shader["u_CameraPosition"] = mc.gameRenderer.camera.pos
111+
112+
filled.render()
113+
outline.render()
114+
}
115+
116+
init {
117+
listener<TickEvent.Post> {
118+
filled.clear()
119+
outline.clear()
120+
121+
RenderEvent.EntityESP().post()
122+
123+
filled.upload()
124+
outline.upload()
125+
}
126+
}
127+
}

common/src/main/kotlin/com/lambda/module/modules/debug/RenderTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.lambda.module.modules.debug
22

3+
import com.lambda.event.events.RenderEvent
34
import com.lambda.event.events.TickEvent
45
import com.lambda.event.listener.SafeListener.Companion.listener
56
import com.lambda.graphics.renderer.esp.CachedEspRenderer
6-
import com.lambda.graphics.renderer.esp.EntityEspRegistry.renderEntityESP
77
import com.lambda.module.Module
88
import com.lambda.module.tag.ModuleTag
99
import com.lambda.util.math.ColorUtils.setAlpha
@@ -30,9 +30,9 @@ object RenderTest : Module(
3030
private val rendeer = CachedEspRenderer(this)
3131

3232
init {
33-
renderEntityESP(this) { renderer ->
34-
val entity = getClosestEntity<LivingEntity>(player.pos, 8.0) ?: return@renderEntityESP
35-
renderer.build(entity, filledColor, outlineColor)
33+
listener<RenderEvent.EntityESP> {
34+
val entity = getClosestEntity<LivingEntity>(player.pos, 8.0) ?: return@listener
35+
it.build(entity, filledColor, outlineColor)
3636
}
3737

3838
listener<TickEvent.Pre> {

0 commit comments

Comments
 (0)