Skip to content

Commit

Permalink
feat: ItemChams (#4902)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccetl authored Dec 15, 2024
1 parent c482931 commit 71b9c3a
Show file tree
Hide file tree
Showing 14 changed files with 516 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.ccbluex.liquidbounce.interfaces.LightmapTextureManagerAddition;
import net.ccbluex.liquidbounce.interfaces.PostEffectPassTextureAddition;
import net.ccbluex.liquidbounce.render.engine.UIRenderer;
import net.ccbluex.liquidbounce.render.shader.shaders.OutlineEffectShader;
import net.ccbluex.liquidbounce.utils.aiming.RaytracingExtensionsKt;
import net.ccbluex.liquidbounce.utils.aiming.Rotation;
import net.ccbluex.liquidbounce.utils.aiming.RotationManager;
Expand Down Expand Up @@ -145,6 +146,22 @@ public void hookWorldRender(RenderTickCounter tickCounter, CallbackInfo ci, @Loc
EventManager.INSTANCE.callEvent(new WorldRenderEvent(newMatStack, this.camera, tickCounter.getTickDelta(false)));
}

@Inject(method = "renderHand", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/LightmapTextureManager;enable()V", shift = At.Shift.AFTER))
public void prepareItemCharms(Camera camera, float tickDelta, Matrix4f matrix4f, CallbackInfo ci) {
if (ModuleItemChams.INSTANCE.getRunning()) {
ModuleItemChams.INSTANCE.setData();
OutlineEffectShader.INSTANCE.prepare();
}
}

@Inject(method = "renderHand", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/item/HeldItemRenderer;renderItem(FLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider$Immediate;Lnet/minecraft/client/network/ClientPlayerEntity;I)V", shift = At.Shift.AFTER))
public void drawItemCharms(Camera camera, float tickDelta, Matrix4f matrix4f, CallbackInfo ci) {
if (ModuleItemChams.INSTANCE.getActive()) {
ModuleItemChams.INSTANCE.setActive(false);
OutlineEffectShader.INSTANCE.apply();
}
}

/**
* Hook screen render event
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ object ModuleManager : EventListener, Iterable<ClientModule> by modules {
ModuleXRay,
ModuleDebug,
ModuleZoom,
ModuleItemChams,

// World
ModuleAutoBuild,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
*
* Copyright (c) 2015 - 2024 CCBlueX
*
* LiquidBounce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiquidBounce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
*/
package net.ccbluex.liquidbounce.features.module.modules.render

import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.injection.mixins.minecraft.render.MixinGameRenderer
import net.ccbluex.liquidbounce.render.engine.Color4b
import net.ccbluex.liquidbounce.render.shader.shaders.OutlineEffectShaderData

/**
* Module ItemChams
*
* Applies visual effects to your held items.
*
* [MixinGameRenderer]
*
* @author ccetl
*/
object ModuleItemChams : ClientModule("ItemChams", Category.RENDER) {

private val blendColor by color("BlendColor", Color4b(0, 64, 255, 186))
private val alpha by int("Alpha", 95, 1..255)
private val glowColor by color("GlowColor", Color4b(0, 64, 255, 15))
private val layers by int("Layers", 3, 1..10)
private val layerSize by float("LayerSize", 1.91f, 1f..5f)
private val falloff by float("Falloff", 6.83f, 0f..20f)

var active = false

fun setData() {
active = true
with(OutlineEffectShaderData) {
falloff = ModuleItemChams.falloff
sampleMul = layerSize
layerCount = layers
glowColor = ModuleItemChams.glowColor
blendColor = ModuleItemChams.blendColor
alpha = ModuleItemChams.alpha / 255f
}
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
*
* Copyright (c) 2015 - 2024 CCBlueX
*
* LiquidBounce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiquidBounce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
*/
package net.ccbluex.liquidbounce.features.module.modules.world.nuker.mode

import net.ccbluex.liquidbounce.config.types.Choice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import net.ccbluex.liquidbounce.integration.interop.ClientInteropServer
import net.ccbluex.liquidbounce.integration.theme.component.Component
import net.ccbluex.liquidbounce.integration.theme.component.ComponentOverlay
import net.ccbluex.liquidbounce.integration.theme.component.ComponentType
import net.ccbluex.liquidbounce.render.shader.Shader
import net.ccbluex.liquidbounce.render.shader.CanvasShader
import net.ccbluex.liquidbounce.utils.client.logger
import net.ccbluex.liquidbounce.utils.client.mc
import net.ccbluex.liquidbounce.utils.io.extractZip
Expand Down Expand Up @@ -202,7 +202,7 @@ class Theme(val name: String) {
get() = File(folder, "background.frag")
private val backgroundImage: File
get() = File(folder, "background.png")
var compiledShaderBackground: Shader? = null
var compiledShaderBackground: CanvasShader? = null
private set
var loadedBackgroundImage: Identifier? = null
private set
Expand All @@ -213,7 +213,7 @@ class Theme(val name: String) {
}

readShaderBackground()?.let { shaderBackground ->
compiledShaderBackground = Shader(resourceToString("/assets/liquidbounce/shaders/vertex.vert"),
compiledShaderBackground = CanvasShader(resourceToString("/assets/liquidbounce/shaders/vertex.vert"),
shaderBackground)
logger.info("Compiled background shader for theme $name")
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package net.ccbluex.liquidbounce.render.engine

import net.minecraft.util.math.Vec3d
import net.minecraft.util.math.Vec3i
import org.lwjgl.opengl.GL20
import java.awt.Color
import java.nio.ByteBuffer
import kotlin.math.cos
Expand Down Expand Up @@ -186,4 +187,8 @@ data class Color4b(val r: Int, val g: Int, val b: Int, val a: Int) {

private fun darkerChannel(value: Int) = (value * 0.7).toInt().coerceAtLeast(0)

fun putToUniform(pointer: Int) {
GL20.glUniform4f(pointer, r / 255f, g / 255f, b / 255f, a / 255f)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
*
* Copyright (c) 2015 - 2024 CCBlueX
*
* LiquidBounce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiquidBounce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
*/
package net.ccbluex.liquidbounce.render.shader

import net.ccbluex.liquidbounce.utils.client.mc
import net.minecraft.client.gl.GlUniform
import net.minecraft.client.gl.VertexBuffer
import net.minecraft.client.render.Tessellator
import net.minecraft.client.render.VertexFormat
import net.minecraft.client.render.VertexFormats
import org.lwjgl.opengl.GL30

/**
* A GLSL shader renderer. Takes a vertex and fragment shader and renders it to the canvas.
*
* Inspired from the GLSL Panorama Shader Mod
* https://github.com/magistermaks/mod-glsl
*/
class CanvasShader(vertex: String, fragment: String) : Shader(vertex, fragment) {

private var canvas = ScalableCanvas()
private var buffer = VertexBuffer(VertexBuffer.Usage.DYNAMIC)

private val timeLocation: Int
private val mouseLocation: Int
private val resolutionLocation: Int

private var time = 0f

init {
// bake buffer data
val builder = Tessellator.getInstance()
val buffer = builder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR)
buffer.vertex(-1.0f, -1.0f, 1.0f).texture(0f, 0f)
.color(1f, 1f, 1f, 1f)
buffer.vertex(1.0f, -1.0f, 1.0f).texture(1f, 0f)
.color(1f, 1f, 1f, 1f)
buffer.vertex(1.0f, 1.0f, 1.0f).texture(1f, 1f)
.color(1f, 1f, 1f, 1f)
buffer.vertex(-1.0f, 1.0f, 1.0f).texture(0f, 1f)
.color(1f, 1f, 1f, 1f)

this.buffer.bind()
this.buffer.upload(buffer.end())
VertexBuffer.unbind()

// get uniform pointers
timeLocation = GlUniform.getUniformLocation(program, "time")
mouseLocation = GlUniform.getUniformLocation(program, "mouse")
resolutionLocation = GlUniform.getUniformLocation(program, "resolution")
}

fun draw(mouseX: Int, mouseY: Int, delta: Float) {
super.use()

canvas.resize(mc.window.framebufferWidth, mc.window.framebufferHeight)
canvas.write()

// update uniforms
GL30.glUniform1f(timeLocation, time)
time += (delta / 10f)
GL30.glUniform2f(mouseLocation, mouseX.toFloat(), mouseY.toFloat())
GL30.glUniform2f(resolutionLocation, canvas.width().toFloat(), canvas.height().toFloat())

// draw
buffer.bind()
buffer.draw()
canvas.blit(buffer)
}

override fun close() {
super.close()
buffer.close()
canvas.close()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
*
* Copyright (c) 2015 - 2024 CCBlueX
*
* LiquidBounce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiquidBounce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
*/
package net.ccbluex.liquidbounce.render.shader

import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import net.ccbluex.liquidbounce.features.module.MinecraftShortcuts
import net.minecraft.client.gl.SimpleFramebuffer
import net.minecraft.client.gl.VertexBuffer
import net.minecraft.client.render.Tessellator
import net.minecraft.client.render.VertexFormat
import net.minecraft.client.render.VertexFormats
import org.lwjgl.opengl.GL11
import org.lwjgl.opengl.GL13
import java.io.Closeable

/**
* @author ccetl
*/
open class FramebufferShader(vararg val shaders: Shader) : MinecraftShortcuts, Closeable {

private val framebuffers = mutableListOf<SimpleFramebuffer>()
private var buffer = VertexBuffer(VertexBuffer.Usage.DYNAMIC)

init {
val width = mc.window.framebufferWidth
val height = mc.window.framebufferHeight
shaders.forEach { _ ->
val framebuffer = SimpleFramebuffer(width, height, false, false)
framebuffer.setClearColor(0f, 0f, 0f, 0f)
framebuffers.add(framebuffer)
}

val builder = Tessellator.getInstance()
val bufferBuilder = builder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE)
bufferBuilder.vertex(-1f, -1f, 0f).texture(0f, 0f)
bufferBuilder.vertex(1f, -1f, 0f).texture(1f, 0f)
bufferBuilder.vertex(1f, 1f, 0f).texture(1f, 1f)
bufferBuilder.vertex(-1f, 1f, 0f).texture(0f, 1f)
buffer.bind()
buffer.upload(bufferBuilder.end())
VertexBuffer.unbind()
}

fun prepare() {
val width = mc.window.framebufferWidth
val height = mc.window.framebufferHeight
framebuffers.forEach {
if (it.textureWidth != width || it.textureHeight != height) {
it.resize(width, height, false)
}
}

framebuffers[0].clear(false)
framebuffers[0].beginWrite(true)
}

fun apply() {
val active = GlStateManager._getActiveTexture()
val alphaTest = GL11.glIsEnabled(GL11.GL_ALPHA_TEST)

GL11.glDisable(GL11.GL_ALPHA_TEST)
GlStateManager._bindTexture(0)

RenderSystem.disableDepthTest()
RenderSystem.enableBlend()
RenderSystem.blendFunc(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA)

RenderSystem.setShaderColor(1f, 1f, 1f, 0f)
shaders.forEachIndexed { i, shader ->
val inputFramebuffer = framebuffers.getOrNull(i) ?: framebuffers.first()
val outputFramebuffer = framebuffers.getOrNull(i + 1)

outputFramebuffer?.clear(false)
outputFramebuffer?.beginWrite(true) ?: mc.framebuffer.beginWrite(false)

GlStateManager._activeTexture(GL13.GL_TEXTURE0 + i)
GlStateManager._bindTexture(inputFramebuffer.colorAttachment)

shader.use()

buffer.bind()
buffer.draw()
VertexBuffer.unbind()

shader.stop()
}

shaders.indices.forEach {
GlStateManager._activeTexture(GL13.GL_TEXTURE0 + it)
GlStateManager._bindTexture(0)
}

RenderSystem.enableDepthTest()
GlStateManager._activeTexture(active)
if (alphaTest) {
GL11.glEnable(GL11.GL_ALPHA_TEST)
}
}

fun render(drawAction: () -> Unit) {
prepare()
drawAction()
apply()
}

override fun close() {
shaders.forEach { it.close() }
buffer.close()
framebuffers.forEach { it.delete() }
}

}
Loading

0 comments on commit 71b9c3a

Please sign in to comment.