From 1ab66ca9caf0be14a8fbf9d87920c96d60f3d38b Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Wed, 8 Feb 2023 08:14:02 +0100 Subject: [PATCH] Proposal for fix of https://github.com/kvverti/colormatic/issues/6 --- .../github/kvverti/colormatic/Lightmaps.java | 12 ++++- .../mixin/render/GameRendererMixin.java | 48 +++++++++++++++++++ .../render/LightmapTextureManagerMixin.java | 30 ++++++++++-- src/main/resources/colormatic.mixins.json | 1 + 4 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/github/kvverti/colormatic/mixin/render/GameRendererMixin.java diff --git a/src/main/java/io/github/kvverti/colormatic/Lightmaps.java b/src/main/java/io/github/kvverti/colormatic/Lightmaps.java index 101c4db..52e071b 100644 --- a/src/main/java/io/github/kvverti/colormatic/Lightmaps.java +++ b/src/main/java/io/github/kvverti/colormatic/Lightmaps.java @@ -34,7 +34,9 @@ */ public final class Lightmaps { - private static final Map lightmaps = new HashMap<>(); + private static final Map lightmaps = new HashMap<>(3); + + private static boolean worldRenderFinished = true; public static Lightmap get(World world) { return lightmaps.get(Colormatic.getDimId(world)); @@ -47,4 +49,12 @@ public static void addLightmap(Identifier id, Lightmap lightmap) { public static void clearLightmaps() { lightmaps.clear(); } + + public static boolean isWorldRenderFinished() { + return worldRenderFinished; + } + + public static void setWorldRenderFinished(final boolean worldRenderFinished) { + Lightmaps.worldRenderFinished = worldRenderFinished; + } } diff --git a/src/main/java/io/github/kvverti/colormatic/mixin/render/GameRendererMixin.java b/src/main/java/io/github/kvverti/colormatic/mixin/render/GameRendererMixin.java new file mode 100644 index 0000000..a9a75c9 --- /dev/null +++ b/src/main/java/io/github/kvverti/colormatic/mixin/render/GameRendererMixin.java @@ -0,0 +1,48 @@ +package io.github.kvverti.colormatic.mixin.render; + +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; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import io.github.kvverti.colormatic.Lightmaps; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.render.LightmapTextureManager; +import net.minecraft.client.util.math.MatrixStack; + +/*** + * + * @author Velnias75 + * + */ +@Mixin(GameRenderer.class) +public abstract class GameRendererMixin { + + @Shadow + @Final + private MinecraftClient client; + + @Shadow + @Final + private LightmapTextureManager lightmapTextureManager; + + @Inject(method = "renderWorld", at = @At("INVOKE")) + private void onRenderWorldHead(final CallbackInfo info) { + Lightmaps.setWorldRenderFinished(false); + } + + @Inject(method = "renderWorld", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD) + private void onRenderWorldReturn(float tickDelta, long limitTime, MatrixStack matrices, final CallbackInfo info) { + + Lightmaps.setWorldRenderFinished(true); + + if (Lightmaps.get(client.world) != null) { + lightmapTextureManager.update(tickDelta); + lightmapTextureManager.tick(); + } + } +} diff --git a/src/main/java/io/github/kvverti/colormatic/mixin/render/LightmapTextureManagerMixin.java b/src/main/java/io/github/kvverti/colormatic/mixin/render/LightmapTextureManagerMixin.java index f7d02e4..7702dda 100644 --- a/src/main/java/io/github/kvverti/colormatic/mixin/render/LightmapTextureManagerMixin.java +++ b/src/main/java/io/github/kvverti/colormatic/mixin/render/LightmapTextureManagerMixin.java @@ -21,10 +21,7 @@ */ package io.github.kvverti.colormatic.mixin.render; -import io.github.kvverti.colormatic.Colormatic; -import io.github.kvverti.colormatic.ColormaticConfig; -import io.github.kvverti.colormatic.Lightmaps; -import io.github.kvverti.colormatic.colormap.Lightmap; +import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -32,8 +29,13 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import io.github.kvverti.colormatic.Colormatic; +import io.github.kvverti.colormatic.ColormaticConfig; +import io.github.kvverti.colormatic.Lightmaps; +import io.github.kvverti.colormatic.colormap.Lightmap; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.GameRenderer; import net.minecraft.client.render.LightmapTextureManager; @@ -59,6 +61,9 @@ public abstract class LightmapTextureManagerMixin { @Final private NativeImage image; + @Shadow + private boolean dirty; + @Shadow private float flickerIntensity; @@ -158,7 +163,7 @@ private void onTickTickFlicker(CallbackInfo info) { ) private void onUpdate(float partialTicks, CallbackInfo info) { ClientWorld world = this.client.world; - if(world == null) { + if(world == null || Lightmaps.isWorldRenderFinished()) { return; } // ambience is a value between 0.2 and 1.0, inclusive. @@ -270,4 +275,19 @@ private float modifyFlickerIntensity(float blockLight) { } return blockLight; } + + /*** + * Force enable LightmapTextureManager.update() + */ + @Redirect( + method = "update", + at = @At( + value = "FIELD", + target = "Lnet/minecraft/client/render/LightmapTextureManager;dirty:Z", + opcode = Opcodes.GETFIELD + ) + ) + private boolean redirectGetDirty(final LightmapTextureManager ltm) { + return Lightmaps.isWorldRenderFinished() ? true : dirty; + } } diff --git a/src/main/resources/colormatic.mixins.json b/src/main/resources/colormatic.mixins.json index 09517c4..79be15d 100644 --- a/src/main/resources/colormatic.mixins.json +++ b/src/main/resources/colormatic.mixins.json @@ -35,6 +35,7 @@ "render.ItemRendererMixin", "render.LightmapTextureManagerMixin", "render.WorldRendererMixin", + "render.GameRendererMixin", "text.AbstractButtonWidgetMixin", "text.ChatFormatMixin", "text.DyeColorMixin",