2025-11-24 22:52:51 +03:00

173 lines
8.1 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* org.joml.Quaternionfc
* org.jspecify.annotations.Nullable
*/
package net.minecraft.client.renderer.blockentity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.model.Model;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.SubmitNodeCollector;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.blockentity.state.SignRenderState;
import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
import net.minecraft.client.renderer.rendertype.RenderType;
import net.minecraft.client.renderer.state.CameraRenderState;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.MaterialSet;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.util.ARGB;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.util.Mth;
import net.minecraft.util.Unit;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.SignBlock;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SignText;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.phys.Vec3;
import org.joml.Quaternionfc;
import org.jspecify.annotations.Nullable;
public abstract class AbstractSignRenderer
implements BlockEntityRenderer<SignBlockEntity, SignRenderState> {
private static final int BLACK_TEXT_OUTLINE_COLOR = -988212;
private static final int OUTLINE_RENDER_DISTANCE = Mth.square(16);
private final Font font;
private final MaterialSet materials;
public AbstractSignRenderer(BlockEntityRendererProvider.Context context) {
this.font = context.font();
this.materials = context.materials();
}
protected abstract Model.Simple getSignModel(BlockState var1, WoodType var2);
protected abstract Material getSignMaterial(WoodType var1);
protected abstract float getSignModelRenderScale();
protected abstract float getSignTextRenderScale();
protected abstract Vec3 getTextOffset();
protected abstract void translateSign(PoseStack var1, float var2, BlockState var3);
@Override
public void submit(SignRenderState state, PoseStack poseStack, SubmitNodeCollector submitNodeCollector, CameraRenderState camera) {
BlockState blockState = state.blockState;
SignBlock signBlock = (SignBlock)blockState.getBlock();
Model.Simple signModel = this.getSignModel(blockState, signBlock.type());
this.submitSignWithText(state, poseStack, blockState, signBlock, signBlock.type(), signModel, state.breakProgress, submitNodeCollector);
}
private void submitSignWithText(SignRenderState state, PoseStack poseStack, BlockState blockState, SignBlock signBlock, WoodType type, Model.Simple signModel, @Nullable ModelFeatureRenderer.CrumblingOverlay breakProgress, SubmitNodeCollector submitNodeCollector) {
poseStack.pushPose();
this.translateSign(poseStack, -signBlock.getYRotationDegrees(blockState), blockState);
this.submitSign(poseStack, state.lightCoords, type, signModel, breakProgress, submitNodeCollector);
this.submitSignText(state, poseStack, submitNodeCollector, true);
this.submitSignText(state, poseStack, submitNodeCollector, false);
poseStack.popPose();
}
protected void submitSign(PoseStack poseStack, int lightCoords, WoodType type, Model.Simple signModel, @Nullable ModelFeatureRenderer.CrumblingOverlay breakProgress, SubmitNodeCollector submitNodeCollector) {
poseStack.pushPose();
float scale = this.getSignModelRenderScale();
poseStack.scale(scale, -scale, -scale);
Material material = this.getSignMaterial(type);
RenderType renderType = material.renderType(signModel::renderType);
submitNodeCollector.submitModel(signModel, Unit.INSTANCE, poseStack, renderType, lightCoords, OverlayTexture.NO_OVERLAY, -1, this.materials.get(material), 0, breakProgress);
poseStack.popPose();
}
private void submitSignText(SignRenderState state, PoseStack poseStack, SubmitNodeCollector submitNodeCollector, boolean isFrontText) {
int lightVal;
boolean drawOutline;
int textColor;
SignText signText;
SignText signText2 = signText = isFrontText ? state.frontText : state.backText;
if (signText == null) {
return;
}
poseStack.pushPose();
this.translateSignText(poseStack, isFrontText, this.getTextOffset());
int darkColor = AbstractSignRenderer.getDarkColor(signText);
int signMidpoint = 4 * state.textLineHeight / 2;
FormattedCharSequence[] formattedLines = signText.getRenderMessages(state.isTextFilteringEnabled, input -> {
List<FormattedCharSequence> components = this.font.split((FormattedText)input, state.maxTextLineWidth);
return components.isEmpty() ? FormattedCharSequence.EMPTY : components.get(0);
});
if (signText.hasGlowingText()) {
textColor = signText.getColor().getTextColor();
drawOutline = textColor == DyeColor.BLACK.getTextColor() || state.drawOutline;
lightVal = 0xF000F0;
} else {
textColor = darkColor;
drawOutline = false;
lightVal = state.lightCoords;
}
for (int i = 0; i < 4; ++i) {
FormattedCharSequence actualLine = formattedLines[i];
float x1 = -this.font.width(actualLine) / 2;
submitNodeCollector.submitText(poseStack, x1, i * state.textLineHeight - signMidpoint, actualLine, false, Font.DisplayMode.POLYGON_OFFSET, lightVal, textColor, 0, drawOutline ? darkColor : 0);
}
poseStack.popPose();
}
private void translateSignText(PoseStack poseStack, boolean isFrontText, Vec3 textOffset) {
if (!isFrontText) {
poseStack.mulPose((Quaternionfc)Axis.YP.rotationDegrees(180.0f));
}
float s = 0.015625f * this.getSignTextRenderScale();
poseStack.translate(textOffset);
poseStack.scale(s, -s, s);
}
private static boolean isOutlineVisible(BlockPos pos) {
Minecraft minecraft = Minecraft.getInstance();
LocalPlayer player = minecraft.player;
if (player != null && minecraft.options.getCameraType().isFirstPerson() && player.isScoping()) {
return true;
}
Entity camera = minecraft.getCameraEntity();
return camera != null && camera.distanceToSqr(Vec3.atCenterOf(pos)) < (double)OUTLINE_RENDER_DISTANCE;
}
public static int getDarkColor(SignText signText) {
int color = signText.getColor().getTextColor();
if (color == DyeColor.BLACK.getTextColor() && signText.hasGlowingText()) {
return -988212;
}
return ARGB.scaleRGB(color, 0.4f);
}
@Override
public SignRenderState createRenderState() {
return new SignRenderState();
}
@Override
public void extractRenderState(SignBlockEntity blockEntity, SignRenderState state, float partialTicks, Vec3 cameraPosition, @Nullable ModelFeatureRenderer.CrumblingOverlay breakProgress) {
BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
state.maxTextLineWidth = blockEntity.getMaxTextLineWidth();
state.textLineHeight = blockEntity.getTextLineHeight();
state.frontText = blockEntity.getFrontText();
state.backText = blockEntity.getBackText();
state.isTextFilteringEnabled = Minecraft.getInstance().isTextFilteringEnabled();
state.drawOutline = AbstractSignRenderer.isOutlineVisible(blockEntity.getBlockPos());
}
}