Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blurred Rectangle Rendering #353

Open
wants to merge 2 commits into
base: develop-v0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/OneConfig.api
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,8 @@ public class cc/polyfrost/oneconfig/gui/pages/SubModsPage : cc/polyfrost/oneconf
public abstract class cc/polyfrost/oneconfig/hud/BasicHud : cc/polyfrost/oneconfig/hud/Hud {
protected field background Z
protected field bgColor Lcc/polyfrost/oneconfig/config/core/OneColor;
protected field blurAmount F
protected field blurBackground Z
protected field border Z
protected field borderColor Lcc/polyfrost/oneconfig/config/core/OneColor;
protected field borderSize F
Expand Down Expand Up @@ -1261,6 +1263,8 @@ public class cc/polyfrost/oneconfig/images/OneImage {
}

public abstract interface class cc/polyfrost/oneconfig/platform/GLPlatform {
public abstract fun drawBlurredRect (FFFFFF)V
public abstract fun drawBlurredRect (FFFFFFFFF)V
public abstract fun drawRect (FFFFI)V
public abstract fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun drawText (Ljava/lang/String;FFIZ)F
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cc/polyfrost/oneconfig/hud/BasicHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
import cc.polyfrost.oneconfig.config.annotations.Exclude;
import cc.polyfrost.oneconfig.config.core.OneColor;
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack;
import cc.polyfrost.oneconfig.platform.Platform;
import cc.polyfrost.oneconfig.renderer.NanoVGHelper;


public abstract class BasicHud extends Hud {
protected boolean background;
protected boolean rounded;
protected boolean border;
protected boolean blurBackground;
protected OneColor bgColor;
protected OneColor borderColor;
protected float cornerRadius;
protected float blurAmount = 4f;
protected float borderSize;
protected float defaultPaddingX, paddingX;
protected float defaultPaddingY, paddingY;
Expand Down Expand Up @@ -149,10 +152,12 @@ protected void drawBackground(float x, float y, float width, float height, float
NanoVGHelper nanoVGHelper = NanoVGHelper.INSTANCE;
nanoVGHelper.setupAndDraw(true, (vg) -> {
if (rounded) {
if (blurBackground) Platform.getGLPlatform().drawBlurredRect(x, y, width, height, blurAmount, cornerRadius * scale);
nanoVGHelper.drawRoundedRect(vg, x, y, width, height, bgColor.getRGB(), cornerRadius * scale);
if (border)
nanoVGHelper.drawHollowRoundRect(vg, x - borderSize * scale, y - borderSize * scale, width + borderSize * scale, height + borderSize * scale, borderColor.getRGB(), cornerRadius * scale, borderSize * scale);
} else {
if (blurBackground) Platform.getGLPlatform().drawBlurredRect(x, y, width, height, blurAmount, 0f);
nanoVGHelper.drawRect(vg, x, y, width, height, bgColor.getRGB());
if (border)
nanoVGHelper.drawHollowRoundRect(vg, x - borderSize * scale, y - borderSize * scale, width + borderSize * scale, height + borderSize * scale, borderColor.getRGB(), 0, borderSize * scale);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ public Object setValue(Object value) {
options.add(new ConfigCheckbox(fields.get("background"), hud, "Background", "If the background of the HUD is enabled.", category, subcategory, 1));
options.add(new ConfigCheckbox(fields.get("rounded"), hud, "Rounded corners", "If the background has rounded corners.", category, subcategory, 1));
options.get(options.size() - 1).addDependency("Background or Border", () -> ((BasicHud) hud).background || ((BasicHud) hud).border);
options.add(new ConfigCheckbox(fields.get("blurBackground"), hud, "Blurred", "If the background is blurred.", category, subcategory, 1));
options.get(options.size() - 1).addDependency("Background", () -> ((BasicHud) hud).background);
options.add(new ConfigCheckbox(fields.get("border"), hud, "Outline/border", "If the hud has an outline.", category, subcategory, 1));
options.add(new ConfigColorElement(fields.get("bgColor"), hud, "Background color:", "The color of the background.", category, subcategory, 1, true));
options.get(options.size() - 1).addDependency("Background", () -> ((BasicHud) hud).background);
options.add(new ConfigColorElement(fields.get("borderColor"), hud, "Border color:", "The color of the border.", category, subcategory, 1, true));
options.get(options.size() - 1).addDependency("Border", () -> ((BasicHud) hud).border);
options.add(new ConfigSlider(fields.get("cornerRadius"), hud, "Corner radius:", "The corner radius of the background.", category, subcategory, 0, 10, 0, false));
options.get(options.size() - 1).addDependency("Rounded", () -> ((BasicHud) hud).rounded);
options.add(new ConfigSlider(fields.get("blurAmount"), hud, "Blur amount:", "The amount of background blur.", category, subcategory, 0.1f, 20f, 0, false));
options.get(options.size() - 1).addDependency("Blurred", () -> ((BasicHud) hud).blurBackground);
options.add(new ConfigSlider(fields.get("borderSize"), hud, "Border thickness:", "The thickness of the outline.", category, subcategory, 0, 10, 0, false));
options.get(options.size() - 1).addDependency("Border", () -> ((BasicHud) hud).border);
Field paddingX = fields.get("paddingX");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cc/polyfrost/oneconfig/platform/GLPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ default float drawText(String text, float x, float y, int color, boolean shadow)
float drawText(UMatrixStack matrixStack, String text, float x, float y, int color, boolean shadow);

int getStringWidth(String text);

void drawBlurredRect(float x, float y, float width, float height, float blur, float TLRadius, float TRRadius, float BLRadius, float BRRadius);

void drawBlurredRect(float x, float y, float width, float height, float blur, float radius);
}
60 changes: 60 additions & 0 deletions src/main/resources/assets/shaders/blur.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#version 120

uniform sampler2D u_texture;

uniform vec2 u_texelSize;

uniform float u_blurRadius;
uniform vec4 u_location;
uniform vec4 u_rectRadius;
uniform float u_pass;

float gauss(float x, float sigma) {
float pow = x / sigma;
return (1.0 / (abs(sigma) * 2.50662827463) * exp(-0.5 * pow * pow));
}

float roundedBoxSDF(vec2 pos, vec2 size, vec4 radius) {
radius.xy = (pos.x > 0.0) ? radius.xy : radius.zw;
radius.x = (pos.y > 0.0) ? radius.x : radius.y;
vec2 q = abs(pos) - size + radius.x;
return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - radius.x;
}

vec4 applyBlur(vec2 direction, vec2 texCoord, float blurRadius) {
vec4 color = vec4(0);
vec2 pos = gl_FragCoord.xy - u_location.xy - u_location.zw;
vec2 size = u_location.zw;

if (direction.x == 1) {
pos.y -= blurRadius;
size.y += blurRadius * 2;
}

float distance = roundedBoxSDF(pos, size, u_rectRadius);

if (distance < 0.0) {
for (float f = -u_blurRadius; f <= u_blurRadius; f++) {
color += texture2D(u_texture, texCoord + f * u_texelSize * direction) * gauss(f, u_blurRadius / 2);
}
}

return color;
}

void main() {
vec4 color = vec4(0);
vec2 texCoord = gl_TexCoord[0].st;

if (u_pass == 1) {
color = applyBlur(vec2(1.0, 0.0), texCoord, u_blurRadius);
gl_FragColor = vec4(color.rgb, 1);
} else if (u_pass == 2) {
color = applyBlur(vec2(0.0, 1.0), texCoord, 0);
float distance = roundedBoxSDF(gl_FragCoord.xy - u_location.xy - u_location.zw, u_location.zw, u_rectRadius);

if (distance < 0.0) {
gl_FragColor = vec4(color.rgb, 1);
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/shaders/blur.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#version 120

void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
2 changes: 2 additions & 0 deletions versions/1.12.2-fabric/api/1.12.2-fabric.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class cc/polyfrost/oneconfig/events/event/TimerUpdateEvent {

public class cc/polyfrost/oneconfig/platform/impl/GLPlatformImpl : cc/polyfrost/oneconfig/platform/GLPlatform {
public fun <init> ()V
public fun drawBlurredRect (FFFFFF)V
public fun drawBlurredRect (FFFFFFFFF)V
public fun drawRect (FFFFI)V
public fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun enableStencil ()V
Expand Down
2 changes: 2 additions & 0 deletions versions/1.12.2-forge/api/1.12.2-forge.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class cc/polyfrost/oneconfig/events/event/TimerUpdateEvent {

public class cc/polyfrost/oneconfig/platform/impl/GLPlatformImpl : cc/polyfrost/oneconfig/platform/GLPlatform {
public fun <init> ()V
public fun drawBlurredRect (FFFFFF)V
public fun drawBlurredRect (FFFFFFFFF)V
public fun drawRect (FFFFI)V
public fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun enableStencil ()V
Expand Down
2 changes: 2 additions & 0 deletions versions/1.16.5-fabric/api/1.16.5-fabric.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class cc/polyfrost/oneconfig/events/event/TimerUpdateEvent {

public class cc/polyfrost/oneconfig/platform/impl/GLPlatformImpl : cc/polyfrost/oneconfig/platform/GLPlatform {
public fun <init> ()V
public fun drawBlurredRect (FFFFFF)V
public fun drawBlurredRect (FFFFFFFFF)V
public fun drawRect (FFFFI)V
public fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun enableStencil ()V
Expand Down
2 changes: 2 additions & 0 deletions versions/1.16.5-forge/api/1.16.5-forge.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class cc/polyfrost/oneconfig/events/event/TimerUpdateEvent {

public class cc/polyfrost/oneconfig/platform/impl/GLPlatformImpl : cc/polyfrost/oneconfig/platform/GLPlatform {
public fun <init> ()V
public fun drawBlurredRect (FFFFFF)V
public fun drawBlurredRect (FFFFFFFFF)V
public fun drawRect (FFFFI)V
public fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun enableStencil ()V
Expand Down
2 changes: 2 additions & 0 deletions versions/1.8.9-fabric/api/1.8.9-fabric.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class cc/polyfrost/oneconfig/events/event/TimerUpdateEvent {

public class cc/polyfrost/oneconfig/platform/impl/GLPlatformImpl : cc/polyfrost/oneconfig/platform/GLPlatform {
public fun <init> ()V
public fun drawBlurredRect (FFFFFF)V
public fun drawBlurredRect (FFFFFFFFF)V
public fun drawRect (FFFFI)V
public fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun enableStencil ()V
Expand Down
2 changes: 2 additions & 0 deletions versions/1.8.9-forge/api/1.8.9-forge.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class cc/polyfrost/oneconfig/events/event/TimerUpdateEvent {

public class cc/polyfrost/oneconfig/platform/impl/GLPlatformImpl : cc/polyfrost/oneconfig/platform/GLPlatform {
public fun <init> ()V
public fun drawBlurredRect (FFFFFF)V
public fun drawBlurredRect (FFFFFFFFF)V
public fun drawRect (FFFFI)V
public fun drawText (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;Ljava/lang/String;FFIZ)F
public fun enableStencil ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import cc.polyfrost.oneconfig.events.EventManager;
import cc.polyfrost.oneconfig.events.event.*;
import cc.polyfrost.oneconfig.internal.OneConfig;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import net.minecraft.client.Minecraft;
import net.minecraft.util.Timer;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -78,6 +79,11 @@ private void onInit(CallbackInfo ci) {
OneConfig.INSTANCE.init();
EventManager.INSTANCE.post(new InitializationEvent());
}

@Inject(method = "updateFramebufferSize", at = @At("HEAD"))
private void updateSize(CallbackInfo ci) {
cc.polyfrost.oneconfig.internal.gui.impl.ShadersKt.getBlurProgram().getBlurBuffer().createBindFramebuffer(UMinecraft.getMinecraft().displayWidth, UMinecraft.getMinecraft().displayHeight);
}
//#endif

@Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = UPDATE_CAMERA_AND_RENDER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ public float drawText(UMatrixStack matrixStack, String text, float x, float y, i
public int getStringWidth(String text) {
return UMinecraft.getFontRenderer().getStringWidth(text);
}

@Override
public void drawBlurredRect(float x, float y, float width, float height, float blur, float TLRadius, float TRRadius, float BLRadius, float BRRadius) {
//#if FORGE==1 && MC<=11202
cc.polyfrost.oneconfig.internal.gui.impl.ShadersKt.getBlurProgram().render(x, y, width, height, TLRadius, TRRadius, BLRadius, BRRadius, blur);
//#endif
}

@Override
public void drawBlurredRect(float x, float y, float width, float height, float blur, float radius) {
drawBlurredRect(x, y, width, height, blur, radius, radius, radius, radius);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021~2023 Polyfrost.
* <https://polyfrost.cc> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.cc/legal/oneconfig/additional-terms>
*/

//#if FORGE==1 && MC<=11202
package cc.polyfrost.oneconfig.internal.gui.impl

import cc.polyfrost.oneconfig.internal.OneConfig
import org.lwjgl.opengl.GL11
import org.lwjgl.opengl.GL20.*

abstract class ShaderProgram(
val program: Int = glCreateProgram(),
val uniforms: MutableList<Uniform> = mutableListOf(),
var active: Boolean = false
) {
abstract fun register()
init {
register()
glLinkProgram(program)
glValidateProgram(program)
}

open fun begin() {
active = true
glUseProgram(program)
}
open fun applyUniforms() {
uniforms.forEach { it.apply() }
}
open fun end() {
if (!active) return
active = false
glUseProgram(0)
}

fun registerShader(
location: String,
type: Int
) {
val source = OneConfig.INSTANCE::class.java.classLoader.getResource(location) ?: throw IllegalStateException("Failed to fetch resource $location")
val shader = initShader(
location.substringAfterLast("/").substringBeforeLast("."),
source.readBytes().toString(Charsets.UTF_8),
type
)

glAttachShader(program, shader)
}
private fun initShader(name: String, shaderSource: String, type: Int) : Int {
var shader = 0

try {
shader = glCreateShader(type)
glShaderSource(shader, shaderSource)
glCompileShader(shader)

if (glGetShaderi(shader, GL_COMPILE_STATUS) == GL11.GL_FALSE)
throw Exception(
"Error in creating shader $name :${
glGetShaderInfoLog(shader, Short.MAX_VALUE.toInt())
}"
)

return shader
} catch (ex: Exception) {
glDeleteShader(shader)
throw ex
}
}

fun getUniform(name: String) = glGetUniformLocation(program, name)
abstract inner class Uniform(
name: String,
var location: Int = 0
) {
init {
location = getUniform(name)
registerUniform()
}
private fun registerUniform() = uniforms.add(this)
internal abstract fun apply()
}

open inner class Uniform1f(
name: String,
var x: Float = 0f
) : Uniform(name) {
override fun apply() {
glUniform1f(location, x)
}
}

open inner class Uniform2f(
name: String,
var x: Float = 0f,
var y: Float = 0f
) : Uniform(name) {
override fun apply() {
glUniform2f(location, x, y)
}
}

open inner class Uniform4f(
name: String,
var x: Float = 0f,
var y: Float = 0f,
var z: Float = 0f,
var w: Float = 0f
) : Uniform(name) {
override fun apply() {
glUniform4f(location, x, y, z, w)
}
}

open inner class UniformSampler(
name: String,
var textureId: Int = 0
): Uniform(name) {
override fun apply() {
glUniform1i(this.location, this.textureId)
}
}

}
//#endif
Loading
Loading