Skip to content

Commit

Permalink
Add rounded rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
VixidDev committed Dec 28, 2023
1 parent 453f114 commit 1dea3a5
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.utils.shader.Shader
import at.hannibal2.skyhanni.utils.shader.Uniform
import net.minecraft.client.Minecraft

object RoundedRectangleShader : Shader("rounded_rect", "rounded_rect") {

val INSTANCE: RoundedRectangleShader
get() = this

var radius: Float = 0f
var smoothness: Float = 0f
var halfSize: FloatArray = floatArrayOf(0f, 0f)
var centerPos: FloatArray = floatArrayOf(0f, 0f)
set(value) {
field = floatArrayOf(value[0], Minecraft.getMinecraft().displayHeight - value[1])
}

override fun registerUniforms() {
registerUniform(Uniform.UniformType.FLOAT, "radius") { radius }
registerUniform(Uniform.UniformType.FLOAT, "smoothness") { smoothness }
registerUniform(Uniform.UniformType.VEC2, "halfSize") { halfSize }
registerUniform(Uniform.UniformType.VEC2, "centerPos") { centerPos }
}
}
32 changes: 32 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getAbsY
import at.hannibal2.skyhanni.events.GuiRenderItemEvent
import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.features.misc.RoundedRectangleShader
import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.shader.ShaderManager
import io.github.moulberry.moulconfig.internal.TextRenderUtils
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.client.Minecraft
Expand All @@ -26,6 +28,8 @@ import net.minecraft.util.MathHelper
import net.minecraft.util.ResourceLocation
import org.lwjgl.opengl.GL11
import java.awt.Color
import net.minecraft.client.gui.ScaledResolution
import org.lwjgl.input.Keyboard
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt
Expand Down Expand Up @@ -1118,4 +1122,32 @@ object RenderUtils {
GlStateManager.enableLighting()
GlStateManager.enableDepth()
}

/**
* Method to draw a rounded rectangle.
*
* @param color color of rect
* @param radius the radius of the corners (default 10)
* @param smoothness how smooth the corners will appear (default 2)
*/
fun drawRoundRect(x: Int, y: Int, width: Int, height: Int, color: Int, radius: Int = 10, smoothness: Int = 2) {
val scaledRes = ScaledResolution(Minecraft.getMinecraft())
val widthIn = width * scaledRes.scaleFactor
val heightIn = height * scaledRes.scaleFactor
val xIn = x * scaledRes.scaleFactor
val yIn = y * scaledRes.scaleFactor

RoundedRectangleShader.radius = radius.toFloat()
RoundedRectangleShader.smoothness = smoothness.toFloat()
RoundedRectangleShader.halfSize = floatArrayOf(widthIn / 2f, heightIn / 2f)
RoundedRectangleShader.centerPos = floatArrayOf(xIn + (widthIn / 2f), yIn + (heightIn / 2f))

GlStateManager.pushMatrix()
ShaderManager.enableShader("rounded_rect")

Gui.drawRect(x, y, x + widthIn, y + heightIn, color)

ShaderManager.disableShader()
GlStateManager.popMatrix()
}
}
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(location: Int, v0: Float, v1: Float) {
if (USING_ARB_SHADERS) ARBShaderObjects.glUniform2fARB(location, v0, v1) else GL20.glUniform2f(
location,
v0,
v1
)
}

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 Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.utils.shader

import at.hannibal2.skyhanni.features.chroma.ChromaShader
import at.hannibal2.skyhanni.features.misc.RoundedRectangleShader
import at.hannibal2.skyhanni.utils.LorenzUtils
import net.minecraft.client.Minecraft
import net.minecraft.util.ResourceLocation
Expand All @@ -18,11 +19,13 @@ object ShaderManager {
* in the when expression
*/
enum class Shaders(val shader: Shader) {
CHROMA(ChromaShader.INSTANCE);
CHROMA(ChromaShader.INSTANCE),
ROUNDED_RECTANGLE(RoundedRectangleShader.INSTANCE);

companion object {
fun getShaderInstance(shaderName: String): Shader? = when (shaderName) {
"chroma" -> CHROMA.shader
"rounded_rect" -> ROUNDED_RECTANGLE.shader
else -> {
null
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/at/hannibal2/skyhanni/utils/shader/Uniform.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.hannibal2.skyhanni.utils.shader

import java.util.*
import java.util.Objects
import java.util.function.Supplier

/**
Expand All @@ -20,6 +20,7 @@ class Uniform<T>(
class UniformType<T> {
companion object {
val FLOAT: UniformType<Float> = UniformType()
val VEC2: UniformType<FloatArray> = UniformType()
val VEC3: UniformType<FloatArray> = UniformType()
val BOOL: UniformType<Boolean> = UniformType()
}
Expand All @@ -32,12 +33,17 @@ class Uniform<T>(
val newUniformValue: T = uniformValuesSupplier.get()
if (!Objects.deepEquals(previousUniformValue, newUniformValue)) {
when (uniformType) {
UniformType.FLOAT -> ShaderHelper.glUniform1f(uniformID, (newUniformValue as Float))
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.BOOL -> ShaderHelper.glUniform1f(uniformID, if (newUniformValue as Boolean) 1f else 0f)
}
previousUniformValue = newUniformValue
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/assets/skyhanni/shaders/rounded_rect.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Based on https://www.shadertoy.com/view/WtdSDs

#version 130

uniform float radius;
uniform float smoothness;
uniform vec2 halfSize;
uniform vec2 centerPos;

varying vec4 color;

float roundedRectSDF(vec2 center, vec2 halfSize, float radius) {
return length(max(abs(center) - halfSize + radius, 0.0)) - radius;
}

void main() {
float distance = roundedRectSDF(gl_FragCoord.xy - centerPos, halfSize, radius);
float smoothed = 1.0 - smoothstep(0.0, smoothness, distance);
gl_FragColor = color * vec4(1.0, 1.0, 1.0, smoothed);
}
8 changes: 8 additions & 0 deletions src/main/resources/assets/skyhanni/shaders/rounded_rect.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 130

varying vec4 color;

void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
color = gl_Color;
}

0 comments on commit 1dea3a5

Please sign in to comment.