Skip to content

Commit ac86037

Browse files
committed
Misc: Use BlendState.ALPHA across all components
1 parent b6711cd commit ac86037

File tree

12 files changed

+85
-10
lines changed

12 files changed

+85
-10
lines changed

api/Elementa.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ public final class gg/essential/elementa/ElementaVersion : java/lang/Enum {
22
public static final field Companion Lgg/essential/elementa/ElementaVersion$Companion;
33
public static final field V0 Lgg/essential/elementa/ElementaVersion;
44
public static final field V1 Lgg/essential/elementa/ElementaVersion;
5+
public static final field V10 Lgg/essential/elementa/ElementaVersion;
56
public static final field V2 Lgg/essential/elementa/ElementaVersion;
67
public static final field V3 Lgg/essential/elementa/ElementaVersion;
78
public static final field V4 Lgg/essential/elementa/ElementaVersion;

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
kotlin = "1.5.10"
33
kotlinx-coroutines = "1.5.2"
44
jetbrains-annotations = "23.0.0"
5-
universalcraft = "389"
5+
universalcraft = "406"
66
commonmark = "0.17.1"
77
dom4j = "2.1.1"
88

src/main/kotlin/gg/essential/elementa/ElementaVersion.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import gg.essential.elementa.constraints.SuperConstraint
88
import gg.essential.elementa.constraints.animation.AnimationComponent
99
import gg.essential.elementa.effects.Effect
1010
import gg.essential.universal.render.URenderPipeline
11+
import gg.essential.universal.shader.BlendState
1112

1213
/**
1314
* Sometimes it is necessary or desirable to introduce breaking behavioral changes to Elementa. In order to maintain
@@ -180,8 +181,16 @@ enum class ElementaVersion {
180181
* All Minecraft versions now use [URenderPipeline] instead of modifying global GL state.
181182
* Additionally, [UIText] and [UIWrappedText] no longer enable (and forget to disable) blending.
182183
*/
184+
@Deprecated(DEPRECATION_MESSAGE)
183185
V9,
184186

187+
/**
188+
* All components now use [BlendState.ALPHA] instead of [BlendState.NORMAL] and variants.
189+
* This fixes the alpha channel of the render result, allowing it to be correctly composited with other textures.
190+
* See [UniversalCraft#105](https://github.com/EssentialGG/UniversalCraft/pull/105) for more details.
191+
*/
192+
V10,
193+
185194
;
186195

187196
/**
@@ -230,10 +239,14 @@ Be sure to read through all the changes between your current version and your ne
230239
internal val v7 = V7
231240
@Suppress("DEPRECATION")
232241
internal val v8 = V8
242+
@Suppress("DEPRECATION")
233243
internal val v9 = V9
244+
internal val v10 = V10
234245

235246
internal val atLeastV9Active: Boolean
236247
get() = active >= v9
248+
internal val atLeastV10Active: Boolean
249+
get() = active >= v10
237250

238251
@PublishedApi
239252
internal var active: ElementaVersion = v0

src/main/kotlin/gg/essential/elementa/components/GradientComponent.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ open class GradientComponent constructor(
139139
bufferBuilder.pos(matrixStack, x1, y1, 0.0).color(colors.topLeft).endVertex()
140140
bufferBuilder.pos(matrixStack, x1, y2, 0.0).color(colors.bottomLeft).endVertex()
141141
bufferBuilder.pos(matrixStack, x2, y2, 0.0).color(colors.bottomRight).endVertex()
142-
bufferBuilder.build()?.drawAndClose(PIPELINE)
142+
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE)
143143
}
144144

145145
@Deprecated("Stops working in 1.21.5")
@@ -169,7 +169,12 @@ open class GradientComponent constructor(
169169
}
170170

171171
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:gradient_block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
172+
@Suppress("DEPRECATION")
172173
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
173174
}.build()
175+
176+
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:gradient_block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
177+
blendState = BlendState.ALPHA
178+
}.build()
174179
}
175180
}

src/main/kotlin/gg/essential/elementa/components/UIBlock.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ open class UIBlock(colorConstraint: ColorConstraint = Color.WHITE.toConstraint()
6464

6565
val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
6666
drawBlock(bufferBuilder, matrixStack, color, x1, y1, x2, y2)
67-
bufferBuilder.build()?.drawAndClose(PIPELINE)
67+
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE)
6868
}
6969

7070
@Deprecated("Stops working in 1.21.5, see UGraphics.Globals")
@@ -133,6 +133,7 @@ open class UIBlock(colorConstraint: ColorConstraint = Color.WHITE.toConstraint()
133133
}
134134

135135
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
136+
@Suppress("DEPRECATION")
136137
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
137138
// At some point MC started enabling its depth test during font rendering but all GUI code is
138139
// essentially flat and has depth tests disabled. This can cause stuff rendered in the background of the
@@ -141,5 +142,15 @@ open class UIBlock(colorConstraint: ColorConstraint = Color.WHITE.toConstraint()
141142
// To work around this, we'll write depth buffer unconditionally in the area where we draw the block.
142143
depthTest = URenderPipeline.DepthTest.Always
143144
}.build()
145+
146+
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
147+
blendState = BlendState.ALPHA
148+
// At some point MC started enabling its depth test during font rendering but all GUI code is
149+
// essentially flat and has depth tests disabled. This can cause stuff rendered in the background of the
150+
// GUI to interfere with text rendered in the foreground because none of the blocks rendered in between
151+
// will actually write to the depth buffer.
152+
// To work around this, we'll write depth buffer unconditionally in the area where we draw the block.
153+
depthTest = URenderPipeline.DepthTest.Always
154+
}.build()
144155
}
145156
}

src/main/kotlin/gg/essential/elementa/components/UICircle.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,22 @@ class UICircle @JvmOverloads constructor(radius: Float = 0f, color: Color = Colo
7878
readElementaShaderSource("rect", "vsh"),
7979
readElementaShaderSource("circle", "fsh"),
8080
).apply {
81+
@Suppress("DEPRECATION")
8182
blendState = BlendState.NORMAL
8283
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
8384
}.build()
8485

86+
private val PIPELINE2 = URenderPipeline.builderWithLegacyShader(
87+
"elementa:circle",
88+
UGraphics.DrawMode.QUADS,
89+
UGraphics.CommonVertexFormats.POSITION_COLOR,
90+
readElementaShaderSource("rect", "vsh"),
91+
readElementaShaderSource("circle", "fsh"),
92+
).apply {
93+
blendState = BlendState.ALPHA
94+
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
95+
}.build()
96+
8597
fun initShaders() {
8698
if (URenderPipeline.isRequired) return
8799
if (::shader.isInitialized)
@@ -120,7 +132,7 @@ class UICircle @JvmOverloads constructor(radius: Float = 0f, color: Color = Colo
120132
(centerX + radius).toDouble(),
121133
(centerY + radius).toDouble()
122134
)
123-
bufferBuilder.build()?.drawAndClose(PIPELINE) {
135+
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
124136
uniform("u_Radius", radius)
125137
uniform("u_CenterPos", centerX, centerY)
126138
}

src/main/kotlin/gg/essential/elementa/components/UIRoundedRectangle.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,22 @@ open class UIRoundedRectangle(radius: Float) : UIComponent() {
4949
readElementaShaderSource("rect", "vsh"),
5050
readElementaShaderSource("rounded_rect", "fsh"),
5151
).apply {
52+
@Suppress("DEPRECATION")
5253
blendState = BlendState.NORMAL
5354
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
5455
}.build()
5556

57+
private val PIPELINE2 = URenderPipeline.builderWithLegacyShader(
58+
"elementa:rounded_rectangle",
59+
UGraphics.DrawMode.QUADS,
60+
UGraphics.CommonVertexFormats.POSITION_COLOR,
61+
readElementaShaderSource("rect", "vsh"),
62+
readElementaShaderSource("rounded_rect", "fsh"),
63+
).apply {
64+
blendState = BlendState.ALPHA
65+
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
66+
}.build()
67+
5668
fun initShaders() {
5769
if (URenderPipeline.isRequired) return
5870
if (::shader.isInitialized)
@@ -86,7 +98,7 @@ open class UIRoundedRectangle(radius: Float) : UIComponent() {
8698

8799
val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
88100
UIBlock.drawBlock(bufferBuilder, matrixStack, color, left.toDouble(), top.toDouble(), right.toDouble(), bottom.toDouble())
89-
bufferBuilder.build()?.drawAndClose(PIPELINE) {
101+
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
90102
uniform("u_Radius", radius)
91103
uniform("u_InnerRect", left + radius, top + radius, right - radius, bottom - radius)
92104
}

src/main/kotlin/gg/essential/elementa/components/UIShape.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ open class UIShape @JvmOverloads constructor(color: Color = Color.WHITE) : UICom
6363
.color(color.red, color.green, color.blue, color.alpha)
6464
.endVertex()
6565
}
66-
bufferBuilder.build()?.drawAndClose(PIPELINE)
66+
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE)
6767
}
6868

6969
@Deprecated("Stops working in 1.21.5, see UGraphics.Globals")
@@ -98,7 +98,11 @@ open class UIShape @JvmOverloads constructor(color: Color = Color.WHITE) : UICom
9898

9999
private companion object {
100100
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:shape", UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
101+
@Suppress("DEPRECATION")
101102
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
102103
}.build()
104+
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:shape", UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
105+
blendState = BlendState.ALPHA
106+
}.build()
103107
}
104108
}

src/main/kotlin/gg/essential/elementa/components/inspector/Inspector.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ class Inspector @JvmOverloads constructor(
382382
UGraphics.DrawMode.QUADS,
383383
UGraphics.CommonVertexFormats.POSITION_COLOR,
384384
).apply {
385-
blendState = BlendState.NORMAL
385+
blendState = BlendState.ALPHA
386386
depthTest = URenderPipeline.DepthTest.Less
387387
}.build()
388388
}

src/main/kotlin/gg/essential/elementa/font/BasicFontRenderer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class BasicFontRenderer(
152152
if (URenderPipeline.isRequired || ElementaVersion.atLeastV9Active) {
153153
val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE_COLOR)
154154
drawStringNow(bufferBuilder, matrixStack, string, color, x, y, originalPointSize)
155-
bufferBuilder.build()?.drawAndClose(PIPELINE) {
155+
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
156156
texture(0, regularFont.getTexture().dynamicGlId)
157157
}
158158
} else {
@@ -275,7 +275,11 @@ class BasicFontRenderer(
275275

276276
private companion object {
277277
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:basic_font", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
278+
@Suppress("DEPRECATION")
278279
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
279280
}.build()
281+
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:basic_font", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
282+
blendState = BlendState.ALPHA
283+
}.build()
280284
}
281285
}

0 commit comments

Comments
 (0)