| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 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.renderer.rendertype.RenderTypes;
|
| 10 | import net.minecraft.client.renderer.state.MapRenderState;
|
| 11 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 12 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 13 | import net.minecraft.client.resources.MapTextureManager;
|
| 14 | import net.minecraft.client.resources.model.sprite.AtlasManager;
|
| 15 | import net.minecraft.data.AtlasIds;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.world.level.saveddata.maps.MapDecoration;
|
| 18 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 19 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class MapRenderer {
|
| 23 | private static final float MAP_Z_OFFSET = -0.01F;
|
| 24 | private static final float DECORATION_Z_OFFSET = -0.001F;
|
| 25 | public static final int WIDTH = 128;
|
| 26 | public static final int HEIGHT = 128;
|
| 27 | private final TextureAtlas decorationSprites;
|
| 28 | private final MapTextureManager mapTextureManager;
|
| 29 |
|
| 30 | public MapRenderer(final AtlasManager atlasManager, final MapTextureManager mapTextureManager) {
|
| 31 | this.decorationSprites = atlasManager.getAtlasOrThrow(AtlasIds.MAP_DECORATIONS);
|
| 32 | this.mapTextureManager = mapTextureManager;
|
| 33 | }
|
| 34 |
|
| 35 | public void render(
|
| 36 | final MapRenderState mapRenderState,
|
| 37 | final PoseStack poseStack,
|
| 38 | final SubmitNodeCollector submitNodeCollector,
|
| 39 | final boolean showOnlyFrame,
|
| 40 | final int lightCoords
|
| 41 | ) {
|
| 42 | submitNodeCollector.submitCustomGeometry(poseStack, RenderTypes.text(mapRenderState.texture), (pose, buffer) -> {
|
| 43 | buffer.addVertex(pose, 0.0F, 128.0F, -0.01F).setColor(-1).setUv(0.0F, 1.0F).setLight(lightCoords);
|
| 44 | buffer.addVertex(pose, 128.0F, 128.0F, -0.01F).setColor(-1).setUv(1.0F, 1.0F).setLight(lightCoords);
|
| 45 | buffer.addVertex(pose, 128.0F, 0.0F, -0.01F).setColor(-1).setUv(1.0F, 0.0F).setLight(lightCoords);
|
| 46 | buffer.addVertex(pose, 0.0F, 0.0F, -0.01F).setColor(-1).setUv(0.0F, 0.0F).setLight(lightCoords);
|
| 47 | });
|
| 48 | int count = 0;
|
| 49 |
|
| 50 | for (MapRenderState.MapDecorationRenderState decoration : mapRenderState.decorations) {
|
| 51 | if (!showOnlyFrame || decoration.renderOnFrame) {
|
| 52 | poseStack.pushPose();
|
| 53 | poseStack.translate(decoration.x / 2.0F + 64.0F, decoration.y / 2.0F + 64.0F, -0.02F);
|
| 54 | poseStack.rotateDegrees(Axis.ZP, decoration.rot * 360 / 16.0F);
|
| 55 | poseStack.scale(4.0F, 4.0F, 3.0F);
|
| 56 | poseStack.translate(-0.125F, 0.125F, 0.0F);
|
| 57 | TextureAtlasSprite atlasSprite = decoration.atlasSprite;
|
| 58 | if (atlasSprite != null) {
|
| 59 | float z = count * -0.001F;
|
| 60 | submitNodeCollector.submitCustomGeometry(poseStack, RenderTypes.text(atlasSprite.atlasLocation()), (pose, buffer) -> {
|
| 61 | buffer.addVertex(pose, -1.0F, 1.0F, z).setColor(-1).setUv(atlasSprite.getU0(), atlasSprite.getV0()).setLight(lightCoords);
|
| 62 | buffer.addVertex(pose, 1.0F, 1.0F, z).setColor(-1).setUv(atlasSprite.getU1(), atlasSprite.getV0()).setLight(lightCoords);
|
| 63 | buffer.addVertex(pose, 1.0F, -1.0F, z).setColor(-1).setUv(atlasSprite.getU1(), atlasSprite.getV1()).setLight(lightCoords);
|
| 64 | buffer.addVertex(pose, -1.0F, -1.0F, z).setColor(-1).setUv(atlasSprite.getU0(), atlasSprite.getV1()).setLight(lightCoords);
|
| 65 | });
|
| 66 | poseStack.popPose();
|
| 67 | }
|
| 68 |
|
| 69 | if (decoration.name != null) {
|
| 70 | Font font = Minecraft.getInstance().font;
|
| 71 | float width = font.width(decoration.name);
|
| 72 | float scale = Mth.clamp(25.0F / width, 0.0F, 6.0F / 9.0F);
|
| 73 | poseStack.pushPose();
|
| 74 | poseStack.translate(decoration.x / 2.0F + 64.0F - width * scale / 2.0F, decoration.y / 2.0F + 64.0F + 4.0F, -0.025F);
|
| 75 | poseStack.scale(scale, scale, -1.0F);
|
| 76 | poseStack.translate(0.0F, 0.0F, 0.1F);
|
| 77 | submitNodeCollector.order(1)
|
| 78 | .submitText(poseStack, 0.0F, 0.0F, decoration.name.getVisualOrderText(), false, Font.DisplayMode.NORMAL, lightCoords, -1, Integer.MIN_VALUE, 0);
|
| 79 | poseStack.popPose();
|
| 80 | }
|
| 81 |
|
| 82 | count++;
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public void extractRenderState(final MapId mapId, final MapItemSavedData mapData, final MapRenderState mapRenderState) {
|
| 88 | mapRenderState.texture = this.mapTextureManager.prepareMapTexture(mapId, mapData);
|
| 89 | mapRenderState.decorations.clear();
|
| 90 |
|
| 91 | for (MapDecoration decoration : mapData.getDecorations()) {
|
| 92 | mapRenderState.decorations.add(this.extractDecorationRenderState(decoration));
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | private MapRenderState.MapDecorationRenderState extractDecorationRenderState(final MapDecoration decoration) {
|
| 97 | MapRenderState.MapDecorationRenderState state = new MapRenderState.MapDecorationRenderState();
|
| 98 | state.atlasSprite = this.decorationSprites.getSprite(decoration.getSpriteLocation());
|
| 99 | state.x = decoration.x();
|
| 100 | state.y = decoration.y();
|
| 101 | state.rot = decoration.rot();
|
| 102 | state.name = decoration.name().orElse(null);
|
| 103 | state.renderOnFrame = decoration.renderOnFrame();
|
| 104 | return state;
|
| 105 | }
|
| 106 | }
|
| 107 |
|