Skip to content

Commit 864068a

Browse files
committed
GradientEffect: Port to Minecraft 1.21.5
Source-Commit: f7dc1166a18a19c8f8ac63f7ba7136f594974e6d
1 parent d62e660 commit 864068a

File tree

1 file changed

+23
-43
lines changed

1 file changed

+23
-43
lines changed

unstable/layoutdsl/src/main/kotlin/gg/essential/elementa/effects/GradientEffect.kt

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import gg.essential.elementa.effects.Effect
44
import gg.essential.elementa.state.v2.State
55
import gg.essential.universal.UGraphics
66
import gg.essential.universal.UMatrixStack
7+
import gg.essential.universal.render.URenderPipeline
78
import gg.essential.universal.shader.BlendState
8-
import gg.essential.universal.shader.UShader
9+
import gg.essential.universal.vertex.UBufferBuilder
910
import org.intellij.lang.annotations.Language
10-
import org.lwjgl.opengl.GL11
1111
import java.awt.Color
1212

1313
/**
@@ -30,22 +30,9 @@ class GradientEffect(
3030
val bottomLeft = this.bottomLeft.get()
3131
val bottomRight = this.bottomRight.get()
3232

33-
lateinit var prevBlendState: BlendState
34-
3533
val dither = topLeft != topRight || topLeft != bottomLeft || bottomLeft != bottomRight
36-
if (dither) {
37-
shader.bind()
38-
} else {
39-
prevBlendState = BlendState.active()
40-
BlendState.NORMAL.activate()
41-
}
4234

43-
val buffer = UGraphics.getFromTessellator()
44-
if (dither) {
45-
buffer.beginWithActiveShader(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
46-
} else {
47-
buffer.beginWithDefaultShader(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
48-
}
35+
val buffer = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
4936

5037
val x1 = boundComponent.getLeft().toDouble()
5138
val x2 = boundComponent.getRight().toDouble()
@@ -57,30 +44,7 @@ class GradientEffect(
5744
buffer.pos(matrixStack, x1, y2, 0.0).color(bottomLeft).endVertex()
5845
buffer.pos(matrixStack, x2, y2, 0.0).color(bottomRight).endVertex()
5946

60-
var prevAlphaTestFunc = 0
61-
var prevAlphaTestRef = 0f
62-
if (!UGraphics.isCoreProfile()) {
63-
prevAlphaTestFunc = GL11.glGetInteger(GL11.GL_ALPHA_TEST_FUNC)
64-
prevAlphaTestRef = GL11.glGetFloat(GL11.GL_ALPHA_TEST_REF)
65-
UGraphics.alphaFunc(GL11.GL_ALWAYS, 0f)
66-
}
67-
68-
// See UIBlock.drawBlock for why we use this depth function
69-
UGraphics.enableDepth()
70-
UGraphics.depthFunc(GL11.GL_ALWAYS)
71-
buffer.drawDirect()
72-
UGraphics.disableDepth()
73-
UGraphics.depthFunc(GL11.GL_LEQUAL)
74-
75-
if (!UGraphics.isCoreProfile()) {
76-
UGraphics.alphaFunc(prevAlphaTestFunc, prevAlphaTestRef)
77-
}
78-
79-
if (dither) {
80-
shader.unbind()
81-
} else {
82-
prevBlendState.activate()
83-
}
47+
buffer.build()?.drawAndClose(if (dither) PIPELINE_DITHERED else PIPELINE_FLAT)
8448
}
8549

8650
companion object {
@@ -110,8 +74,24 @@ class GradientEffect(
11074
}
11175
""".trimIndent()
11276

113-
private val shader: UShader by lazy {
114-
UShader.fromLegacyShader(vertSource, fragSource, BlendState.NORMAL, UGraphics.CommonVertexFormats.POSITION_COLOR)
115-
}
77+
private val PIPELINE_DITHERED = URenderPipeline.builderWithLegacyShader(
78+
"elementa:gradient_effect/dithered",
79+
UGraphics.DrawMode.QUADS,
80+
UGraphics.CommonVertexFormats.POSITION_COLOR,
81+
vertSource,
82+
fragSource,
83+
).apply {
84+
blendState = BlendState.NORMAL
85+
depthTest = URenderPipeline.DepthTest.Always
86+
}.build()
87+
88+
private val PIPELINE_FLAT = URenderPipeline.builderWithDefaultShader(
89+
"elementa:gradient_effect/flat",
90+
UGraphics.DrawMode.QUADS,
91+
UGraphics.CommonVertexFormats.POSITION_COLOR,
92+
).apply {
93+
blendState = BlendState.NORMAL
94+
depthTest = URenderPipeline.DepthTest.Always
95+
}.build()
11696
}
11797
}

0 commit comments

Comments
 (0)