mirror of
https://github.com/Motschen/Blur.git
synced 2025-12-17 12:15:10 +01:00
Rewrite large parts of the mod
Blur is now more alive than ever, as I've been working on a new update: Blur+ 🪩 The mod's goal now is to improve upon the vanilla blur effect, adding smooth & configurable animations, the ability to apply it to containers (such as the inventory, chests, etc.), as well as customizable gradient backgrounds. 🌈
This commit is contained in:
74
src/main/java/eu/midnightdust/blur/Blur.java
Normal file
74
src/main/java/eu/midnightdust/blur/Blur.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package eu.midnightdust.blur;
|
||||
|
||||
import eu.midnightdust.blur.config.BlurConfig;
|
||||
import eu.midnightdust.lib.util.MidnightColorUtil;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
|
||||
public class Blur implements ClientModInitializer {
|
||||
|
||||
public static long start;
|
||||
public static float progress;
|
||||
|
||||
public static boolean prevScreenHasBlur;
|
||||
public static boolean screenHasBlur;
|
||||
|
||||
public static boolean prevScreenHasBackground;
|
||||
public static boolean screenHasBackground;
|
||||
|
||||
public static boolean doTest = true;
|
||||
public static boolean screenChanged = true;
|
||||
public static long lastScreenChange = System.currentTimeMillis();
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
BlurConfig.init("blur", BlurConfig.class);
|
||||
}
|
||||
|
||||
public static boolean doFade = false;
|
||||
|
||||
public static void onScreenChange() {
|
||||
if (screenHasBlur) {
|
||||
if (doFade) {
|
||||
start = System.currentTimeMillis();
|
||||
doFade = false;
|
||||
}
|
||||
} else if (prevScreenHasBlur && BlurConfig.fadeOutTimeMillis > 0) {
|
||||
start = System.currentTimeMillis();
|
||||
doFade = true;
|
||||
} else {
|
||||
start = -1;
|
||||
doFade = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateProgress(boolean fadeIn) {
|
||||
float x;
|
||||
if (fadeIn) {
|
||||
x = Math.min((System.currentTimeMillis() - start) / (float) BlurConfig.fadeTimeMillis, 1);
|
||||
if (BlurConfig.animationCurve.equals(BlurConfig.AnimationCurve.EASE)) x *= (2 - x); // easeInCubic
|
||||
}
|
||||
else {
|
||||
x = Math.max(1 + (start - System.currentTimeMillis()) / (float) BlurConfig.fadeOutTimeMillis, 0);
|
||||
if (BlurConfig.animationCurve.equals(BlurConfig.AnimationCurve.EASE)) x *= (2 - x); // easeOutCubic
|
||||
if (x <= 0) {
|
||||
start = -1;
|
||||
x = 0;
|
||||
}
|
||||
}
|
||||
Blur.progress = x;
|
||||
}
|
||||
|
||||
public static int getBackgroundColor(boolean second) {
|
||||
int a = second ? BlurConfig.gradientEndAlpha : BlurConfig.gradientStartAlpha;
|
||||
var col = MidnightColorUtil.hex2Rgb(second ? BlurConfig.gradientEnd : BlurConfig.gradientStart);
|
||||
int r = (col.getRGB() >> 16) & 0xFF;
|
||||
int b = (col.getRGB() >> 8) & 0xFF;
|
||||
int g = col.getRGB() & 0xFF;
|
||||
float prog = progress;
|
||||
a *= prog;
|
||||
r *= prog;
|
||||
g *= prog;
|
||||
b *= prog;
|
||||
return a << 24 | r << 16 | b << 8 | g;
|
||||
}
|
||||
}
|
||||
32
src/main/java/eu/midnightdust/blur/config/BlurConfig.java
Normal file
32
src/main/java/eu/midnightdust/blur/config/BlurConfig.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package eu.midnightdust.blur.config;
|
||||
|
||||
import eu.midnightdust.lib.config.MidnightConfig;
|
||||
|
||||
public class BlurConfig extends MidnightConfig {
|
||||
public static final String ANIMATIONS = "animations";
|
||||
public static final String STYLE = "style";
|
||||
@Entry @Hidden public static int configVersion = 2;
|
||||
|
||||
@Entry(category = STYLE)
|
||||
public static boolean blurContainers = true;
|
||||
@Entry(category = ANIMATIONS, min = 0, max = 2000, isSlider = true)
|
||||
public static int fadeTimeMillis = 200;
|
||||
@Entry(category = ANIMATIONS, min = 0, max = 2000, isSlider = true)
|
||||
public static int fadeOutTimeMillis = 200;
|
||||
@Entry(category = ANIMATIONS)
|
||||
public static AnimationCurve animationCurve = AnimationCurve.EASE;
|
||||
@Entry(category = STYLE)
|
||||
public static boolean useGradient = true;
|
||||
@Entry(category = STYLE, isColor = true, width = 7, min = 7)
|
||||
public static String gradientStart = "#000000";
|
||||
@Entry(category = STYLE, isSlider = true, min = 0, max = 255)
|
||||
public static int gradientStartAlpha = 75;
|
||||
@Entry(category = STYLE, isColor = true, width = 7, min = 7)
|
||||
public static String gradientEnd = "#000000";
|
||||
@Entry(category = STYLE, isSlider = true, min = 0, max = 255)
|
||||
public static int gradientEndAlpha = 75;
|
||||
|
||||
public enum AnimationCurve {
|
||||
EASE, FLAT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package eu.midnightdust.blur.mixin;
|
||||
|
||||
import eu.midnightdust.blur.Blur;
|
||||
import net.minecraft.client.render.GameRenderer;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||
|
||||
@Mixin(GameRenderer.class)
|
||||
public class MixinGameRenderer {
|
||||
@ModifyVariable(method = "renderBlur", at = @At("STORE"), ordinal = 1)
|
||||
private float blur$modifyRadius(float radius) { // Modify the radius based on the animation progress
|
||||
|
||||
if (!Blur.screenChanged && Blur.start >= 0) // Only update the progress after all tests have been completed
|
||||
Blur.updateProgress(Blur.screenHasBlur);
|
||||
return radius * Blur.progress;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package eu.midnightdust.blur.mixin;
|
||||
|
||||
import eu.midnightdust.blur.config.BlurConfig;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
||||
import net.minecraft.text.Text;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(HandledScreen.class)
|
||||
public class MixinHandledScreen extends Screen {
|
||||
protected MixinHandledScreen(Text title) {
|
||||
super(title);
|
||||
}
|
||||
|
||||
@Inject(method = "renderBackground", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawBackground(Lnet/minecraft/client/gui/DrawContext;FII)V", shift = At.Shift.BEFORE))
|
||||
private void blur$renderContainerBlur(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) { // Applies the blur effect in containers (Inventory, Chest, etc.)
|
||||
if (BlurConfig.blurContainers) this.applyBlur(delta);
|
||||
}
|
||||
}
|
||||
29
src/main/java/eu/midnightdust/blur/mixin/MixinInGameHud.java
Normal file
29
src/main/java/eu/midnightdust/blur/mixin/MixinInGameHud.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package eu.midnightdust.blur.mixin;
|
||||
|
||||
import eu.midnightdust.blur.Blur;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.hud.InGameHud;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(InGameHud.class)
|
||||
public class MixinInGameHud {
|
||||
@Final @Shadow private MinecraftClient client;
|
||||
|
||||
@Inject(at = @At("TAIL"), method = "render")
|
||||
public void blur$renderFadeOut(DrawContext context, float delta, CallbackInfo ci) { // Adds a fade-out effect when a player is in a world and closes all screens
|
||||
if (client.currentScreen == null && client.world != null && Blur.start >= 0 && Blur.prevScreenHasBlur) {
|
||||
Blur.doTest = false;
|
||||
Blur.screenChanged = false;
|
||||
this.client.gameRenderer.renderBlur(delta);
|
||||
this.client.getFramebuffer().beginWrite(false);
|
||||
|
||||
if (Blur.prevScreenHasBackground) context.fillGradient(0, 0, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight(), Blur.getBackgroundColor(false), Blur.getBackgroundColor(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/main/java/eu/midnightdust/blur/mixin/MixinMinecraftClient.java
Executable file
37
src/main/java/eu/midnightdust/blur/mixin/MixinMinecraftClient.java
Executable file
@@ -0,0 +1,37 @@
|
||||
package eu.midnightdust.blur.mixin;
|
||||
|
||||
import eu.midnightdust.blur.Blur;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
|
||||
@Mixin(MinecraftClient.class)
|
||||
public class MixinMinecraftClient {
|
||||
|
||||
@Inject(method = "setScreen",
|
||||
at = @At(value = "FIELD",
|
||||
target = "Lnet/minecraft/client/MinecraftClient;currentScreen:Lnet/minecraft/client/gui/screen/Screen;",
|
||||
opcode = Opcodes.PUTFIELD))
|
||||
private void blur$onScreenOpen(Screen newScreen, CallbackInfo info) {
|
||||
if (Blur.lastScreenChange < System.currentTimeMillis() - 100) { // For some reason, in certain scenarios the screen is set to a new one multiple times in a tick. We want to avoid that.
|
||||
|
||||
// Here, we reset all tests, to check if the new screen has blur and/or a background
|
||||
Blur.prevScreenHasBlur = Blur.screenHasBlur;
|
||||
Blur.prevScreenHasBackground = Blur.screenHasBackground;
|
||||
Blur.screenHasBlur = false;
|
||||
Blur.screenHasBackground = false;
|
||||
Blur.doTest = true;
|
||||
Blur.screenChanged = true;
|
||||
Blur.start = -1;
|
||||
Blur.lastScreenChange = System.currentTimeMillis();
|
||||
|
||||
// Manually activate the onScreenChange method when all screens are closed (in-game)
|
||||
if (newScreen == null) Blur.onScreenChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
83
src/main/java/eu/midnightdust/blur/mixin/MixinScreen.java
Executable file
83
src/main/java/eu/midnightdust/blur/mixin/MixinScreen.java
Executable file
@@ -0,0 +1,83 @@
|
||||
package eu.midnightdust.blur.mixin;
|
||||
|
||||
import eu.midnightdust.blur.config.BlurConfig;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.text.Text;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Constant;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyConstant;
|
||||
|
||||
import eu.midnightdust.blur.Blur;
|
||||
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(Screen.class)
|
||||
public abstract class MixinScreen {
|
||||
|
||||
@Shadow protected MinecraftClient client;
|
||||
|
||||
@Shadow @Final protected Text title;
|
||||
|
||||
@Shadow public abstract void renderInGameBackground(DrawContext context);
|
||||
|
||||
// @Unique
|
||||
// private final Text blurConfig = Text.translatable("blur.midnightconfig.title");
|
||||
|
||||
// @Inject(at = @At("HEAD"), method = "tick")
|
||||
// private void blur$reloadShader(CallbackInfo ci) {
|
||||
// if (this.client != null && this.title.equals(blurConfig)) {
|
||||
// //Blur.onScreenChange();
|
||||
// }
|
||||
// }
|
||||
@Inject(at = @At("HEAD"), method = "render")
|
||||
public void blur$processScreenChange(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
|
||||
if (!Blur.doTest && Blur.screenChanged) { // After the tests for blur and background color have been completed
|
||||
Blur.onScreenChange();
|
||||
Blur.screenChanged = false;
|
||||
}
|
||||
|
||||
if (Blur.start >= 0 && !Blur.screenHasBlur && Blur.prevScreenHasBlur) { // Fade out in non-blurred screens
|
||||
this.client.gameRenderer.renderBlur(delta);
|
||||
this.client.getFramebuffer().beginWrite(false);
|
||||
|
||||
if (Blur.prevScreenHasBackground) context.fillGradient(0, 0, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight(), Blur.getBackgroundColor(false), Blur.getBackgroundColor(true));
|
||||
}
|
||||
Blur.doTest = false; // Set the test state to completed, as tests will happen in the same tick.
|
||||
}
|
||||
@Inject(at = @At("HEAD"), method = "renderInGameBackground")
|
||||
public void blur$getBackgroundEnabled(DrawContext context, CallbackInfo ci) {
|
||||
Blur.screenHasBackground = true; // Test if the screen has a background
|
||||
}
|
||||
@Inject(at = @At("HEAD"), method = "applyBlur")
|
||||
public void blur$getBlurEnabled(float delta, CallbackInfo ci) {
|
||||
Blur.screenHasBlur = true; // Test if the screen has blur
|
||||
}
|
||||
|
||||
@Inject(at = @At("HEAD"), method = "renderDarkening(Lnet/minecraft/client/gui/DrawContext;IIII)V", cancellable = true)
|
||||
public void blur$applyGradient(DrawContext context, int x, int y, int width, int height, CallbackInfo ci) {
|
||||
if (BlurConfig.useGradient) { // Replaces the background texture with a gradient
|
||||
renderInGameBackground(context);
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@ModifyConstant(
|
||||
method = "renderInGameBackground",
|
||||
constant = @Constant(intValue = -1072689136))
|
||||
private int blur$getFirstBackgroundColor(int color) {
|
||||
return Blur.getBackgroundColor(false);
|
||||
}
|
||||
|
||||
@ModifyConstant(
|
||||
method = "renderInGameBackground",
|
||||
constant = @Constant(intValue = -804253680))
|
||||
private int blur$getSecondBackgroundColor(int color) {
|
||||
return Blur.getBackgroundColor(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user