Skip to content

Commit

Permalink
misc / fix: make phosphor blur work alongside other shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyvest committed Aug 18, 2022
1 parent 3772e11 commit 546e229
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod_name = PolyBlur
mod_id = polyblur
mod_version = 1.0.0-alpha3
mod_version = 1.0.0-alpha4

essential.defaults.loom=0

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/cc/polyfrost/polyblur/PolyBlur.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class PolyBlur {

@Mod.EventHandler
public void init(FMLInitializationEvent event) {
config.initialize();

CommandManager.INSTANCE.registerCommand(PolyBlurCommand.class);
EventManager.INSTANCE.register(new PhosphorBlur());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cc.polyfrost.polyblur.blurs.phosphor;

import net.minecraft.client.shader.ShaderGroup;

public interface EntityRendererHook {
ShaderGroup getPhosphorShader();
void setPhosphorShader(ShaderGroup phosphorShader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import cc.polyfrost.oneconfig.libs.universal.UResolution;
import cc.polyfrost.polyblur.PolyBlur;
import cc.polyfrost.polyblur.mixin.ShaderGroupAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.shader.Shader;
import net.minecraft.client.shader.ShaderGroup;
import net.minecraft.client.shader.ShaderUniform;
import net.minecraft.util.ResourceLocation;

import java.io.IOException;
import java.util.List;

public class PhosphorBlur {
Expand Down Expand Up @@ -37,29 +41,28 @@ public static void reloadBlur() {
return;
}

if (!isShaderActive() && PolyBlur.instance.config.enabled) {
//#if FABRIC==1
//$$ ((GameRendererAccessor) UMinecraft.getMinecraft().gameRenderer).invokeLoadShader(motionBlur);
//#else
UMinecraft.getMinecraft().entityRenderer.loadShader(phosphorBlur);
//#endif

reloadIntensity();
} else if (isShaderActive() && !PolyBlur.instance.config.enabled) {
String name = UMinecraft.getMinecraft().entityRenderer.getShaderGroup().getShaderGroupName();

// Only stop our specific blur ;)
if (!name.endsWith("phosphor_motion_blur.json")) {
return;
if (!isShaderActive() && PolyBlur.instance.config.enabled && PolyBlur.instance.config.blurMode == 1) {
try {
final ShaderGroup phosphorBlurShader = new ShaderGroup(Minecraft.getMinecraft().getTextureManager(), Minecraft.getMinecraft().getResourceManager(), Minecraft.getMinecraft().getFramebuffer(), phosphorBlur);
phosphorBlurShader.createBindFramebuffers(UResolution.getWindowWidth(), UResolution.getWindowHeight());
((EntityRendererHook) Minecraft.getMinecraft().entityRenderer).setPhosphorShader(phosphorBlurShader);
reloadIntensity();
} catch (IOException e) {
e.printStackTrace();
}
} else if (isShaderActive() && (!PolyBlur.instance.config.enabled || PolyBlur.instance.config.blurMode != 1)) {
final EntityRendererHook entityRenderer = (EntityRendererHook) UMinecraft.getMinecraft().entityRenderer;
if (entityRenderer.getPhosphorShader() != null) {
entityRenderer.getPhosphorShader().deleteShaderGroup();
}

UMinecraft.getMinecraft().entityRenderer.stopUseShader();
entityRenderer.setPhosphorShader(null);
}
}

public static void reloadIntensity() {
try {
final List<Shader> listShaders = ((ShaderGroupAccessor) UMinecraft.getMinecraft().entityRenderer.getShaderGroup()).getListShaders();
final List<Shader> listShaders = ((ShaderGroupAccessor) ((EntityRendererHook) UMinecraft.getMinecraft().entityRenderer).getPhosphorShader()).getListShaders();

if (listShaders == null) {
return;
Expand All @@ -80,7 +83,7 @@ public static void reloadIntensity() {
}

private static boolean isShaderActive() {
return UMinecraft.getMinecraft().entityRenderer.getShaderGroup() != null
return ((EntityRendererHook) UMinecraft.getMinecraft().entityRenderer).getPhosphorShader() != null
//#if MC<=11202
&& net.minecraft.client.renderer.OpenGlHelper.shadersSupported
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class PolyBlurConfig extends Config {

public PolyBlurConfig() {
super(new Mod("PolyBlur", ModType.PVP), "polyblur.json");
initialize();
addListener("strength", () -> {
if (blurMode == 1 && enabled) {
PhosphorBlur.reloadIntensity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cc.polyfrost.oneconfig.internal.gui.impl.BlurHandlerImpl;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import cc.polyfrost.polyblur.blurs.phosphor.EntityRendererHook;
import org.spongepowered.asm.mixin.Dynamic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
Expand All @@ -27,8 +28,7 @@ private boolean redirectShaderActive(BlurHandlerImpl a) { // works without any p
//#else
//$$ true
//#endif
&& UMinecraft.getMinecraft().entityRenderer.getShaderGroup() != null
&& UMinecraft.getMinecraft().entityRenderer.getShaderGroup().getShaderGroupName().endsWith("phosphor_motion_blur.json")
&& ((EntityRendererHook) UMinecraft.getMinecraft().entityRenderer).getPhosphorShader() != null
) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cc/polyfrost/polyblur/mixin/BlurModMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.polyfrost.polyblur.mixin;

import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import cc.polyfrost.polyblur.blurs.phosphor.EntityRendererHook;
import net.minecraft.client.renderer.EntityRenderer;
import org.spongepowered.asm.mixin.Dynamic;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -23,8 +24,7 @@ private boolean isShaderActive(EntityRenderer er) {
//#else
//$$ true
//#endif
&& UMinecraft.getMinecraft().entityRenderer.getShaderGroup() != null
&& UMinecraft.getMinecraft().entityRenderer.getShaderGroup().getShaderGroupName().endsWith("phosphor_motion_blur.json")
&& ((EntityRendererHook) UMinecraft.getMinecraft().entityRenderer).getPhosphorShader() != null
) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cc.polyfrost.polyblur.mixin;

import cc.polyfrost.polyblur.blurs.phosphor.EntityRendererHook;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.shader.ShaderGroup;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(EntityRenderer.class)
public class EntityRendererMixin_PhosphorBlur implements EntityRendererHook {
@Shadow private ShaderGroup theShaderGroup;
private ShaderGroup phosphorShader;

@Inject(method = "isShaderActive", at = @At("HEAD"), cancellable = true)
private void onIsShaderActive(CallbackInfoReturnable<Boolean> cir) {
if (phosphorShader != null && OpenGlHelper.shadersSupported) {
cir.setReturnValue(true);
}
}

@Inject(method = "getShaderGroup", at = @At("HEAD"), cancellable = true)
private void onGetShaderGroup(CallbackInfoReturnable<ShaderGroup> cir) {
if (phosphorShader != null && OpenGlHelper.shadersSupported && theShaderGroup == null) {
cir.setReturnValue(phosphorShader);
}
}

@Inject(method = "updateShaderGroupSize", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/RenderGlobal;createBindEntityOutlineFbs(II)V"))
private void updatePhosphor(int width, int height, CallbackInfo ci) {
if (phosphorShader != null) {
phosphorShader.createBindFramebuffers(width, height);
}
}

@Inject(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/RenderGlobal;renderEntityOutlineFramebuffer()V", shift = At.Shift.AFTER))
private void renderPhosphor(float partialTicks, long nanoTime, CallbackInfo ci) {
if (this.phosphorShader != null) {
GlStateManager.matrixMode(5890);
GlStateManager.pushMatrix();
GlStateManager.loadIdentity();
this.phosphorShader.loadShaderGroup(partialTicks);
GlStateManager.popMatrix();
}
}

@Override
public ShaderGroup getPhosphorShader() {
return phosphorShader;
}

@Override
public void setPhosphorShader(ShaderGroup phosphorShader) {
this.phosphorShader = phosphorShader;
}
}
1 change: 1 addition & 0 deletions src/main/resources/mixins.polyblur.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"BlurHandlerImplMixin",
"BlurModMixin",
"EntityRendererMixin",
"EntityRendererMixin_PhosphorBlur",
"MinecraftMixin",
"OptifineConfigMixin",
"ShaderGroupAccessor"
Expand Down

0 comments on commit 546e229

Please sign in to comment.