| 1 | package net.minecraft.client.renderer.blockentity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.player.LocalPlayer;
|
| 10 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 11 | import net.minecraft.client.renderer.blockentity.state.SignRenderState;
|
| 12 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 13 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 14 | import net.minecraft.core.BlockPos;
|
| 15 | import net.minecraft.util.ARGB;
|
| 16 | import net.minecraft.util.FormattedCharSequence;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.item.DyeColor;
|
| 20 | import net.minecraft.world.level.block.entity.SignBlockEntity;
|
| 21 | import net.minecraft.world.level.block.entity.SignText;
|
| 22 | import net.minecraft.world.level.block.entity.SignTextSlot;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public abstract class AbstractSignRenderer<S extends SignRenderState> implements BlockEntityRenderer<SignBlockEntity, S> {
|
| 28 | private static final int BLACK_TEXT_OUTLINE_COLOR = -988212;
|
| 29 | private static final int OUTLINE_RENDER_DISTANCE = Mth.square(16);
|
| 30 | private final Font font;
|
| 31 |
|
| 32 | public AbstractSignRenderer(final BlockEntityRendererProvider.Context context) {
|
| 33 | this.font = context.font();
|
| 34 | }
|
| 35 |
|
| 36 | public void submit(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 37 | if (state.frontText != null) {
|
| 38 | poseStack.pushPose();
|
| 39 | poseStack.mulPose(state.transformations.frontText());
|
| 40 | this.submitSignText(state, poseStack, submitNodeCollector, state.frontText);
|
| 41 | poseStack.popPose();
|
| 42 | }
|
| 43 |
|
| 44 | if (state.backText != null) {
|
| 45 | poseStack.pushPose();
|
| 46 | poseStack.mulPose(state.transformations.backText());
|
| 47 | this.submitSignText(state, poseStack, submitNodeCollector, state.backText);
|
| 48 | poseStack.popPose();
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | private void submitSignText(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final SignText signText) {
|
| 53 | int darkColor = getDarkColor(signText);
|
| 54 | int signMidpoint = 4 * state.textLineHeight / 2;
|
| 55 | FormattedCharSequence[] formattedLines = signText.getRenderMessages(state.isTextFilteringEnabled, input -> {
|
| 56 | List<FormattedCharSequence> components = this.font.split(input, state.maxTextLineWidth);
|
| 57 | return components.isEmpty() ? FormattedCharSequence.EMPTY : components.get(0);
|
| 58 | });
|
| 59 | int textColor;
|
| 60 | boolean drawOutline;
|
| 61 | int lightVal;
|
| 62 | if (signText.hasGlowingText()) {
|
| 63 | textColor = signText.getColor().getTextColor();
|
| 64 | drawOutline = textColor == DyeColor.BLACK.getTextColor() || state.drawOutline;
|
| 65 | lightVal = 15728880;
|
| 66 | } else {
|
| 67 | textColor = darkColor;
|
| 68 | drawOutline = false;
|
| 69 | lightVal = state.lightCoords;
|
| 70 | }
|
| 71 |
|
| 72 | for (int i = 0; i < formattedLines.length; i++) {
|
| 73 | FormattedCharSequence actualLine = formattedLines[i];
|
| 74 | float x1 = -this.font.width(actualLine) / 2;
|
| 75 | submitNodeCollector.submitText(
|
| 76 | poseStack,
|
| 77 | x1,
|
| 78 | i * state.textLineHeight - signMidpoint,
|
| 79 | actualLine,
|
| 80 | false,
|
| 81 | Font.DisplayMode.POLYGON_OFFSET,
|
| 82 | lightVal,
|
| 83 | textColor,
|
| 84 | 0,
|
| 85 | drawOutline ? darkColor : 0
|
| 86 | );
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private static boolean isOutlineVisible(final BlockPos pos) {
|
| 91 | Minecraft minecraft = Minecraft.getInstance();
|
| 92 | LocalPlayer player = minecraft.player;
|
| 93 | if (player != null && minecraft.options.getCameraType().isFirstPerson() && player.isScoping()) {
|
| 94 | return true;
|
| 95 | }
|
| 96 |
|
| 97 | Entity camera = minecraft.getCameraEntity();
|
| 98 | return camera != null && camera.distanceToSqr(Vec3.atCenterOf(pos)) < OUTLINE_RENDER_DISTANCE;
|
| 99 | }
|
| 100 |
|
| 101 | public static int getDarkColor(final SignText signText) {
|
| 102 | int color = signText.getColor().getTextColor();
|
| 103 | return color == DyeColor.BLACK.getTextColor() && signText.hasGlowingText() ? -988212 : ARGB.scaleRGB(color, 0.4F);
|
| 104 | }
|
| 105 |
|
| 106 | public void extractRenderState(
|
| 107 | final SignBlockEntity blockEntity,
|
| 108 | final S state,
|
| 109 | final float partialTicks,
|
| 110 | final Vec3 cameraPosition,
|
| 111 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 112 | ) {
|
| 113 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 114 | state.maxTextLineWidth = blockEntity.getMaxTextLineWidth();
|
| 115 | state.textLineHeight = blockEntity.getTextLineHeight();
|
| 116 | state.frontText = blockEntity.getText(SignTextSlot.FRONT);
|
| 117 | state.backText = blockEntity.getText(SignTextSlot.BACK);
|
| 118 | state.isTextFilteringEnabled = Minecraft.getInstance().isTextFilteringEnabled();
|
| 119 | state.drawOutline = isOutlineVisible(blockEntity.getBlockPos());
|
| 120 | }
|
| 121 | }
|
| 122 |
|