Add blur fade-in

This commit is contained in:
tterrag1098
2017-05-24 20:06:54 -04:00
parent 53c03382a2
commit 8bd87e14c5
4 changed files with 99 additions and 5 deletions

View File

@@ -1,12 +1,19 @@
package com.tterrag.blurbg;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import com.google.common.base.Throwables;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.shader.Shader;
import net.minecraft.client.shader.ShaderGroup;
import net.minecraft.client.shader.ShaderUniform;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
@@ -15,11 +22,18 @@ import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.RenderTickEvent;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
@Mod(modid = "blurbg", name = "BlurBG", version = "@VERSION@", acceptedMinecraftVersions = "[1.9, 1.12)")
public class BlurBG {
private String[] blurExclusions;
private Field _listShaders;
private long start;
private int fadeTime;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
@@ -31,20 +45,46 @@ public class BlurBG {
GuiChat.class.getName(),
}, "A list of classes to be excluded from the blur shader.");
fadeTime = config.getInt("fadeTime", Configuration.CATEGORY_GENERAL, 300, 0, Integer.MAX_VALUE, "The time it takes for the blur to fade in, in ms.");
config.save();
}
@SuppressWarnings("null")
@SubscribeEvent
public void onGuiChange(GuiOpenEvent event) {
if (_listShaders == null) {
_listShaders = ReflectionHelper.findField(ShaderGroup.class, "field_148031_d", "listShaders");
}
if (Minecraft.getMinecraft().world != null) {
EntityRenderer er = Minecraft.getMinecraft().entityRenderer;
if (!er.isShaderActive() && event.getGui() != null && !ArrayUtils.contains(blurExclusions, event.getGui().getClass().getName())) {
er.loadShader(new ResourceLocation("blurbg", "shaders/post/blur.json"));
er.loadShader(new ResourceLocation("shaders/post/fade_in_blur.json"));
start = System.currentTimeMillis();
} else if (er.isShaderActive() && event.getGui() == null) {
er.stopUseShader();
}
}
}
@SuppressWarnings("null")
@SubscribeEvent
public void onRenderTick(RenderTickEvent event) {
if (event.phase == Phase.END && Minecraft.getMinecraft().currentScreen != null && Minecraft.getMinecraft().entityRenderer.isShaderActive()) {
ShaderGroup sg = Minecraft.getMinecraft().entityRenderer.getShaderGroup();
try {
@SuppressWarnings("unchecked")
List<Shader> shaders = (List<Shader>) _listShaders.get(sg);
for (Shader s : shaders) {
ShaderUniform su = s.getShaderManager().getShaderUniform("Progress");
if (su != null) {
su.set(Math.min((System.currentTimeMillis() - start) / (float) fadeTime, 1));
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
Throwables.propagate(e);
}
}
}
}

View File

@@ -4,7 +4,7 @@
],
"passes": [
{
"name": "blur",
"name": "fade_in_blur",
"intarget": "minecraft:main",
"outtarget": "swap",
"uniforms": [
@@ -19,7 +19,7 @@
]
},
{
"name": "blur",
"name": "fade_in_blur",
"intarget": "swap",
"outtarget": "minecraft:main",
"uniforms": [
@@ -34,7 +34,7 @@
]
},
{
"name": "blur",
"name": "fade_in_blur",
"intarget": "minecraft:main",
"outtarget": "swap",
"uniforms": [
@@ -49,7 +49,7 @@
]
},
{
"name": "blur",
"name": "fade_in_blur",
"intarget": "swap",
"outtarget": "minecraft:main",
"uniforms": [

View File

@@ -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);
}

View File

@@ -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 ] }
]
}