Skip to content

Commit

Permalink
Tried rounded rectangles with shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
j10a1n15 committed Nov 26, 2023
1 parent fb6d92f commit f4401e8
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -88,16 +88,21 @@ 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,
x + elementWidth + border * 2,
y + elementHeight + border * 2,
SpecialColour.specialToChromaRGB(config.backgroundConfig.color)
)
}*/
}

position.renderStrings(display, posLabel = "Custom Scoreboard")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
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") {

val INSTANCE: RoundedRectangleShader
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)
)
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/shader/ShaderHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/shader/Uniform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Uniform<T>(
class UniformType<T> {
companion object {
val FLOAT: UniformType<Float> = UniformType()
val VEC2: UniformType<FloatArray> = UniformType()
val VEC3: UniformType<FloatArray> = UniformType()
val VEC4: UniformType<FloatArray> = UniformType()
val BOOL: UniformType<Boolean> = UniformType()
}
}
Expand All @@ -33,10 +35,18 @@ class Uniform<T>(
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)
}
Expand Down
37 changes: 18 additions & 19 deletions src/main/resources/assets/skyhanni/shaders/rounded_rectangle.fsh
Original file line number Diff line number Diff line change
@@ -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);
}
12 changes: 12 additions & 0 deletions src/main/resources/assets/skyhanni/shaders/rounded_rectangle.vsh
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f4401e8

Please sign in to comment.