Skip to content

Commit

Permalink
Proposal for fix of kvverti#6
Browse files Browse the repository at this point in the history
  • Loading branch information
velnias75 committed Feb 9, 2023
1 parent 9fe5adf commit 1ab66ca
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/main/java/io/github/kvverti/colormatic/Lightmaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
*/
public final class Lightmaps {

private static final Map<Identifier, Lightmap> lightmaps = new HashMap<>();
private static final Map<Identifier, Lightmap> lightmaps = new HashMap<>(3);

private static boolean worldRenderFinished = true;

public static Lightmap get(World world) {
return lightmaps.get(Colormatic.getDimId(world));
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@
*/
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;
import org.spongepowered.asm.mixin.Unique;
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;
Expand All @@ -59,6 +61,9 @@ public abstract class LightmapTextureManagerMixin {
@Final
private NativeImage image;

@Shadow
private boolean dirty;

@Shadow
private float flickerIntensity;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/colormatic.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"render.ItemRendererMixin",
"render.LightmapTextureManagerMixin",
"render.WorldRendererMixin",
"render.GameRendererMixin",
"text.AbstractButtonWidgetMixin",
"text.ChatFormatMixin",
"text.DyeColorMixin",
Expand Down

0 comments on commit 1ab66ca

Please sign in to comment.