Skip to content

Commit

Permalink
Implement Intro Sound Effect and Blur effect (Credit: mineblock11)
Browse files Browse the repository at this point in the history
- Include Blur Shader Program (Credit: @glisco)

Co-Authored-By: Calum <93472213+mineblock11@users.noreply.github.com>
  • Loading branch information
Dragon-Seeker and IMB11 committed Aug 18, 2023
1 parent e5063f4 commit 3e489d5
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 5 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ allprojects {
url "https://maven.terraformersmc.com/releases/"
}
maven {
url = "https://api.modrinth.com/maven"
name = "Modrinth"
url "https://api.modrinth.com/maven"
content { includeGroup "maven.modrinth" }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package com.minenash.seamless_loading_screen;

import com.minenash.seamless_loading_screen.config.Config;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.DrawableHelper;
import com.mojang.logging.LogUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.render.*;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.joml.Matrix4f;
import org.joml.Vector4f;
import org.slf4j.Logger;

import java.util.function.Consumer;

public class FadeScreen extends Screen {
private static final Logger LOGGER = LogUtils.getLogger();

private final int fadeFrames;
private int frames;
private Consumer<Boolean> callback;
Expand All @@ -36,6 +47,21 @@ public void removed() {
super.removed();
}

@Override
protected void init() {
if(Config.playSoundEffect) {
var id = Identifier.tryParse(Config.soundEffect);

if(id != null) {
SoundEvent soundEvent = Registries.SOUND_EVENT.getOrEmpty(id).orElse(SoundEvents.ENTITY_ENDER_DRAGON_GROWL);

MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(soundEvent, Config.soundPitch, Config.soundVolume));
} else {
LOGGER.error("[SeamlessLoadingScreen]: Unable to parse the above SoundEffect due to it not being a valid Identifier. [Value: {}]", Config.soundEffect);
}
}
}

private void markDone(boolean forceClosed) {
if(this.done) return;

Expand Down Expand Up @@ -63,7 +89,7 @@ public void render(MatrixStack stack, int mouseX, int mouseY, float delta) {

boolean doFade = frames <= fadeFrames;

float alpha = doFade ? (float) frames / fadeFrames : 1.0f;
float alpha = doFade ? Math.min(frames / (float) fadeFrames, 1.0f) : 1.0f;

Vector4f color = new Vector4f(1, 1, 1, alpha);

Expand Down Expand Up @@ -92,7 +118,7 @@ public void render(MatrixStack stack, int mouseX, int mouseY, float delta) {
loadQuad(stack, color, 0, 0, width, height, 0, 0, width/32f, height/32f).draw();
}

ScreenshotLoader.renderTint(this, stack, alpha);
ScreenshotLoader.renderAfterEffects(this, stack, alpha);

if (!doFade) {
DrawableHelper.drawCenteredTextWithShadow(stack, client.textRenderer, title, width / 2, 70, 0xFFFFFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
import com.mojang.logging.LogUtils;
import net.minecraft.SharedConstants;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gl.GlUniform;
import net.minecraft.client.gl.ShaderProgram;
import net.minecraft.client.gl.SimpleFramebuffer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.NativeImageBackedTexture;
import net.minecraft.client.util.Window;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import org.lwjgl.opengl.GL30;
import org.slf4j.Logger;

import java.awt.*;
Expand Down Expand Up @@ -93,12 +102,22 @@ private static String cleanFileName(String fileName) {
public static boolean replacebg = false;

public static void render(Screen screen, MatrixStack stack) {
RenderSystem.enableBlend();

RenderSystem.setShader(GameRenderer::getPositionTexProgram);
RenderSystem.setShaderTexture(0, SCREENSHOT);

int w = (int) (imageRatio * screen.height);
DrawableHelper.drawTexture(stack, screen.width / 2 - w / 2, 0, 0.0F, 0.0F, w, screen.height, w, screen.height);
renderTint(screen, stack, 1f);
renderAfterEffects(screen, stack, 1f);
}

public static void renderAfterEffects(Screen screen, MatrixStack stack, float fadeValue){
renderTint(screen, stack, fadeValue);

if(Config.enableScreenshotBlur && SeamlessLoadingScreen.BLUR_PROGRAM.loaded) {
renderBlur(screen, stack, Config.screenshotBlurStrength * fadeValue, Config.screenshotBlurQuality);
}
}

public static void renderTint(Screen screen, MatrixStack stack, float fadeValue){
Expand All @@ -107,7 +126,7 @@ public static void renderTint(Screen screen, MatrixStack stack, float fadeValue)
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int alpha = Math.round((Config.tintStrength * 255) * fadeValue);
int alpha = Math.round(255 * (Config.tintStrength * fadeValue));

int argb_color = getArgb(alpha, red, green, blue);

Expand All @@ -124,4 +143,85 @@ public static Color hex2Rgb(String colorStr) {
} catch (Exception ignored) {}
return Color.BLACK;
}

//-----

public static void renderBlur(Screen screen, MatrixStack stack, float size, float quality){
var buffer = Tessellator.getInstance().getBuffer();
var matrix = stack.peek().getPositionMatrix();

buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION);
buffer.vertex(matrix, 0, 0, 0).next();
buffer.vertex(matrix, 0, screen.height, 0).next();
buffer.vertex(matrix, screen.width, screen.height, 0).next();
buffer.vertex(matrix, screen.width, 0, 0).next();

SeamlessLoadingScreen.BLUR_PROGRAM.setParameters(16, quality, size);
SeamlessLoadingScreen.BLUR_PROGRAM.use();

Tessellator.getInstance().draw();
}

/**
* Credit to glisco for <a href="https://github.com/wisp-forest/owo-lib/blob/1.20/src/main/java/io/wispforest/owo/shader/BlurProgram.java">BlurProgram</a>
* <p>
* Altered for use with Multi loader
*/
public static class BlurHelper {
private GlUniform inputResolution;
private GlUniform directions;
private GlUniform quality;
private GlUniform size;
private Framebuffer input;

private ShaderProgram backingProgram;

public boolean loaded = false;

public void onWindowResize(MinecraftClient client, Window window){
if (this.input == null) return;
this.input.resize(window.getFramebufferWidth(), window.getFramebufferHeight(), MinecraftClient.IS_SYSTEM_MAC);
}

public void load(ShaderProgram backingProgram){
this.backingProgram = backingProgram;
this.setup();

this.loaded = true;
}

public void setParameters(int directions, float quality, float size) {
this.directions.set((float) directions);
this.size.set(size);
this.quality.set(quality);
}

public void use() {
Framebuffer buffer = MinecraftClient.getInstance().getFramebuffer();

this.input.beginWrite(false);
GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, buffer.fbo);
GL30.glBlitFramebuffer(0, 0, buffer.textureWidth, buffer.textureHeight, 0, 0, buffer.textureWidth, buffer.textureHeight, GL30.GL_COLOR_BUFFER_BIT, GL30.GL_LINEAR);
buffer.beginWrite(false);

this.inputResolution.set((float) buffer.textureWidth, (float) buffer.textureHeight);
this.backingProgram.addSampler("InputSampler", this.input.getColorAttachment());

RenderSystem.setShader(() -> this.backingProgram);
}

protected void setup() {
this.inputResolution = this.findUniform("InputResolution");
this.directions = this.findUniform("Directions");
this.quality = this.findUniform("Quality");
this.size = this.findUniform("Size");

Window window = MinecraftClient.getInstance().getWindow();
this.input = new SimpleFramebuffer(window.getFramebufferWidth(), window.getFramebufferHeight(), false, MinecraftClient.IS_SYSTEM_MAC);
}

private GlUniform findUniform(String key){
return backingProgram.getUniform(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public class SeamlessLoadingScreen {

public static boolean changeWorldJoinScreen = false;

public static ScreenshotLoader.BlurHelper BLUR_PROGRAM;

public static void onInitializeClient() {
MidnightConfig.init(MODID, Config.class);

SeamlessLoadingScreen.BLUR_PROGRAM = new ScreenshotLoader.BlurHelper();

try {
Path path = PlatformFunctions.getGameDir().resolve("screenshots/worlds");
Files.createDirectories(path.resolve("singleplayer"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.minenash.seamless_loading_screen.config;

import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;

public class Config extends MidnightConfig {

@Entry(min = 0)
Expand All @@ -14,6 +17,22 @@ public class Config extends MidnightConfig {
@Entry(min = 0f, max = 1f, isSlider = true)
public static float tintStrength = 0.3f;

@Entry
public static boolean enableScreenshotBlur = false;
@Entry(min = 1f, max = 16f, isSlider = true)
public static float screenshotBlurStrength = 1f;
@Entry(min = 1f, max = 16f, isSlider = true)
public static float screenshotBlurQuality = 5f;

@Entry
public static boolean playSoundEffect = false;
@Entry
public static String soundEffect = "minecraft:ui.toast.out";
@Entry(min = 0f, max = 10f, isSlider = true)
public static float soundPitch = 1f;
@Entry(min = 0f, max = 10f, isSlider = true)
public static float soundVolume = 1f;

@Entry public static ScreenshotResolution resolution = ScreenshotResolution.Normal;
@Entry public static boolean disableCamera = true;
@Entry public static boolean archiveScreenshots = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.minenash.seamless_loading_screen.config.Config;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.*;
import net.minecraft.client.util.Window;
import net.minecraft.client.world.ClientWorld;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand Down Expand Up @@ -61,4 +63,17 @@ private void onWindowClose(CallbackInfo info) {
info.cancel();
}

//----

@Shadow
@Final
private Window window;

@Inject(method = "onResolutionChanged", at = @At("TAIL"))
private void captureResize(CallbackInfo ci) {
if(SeamlessLoadingScreen.BLUR_PROGRAM.loaded) {
SeamlessLoadingScreen.BLUR_PROGRAM.onWindowResize((MinecraftClient) (Object) this, window);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
"seamless_loading_screen.midnightconfig.updateWorldIcon": "Update World Icon",
"seamless_loading_screen.midnightconfig.boolean.true": "True",
"seamless_loading_screen.midnightconfig.boolean.false": "False",

"seamless_loading_screen.midnightconfig.soundEffect": "Sound Effect",
"seamless_loading_screen.midnightconfig.playSoundEffect": "Enable Sound Effect",
"seamless_loading_screen.midnightconfig.soundPitch": "Sound Pitch",
"seamless_loading_screen.midnightconfig.soundVolume": "Sound Volume",
"seamless_loading_screen.midnightconfig.screenshotBlurStrength": "Background Blur Strength",
"seamless_loading_screen.midnightconfig.screenshotBlurQuality": "Background Blur Quality",
"seamless_loading_screen.midnightconfig.enableScreenshotBlur": "Enable Background Blur",
"seamless_loading_screen.midnightconfig.tintColor": "Tint Color (RGB)",
"seamless_loading_screen.midnightconfig.tintStrength": "Tint Strength",

"seamless_loading_screen.midnightconfig.resolution": "Screenshot Resolution",
"seamless_loading_screen.midnightconfig.resolution.tooltip": "Native - Optifine TAA compatibility, game's resolution\nNormal - Supports up to 21:9 ultrawide monitors\n4K - For 4K monitors\n8K - For 8K monitors",
"seamless_loading_screen.midnightconfig.enum.ScreenshotResolution.Native": "Native",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 150

uniform sampler2D InputSampler;
uniform vec2 InputResolution;
uniform vec4 ColorModulator;

uniform float Directions;
uniform float Quality;
uniform float Size;

out vec4 fragColor;

// Copy of https://github.com/wisp-forest/owo-lib/blob/1.20/src/main/resources/assets/owo/shaders/core/blur.fsh from owo
// shader adapted from https://www.shadertoy.com/view/Xltfzj

void main() {
#define TAU 6.28318530718

vec2 Radius = Size / InputResolution.xy;

// Normalized pixel coordinates (from 0 to 1)
vec2 uv = gl_FragCoord.xy / InputResolution.xy;
// Pixel colour
vec4 Color = texture(InputSampler, uv);

// Blur calculations
for (float d = 0.0; d < TAU; d += TAU / Directions) {
for (float i = 1.0 / Quality; i <= 1.0; i += 1.0 / Quality) {
Color += texture(InputSampler, uv + vec2(cos(d), sin(d)) * Radius * i);
}
}

// Output to screen
Color /= Quality * Directions;
fragColor = Color * ColorModulator;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "position",
"fragment": "seamless_loading_screen:blur",
"attributes": [],
"samplers": [
{ "name": "InputSampler" }
],
"uniforms": [
{ "name": "ModelViewMat", "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": "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": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] },
{ "name": "InputResolution", "type": "float", "count": 2, "values": [ 1.0, 1.0 ] },
{ "name": "Directions", "type": "float", "count": 1, "values": [ 1.0] },
{ "name": "Quality", "type": "float", "count": 1, "values": [ 1.0] },
{ "name": "Size", "type": "float", "count": 1, "values": [ 1.0] }
]
}
5 changes: 5 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ dependencies {
exclude module: "fabric-api"
}

// modLocalRuntime ("maven.modrinth:bedrockify:1.9+mc1.20")
// modLocalRuntime ("me.shedaniel.cloth:cloth-config-fabric:11.0.98", { exclude group: "net.fabricmc.fabric-api" })

//modLocalRuntime ("maven.modrinth:fastload:3.4.0")

common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionFabric")) { transitive false }
}
Expand Down
Loading

0 comments on commit 3e489d5

Please sign in to comment.