SnowyGUI/common/src/main/kotlin/fr/username404/snowygui/utils/RenderingUtil.kt
Username404-59 92c8e36c71
Update to 1.21.5
Signed-off-by: Username404-59 <w.iron.zombie@gmail.com>
2025-04-16 02:46:59 +02:00

82 lines
3.4 KiB
Kotlin

package fr.username404.snowygui.utils
import com.mojang.blaze3d.buffers.BufferType
import com.mojang.blaze3d.buffers.BufferUsage
import com.mojang.blaze3d.opengl.GlStateManager
import com.mojang.blaze3d.pipeline.RenderPipeline
import com.mojang.blaze3d.pipeline.RenderTarget
import com.mojang.blaze3d.systems.RenderPass
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.BufferBuilder
import com.mojang.blaze3d.vertex.Tesselator
import com.mojang.blaze3d.vertex.VertexConsumer
import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.gui.hextoRGB
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.RenderPipelines
import java.util.OptionalInt
import java.util.OptionalDouble
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.setShaderColor(1F, 1F, 1F, 1F)
}
fun prepareDraw() {
colorShader()
GlStateManager._enableBlend()
}
fun endDraw() {
GlStateManager._disableBlend()
}
fun drawRectangle(
x: Double, y: Double, height: Int, width: Int,
color: Int = Colors.TRANSPARENT(), opacity: Float = 1F
): Unit = renderBufferWithPipeline(renderPipeline = RenderPipelines.DEBUG_QUADS) { buffer -> buffer.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()
} }
fun renderBufferWithPipeline(
name: String? = "Dynamic vertex buffer",
renderPipeline: RenderPipeline,
renderTarget: RenderTarget = Minecraft.getInstance().mainRenderTarget,
uniformAndSamplerConsumer: ((RenderPass) -> Unit)? = null,
bufferBuilderConsumer: (BufferBuilder) -> Unit,
) {
val mode = renderPipeline.vertexFormatMode
val builder = Tesselator.getInstance().begin(mode, renderPipeline.vertexFormat)
bufferBuilderConsumer(builder)
builder.buildOrThrow().use { meshData ->
RenderSystem.getDevice().createCommandEncoder().createRenderPass(
renderTarget.colorTexture!!,
OptionalInt.empty(),
renderTarget.depthTexture,
OptionalDouble.empty()
).use { renderPass ->
RenderSystem.getDevice().createBuffer(
{ name }, BufferType.VERTICES, BufferUsage.DYNAMIC_WRITE, meshData.vertexBuffer()
).use { buffer ->
val autoStorageIndexBuffer = RenderSystem.getSequentialBuffer(mode)
renderPass.setPipeline(renderPipeline)
renderPass.setVertexBuffer(0, buffer)
renderPass.setIndexBuffer(
autoStorageIndexBuffer.getBuffer(meshData.drawState().indexCount()),
autoStorageIndexBuffer.type()
)
uniformAndSamplerConsumer?.invoke(renderPass)
renderPass.drawIndexed(0, meshData.drawState().indexCount())
}
}
}
}
}