Move Blur to Satin

This commit is contained in:
Pyrofab
2019-07-29 22:30:48 +02:00
parent a7c8ff7526
commit d1d9468482
13 changed files with 49 additions and 275 deletions

View File

@@ -20,11 +20,17 @@ targetCompatibility = '1.8'
minecraft {
}
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
minecraft "com.mojang:minecraft:${minecraft_version}"
mappings "net.fabricmc:yarn:${mappings_version}"
modCompile "net.fabricmc:fabric-loader:${fabric_loader_version}"
modCompile "net.fabricmc.fabric-api:fabric-api:${fabric_version}"
modCompile "com.github.Ladysnake:Satin:${satin_version}"
include "com.github.Ladysnake:Satin:${satin_version}"
implementation 'com.google.code.findbugs:jsr305:3.0.2'
}

View File

@@ -6,5 +6,8 @@ fabric_loader_version=0.4.8+build.157
fabric_version=0.3.0+build.200
mappings_version=1.14.4+build.2
# Satin library
satin_version = 1.3.1
project_id=268324
release_type=release

View File

@@ -1,42 +1,24 @@
package com.tterrag.blur;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ladysnake.satin.api.event.ShaderEffectRenderCallback;
import ladysnake.satin.api.experimental.managed.Uniform1f;
import ladysnake.satin.api.managed.ManagedShaderEffect;
import ladysnake.satin.api.managed.ShaderEffectManager;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.Identifier;
import org.apache.commons.lang3.ArrayUtils;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tterrag.blur.mixin.MixinGameRenderer;
import com.tterrag.blur.util.ReflectionHelper;
import com.tterrag.blur.util.ShaderResourcePack;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.GlUniform;
import net.minecraft.client.gl.PostProcessShader;
import net.minecraft.client.gl.ShaderEffect;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.resource.ClientResourcePackContainer;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.ResourcePackCompatibility;
import net.minecraft.resource.ResourcePackContainer;
import net.minecraft.resource.ResourcePackContainer.Factory;
import net.minecraft.resource.ResourcePackContainerManager;
import net.minecraft.resource.ResourcePackCreator;
import net.minecraft.resource.ReloadableResourceManager;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
public class Blur implements ClientModInitializer {
@@ -52,39 +34,16 @@ public class Blur implements ClientModInitializer {
String gradientEndColor = "75000000";
}
private Field _listShaders;
private long start;
public ConfigJson configs = new ConfigJson();
public int colorFirst, colorSecond;
private ShaderResourcePack dummyPack = new ShaderResourcePack();
private final ManagedShaderEffect blur = ShaderEffectManager.getInstance().manage(new Identifier(MODID, "shaders/post/fade_in_blur.json"),
shader -> shader.setUniformValue("Radius", (float) getRadius()));
private final Uniform1f blurProgress = blur.findUniform1f("Progress");
public static Blur instance;
public Blur() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
ResourcePackContainerManager<ClientResourcePackContainer> rps = ReflectionHelper.getValue(MinecraftClient.class, MinecraftClient.getInstance(), "field_1715", "resourcePackContainerManager");
rps.addCreator(new ResourcePackCreator() {
@Override
public <T extends ResourcePackContainer> void registerContainer(Map<String, T> var1, Factory<T> factory) {
NativeImage img = null;
try {
img = NativeImage.read(dummyPack.openRoot("icon.png"));
} catch (IOException e) {
LogManager.getLogger().error("Could not load blur's icon.png", e);
}
@SuppressWarnings("unchecked")
T var3 = (T) new ClientResourcePackContainer("blur", true, () -> dummyPack, new LiteralText(dummyPack.getName()), new LiteralText("Default shaders for Blur"),
ResourcePackCompatibility.COMPATIBLE, ResourcePackContainer.InsertionPosition.BOTTOM, true, img);
if (var3 != null) {
var1.put("blur", var3);
}
}
});
instance = this;
}
public static final Blur INSTANCE = new Blur();
@Override
public void onInitializeClient() {
@@ -101,24 +60,19 @@ public class Blur implements ClientModInitializer {
}
colorFirst = Integer.parseUnsignedInt(configs.gradientStartColor, 16);
colorSecond = Integer.parseUnsignedInt(configs.gradientEndColor, 16);
}
public void registerReloadListeners(ReloadableResourceManager manager) {
manager.addPack(dummyPack);
ShaderEffectRenderCallback.EVENT.register((deltaTick) -> {
if (MinecraftClient.getInstance().currentScreen != null) {
blurProgress.set(getProgress());
blur.render(deltaTick);
}
});
}
public void onScreenChange(Screen newGui) {
if (_listShaders == null) {
_listShaders = ReflectionHelper.getField(ShaderEffect.class, "field_1497", "passes");
}
if (MinecraftClient.getInstance().world != null) {
GameRenderer er = MinecraftClient.getInstance().gameRenderer;
boolean excluded = newGui == null || ArrayUtils.contains(configs.blurExclusions, newGui.getClass().getName());
if (!er.isShaderEnabled() && !excluded) {
((MixinGameRenderer) er).invokeLoadShader(new Identifier("shaders/post/fade_in_blur.json"));
if (!excluded) {
start = System.currentTimeMillis();
} else if (er.isShaderEnabled() && excluded) {
er.disableShader();
}
}
}
@@ -131,31 +85,13 @@ public class Blur implements ClientModInitializer {
return Math.min((System.currentTimeMillis() - start) / (float) configs.fadeTimeMillis, 1);
}
public void onPostRenderTick() {
if (MinecraftClient.getInstance().currentScreen != null && MinecraftClient.getInstance().gameRenderer.isShaderEnabled()) {
ShaderEffect sg = MinecraftClient.getInstance().gameRenderer.getShader();
try {
@SuppressWarnings("unchecked")
List<PostProcessShader> shaders = (List<PostProcessShader>) _listShaders.get(sg);
for (PostProcessShader s : shaders) {
GlUniform su = s.getProgram().getUniformByName("Progress");
if (su != null) {
su.set(getProgress());
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public int getBackgroundColor(boolean second) {
int color = second ? colorSecond : colorFirst;
int a = color >>> 24;
int r = (color >> 16) & 0xFF;
int b = (color >> 8) & 0xFF;
int g = color & 0xFF;
float prog = instance.getProgress();
float prog = INSTANCE.getProgress();
a *= prog;
r *= prog;
g *= prog;

View File

@@ -1,15 +0,0 @@
package com.tterrag.blur.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.util.Identifier;
@Mixin(GameRenderer.class)
public interface MixinGameRenderer {
@Invoker
void invokeLoadShader(Identifier loc);
}

View File

@@ -21,23 +21,6 @@ public class MixinMinecraftClient {
target = "Lnet/minecraft/client/MinecraftClient;currentScreen:Lnet/minecraft/client/gui/screen/Screen;",
opcode = Opcodes.PUTFIELD))
public void onScreenOpen(Screen newScreen, CallbackInfo info) {
Blur.instance.onScreenChange(newScreen);
}
@Inject(method = "render",
at = @At(value = "INVOKE",
target = "net/minecraft/client/toast/ToastManager.draw()V"),
require = 1)
public void onPostRenderTick(CallbackInfo info) {
Blur.instance.onPostRenderTick();
}
@Inject(method = "init()V",
at = @At(value = "FIELD",
target = "Lnet/minecraft/client/MinecraftClient;resourceManager:Lnet/minecraft/resource/ReloadableResourceManager;",
opcode = Opcodes.PUTFIELD,
shift = Shift.AFTER))
public void onResourceManagerAssign(CallbackInfo info) {
Blur.instance.registerReloadListeners((ReloadableResourceManager) MinecraftClient.getInstance().getResourceManager());
Blur.INSTANCE.onScreenChange(newScreen);
}
}

View File

@@ -15,13 +15,13 @@ public class MixinScreen {
method = "renderBackground(I)V",
constant = @Constant(intValue = -1072689136))
public int getFirstBackgroundColor(int color) {
return Blur.instance.getBackgroundColor(false);
return Blur.INSTANCE.getBackgroundColor(false);
}
@ModifyConstant(
method = "renderBackground(I)V",
constant = @Constant(intValue = -804253680))
public int getSecondBackgroundColor(int color) {
return Blur.instance.getBackgroundColor(true);
return Blur.INSTANCE.getBackgroundColor(true);
}
}

View File

@@ -1,36 +0,0 @@
package com.tterrag.blur.util;
import java.lang.reflect.Field;
import java.util.Arrays;
public class ReflectionHelper {
@SuppressWarnings("unchecked")
public static <T> T getValue(Class<?> cls, Object instance, String...names) {
try {
return (T) getField(cls, names).get(instance);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static Field getField(Class<?> cls, String... names) {
for (String name : names) {
Field f = getFieldInternal(cls, name);
if (f != null) {
return f;
}
}
throw new IllegalArgumentException("Could not find any of fields " + Arrays.toString(names) + " on class " + cls);
}
private static Field getFieldInternal(Class<?> cls, String name) {
try {
Field f = cls.getDeclaredField(name);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException | SecurityException e) {
return null;
}
}
}

View File

@@ -1,102 +0,0 @@
package com.tterrag.blur.util;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Predicate;
import com.google.common.collect.ImmutableSet;
import com.tterrag.blur.Blur;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceReloadListener;
import net.minecraft.resource.ResourceType;
import net.minecraft.resource.metadata.PackResourceMetadata;
import net.minecraft.resource.metadata.ResourceMetadataReader;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
public class ShaderResourcePack implements ResourcePack, ResourceReloadListener {
protected boolean validPath(Identifier location) {
return location.getNamespace().equals("minecraft") && location.getPath().startsWith("shaders/");
}
private final Map<Identifier, String> loadedData = new HashMap<>();
@Override
public InputStream open(ResourceType type, Identifier location) throws IOException {
if (type == ResourceType.CLIENT_RESOURCES && validPath(location)) {
String s = loadedData.computeIfAbsent(location, loc -> {
InputStream in = Blur.class.getResourceAsStream("/" + location.getPath());
StringBuilder data = new StringBuilder();
Scanner scan = new Scanner(in);
try {
while (scan.hasNextLine()) {
data.append(scan.nextLine().replaceAll("@radius@", Integer.toString(Blur.instance.getRadius()))).append('\n');
}
} finally {
scan.close();
}
return data.toString();
});
return new ByteArrayInputStream(s.getBytes());
}
throw new FileNotFoundException(location.toString());
}
@Override
public boolean contains(ResourceType type, Identifier location) {
return type == ResourceType.CLIENT_RESOURCES && validPath(location) && Blur.class.getResource("/" + location.getPath()) != null;
}
@Override
public Set<String> getNamespaces(ResourceType type) {
return ImmutableSet.of("minecraft");
}
@SuppressWarnings({ "unchecked", "null" })
@Override
public <T> T parseMetadata(ResourceMetadataReader<T> var1) throws IOException {
if ("pack".equals(var1.getKey())) {
return (T) new PackResourceMetadata(new LiteralText("Blur's default shaders"), 4);
}
return null;
}
@Override
public String getName() {
return "Blur Shaders";
}
@Override
public CompletableFuture<Void> reload(Synchronizer var1, ResourceManager var2, Profiler var3, Profiler var4, Executor var5, Executor var6) {
return new CompletableFuture<>().thenRun(loadedData::clear);
}
@Override
public void close() throws IOException {}
@Override
public InputStream openRoot(String var1) throws IOException {
return Blur.class.getResourceAsStream("/assets/blur/" + var1);
}
@Override
public Collection<Identifier> findResources(ResourceType var1, String var2, int var3, Predicate<String> var4) {
return Collections.emptyList();
}
}

View File

@@ -4,7 +4,7 @@
],
"passes": [
{
"name": "fade_in_blur",
"name": "blur:fade_in_blur",
"intarget": "minecraft:main",
"outtarget": "swap",
"uniforms": [
@@ -14,12 +14,12 @@
},
{
"name": "Radius",
"values": [ @radius@.0 ]
"values": [ 8.0 ]
}
]
},
{
"name": "fade_in_blur",
"name": "blur:fade_in_blur",
"intarget": "swap",
"outtarget": "minecraft:main",
"uniforms": [
@@ -29,12 +29,12 @@
},
{
"name": "Radius",
"values": [ @radius@.0 ]
"values": [ 8.0 ]
}
]
},
{
"name": "fade_in_blur",
"name": "blur:fade_in_blur",
"intarget": "minecraft:main",
"outtarget": "swap",
"uniforms": [
@@ -44,12 +44,12 @@
},
{
"name": "Radius",
"values": [ @radius@.0 ]
"values": [ 8.0 ]
}
]
},
{
"name": "fade_in_blur",
"name": "blur:fade_in_blur",
"intarget": "swap",
"outtarget": "minecraft:main",
"uniforms": [
@@ -59,7 +59,7 @@
},
{
"name": "Radius",
"values": [ @radius@.0 ]
"values": [ 8.0 ]
}
]
}

View File

@@ -5,7 +5,7 @@
"dstrgb": "zero"
},
"vertex": "sobel",
"fragment": "fade_in_blur",
"fragment": "blur:fade_in_blur",
"attributes": [ "Position" ],
"samplers": [
{ "name": "DiffuseSampler" }

View File

@@ -8,7 +8,7 @@
"icon": "assets/blur/icon.png",
"entrypoints": {
"client": [
"com.tterrag.blur.Blur"
"com.tterrag.blur.Blur::INSTANCE"
]
},
"contact": {

View File

@@ -4,8 +4,7 @@
"compatibilityLevel": "JAVA_8",
"client": [
"MixinScreen",
"MixinMinecraftClient",
"MixinGameRenderer"
"MixinMinecraftClient"
],
"injectors": {
"defaultRequire": 1