From 0bb5ecf7bfb5bc72e5ca76549fca0bc2bf5e4bdf Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 27 Sep 2024 22:34:14 -0700 Subject: [PATCH] Can't read the signs - Handle obfuscation in SignVisuals --- .../flywheel/lib/visual/text/TextLayer.java | 2 +- .../flywheel/vanilla/SignVisual.java | 49 +++++++++++++------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/text/TextLayer.java b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/text/TextLayer.java index 22c27854e..7bc1f8bcc 100644 --- a/common/src/lib/java/dev/engine_room/flywheel/lib/visual/text/TextLayer.java +++ b/common/src/lib/java/dev/engine_room/flywheel/lib/visual/text/TextLayer.java @@ -95,7 +95,7 @@ static GlyphColor defaultTo(int color) { * @return A new GlyphColor. */ static GlyphColor always(int color) { - return style -> adjustColor(color); + return style -> color; } /** diff --git a/common/src/main/java/dev/engine_room/flywheel/vanilla/SignVisual.java b/common/src/main/java/dev/engine_room/flywheel/vanilla/SignVisual.java index ad03a5f13..531760ef3 100644 --- a/common/src/main/java/dev/engine_room/flywheel/vanilla/SignVisual.java +++ b/common/src/main/java/dev/engine_room/flywheel/vanilla/SignVisual.java @@ -54,6 +54,10 @@ public class SignVisual extends AbstractBlockEntityVisual imple private final TextVisual[] frontText = new TextVisual[4]; private final TextVisual[] backText = new TextVisual[4]; + // Need to update these every frame, so just remember which ones are obfuscated + // Most of the time this will be empty. + private final List obfuscated = new ArrayList<>(); + private SignText lastFrontText; private SignText lastBackText; @@ -93,22 +97,34 @@ public SignVisual(VisualizationContext ctx, SignBlockEntity blockEntity, float p lastFrontText = blockEntity.getFrontText(); lastBackText = blockEntity.getBackText(); - this.setupText(lastFrontText, frontText, true); - this.setupText(lastBackText, backText, false); + this.setupText(lastFrontText, true); + this.setupText(lastBackText, false); } @Override public void beginFrame(Context ctx) { - // Need to update every frame if the text is obfuscated - + boolean setup = false; if (lastFrontText != blockEntity.getFrontText()) { lastFrontText = blockEntity.getFrontText(); - this.setupText(lastFrontText, frontText, true); + setup = true; } if (lastBackText != blockEntity.getBackText()) { lastBackText = blockEntity.getBackText(); - this.setupText(lastBackText, backText, false); + setup = true; + } + + if (setup) { + // Setup both to make it easier to track obfuscation + obfuscated.clear(); + this.setupText(lastBackText, false); + this.setupText(lastFrontText, true); + } else { + // The is visible check is relatively expensive compared to the boolean checks above, + // so only do it when it'll actually save some work in obfuscating. + if (isVisible(ctx.frustum())) { + obfuscated.forEach(TextVisual::setup); + } } } @@ -158,7 +174,7 @@ public Vec3 getTextOffset() { return TEXT_OFFSET; } - void setupText(SignText text, TextVisual[] dst, boolean isFrontText) { + protected void setupText(SignText text, boolean isFrontText) { FormattedCharSequence[] formattedCharSequences = text.getRenderMessages(Minecraft.getInstance() .isTextFilteringEnabled(), component -> { List list = Minecraft.getInstance().font.split(component, blockEntity.getMaxTextLineWidth()); @@ -167,10 +183,10 @@ void setupText(SignText text, TextVisual[] dst, boolean isFrontText) { List layers = new ArrayList<>(); - int darkColor = adjustColor(getDarkColor(text)); + int darkColor = TextLayer.GlyphColor.adjustColor(getDarkColor(text)); int textColor; if (text.hasGlowingText()) { - textColor = adjustColor(text.getColor() + textColor = TextLayer.GlyphColor.adjustColor(text.getColor() .getTextColor()); layers.add(new SimpleTextLayer.Builder().style(TextLayer.GlyphMeshStyle.OUTLINE) @@ -187,6 +203,8 @@ void setupText(SignText text, TextVisual[] dst, boolean isFrontText) { .bias(1) .build()); + var dst = isFrontText ? frontText : backText; + int lineHeight = blockEntity.getTextLineHeight(); int lineDelta = 4 * lineHeight / 2; for (int m = 0; m < 4; ++m) { @@ -213,17 +231,18 @@ void setupText(SignText text, TextVisual[] dst, boolean isFrontText) { textPose.scale(scale, -scale, scale); textVisual.setup(); + + if (hasObfuscation(formattedCharSequence)) { + this.obfuscated.add(textVisual); + } } } - private static int adjustColor(int color) { - if ((color & 0xFC000000) == 0) { - return color | 0xFF000000; - } - return color; + public static boolean hasObfuscation(FormattedCharSequence text) { + return text.accept((i, s, j) -> s.isObfuscated()); } - static int getDarkColor(SignText signText) { + public static int getDarkColor(SignText signText) { int i = signText.getColor() .getTextColor(); if (i == DyeColor.BLACK.getTextColor() && signText.hasGlowingText()) {