Skip to content

Commit 08f880a

Browse files
committed
Laying particles
1 parent 391adc8 commit 08f880a

File tree

4 files changed

+96
-60
lines changed

4 files changed

+96
-60
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import com.lambda.event.EventFlow.post
55
import com.lambda.event.events.RenderEvent
66
import com.lambda.graphics.gl.GlStateUtils.setupGL
77
import com.lambda.graphics.gl.Matrices
8-
import com.lambda.graphics.gl.Matrices.resetMatrix
9-
import com.lambda.graphics.gl.Matrices.translate
8+
import com.lambda.graphics.gl.Matrices.resetMatrices
109
import com.lambda.graphics.renderer.esp.global.StaticESP
1110
import com.lambda.graphics.renderer.esp.global.DynamicESP
1211
import com.lambda.module.modules.client.GuiSettings
@@ -22,8 +21,7 @@ object RenderMain {
2221

2322
@JvmStatic
2423
fun render2D() {
25-
resetMatrix()
26-
translate(0.0, 0.0, -3000.0)
24+
resetMatrices(Matrix4f().translate(0f, 0f, -3000f))
2725

2826
setupGL {
2927
rescale(1.0)
@@ -36,7 +34,7 @@ object RenderMain {
3634

3735
@JvmStatic
3836
fun render3D(matrix: Matrix4f) {
39-
resetMatrix(matrix)
37+
resetMatrices(matrix)
4038
projectionMatrix.set(getProjectionMatrix())
4139

4240
setupGL {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.lambda.graphics.gl
22

33
import com.lambda.Lambda.mc
4+
import net.minecraft.util.math.RotationAxis
45
import net.minecraft.util.math.Vec3d
5-
import org.joml.Matrix4d
6-
import org.joml.Matrix4f
7-
import org.joml.Quaternionf
6+
import org.joml.*
87

98
object Matrices {
109
private val stack = ArrayDeque<Matrix4f>(1)
@@ -52,7 +51,7 @@ object Matrices {
5251

5352
fun peek() = stack.last()
5453

55-
fun resetMatrix(entry: Matrix4f = Matrix4f()) {
54+
fun resetMatrices(entry: Matrix4f) {
5655
stack.clear()
5756
stack.add(entry)
5857
}
@@ -63,11 +62,21 @@ object Matrices {
6362
vertexTransformer = null
6463
}
6564

66-
fun buildWorldProjection(pos: Vec3d, scale: Double = 1.0) = Matrix4f().apply {
65+
fun buildWorldProjection(pos: Vec3d, scale: Double = 1.0, mode: ProjRotationMode = ProjRotationMode.TO_CAMERA) = Matrix4f().apply {
6766
val s = 0.025f * scale.toFloat()
6867

68+
val rotation = when(mode) {
69+
ProjRotationMode.TO_CAMERA -> mc.gameRenderer.camera.rotation
70+
ProjRotationMode.UP -> RotationAxis.POSITIVE_X.rotationDegrees(90f)
71+
}
72+
6973
translate(pos.x.toFloat(), pos.y.toFloat(), pos.z.toFloat())
70-
rotate(mc.gameRenderer.camera.rotation)
74+
rotate(rotation)
7175
scale(-s, -s, s)
7276
}
77+
78+
enum class ProjRotationMode {
79+
TO_CAMERA,
80+
UP
81+
}
7382
}

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

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.lambda.module.modules.render
22

3+
import com.lambda.Lambda.mc
4+
import com.lambda.context.SafeContext
35
import com.lambda.event.events.AttackEvent
46
import com.lambda.event.events.MovementEvent
57
import com.lambda.event.events.RenderEvent
@@ -10,22 +12,25 @@ import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
1012
import com.lambda.graphics.buffer.vao.vertex.VertexMode
1113
import com.lambda.graphics.gl.GlStateUtils.withBlendFunc
1214
import com.lambda.graphics.gl.GlStateUtils.withDepth
15+
import com.lambda.graphics.gl.Matrices
1316
import com.lambda.graphics.gl.Matrices.buildWorldProjection
1417
import com.lambda.graphics.gl.Matrices.withVertexTransform
1518
import com.lambda.graphics.shader.Shader
19+
import com.lambda.interaction.rotation.Rotation
1620
import com.lambda.module.Module
1721
import com.lambda.module.modules.client.GuiSettings
1822
import com.lambda.module.modules.client.GuiSettings.colorSpeed
1923
import com.lambda.module.tag.ModuleTag
20-
import com.lambda.threading.runSafe
2124
import com.lambda.util.math.ColorUtils.multAlpha
2225
import com.lambda.util.math.MathUtils.lerp
2326
import com.lambda.util.math.MathUtils.random
27+
import com.lambda.util.math.VecUtils
28+
import com.lambda.util.math.VecUtils.plus
29+
import com.lambda.util.math.VecUtils.times
2430
import com.lambda.util.math.transform
2531
import com.lambda.util.player.MovementUtils.moveDelta
26-
import com.lambda.util.player.MovementUtils.movementVector
27-
import com.lambda.util.player.MovementUtils.randomDirection
2832
import com.lambda.util.primitives.extension.partialTicks
33+
import com.lambda.util.world.raycast.RayCastMask
2934
import net.minecraft.entity.Entity
3035
import net.minecraft.util.math.Vec3d
3136

@@ -38,26 +43,33 @@ object Particles : Module(
3843
description = "Spawns fancy particles",
3944
defaultTags = setOf(ModuleTag.RENDER)
4045
) {
46+
// ToDo: resort, cleanup settings
4147
private val duration by setting("Duration", 5.0, 1.0..500.0, 1.0)
4248
private val fadeDuration by setting("Fade Ticks", 5.0, 1.0..30.0, 1.0)
43-
private val spawnAmount by setting("Spawn Amount", 30.0, 3.0..500.0, 1.0)
44-
private val size by setting("Size", 2.0, 0.1..50.0, 0.1)
49+
private val spawnAmount by setting("Spawn Amount", 20, 3..500, 1)
50+
private val sizeSetting by setting("Size", 2.0, 0.1..50.0, 0.1)
4551
private val alphaSetting by setting("Alpha", 1.5, 0.01..2.0, 0.01)
4652
private val speedH by setting("Speed H", 1.0, 0.0..10.0, 0.1)
4753
private val speedV by setting("Speed V", 1.0, 0.0..10.0, 0.1)
4854
private val inertia by setting("Inertia", 0.0, 0.0..1.0, 0.01)
4955
private val gravity by setting("Gravity", 0.2, 0.0..1.0, 0.01)
56+
private val onMove by setting("On Move", false)
5057

51-
private val onMove by setting("On Move", true)
58+
private val environment by setting("Environment", true)
59+
private val environmentSpawnAmount by setting("E Spawn Amount", 10, 3..100, 1) { environment }
60+
private val environmentSize by setting("E Size", 2.0, 0.1..50.0, 0.1) { environment }
61+
private val environmentRange by setting("E Spread", 5.0, 1.0..20.0, 0.1) { environment }
62+
private val environmentSpeedH by setting("E Speed H", 0.0, 0.0..10.0, 0.1) { environment }
63+
private val environmentSpeedV by setting("E Speed V", 0.1, 0.0..10.0, 0.1) { environment }
5264

5365
private var particles = mutableListOf<Particle>()
5466
private val vao = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.PARTICLE)
5567
private val shader = Shader("renderer/particle", "renderer/particle")
5668

5769
init {
5870
listener<TickEvent.Pre> {
59-
particles.forEach(Particle::tick)
60-
particles.removeIf(Particle::shouldRemove)
71+
if (environment) spawnForEnvironment()
72+
particles.removeIf(Particle::update)
6173
}
6274

6375
listener<RenderEvent.World> {
@@ -85,77 +97,91 @@ object Particles : Module(
8597
}
8698

8799
private fun spawnForEntity(entity: Entity) {
88-
repeat(spawnAmount.toInt()) {
89-
val i = (it + 1) / spawnAmount
100+
repeat(spawnAmount) {
101+
val i = (it + 1) / spawnAmount.toDouble()
90102

91103
val pos = entity.pos
92104
val height = entity.boundingBox.lengthY
93105
val spawnHeight = height * transform(i, 0.0, 1.0, 0.2, 0.8)
94106
val particlePos = pos.add(0.0, spawnHeight, 0.0)
107+
val particleMotion = Rotation(
108+
random(-180.0, 180.0),
109+
random(-90.0, 90.0)
110+
).vector * Vec3d(speedH, speedV, speedH) * 0.1
95111

96-
particles.add(Particle(particlePos))
112+
particles += Particle(particlePos, particleMotion, false)
97113
}
98114
}
99115

100-
private class Particle(posIn: Vec3d) {
101-
var initTick = 0
102-
var maxAge = 0
103-
var fadeTicks = fadeDuration
116+
private fun SafeContext.spawnForEnvironment() {
117+
if (mc.paused) return
118+
repeat(environmentSpawnAmount) {
119+
var particlePos = player.pos + Rotation(random(-180.0, 180.0), 0.0).vector * random(0.0, environmentRange)
104120

105-
var shouldRemove = false
121+
Rotation.DOWN.rayCast(6.0, particlePos + VecUtils.UP * 2.0, true, RayCastMask.BLOCK)?.pos?.let {
122+
particlePos = it + VecUtils.UP * 0.03
123+
} ?: return@repeat
106124

107-
var prevPos = posIn
108-
var pos = posIn
125+
val particleMotion = Rotation(
126+
random(-180.0, 180.0),
127+
random(-90.0, 90.0)
128+
).vector * Vec3d(environmentSpeedH, environmentSpeedV, environmentSpeedH) * 0.1
109129

110-
var motion: Vec3d = Vec3d.ZERO
130+
particles += Particle(particlePos, particleMotion, true)
131+
}
132+
}
111133

112-
init {
113-
runSafe {
114-
initTick = player.age
115-
maxAge = (duration + random(0.0, 20.0)).toInt()
134+
private class Particle(
135+
initialPosition: Vec3d,
136+
initialMotion: Vec3d,
137+
val lay: Boolean
138+
) {
139+
private val fadeTicks = fadeDuration
116140

141+
private var age = 0
142+
private val maxAge = (duration + random(0.0, 20.0)).toInt()
117143

118-
motion = movementVector(randomDirection(), sin(random(0.0, Math.PI)))
119-
.multiply(random(0.0, 1.0), random(0.0, 1.0), random(0.0, 1.0))
120-
.multiply(
121-
speedH * random(0.9, 1.1),
122-
speedV * random(0.9, 1.1),
123-
speedH * random(0.9, 1.1)
124-
).multiply(0.1)
125-
}
126-
}
144+
private var prevPos = initialPosition
145+
private var position = initialPosition
146+
private var motion = initialMotion
127147

128-
fun tick() = runSafe {
129-
prevPos = pos
130-
pos = pos.add(motion)
148+
private val projRotation = if (lay) Matrices.ProjRotationMode.UP else Matrices.ProjRotationMode.TO_CAMERA
131149

132-
motion = motion
133-
.subtract(0.0, gravity * 0.01, 0.0)
134-
.multiply(0.9 + inertia * 0.1)
150+
fun update(): Boolean {
151+
if (mc.paused) return false
152+
age++
135153

136-
shouldRemove = player.age - initTick > 5.0 + maxAge + fadeTicks * 2
154+
prevPos = position
155+
156+
if (!lay) motion += VecUtils.DOWN * gravity * 0.01
157+
motion *= 0.9 + inertia * 0.1
158+
159+
position += motion
160+
161+
return age > maxAge + fadeTicks * 2 + 5
137162
}
138163

139-
fun build() = runSafe {
140-
val age = player.age - initTick + mc.partialTicks
141-
val colorTicks = age * 0.1 / colorSpeed
164+
fun build() {
165+
val smoothAge = age + mc.partialTicks
166+
val colorTicks = smoothAge * 0.1 / colorSpeed
142167

143168
val alpha = when {
144-
age < fadeTicks -> age / fadeTicks
145-
age in fadeTicks..fadeTicks + maxAge -> 1.0
169+
smoothAge < fadeTicks -> smoothAge / fadeTicks
170+
smoothAge in fadeTicks..fadeTicks + maxAge -> 1.0
146171
else -> {
147172
val min = fadeTicks + maxAge
148173
val max = fadeTicks * 2 + maxAge
149-
transform(age, min, max, 1.0, 0.0)
174+
transform(smoothAge, min, max, 1.0, 0.0)
150175
}
151-
} * alphaSetting
176+
}
152177

153178
val (c1, c2) = GuiSettings.primaryColor to GuiSettings.secondaryColor
154-
val color = lerp(c1, c2, sin(colorTicks) * 0.5 + 0.5).multAlpha(alpha)
179+
val color = lerp(c1, c2, sin(colorTicks) * 0.5 + 0.5).multAlpha(alpha * alphaSetting)
155180

156-
val position = lerp(prevPos, pos, mc.partialTicks)
181+
val position = lerp(prevPos, position, mc.partialTicks)
182+
val size = if (lay) environmentSize else sizeSetting * lerp(0.5, 1.0, alpha)
157183

158-
withVertexTransform(buildWorldProjection(position, size)) {
184+
withVertexTransform(buildWorldProjection(position, size, projRotation)) {
159185
vao.use {
160186
grow(4) // DO NOT FUCKING FORGOTEOIJTOWKET TO GROW (cost me an hour)
161187
putQuad(

common/src/main/kotlin/com/lambda/util/math/VecUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ object VecUtils {
3232
infix operator fun Vec3d.times(other: Double): Vec3d = this.multiply(other)
3333

3434
infix operator fun Vec3d.div(other: Double): Vec3d = this.multiply(1.0 / other)
35+
36+
val UP = Vec3d(0.0, 1.0, 0.0)
37+
val DOWN = Vec3d(0.0, -1.0, 0.0)
3538
}

0 commit comments

Comments
 (0)