forked from hannibal002/SkyHanni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tried rounded rectangles with shaders
- Loading branch information
Showing
6 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/at/hannibal2/skyhanni/features/misc/customscoreboard/RoundedRectangleShader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 18 additions & 19 deletions
37
src/main/resources/assets/skyhanni/shaders/rounded_rectangle.fsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/resources/assets/skyhanni/shaders/rounded_rectangle.vsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |