minecraft-src/net/minecraft/client/renderer/MapRenderer.java
2025-07-04 03:45:38 +03:00

114 lines
5.4 KiB
Java

package net.minecraft.client.renderer;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.Font.DisplayMode;
import net.minecraft.client.renderer.state.MapRenderState;
import net.minecraft.client.renderer.state.MapRenderState.MapDecorationRenderState;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.MapDecorationTextureManager;
import net.minecraft.client.resources.MapTextureManager;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.level.saveddata.maps.MapDecoration;
import net.minecraft.world.level.saveddata.maps.MapId;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import org.joml.Matrix4f;
@Environment(EnvType.CLIENT)
public class MapRenderer {
private static final float MAP_Z_OFFSET = -0.01F;
private static final float DECORATION_Z_OFFSET = -0.001F;
private static final int WIDTH = 128;
private static final int HEIGHT = 128;
private final MapTextureManager mapTextureManager;
private final MapDecorationTextureManager decorationTextures;
public MapRenderer(MapDecorationTextureManager decorationTextures, MapTextureManager mapTextureManager) {
this.decorationTextures = decorationTextures;
this.mapTextureManager = mapTextureManager;
}
public void render(MapRenderState renderState, PoseStack poseStack, MultiBufferSource bufferSource, boolean active, int packedLight) {
Matrix4f matrix4f = poseStack.last().pose();
VertexConsumer vertexConsumer = bufferSource.getBuffer(RenderType.text(renderState.texture));
vertexConsumer.addVertex(matrix4f, 0.0F, 128.0F, -0.01F).setColor(-1).setUv(0.0F, 1.0F).setLight(packedLight);
vertexConsumer.addVertex(matrix4f, 128.0F, 128.0F, -0.01F).setColor(-1).setUv(1.0F, 1.0F).setLight(packedLight);
vertexConsumer.addVertex(matrix4f, 128.0F, 0.0F, -0.01F).setColor(-1).setUv(1.0F, 0.0F).setLight(packedLight);
vertexConsumer.addVertex(matrix4f, 0.0F, 0.0F, -0.01F).setColor(-1).setUv(0.0F, 0.0F).setLight(packedLight);
int i = 0;
for (MapDecorationRenderState mapDecorationRenderState : renderState.decorations) {
if (!active || mapDecorationRenderState.renderOnFrame) {
poseStack.pushPose();
poseStack.translate(mapDecorationRenderState.x / 2.0F + 64.0F, mapDecorationRenderState.y / 2.0F + 64.0F, -0.02F);
poseStack.mulPose(Axis.ZP.rotationDegrees(mapDecorationRenderState.rot * 360 / 16.0F));
poseStack.scale(4.0F, 4.0F, 3.0F);
poseStack.translate(-0.125F, 0.125F, 0.0F);
Matrix4f matrix4f2 = poseStack.last().pose();
TextureAtlasSprite textureAtlasSprite = mapDecorationRenderState.atlasSprite;
if (textureAtlasSprite != null) {
VertexConsumer vertexConsumer2 = bufferSource.getBuffer(RenderType.text(textureAtlasSprite.atlasLocation()));
vertexConsumer2.addVertex(matrix4f2, -1.0F, 1.0F, i * -0.001F)
.setColor(-1)
.setUv(textureAtlasSprite.getU0(), textureAtlasSprite.getV0())
.setLight(packedLight);
vertexConsumer2.addVertex(matrix4f2, 1.0F, 1.0F, i * -0.001F)
.setColor(-1)
.setUv(textureAtlasSprite.getU1(), textureAtlasSprite.getV0())
.setLight(packedLight);
vertexConsumer2.addVertex(matrix4f2, 1.0F, -1.0F, i * -0.001F)
.setColor(-1)
.setUv(textureAtlasSprite.getU1(), textureAtlasSprite.getV1())
.setLight(packedLight);
vertexConsumer2.addVertex(matrix4f2, -1.0F, -1.0F, i * -0.001F)
.setColor(-1)
.setUv(textureAtlasSprite.getU0(), textureAtlasSprite.getV1())
.setLight(packedLight);
poseStack.popPose();
}
if (mapDecorationRenderState.name != null) {
Font font = Minecraft.getInstance().font;
float f = font.width(mapDecorationRenderState.name);
float g = Mth.clamp(25.0F / f, 0.0F, 6.0F / 9.0F);
poseStack.pushPose();
poseStack.translate(mapDecorationRenderState.x / 2.0F + 64.0F - f * g / 2.0F, mapDecorationRenderState.y / 2.0F + 64.0F + 4.0F, -0.025F);
poseStack.scale(g, g, 1.0F);
poseStack.translate(0.0F, 0.0F, -0.1F);
font.drawInBatch(
mapDecorationRenderState.name, 0.0F, 0.0F, -1, false, poseStack.last().pose(), bufferSource, DisplayMode.NORMAL, Integer.MIN_VALUE, packedLight, false
);
poseStack.popPose();
}
i++;
}
}
}
public void extractRenderState(MapId id, MapItemSavedData savedData, MapRenderState renderState) {
renderState.texture = this.mapTextureManager.prepareMapTexture(id, savedData);
renderState.decorations.clear();
for (MapDecoration mapDecoration : savedData.getDecorations()) {
renderState.decorations.add(this.extractDecorationRenderState(mapDecoration));
}
}
private MapDecorationRenderState extractDecorationRenderState(MapDecoration decoration) {
MapDecorationRenderState mapDecorationRenderState = new MapDecorationRenderState();
mapDecorationRenderState.atlasSprite = this.decorationTextures.get(decoration);
mapDecorationRenderState.x = decoration.x();
mapDecorationRenderState.y = decoration.y();
mapDecorationRenderState.rot = decoration.rot();
mapDecorationRenderState.name = (Component)decoration.name().orElse(null);
mapDecorationRenderState.renderOnFrame = decoration.renderOnFrame();
return mapDecorationRenderState;
}
}