From e5ef86b9b3fd0f5e687416acc667f368444e4459 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Tue, 3 May 2022 14:42:49 +0700 Subject: stolen blur --- .../java/io/polyfrost/oneconfig/OneConfig.java | 2 + .../oneconfig/config/interfaces/Config.java | 2 +- .../io/polyfrost/oneconfig/lwjgl/BlurHandler.java | 159 +++++++++++++++++++++ .../java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 1 - .../io/polyfrost/oneconfig/utils/TickDelay.java | 7 +- .../minecraft/shaders/post/fade_in_blur.json | 67 +++++++++ .../minecraft/shaders/program/fade_in_blur.fsh | 33 +++++ .../minecraft/shaders/program/fade_in_blur.json | 21 +++ src/main/resources/licenses/BlurMC-License.txt | 24 ++++ src/main/resources/oneconfig_at.cfg | 2 + src/main/resources/post/fade_in_blur.json | 67 +++++++++ src/main/resources/program/fade_in_blur.fsh | 33 +++++ src/main/resources/program/fade_in_blur.json | 21 +++ 13 files changed, 432 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/BlurHandler.java create mode 100644 src/main/resources/assets/minecraft/shaders/post/fade_in_blur.json create mode 100644 src/main/resources/assets/minecraft/shaders/program/fade_in_blur.fsh create mode 100644 src/main/resources/assets/minecraft/shaders/program/fade_in_blur.json create mode 100644 src/main/resources/licenses/BlurMC-License.txt create mode 100644 src/main/resources/oneconfig_at.cfg create mode 100644 src/main/resources/post/fade_in_blur.json create mode 100644 src/main/resources/program/fade_in_blur.fsh create mode 100644 src/main/resources/program/fade_in_blur.json (limited to 'src/main') diff --git a/src/main/java/io/polyfrost/oneconfig/OneConfig.java b/src/main/java/io/polyfrost/oneconfig/OneConfig.java index 3c867b9..dedf6b8 100644 --- a/src/main/java/io/polyfrost/oneconfig/OneConfig.java +++ b/src/main/java/io/polyfrost/oneconfig/OneConfig.java @@ -6,6 +6,7 @@ import io.polyfrost.oneconfig.config.core.ConfigCore; import io.polyfrost.oneconfig.config.data.Mod; import io.polyfrost.oneconfig.config.data.ModType; import io.polyfrost.oneconfig.hud.HudCore; +import io.polyfrost.oneconfig.lwjgl.BlurHandler; import io.polyfrost.oneconfig.lwjgl.RenderManager; import io.polyfrost.oneconfig.lwjgl.font.Fonts; import io.polyfrost.oneconfig.lwjgl.image.Images; @@ -46,6 +47,7 @@ public class OneConfig { @net.minecraftforge.fml.common.Mod.EventHandler public void onFMLInitialization(FMLInitializationEvent event) { + BlurHandler.INSTANCE.load(); testConfig = new TestConfig(); ClientCommandHandler.instance.registerCommand(new OneConfigCommand()); MinecraftForge.EVENT_BUS.register(this); diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java index f511ae3..cbba2e0 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java +++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java @@ -43,7 +43,7 @@ public class Config { mod.config = this; generateOptionList(this.getClass(), mod.defaultPage, mod); ConfigCore.oneConfigMods.add(mod); - this.mod = mod; + Config.mod = mod; } /** diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/BlurHandler.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/BlurHandler.java new file mode 100644 index 0000000..86e4e20 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/BlurHandler.java @@ -0,0 +1,159 @@ +package io.polyfrost.oneconfig.lwjgl; + +import io.polyfrost.oneconfig.gui.OneConfigGui; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.shader.Shader; +import net.minecraft.client.shader.ShaderUniform; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.event.GuiOpenEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; + +/** + * An implementation of the BlurMC mod by tterrag1098. + * + * For the original source see https://github.com/tterrag1098/Blur/blob/1.8.9/src/main/java/com/tterrag/blur/Blur.java + * For the public license, see https://github.com/tterrag1098/Blur/blob/1.8.9/LICENSE + * + * License available under https://github.com/boomboompower/ToggleChat/blob/master/src/main/resources/licenses/BlurMC-License.txt + * + * @author tterrag1098, boomboompower + * + * Taken from ToggleChat + * https://github.com/boomboompower/ToggleChat/blob/master/LICENSE + */ +public class BlurHandler { + private final ResourceLocation blurShader = new ResourceLocation("shaders/post/fade_in_blur.json"); + private final Logger logger = LogManager.getLogger("OneConfig - Blur"); + private final Minecraft mc = Minecraft.getMinecraft(); + + private long start; + private float lastProgress = 0; + + public static BlurHandler INSTANCE = new BlurHandler(); + + /** + * Simply initializes the blur mod so events are properly handled by forge. + */ + public void load() { + MinecraftForge.EVENT_BUS.register(this); + } + + @SubscribeEvent + public void onGuiChange(GuiOpenEvent event) { + reloadBlur(event.gui); + } + + @SubscribeEvent + public void onRenderTick(final TickEvent.RenderTickEvent event) { + this.mc.mcProfiler.startSection("blur"); + + if (event.phase != TickEvent.Phase.END) { + this.mc.mcProfiler.endSection(); + return; + } + + // Only blur on our own menus + if (this.mc.currentScreen == null) { + this.mc.mcProfiler.endSection(); + return; + } + + // Only update the shader if one is active + if (!this.mc.entityRenderer.isShaderActive()) { + this.mc.mcProfiler.endSection(); + return; + } + + float progress = getBlurStrengthProgress(); + + // If the new progress value matches the old one this + // will skip the frame update, which (hopefully) resolves the issue + // with the heavy computations after the "animation" is complete. + if (progress == this.lastProgress) { + this.mc.mcProfiler.endSection(); + return; + } + + // Store it for the next iteration! + this.lastProgress = progress; + + // This is hilariously bad, and could cause frame issues on low-end computers. + // Why is this being computed every tick? Surely there is a better way? + // This needs to be optimized. + try { + final List listShaders = this.mc.entityRenderer.getShaderGroup().listShaders; + + // Should not happen. Something bad happened. + if (listShaders == null) { + this.mc.mcProfiler.endSection(); + return; + } + + // Iterate through the list of shaders. + for (Shader shader : listShaders) { + ShaderUniform su = shader.getShaderManager().getShaderUniform("Progress"); + + if (su == null) { + continue; + } + + // All this for this. + su.set(progress); + } + } catch (IllegalArgumentException ex) { + this.logger.error("An error occurred while updating ToggleChat's blur. Please report this!", ex); + } + + this.mc.mcProfiler.endSection(); + } + + /** + * Activates/deactivates the blur in the current world if + * one of many conditions are met, such as no current other shader + * is being used, we actually have the blur setting enabled + */ + public void reloadBlur(GuiScreen gui) { + // Don't do anything if no world is loaded + if (this.mc.theWorld == null) { + return; + } + + EntityRenderer er = this.mc.entityRenderer; + + // If a shader is not already active and the UI is + // a one of ours, we should load our own blur! + if (!er.isShaderActive() && gui instanceof OneConfigGui) { + this.mc.entityRenderer.loadShader(this.blurShader); + + this.start = System.currentTimeMillis(); + + // If a shader is active and the incoming UI is null or we have blur disabled, stop using the shader. + } else if (er.isShaderActive() && (gui == null)) { + String name = er.getShaderGroup().getShaderGroupName(); + + // Only stop our specific blur ;) + if (!name.endsWith("fade_in_blur.json")) { + return; + } + + er.stopUseShader(); + } + } + + /** + * Returns the strength of the blur as determined by the duration the effect of the blur. + * + * The strength of the blur does not go below 5.0F. + */ + private float getBlurStrengthProgress() { + return Math.min((System.currentTimeMillis() - this.start) / 50F, 5.0F); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java index de0f3d7..63026e6 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -21,7 +21,6 @@ public final class IOUtil { * Taken from legui under MIT License * https://github.com/SpinyOwl/legui/blob/develop/LICENSE */ - @SuppressWarnings("RedundantCast") public static ByteBuffer resourceToByteBuffer(String path) throws IOException { byte[] bytes; path = path.trim(); diff --git a/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java b/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java index b28a20a..0babff1 100644 --- a/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java +++ b/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java @@ -1,13 +1,12 @@ package io.polyfrost.oneconfig.utils; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; public class TickDelay { - int delay; - Runnable function; + private int delay; + private final Runnable function; public TickDelay(Runnable functionName, int ticks) { register(); @@ -27,12 +26,10 @@ public class TickDelay { } } - @EventHandler() private void destroy() { MinecraftForge.EVENT_BUS.unregister(this); } - @EventHandler() private void register() { MinecraftForge.EVENT_BUS.register(this); } diff --git a/src/main/resources/assets/minecraft/shaders/post/fade_in_blur.json b/src/main/resources/assets/minecraft/shaders/post/fade_in_blur.json new file mode 100644 index 0000000..3214a10 --- /dev/null +++ b/src/main/resources/assets/minecraft/shaders/post/fade_in_blur.json @@ -0,0 +1,67 @@ +{ + "targets": [ + "swap" + ], + "passes": [ + { + "name": "fade_in_blur", + "intarget": "minecraft:main", + "outtarget": "swap", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 1.0, 0.0 ] + }, + { + "name": "Radius", + "values": [ 2.0 ] + } + ] + }, + { + "name": "fade_in_blur", + "intarget": "swap", + "outtarget": "minecraft:main", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 0.0, 1.0 ] + }, + { + "name": "Radius", + "values": [ 2.0 ] + } + ] + }, + { + "name": "fade_in_blur", + "intarget": "minecraft:main", + "outtarget": "swap", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 1.0, 0.0 ] + }, + { + "name": "Radius", + "values": [ 2.0 ] + } + ] + }, + { + "name": "fade_in_blur", + "intarget": "swap", + "outtarget": "minecraft:main", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 0.0, 1.0 ] + }, + { + "name": "Radius", + "values": [ 2.0 ] + } + ] + } + ] +} diff --git a/src/main/resources/assets/minecraft/shaders/program/fade_in_blur.fsh b/src/main/resources/assets/minecraft/shaders/program/fade_in_blur.fsh new file mode 100644 index 0000000..5539f39 --- /dev/null +++ b/src/main/resources/assets/minecraft/shaders/program/fade_in_blur.fsh @@ -0,0 +1,33 @@ +#version 120 + +uniform sampler2D DiffuseSampler; + +varying vec2 texCoord; +varying vec2 oneTexel; + +uniform vec2 InSize; + +uniform vec2 BlurDir; +uniform float Radius; +uniform float Progress; + +void main() { + vec4 blurred = vec4(0.0); + float totalStrength = 0.0; + float totalAlpha = 0.0; + float totalSamples = 0.0; + float progRadius = floor(Radius * Progress); + for(float r = -progRadius; r <= progRadius; r += 1.0) { + vec4 sample = texture2D(DiffuseSampler, texCoord + oneTexel * r * BlurDir); + + // Accumulate average alpha + totalAlpha = totalAlpha + sample.a; + totalSamples = totalSamples + 1.0; + + // Accumulate smoothed blur + float strength = 1.0 - abs(r / progRadius); + totalStrength = totalStrength + strength; + blurred = blurred + sample; + } + gl_FragColor = vec4(blurred.rgb / (progRadius * 2.0 + 1.0), totalAlpha); +} diff --git a/src/main/resources/assets/minecraft/shaders/program/fade_in_blur.json b/src/main/resources/assets/minecraft/shaders/program/fade_in_blur.json new file mode 100644 index 0000000..3d09d02 --- /dev/null +++ b/src/main/resources/assets/minecraft/shaders/program/fade_in_blur.json @@ -0,0 +1,21 @@ +{ + "blend": { + "func": "add", + "srcrgb": "one", + "dstrgb": "zero" + }, + "vertex": "sobel", + "fragment": "fade_in_blur", + "attributes": [ "Position" ], + "samplers": [ + { "name": "DiffuseSampler" } + ], + "uniforms": [ + { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "InSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "OutSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "BlurDir", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "Radius", "type": "float", "count": 1, "values": [ 5.0 ] }, + { "name": "Progress", "type": "float", "count": 1, "values": [ 0.0 ] } + ] +} diff --git a/src/main/resources/licenses/BlurMC-License.txt b/src/main/resources/licenses/BlurMC-License.txt new file mode 100644 index 0000000..cfbfd83 --- /dev/null +++ b/src/main/resources/licenses/BlurMC-License.txt @@ -0,0 +1,24 @@ +Copyright (c) 2019 tterrag1098 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +---------------- + +The proof of permission is listed in this +folder, feel free to view it if you wish \ No newline at end of file diff --git a/src/main/resources/oneconfig_at.cfg b/src/main/resources/oneconfig_at.cfg new file mode 100644 index 0000000..a3826b2 --- /dev/null +++ b/src/main/resources/oneconfig_at.cfg @@ -0,0 +1,2 @@ +public net.minecraft.client.shader.ShaderGroup field_148031_d # listShaders +public net.minecraft.client.renderer.EntityRenderer func_175069_a(Lnet/minecraft/util/ResourceLocation;)V # loadShader \ No newline at end of file diff --git a/src/main/resources/post/fade_in_blur.json b/src/main/resources/post/fade_in_blur.json new file mode 100644 index 0000000..c2cab8f --- /dev/null +++ b/src/main/resources/post/fade_in_blur.json @@ -0,0 +1,67 @@ +{ + "targets": [ + "swap" + ], + "passes": [ + { + "name": "fade_in_blur", + "intarget": "minecraft:main", + "outtarget": "swap", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 1.0, 0.0 ] + }, + { + "name": "Radius", + "values": [ "@radius@.0" ] + } + ] + }, + { + "name": "fade_in_blur", + "intarget": "swap", + "outtarget": "minecraft:main", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 0.0, 1.0 ] + }, + { + "name": "Radius", + "values": [ "@radius@.0" ] + } + ] + }, + { + "name": "fade_in_blur", + "intarget": "minecraft:main", + "outtarget": "swap", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 1.0, 0.0 ] + }, + { + "name": "Radius", + "values": [ "@radius@.0" ] + } + ] + }, + { + "name": "fade_in_blur", + "intarget": "swap", + "outtarget": "minecraft:main", + "uniforms": [ + { + "name": "BlurDir", + "values": [ 0.0, 1.0 ] + }, + { + "name": "Radius", + "values": [ "@radius@.0" ] + } + ] + } + ] +} diff --git a/src/main/resources/program/fade_in_blur.fsh b/src/main/resources/program/fade_in_blur.fsh new file mode 100644 index 0000000..5539f39 --- /dev/null +++ b/src/main/resources/program/fade_in_blur.fsh @@ -0,0 +1,33 @@ +#version 120 + +uniform sampler2D DiffuseSampler; + +varying vec2 texCoord; +varying vec2 oneTexel; + +uniform vec2 InSize; + +uniform vec2 BlurDir; +uniform float Radius; +uniform float Progress; + +void main() { + vec4 blurred = vec4(0.0); + float totalStrength = 0.0; + float totalAlpha = 0.0; + float totalSamples = 0.0; + float progRadius = floor(Radius * Progress); + for(float r = -progRadius; r <= progRadius; r += 1.0) { + vec4 sample = texture2D(DiffuseSampler, texCoord + oneTexel * r * BlurDir); + + // Accumulate average alpha + totalAlpha = totalAlpha + sample.a; + totalSamples = totalSamples + 1.0; + + // Accumulate smoothed blur + float strength = 1.0 - abs(r / progRadius); + totalStrength = totalStrength + strength; + blurred = blurred + sample; + } + gl_FragColor = vec4(blurred.rgb / (progRadius * 2.0 + 1.0), totalAlpha); +} diff --git a/src/main/resources/program/fade_in_blur.json b/src/main/resources/program/fade_in_blur.json new file mode 100644 index 0000000..3d09d02 --- /dev/null +++ b/src/main/resources/program/fade_in_blur.json @@ -0,0 +1,21 @@ +{ + "blend": { + "func": "add", + "srcrgb": "one", + "dstrgb": "zero" + }, + "vertex": "sobel", + "fragment": "fade_in_blur", + "attributes": [ "Position" ], + "samplers": [ + { "name": "DiffuseSampler" } + ], + "uniforms": [ + { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] }, + { "name": "InSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "OutSize", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "BlurDir", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] }, + { "name": "Radius", "type": "float", "count": 1, "values": [ 5.0 ] }, + { "name": "Progress", "type": "float", "count": 1, "values": [ 0.0 ] } + ] +} -- cgit