Skip to content

Commit

Permalink
Fix text being rendered with z offset
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jul 21, 2024
1 parent 68fc733 commit 1aa0188
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.smyler.smylib.gui;

/**
* Exposes methods declared in {@link net.smyler.smylib.mixins.FontMixinNoShadowOffset}.
*/
public interface PatchedFont {
void smylib$setCancelShadowOffset(boolean cancelShadowOffset);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public float draw(float x, float y, @NotNull String text, @NotNull Color color,
gl.scale(this.scale, this.scale);
x *= invScale;
y *= invScale;

// Vanilla does a z-translation call to draw the shadow,
// We need to work around that (it is useless and messes z coordinates).
PatchedFont patchedFont = (PatchedFont) this.vanillaFont;
patchedFont.smylib$setCancelShadowOffset(true);
float result = this.vanillaGraphics.drawString(this.vanillaFont, text, (int) x, (int) y, color.asInt());
patchedFont.smylib$setCancelShadowOffset(false);
gl.scale(invScale, invScale);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.smyler.smylib.mixins;

import net.minecraft.client.gui.Font;
import net.smyler.smylib.gui.PatchedFont;
import org.joml.Matrix4f;
import org.joml.Vector3fc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;


@Mixin(Font.class)
public abstract class FontMixinNoShadowOffset implements PatchedFont {

@Unique
private boolean cancelShadowOffset = false;

@Redirect(
method = "drawInternal(Ljava/lang/String;FFIZLorg/joml/Matrix4f;Lnet/minecraft/client/renderer/MultiBufferSource;Lnet/minecraft/client/gui/Font$DisplayMode;IIZ)I",
at = @At(
value = "INVOKE",
target = "Lorg/joml/Matrix4f;translate(Lorg/joml/Vector3fc;)Lorg/joml/Matrix4f;",
remap = false // Method is from JOML, it isn't obfuscated
)
)
public Matrix4f applyShadowOffset(Matrix4f matrix, Vector3fc offset) {
if (!this.cancelShadowOffset) {
matrix.translate(offset);
}
return matrix;
}

public void smylib$setCancelShadowOffset(boolean cancelShadowOffset) {
this.cancelShadowOffset = cancelShadowOffset;
}

}
1 change: 1 addition & 0 deletions smylib/fabric/src/main/resources/smylib.fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"compatibilityLevel": "JAVA_21",
"mixins": [],
"client": [
"FontMixinNoShadowOffset",
"UiGraphicsInjectorMixin"
],
"server": [],
Expand Down

0 comments on commit 1aa0188

Please sign in to comment.