From f4401e8f79a822ad5a256827c05c0a3f98c1f75b Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Sun, 26 Nov 2023 13:08:28 +0100 Subject: [PATCH] Tried rounded rectangles with shaders --- .../misc/customscoreboard/CustomScoreboard.kt | 11 ++++-- .../RoundedRectangleShader.kt | 38 +++++++++++++++++++ .../skyhanni/utils/shader/ShaderHelper.kt | 18 +++++++++ .../skyhanni/utils/shader/Uniform.kt | 10 +++++ .../skyhanni/shaders/rounded_rectangle.fsh | 37 +++++++++--------- .../skyhanni/shaders/rounded_rectangle.vsh | 12 ++++++ 6 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 src/main/resources/assets/skyhanni/shaders/rounded_rectangle.vsh diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/CustomScoreboard.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/CustomScoreboard.kt index ba2dc49b5e52..4eb459f56ac5 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/CustomScoreboard.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/CustomScoreboard.kt @@ -61,7 +61,7 @@ class CustomScoreboard { @SubscribeEvent fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) { if (!isCustomScoreboardEnabled()) return - if (display.isEmpty()) return + val display = display ?: return val position = config.position val border = 5 @@ -88,8 +88,13 @@ class CustomScoreboard { position.isCenter ) ) - /*if (config.backgroundConfig.enabled) { + ShaderManager.enableShader("rounded_rectangle") + } else { + ShaderManager.disableShader() + }*/ + + if (config.backgroundConfig.enabled) { Gui.drawRect( x - border, y - border, @@ -97,7 +102,7 @@ class CustomScoreboard { y + elementHeight + border * 2, SpecialColour.specialToChromaRGB(config.backgroundConfig.color) ) - }*/ + } position.renderStrings(display, posLabel = "Custom Scoreboard") } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/RoundedRectangleShader.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/RoundedRectangleShader.kt index 9ffaa887992c..b7e77716a688 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/RoundedRectangleShader.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/RoundedRectangleShader.kt @@ -1,6 +1,16 @@ package at.hannibal2.skyhanni.features.misc.customscoreboard +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getDummySize +import at.hannibal2.skyhanni.utils.SpecialColour import at.hannibal2.skyhanni.utils.shader.Shader +import at.hannibal2.skyhanni.utils.shader.Uniform +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.ScaledResolution +/* + DOES NOT WORK + */ +private val config get() = SkyHanniMod.feature.gui.customScoreboard object RoundedRectangleShader : Shader("rounded_rectangle", "rounded_rectangle") { @@ -8,6 +18,34 @@ object RoundedRectangleShader : Shader("rounded_rectangle", "rounded_rectangle") get() = this override fun registerUniforms() { + val size = config.position.getDummySize() + val screenWidth = ScaledResolution(Minecraft.getMinecraft()).scaledWidth + val screenHeight = ScaledResolution(Minecraft.getMinecraft()).scaledHeight + val border = 5 + registerUniform(Uniform.UniformType.VEC2, "v_texCoord") { + floatArrayOf(((size.x - border) / screenWidth).toFloat(), ((size.y - border) / screenHeight).toFloat()) + } + registerUniform(Uniform.UniformType.FLOAT, "width") { + ((size.x + border * 2) / screenWidth).toFloat() + } + + registerUniform(Uniform.UniformType.FLOAT, "height") { + ((size.y + border * 2) / screenHeight).toFloat() + } + + registerUniform(Uniform.UniformType.FLOAT, "roundness") { + 0.05f + } + + registerUniform(Uniform.UniformType.VEC4, "color") { + val color: Int = SpecialColour.specialToChromaRGB(config.backgroundConfig.color) + floatArrayOf( + ((color shr 16 and 255) / 255.0f), + ((color shr 8 and 255) / 255.0f), + ((color and 255) / 255.0f), + ((color shr 24 and 255) / 255.0f) + ) + } } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderHelper.kt b/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderHelper.kt index 2576b42489ce..fca6c10134fd 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderHelper.kt @@ -135,6 +135,14 @@ class ShaderHelper { if (USING_ARB_SHADERS) ARBShaderObjects.glUniform1fARB(location, v0) else GL20.glUniform1f(location, v0) } + fun glUniform2f(uniformID: Int, fl: Float, fl1: Float) { + if (USING_ARB_SHADERS) ARBShaderObjects.glUniform2fARB(uniformID, fl, fl1) else GL20.glUniform2f( + uniformID, + fl, + fl1 + ) + } + fun glUniform3f(location: Int, v0: Float, v1: Float, v2: Float) { if (USING_ARB_SHADERS) ARBShaderObjects.glUniform3fARB(location, v0, v1, v2) else GL20.glUniform3f( location, @@ -144,6 +152,16 @@ class ShaderHelper { ) } + fun glUniform4f(location: Int, v0: Float, v1: Float, v2: Float, v3: Float) { + if (USING_ARB_SHADERS) ARBShaderObjects.glUniform4fARB(location, v0, v1, v2, v3) else GL20.glUniform4f( + location, + v0, + v1, + v2, + v3 + ) + } + fun glGetUniformLocation(program: Int, name: CharSequence): Int { return if (USING_ARB_SHADERS) ARBShaderObjects.glGetUniformLocationARB( program, diff --git a/src/main/java/at/hannibal2/skyhanni/utils/shader/Uniform.kt b/src/main/java/at/hannibal2/skyhanni/utils/shader/Uniform.kt index d57398ea447b..a850f146f973 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/shader/Uniform.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/shader/Uniform.kt @@ -20,7 +20,9 @@ class Uniform( class UniformType { companion object { val FLOAT: UniformType = UniformType() + val VEC2: UniformType = UniformType() val VEC3: UniformType = UniformType() + val VEC4: UniformType = UniformType() val BOOL: UniformType = UniformType() } } @@ -33,10 +35,18 @@ class Uniform( if (!Objects.deepEquals(previousUniformValue, newUniformValue)) { when (uniformType) { UniformType.FLOAT -> ShaderHelper.glUniform1f(uniformID, (newUniformValue as Float)) + UniformType.VEC2 -> { + val values = newUniformValue as FloatArray + ShaderHelper.glUniform2f(uniformID, values[0], values[1]) + } UniformType.VEC3 -> { val values = newUniformValue as FloatArray ShaderHelper.glUniform3f(uniformID, values[0], values[1], values[2]) } + UniformType.VEC4 -> { + val values = newUniformValue as FloatArray + ShaderHelper.glUniform4f(uniformID, values[0], values[1], values[2], values[3]) + } UniformType.BOOL -> ShaderHelper.glUniform1f(uniformID, if (newUniformValue as Boolean) 1f else 0f) } diff --git a/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.fsh b/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.fsh index 062258d5ecc1..d05ab3cc37ca 100644 --- a/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.fsh +++ b/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.fsh @@ -1,26 +1,25 @@ -#version 330 core +precision mediump float; -uniform vec2 size; -uniform float radius; -uniform vec4 color; - -void main() { - vec2 position = gl_FragCoord.xy; - vec2 halfSize = size * 0.5; +varying vec2 v_texCoord; - // Calculate distance to the closest point on the rectangle - vec2 dist = abs(position - halfSize); +uniform float width; +uniform float height; +uniform float roundness; +uniform vec4 color; - // Calculate distance to the closest point on the rounded corners - float cornerDistance = max(dist.x - halfSize.x + radius, dist.y - halfSize.y + radius); +void main() +{ + // Calculate the distance from the center of the rectangle + vec2 center = vec2(width, height) * 0.5; + vec2 distance = abs(v_texCoord - center); - // Use smoothstep to create a smooth transition between the rectangle and the rounded corners - float smoothStep = 1.0 - smoothstep(0.0, radius, cornerDistance); + // Calculate the distance from the center to the corner of the rounded rectangle + float cornerRadius = min(roundness, min(width, height) * 0.5); + vec2 roundedDistance = max(distance - vec2(width, height) * 0.5 + cornerRadius, vec2(0.0)); - // Use smoothstep to create a smooth transition between the rounded corners and the interior of the rectangle - float insideSmoothStep = smoothstep(radius, 0.0, cornerDistance); + // Calculate the alpha value based on the distance from the center and the corner radius + float alpha = smoothstep(cornerRadius, cornerRadius + 0.01, length(roundedDistance)); - // Combine the results to get the final color - vec4 finalColor = mix(color, vec4(0.0, 0.0, 0.0, 0.0), smoothStep); - gl_FragColor = mix(finalColor, color, insideSmoothStep); + // Output the final color with alpha blending + gl_FragColor = vec4(color.rgb, color.a * alpha); } diff --git a/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.vsh b/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.vsh new file mode 100644 index 000000000000..b865faff62e6 --- /dev/null +++ b/src/main/resources/assets/skyhanni/shaders/rounded_rectangle.vsh @@ -0,0 +1,12 @@ +attribute vec4 position; +attribute vec2 texCoord; + +uniform mat4 modelViewProjection; + +varying vec2 v_texCoord; + +void main() +{ + gl_Position = modelViewProjection * position; + v_texCoord = texCoord; +}