@@ -4,10 +4,10 @@ import gg.essential.elementa.effects.Effect
44import gg.essential.elementa.state.v2.State
55import gg.essential.universal.UGraphics
66import gg.essential.universal.UMatrixStack
7+ import gg.essential.universal.render.URenderPipeline
78import gg.essential.universal.shader.BlendState
8- import gg.essential.universal.shader.UShader
9+ import gg.essential.universal.vertex.UBufferBuilder
910import org.intellij.lang.annotations.Language
10- import org.lwjgl.opengl.GL11
1111import 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