SnowyGUI/common/src/main/kotlin/fr/username404/snowygui/utils/RenderingUtil.kt
Username404-59 dc286683a5
Switch to architectury-loom 1.7, fix the port to 1.21
Signed-off-by: Username404-59 <w.iron.zombie@gmail.com>
2025-02-02 18:43:40 +01:00

44 lines
1.6 KiB
Kotlin

package fr.username404.snowygui.utils
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.BufferUploader
import com.mojang.blaze3d.vertex.DefaultVertexFormat
import com.mojang.blaze3d.vertex.Tesselator
import com.mojang.blaze3d.vertex.VertexConsumer
import com.mojang.blaze3d.vertex.VertexFormat
import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.gui.hextoRGB
import net.minecraft.client.renderer.GameRenderer
object RenderingUtil {
@JvmField val tessellator: Tesselator = Tesselator.getInstance()
@JvmStatic
fun VertexConsumer.colorIt(color: Int, opacity: Float = 1F): VertexConsumer = hextoRGB(color).run {
setColor(get(0), get(1), get(2), opacity)
}
fun colorShader() {
RenderSystem.setShader(GameRenderer::getPositionColorShader)
RenderSystem.setShaderColor(1F, 1F, 1F, 1F)
}
fun prepareDraw() {
colorShader()
RenderSystem.enableBlend()
RenderSystem.defaultBlendFunc()
}
fun endDraw() {
RenderSystem.disableBlend()
}
fun drawRectangle(
x: Double, y: Double, height: Int, width: Int,
color: Int = Colors.TRANSPARENT(), opacity: Float = 1F
): Unit = tessellator.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR).run {
fun VertexConsumer.colorIt() = colorIt(color, opacity)
val x = x.toFloat() ; val y = y.toFloat()
addVertex(x, y + height, 0.0F).colorIt()
addVertex(x + width, y + height, 0.0F).colorIt()
addVertex(x + width, y, 0.0F).colorIt()
addVertex(x, y, 0.0F).colorIt()
BufferUploader.drawWithShader(buildOrThrow())
}
}