From 08cbbbb537f38e34ace62068b87e1e1e912403ac Mon Sep 17 00:00:00 2001 From: sisyphussy Date: Thu, 21 May 2026 00:38:32 +0200 Subject: [PATCH] add defines --- .../client/font/BatchingFontRenderer.java | 23 ++-- .../client/font/FontOverlayShader.java | 29 +++-- .../angelica/shaders/font/fontFilter.fsh | 85 ++++++++++++++ .../shaders/{ => font}/fontFilter.vsh | 0 .../angelica/shaders/font/fontOutline.fsh | 111 ++++++++++-------- .../assets/angelica/shaders/fontFilter.fsh | 79 ------------- 6 files changed, 178 insertions(+), 149 deletions(-) create mode 100644 src/main/resources/assets/angelica/shaders/font/fontFilter.fsh rename src/main/resources/assets/angelica/shaders/{ => font}/fontFilter.vsh (100%) delete mode 100644 src/main/resources/assets/angelica/shaders/fontFilter.fsh diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java b/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java index 39c656f73..a880e2844 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/BatchingFontRenderer.java @@ -3,6 +3,8 @@ package com.gtnewhorizons.angelica.client.font; import com.google.common.collect.ImmutableSet; import com.gtnewhorizon.gtnhlib.client.renderer.MatrixHelper; import com.gtnewhorizon.gtnhlib.client.renderer.postprocessing.CustomFramebuffer; +import com.gtnewhorizon.gtnhlib.client.renderer.shader.ShaderProgram; +import com.gtnewhorizon.gtnhlib.client.renderer.shader.SimpleShaderDefine; import com.gtnewhorizon.gtnhlib.client.renderer.vao.IndexBuffer; import com.gtnewhorizon.gtnhlib.util.font.GlyphReplacements; import com.gtnewhorizons.angelica.config.AngelicaConfig; @@ -63,9 +65,7 @@ public final class BatchingFontRenderer { /** Location of the primary font atlas to bind. */ protected final ResourceLocation locationFontTexture; - private final int AAMode; private final int AAStrength; - private final int alphaTestRefLocation; private final int mvpMatrixLocation; private static int fontShaderId; @@ -81,6 +81,7 @@ public final class BatchingFontRenderer { private static Program fontShader = null; public static Program getProgram() { if (fontShader == null) { + final String vsh = ShaderLoader.getShaderSource("angelica:fontFilter.vsh"); final String fsh = ShaderLoader.getShaderSource("angelica:fontFilter.fsh"); fontShader = ProgramBuilder.begin("fontFilter", vsh, null, fsh, ImmutableSet.of(0)).build(); @@ -105,10 +106,16 @@ public final class BatchingFontRenderer { FontProviderMC.get(this.isSGA).charWidth = this.charWidth; FontProviderMC.get(this.isSGA).locationFontTexture = this.locationFontTexture; - fontShaderId = FontAAShader.getProgram().getProgramId(); - AAMode = GLStateManager.glGetUniformLocation(fontShaderId, "aaMode"); + final String vsh = ShaderProgram.loadShaderSource( + FontAAShader.class.getResourceAsStream("/assets/angelica/shaders/font/fontFilter.vsh") + ); + final String fsh = ShaderProgram.loadShaderSource( + FontAAShader.class.getResourceAsStream("/assets/angelica/shaders/font/fontFilter.fsh"), + new SimpleShaderDefine("AA_MODE", FontConfig.fontAAMode) + ); + + fontShaderId = ShaderProgram.createProgramFromSource(vsh, fsh); AAStrength = GLStateManager.glGetUniformLocation(fontShaderId, "strength"); - alphaTestRefLocation = GLStateManager.glGetUniformLocation(fontShaderId, "alphaTestRef"); mvpMatrixLocation = GLStateManager.glGetUniformLocation(fontShaderId, "u_MVPMatrix"); if (ebo == null) { ebo = new IndexBuffer(); @@ -462,7 +469,6 @@ public final class BatchingFontRenderer { } } - private int fontAAModeLast = -1; private int fontAAStrengthLast = -1; private static final Comparator DRAW_ORDER_COMPARATOR = Comparator.comparing((FontDrawCmd fdc) -> fdc.texture, @@ -498,15 +504,10 @@ public final class BatchingFontRenderer { GLStateManager.glShadeModel(GL11.GL_FLAT); GLStateManager.glUseProgram(fontShaderId); - if (FontConfig.fontAAMode != fontAAModeLast) { - fontAAModeLast = FontConfig.fontAAMode; - GLStateManager.glUniform1i(AAMode, FontConfig.fontAAMode); - } if (FontConfig.fontAAStrength != fontAAStrengthLast) { fontAAStrengthLast = FontConfig.fontAAStrength; GLStateManager.glUniform1f(AAStrength, FontConfig.fontAAStrength / 120.f); } - GLStateManager.glUniform1f(alphaTestRefLocation, GLStateManager.getAlphaState().getReference()); GLStateManager.uploadMVPMatrix(mvpMatrixLocation); if (fontVAO == 0) { diff --git a/src/main/java/com/gtnewhorizons/angelica/client/font/FontOverlayShader.java b/src/main/java/com/gtnewhorizons/angelica/client/font/FontOverlayShader.java index 623d704e9..9b0fa8ec7 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/font/FontOverlayShader.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/font/FontOverlayShader.java @@ -1,6 +1,8 @@ package com.gtnewhorizons.angelica.client.font; import com.gtnewhorizon.gtnhlib.client.renderer.shader.AutoShaderUpdater; +import com.gtnewhorizon.gtnhlib.client.renderer.shader.IShaderDefinesWriter; +import com.gtnewhorizon.gtnhlib.client.renderer.shader.IShaderReloadRunnable; import com.gtnewhorizon.gtnhlib.client.renderer.shader.ShaderProgram; import com.gtnewhorizons.angelica.AngelicaMod; import com.gtnewhorizons.angelica.glsm.GLStateManager; @@ -31,18 +33,25 @@ public final class FontOverlayShader extends ShaderProgram { this, AngelicaMod.MOD_ID, "shaders/font/fontOutline.vsh", "shaders/font/fontOutline.fsh", - (program, vert, frag) -> { + new IShaderReloadRunnable() { + @Override + public void run(ShaderProgram shader) { + mvpMatrixLocation = shader.getUniformLocation("u_MVPMatrix"); + uTexelSize = shader.getUniformLocation("uTexelSize"); + uTime = shader.getUniformLocation("uTime"); + uTexBounds = shader.getUniformLocation("uTexBounds"); + shader.use(); + shader.bindTextureSlot("textFBO", 0); + shader.bindTextureSlot("sceneFBO", 1); + clear(); + } - mvpMatrixLocation = program.getUniformLocation("u_MVPMatrix"); - uTexelSize = this.getUniformLocation("uTexelSize"); - uTime = this.getUniformLocation("uTime"); - uTexBounds = this.getUniformLocation("uTexBounds"); - program.use(); - this.bindTextureSlot("textFBO", 0); - this.bindTextureSlot("sceneFBO", 1); - clear(); + @Override + public IShaderDefinesWriter[] getDefines() { + return new IShaderDefinesWriter[0]; + } } - ); + ); startTime = System.currentTimeMillis(); } diff --git a/src/main/resources/assets/angelica/shaders/font/fontFilter.fsh b/src/main/resources/assets/angelica/shaders/font/fontFilter.fsh new file mode 100644 index 000000000..0583f1b68 --- /dev/null +++ b/src/main/resources/assets/angelica/shaders/font/fontFilter.fsh @@ -0,0 +1,85 @@ +#version 330 core + +uniform sampler2D sampler; +uniform float strength; + +in vec4 color; +in vec4 tB; +in vec2 texCoord; + +out vec4 fragColor; + +/* +Hacky MSAA and anisotropic filtering. Cursed beyond belief. There _may_ have been simpler means of +achieving the produced effects, but this appears to work without noticeable performance losses. +*/ + +float totalWt; + +float txSample(vec2 uv, float du, float dv, float factorU, float factorV) { + float distSquared = sqrt(du * du + dv * dv); + float weight = exp(-distSquared / 6); + totalWt += weight; + float finalU = uv.x + factorU * du; + float finalV = uv.y + factorV * dv; + if (finalU < tB.x || finalU > tB.y || finalV < tB.z || finalV > tB.w) { + return 0.0f; + } + return weight * texture(sampler, vec2(finalU, finalV)).a; +} + +void main() { + vec4 col = color; + float original_alpha = col.a; + #if AA_MODE == 0 + + col.a = original_alpha * texture(sampler, texCoord).a; + + #else + + vec2 texScaled = texCoord * strength; + float res = 0; + float fu = abs(dFdx(texScaled.x)) + abs(dFdy(texScaled.x)); + float fv = abs(dFdx(texScaled.y)) + abs(dFdy(texScaled.y)); + totalWt = 0; + + + #if AA_MODE == 1 + + res += txSample(texCoord, 2, 6, fu, fv); + res += txSample(texCoord, 6, -2, fu, fv); + res += txSample(texCoord, -2, -6, fu, fv); + res += txSample(texCoord, -6, 2, fu, fv); + + #elif AA_MODE == 2 + + res += txSample(texCoord, 1, 1, fu, fv); + res += txSample(texCoord, -1, -3, fu, fv); + res += txSample(texCoord, -3, 2, fu, fv); + res += txSample(texCoord, 4, -1, fu, fv); + res += txSample(texCoord, -5, -2, fu, fv); + res += txSample(texCoord, 2, 5, fu, fv); + res += txSample(texCoord, 5, 3, fu, fv); + res += txSample(texCoord, 3, -5, fu, fv); + res += txSample(texCoord, -2, 6, fu, fv); + res += txSample(texCoord, 0, -7, fu, fv); + res += txSample(texCoord, -4, -6, fu, fv); + res += txSample(texCoord, -6, 4, fu, fv); + res += txSample(texCoord, -8, 0, fu, fv); + res += txSample(texCoord, 7, -4, fu, fv); + res += txSample(texCoord, 6, 7, fu, fv); + res += txSample(texCoord, -7, -8, fu, fv); + + #endif + + res /= totalWt; + col.a = original_alpha * res; + + #endif + + if (col.a <= 0.1) { + discard; + } + + fragColor = col; +} diff --git a/src/main/resources/assets/angelica/shaders/fontFilter.vsh b/src/main/resources/assets/angelica/shaders/font/fontFilter.vsh similarity index 100% rename from src/main/resources/assets/angelica/shaders/fontFilter.vsh rename to src/main/resources/assets/angelica/shaders/font/fontFilter.vsh diff --git a/src/main/resources/assets/angelica/shaders/font/fontOutline.fsh b/src/main/resources/assets/angelica/shaders/font/fontOutline.fsh index 59ad8a487..915155139 100644 --- a/src/main/resources/assets/angelica/shaders/font/fontOutline.fsh +++ b/src/main/resources/assets/angelica/shaders/font/fontOutline.fsh @@ -1,14 +1,23 @@ #version 330 core -/* + // ----- GLOW ----- +#define USE_GLOW +#define RGB_GLOW +#define GLOW_COLOR vec3(0, 1, 1) -#define test 0 +// ----- SPARKLES ----- +#define USE_SPARKLES +#define SPARKLE_COLOR vec3(1) + +// ----- WAVE ----- +#define USE_WAVE + +// ----- TEXT ----- +#define TEXT_COLOR vec3(0, 0, 0) -*/ - uniform sampler2D textFBO; uniform sampler2D sceneFBO; @@ -57,8 +66,6 @@ float glow(vec2 uv) { // Manhatten for better readability float currDist = abs(x) + abs(y); - float weight = 1; - vec2 offset = vec2(x, y) * uTexelSize; if (texture(textFBO, vec2(uv.x + offset.x, uv.y + offset.y)).a > 0.1) { @@ -74,8 +81,8 @@ float glow(vec2 uv) { dist = dist - 1.5; //TODO - float jitter = (fract(sin(uTime + (texCoord.x + texCoord.y) * 2) * 43758.0) - 0.5) * 5; - dist += jitter; + //float jitter = (fract(sin(uTime + (texCoord.x + texCoord.y) * 2) * 43758.0) - 0.5) * 5; + //dist += jitter; //TODO if (dist <= 0) return 1; @@ -98,7 +105,7 @@ vec2 random2(vec2 p) { } // Adapted from https://www.shadertoy.com/view/3d33zM -vec4 starfield(vec2 uv) { +float starfield(vec2 uv) { vec2 resolution = 1.0 / uTexelSize; vec2 fragCoord = uv * resolution; @@ -176,7 +183,7 @@ vec4 starfield(vec2 uv) { dist += sparkle * twinkle * brightness * depth; } - return vec4(1, 1, 1, dist * 2); + return dist * 2; } vec3 hsv2rgb(vec3 c) { @@ -185,7 +192,7 @@ vec3 hsv2rgb(vec3 c) { return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } -vec3 rainbow(vec2 uv) { +vec3 rgb(vec2 uv) { float h = (uv.x + uv.y) * 4 + uTime / 10; // 0 → 1 hue return hsv2rgb(vec3(h, 1.0, 1.0)); } @@ -208,62 +215,68 @@ float wave(vec2 uv) { return 1.0 - smoothstep(0.0, thickness, dist); } +//0xfda63a orang + + +//0xfda63a +//0xe1afff + void main() { - float minX = uTexBounds.x - uTexelSize.x * 6; - float maxX = uTexBounds.y + uTexelSize.x * 6; - float minY = uTexBounds.z - uTexelSize.y * 6; - float maxY = uTexBounds.w + uTexelSize.y * 6; - vec2 relativeUV = vec2(texCoord.x - minX, texCoord.y - maxX); - //float middleY = (minY + maxY) / 2; - float middleY = 0; - float yWidth = (maxY - minY) * 2; + float minX = uTexBounds.x - uTexelSize.x * 3; + float maxX = uTexBounds.y + uTexelSize.x * 3; + float minY = uTexBounds.z - uTexelSize.y * 3; + float maxY = uTexBounds.w + uTexelSize.y * 3; + vec2 relativeUV = vec2(texCoord.x - minX, texCoord.y - minY); - vec4 scene = texture(sceneFBO, texCoord); - vec4 textColor = texture(textFBO, texCoord); + vec3 overlay = vec3(0); - // ───────────────────────────── - // LIGHT (independent) - // ───────────────────────────── - float glowVal = glow(texCoord); - glowVal = glowVal > 0.2 ? glowVal : 0; - - //0xfda63a orang - - - //0xfda63a - //0xe1afff - vec3 glowColor = mixHexColors(0xe1afff, 0xd0a4fa, 0); - //vec3 glowColor = rainbow(texCoord); - //glowColor = mix(glowColor, vec3(1, 1, 1), wave(relativeUV)); - - - - vec3 light = glowColor * glowVal; + #ifdef USE_SPARKLES if ( texCoord.x >= minX && texCoord.x <= maxX && texCoord.y >= minY && texCoord.y <= maxY ) { - vec4 sparkle = starfield(vec2(texCoord.x - minX, texCoord.y - minY)); - if (sparkle.a > 0.1) { - vec3 sparkleLayer = sparkle.rgb * sparkle.a; - + float sparkle = starfield(vec2(texCoord.x - minX, texCoord.y - minY)); + if (sparkle > 0.1) { // glow sits in front of sparkle - light = mix(sparkleLayer, glowColor, glowVal); + overlay = mix(overlay, SPARKLE_COLOR, sparkle); } } + #endif + //vec3 glowColor = mixHexColors(0xe1afff, 0xd0a4fa, 0); - float mask = 1.0 - textColor.a; - if (textColor.a > 0.1) { - light = vec3(0); + #ifdef USE_GLOW + float glowVal = glow(texCoord); + if (glowVal > 0.1) { + #ifdef RGB_GLOW + vec3 glowColor = rgb(relativeUV); + #else + vec3 glowColor = GLOW_COLOR; + #endif + #ifdef USE_WAVE + glowColor = mix(glowColor, vec3(1, 1, 1), wave(relativeUV)); + #endif + overlay = mix(overlay, glowColor, glowVal); } + #endif + + + + // Mix based on text color + vec4 textColor = texture(textFBO, texCoord); + if (textColor.a > 0.1) { + overlay = mix(overlay, TEXT_COLOR, textColor.a); + } + + + // Blend scene together + vec4 scene = texture(sceneFBO, texCoord); vec3 base = scene.rgb; - vec3 finalColor = vec3(1.0) - (vec3(1.0) - base) * (vec3(1.0) - light); - finalColor = mix(finalColor, vec3(0, 0, 0), textColor.a); + vec3 finalColor = vec3(1.0) - (vec3(1.0) - base) * (vec3(1.0) - overlay); fragColor = vec4(finalColor, 1.0); } diff --git a/src/main/resources/assets/angelica/shaders/fontFilter.fsh b/src/main/resources/assets/angelica/shaders/fontFilter.fsh deleted file mode 100644 index 642a19a94..000000000 --- a/src/main/resources/assets/angelica/shaders/fontFilter.fsh +++ /dev/null @@ -1,79 +0,0 @@ -#version 330 core - -uniform sampler2D sampler; -uniform int aaMode; -uniform float strength; -uniform float alphaTestRef; - -in vec4 color; -in vec4 tB; -in vec2 texCoord; - -out vec4 fragColor; - -/* -Hacky MSAA and anisotropic filtering. Cursed beyond belief. There _may_ have been simpler means of -achieving the produced effects, but this appears to work without noticeable performance losses. -*/ - -float totalWt; - -float txSample(vec2 uv, float du, float dv, float factorU, float factorV) { - float distSquared = sqrt(du * du + dv * dv); - float weight = exp(-distSquared / 6); - totalWt += weight; - float finalU = uv.x + factorU * du; - float finalV = uv.y + factorV * dv; - if (finalU < tB.x || finalU > tB.y || finalV < tB.z || finalV > tB.w) { - return 0.0f; - } - return weight * texture(sampler, vec2(finalU, finalV)).a; -} - -void main() { - vec4 col = color; - float original_alpha = col.a; - if (texCoord.s != 0 || texCoord.t != 0) { - if (aaMode == 0) { - // No AA - single texture sample - col.a = original_alpha * texture(sampler, texCoord).a; - } else { - vec2 texScaled = texCoord * strength; - float res = 0; - float fu = abs(dFdx(texScaled.x)) + abs(dFdy(texScaled.x)); - float fv = abs(dFdx(texScaled.y)) + abs(dFdy(texScaled.y)); - totalWt = 0; - if (aaMode == 1) { - res += txSample(texCoord, 2, 6, fu, fv); - res += txSample(texCoord, 6, -2, fu, fv); - res += txSample(texCoord, -2, -6, fu, fv); - res += txSample(texCoord, -6, 2, fu, fv); - } else { - res += txSample(texCoord, 1, 1, fu, fv); - res += txSample(texCoord, -1, -3, fu, fv); - res += txSample(texCoord, -3, 2, fu, fv); - res += txSample(texCoord, 4, -1, fu, fv); - res += txSample(texCoord, -5, -2, fu, fv); - res += txSample(texCoord, 2, 5, fu, fv); - res += txSample(texCoord, 5, 3, fu, fv); - res += txSample(texCoord, 3, -5, fu, fv); - res += txSample(texCoord, -2, 6, fu, fv); - res += txSample(texCoord, 0, -7, fu, fv); - res += txSample(texCoord, -4, -6, fu, fv); - res += txSample(texCoord, -6, 4, fu, fv); - res += txSample(texCoord, -8, 0, fu, fv); - res += txSample(texCoord, 7, -4, fu, fv); - res += txSample(texCoord, 6, 7, fu, fv); - res += txSample(texCoord, -7, -8, fu, fv); - } - res /= totalWt; - col.a = original_alpha * res; - } - } - - if (col.a <= alphaTestRef) { - discard; - } - - fragColor = col; -}