| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import com.mojang.math.Axis;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 9 | import net.minecraft.client.renderer.entity.state.PaintingRenderState;
|
| 10 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 11 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 12 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 13 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 14 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 15 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 16 | import net.minecraft.core.BlockPos;
|
| 17 | import net.minecraft.core.Direction;
|
| 18 | import net.minecraft.data.AtlasIds;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.LightCoordsUtil;
|
| 21 | import net.minecraft.util.Mth;
|
| 22 | import net.minecraft.world.entity.decoration.painting.Painting;
|
| 23 | import net.minecraft.world.entity.decoration.painting.PaintingVariant;
|
| 24 | import net.minecraft.world.level.Level;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class PaintingRenderer extends EntityRenderer<Painting, PaintingRenderState> {
|
| 28 | private static final Identifier BACK_SPRITE_LOCATION = Identifier.withDefaultNamespace("back");
|
| 29 | private final TextureAtlas paintingsAtlas;
|
| 30 |
|
| 31 | public PaintingRenderer(final EntityRendererProvider.Context context) {
|
| 32 | super(context);
|
| 33 | this.paintingsAtlas = context.getAtlas(AtlasIds.PAINTINGS);
|
| 34 | }
|
| 35 |
|
| 36 | public void submit(final PaintingRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 37 | PaintingVariant variant = state.variant;
|
| 38 | if (variant != null) {
|
| 39 | poseStack.pushPose();
|
| 40 | poseStack.rotateDegrees(Axis.YP, 180 - state.direction.get2DDataValue() * 90);
|
| 41 | TextureAtlasSprite frontSprite = this.paintingsAtlas.getSprite(variant.assetId());
|
| 42 | TextureAtlasSprite backSprite = this.paintingsAtlas.getSprite(BACK_SPRITE_LOCATION);
|
| 43 | this.renderPainting(
|
| 44 | poseStack,
|
| 45 | submitNodeCollector,
|
| 46 | RenderTypes.entitySolidZOffsetForward(backSprite.atlasLocation()),
|
| 47 | state.lightCoordsPerBlock,
|
| 48 | variant.width(),
|
| 49 | variant.height(),
|
| 50 | frontSprite,
|
| 51 | backSprite
|
| 52 | );
|
| 53 | poseStack.popPose();
|
| 54 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public PaintingRenderState createRenderState() {
|
| 59 | return new PaintingRenderState();
|
| 60 | }
|
| 61 |
|
| 62 | public void extractRenderState(final Painting entity, final PaintingRenderState state, final float partialTicks) {
|
| 63 | super.extractRenderState(entity, state, partialTicks);
|
| 64 | Direction direction = entity.getDirection();
|
| 65 | PaintingVariant variant = entity.getVariant().value();
|
| 66 | state.direction = direction;
|
| 67 | state.variant = variant;
|
| 68 | int width = variant.width();
|
| 69 | int height = variant.height();
|
| 70 | if (state.lightCoordsPerBlock.length != width * height) {
|
| 71 | state.lightCoordsPerBlock = new int[width * height];
|
| 72 | }
|
| 73 |
|
| 74 | float offsetX = -width / 2.0F;
|
| 75 | float offsetY = -height / 2.0F;
|
| 76 | Level level = entity.level();
|
| 77 |
|
| 78 | for (int segmentY = 0; segmentY < height; segmentY++) {
|
| 79 | for (int segmentX = 0; segmentX < width; segmentX++) {
|
| 80 | float segmentOffsetX = segmentX + offsetX + 0.5F;
|
| 81 | float segmentOffsetY = segmentY + offsetY + 0.5F;
|
| 82 | int x = entity.getBlockX();
|
| 83 | int y = Mth.floor(entity.getY() + segmentOffsetY);
|
| 84 | int z = entity.getBlockZ();
|
| 85 | switch (direction) {
|
| 86 | case NORTH:
|
| 87 | x = Mth.floor(entity.getX() + segmentOffsetX);
|
| 88 | break;
|
| 89 | case WEST:
|
| 90 | z = Mth.floor(entity.getZ() - segmentOffsetX);
|
| 91 | break;
|
| 92 | case SOUTH:
|
| 93 | x = Mth.floor(entity.getX() - segmentOffsetX);
|
| 94 | break;
|
| 95 | case EAST:
|
| 96 | z = Mth.floor(entity.getZ() + segmentOffsetX);
|
| 97 | }
|
| 98 |
|
| 99 | state.lightCoordsPerBlock[segmentX + segmentY * width] = LightCoordsUtil.getLightCoords(level, new BlockPos(x, y, z));
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | private void renderPainting(
|
| 105 | final PoseStack poseStack,
|
| 106 | final SubmitNodeCollector submitNodeCollector,
|
| 107 | final RenderType renderType,
|
| 108 | final int[] lightCoordsMap,
|
| 109 | final int width,
|
| 110 | final int height,
|
| 111 | final TextureAtlasSprite front,
|
| 112 | final TextureAtlasSprite back
|
| 113 | ) {
|
| 114 | submitNodeCollector.submitCustomGeometry(poseStack, renderType, (pose, buffer) -> {
|
| 115 | float offsetX = -width / 2.0F;
|
| 116 | float offsetY = -height / 2.0F;
|
| 117 | float edgeHalfWidth = 0.03125F;
|
| 118 | float backU0 = back.getU0();
|
| 119 | float backU1 = back.getU1();
|
| 120 | float backV0 = back.getV0();
|
| 121 | float backV1 = back.getV1();
|
| 122 | float topBottomU0 = back.getU0();
|
| 123 | float topBottomU1 = back.getU1();
|
| 124 | float topBottomV0 = back.getV0();
|
| 125 | float topBottomV1 = back.getV(0.0625F);
|
| 126 | float leftRightU0 = back.getU0();
|
| 127 | float leftRightU1 = back.getU(0.0625F);
|
| 128 | float leftRightV0 = back.getV0();
|
| 129 | float leftRightV1 = back.getV1();
|
| 130 | double deltaU = 1.0 / width;
|
| 131 | double deltaV = 1.0 / height;
|
| 132 |
|
| 133 | for (int segmentX = 0; segmentX < width; segmentX++) {
|
| 134 | for (int segmentY = 0; segmentY < height; segmentY++) {
|
| 135 | float x0 = offsetX + (segmentX + 1);
|
| 136 | float x1 = offsetX + segmentX;
|
| 137 | float y0 = offsetY + (segmentY + 1);
|
| 138 | float y1 = offsetY + segmentY;
|
| 139 | int lightCoords = lightCoordsMap[segmentX + segmentY * width];
|
| 140 | float frontU0 = front.getU((float)(deltaU * (width - segmentX)));
|
| 141 | float frontU1 = front.getU((float)(deltaU * (width - (segmentX + 1))));
|
| 142 | float frontV0 = front.getV((float)(deltaV * (height - segmentY)));
|
| 143 | float frontV1 = front.getV((float)(deltaV * (height - (segmentY + 1))));
|
| 144 | vertex(pose, buffer, x0, y1, frontU1, frontV0, -0.03125F, 0, 0, -1, lightCoords);
|
| 145 | vertex(pose, buffer, x1, y1, frontU0, frontV0, -0.03125F, 0, 0, -1, lightCoords);
|
| 146 | vertex(pose, buffer, x1, y0, frontU0, frontV1, -0.03125F, 0, 0, -1, lightCoords);
|
| 147 | vertex(pose, buffer, x0, y0, frontU1, frontV1, -0.03125F, 0, 0, -1, lightCoords);
|
| 148 | vertex(pose, buffer, x0, y0, backU1, backV0, 0.03125F, 0, 0, 1, lightCoords);
|
| 149 | vertex(pose, buffer, x1, y0, backU0, backV0, 0.03125F, 0, 0, 1, lightCoords);
|
| 150 | vertex(pose, buffer, x1, y1, backU0, backV1, 0.03125F, 0, 0, 1, lightCoords);
|
| 151 | vertex(pose, buffer, x0, y1, backU1, backV1, 0.03125F, 0, 0, 1, lightCoords);
|
| 152 | if (segmentY == height - 1) {
|
| 153 | vertex(pose, buffer, x0, y0, topBottomU0, topBottomV0, -0.03125F, 0, 1, 0, lightCoords);
|
| 154 | vertex(pose, buffer, x1, y0, topBottomU1, topBottomV0, -0.03125F, 0, 1, 0, lightCoords);
|
| 155 | vertex(pose, buffer, x1, y0, topBottomU1, topBottomV1, 0.03125F, 0, 1, 0, lightCoords);
|
| 156 | vertex(pose, buffer, x0, y0, topBottomU0, topBottomV1, 0.03125F, 0, 1, 0, lightCoords);
|
| 157 | }
|
| 158 |
|
| 159 | if (segmentY == 0) {
|
| 160 | vertex(pose, buffer, x0, y1, topBottomU0, topBottomV0, 0.03125F, 0, -1, 0, lightCoords);
|
| 161 | vertex(pose, buffer, x1, y1, topBottomU1, topBottomV0, 0.03125F, 0, -1, 0, lightCoords);
|
| 162 | vertex(pose, buffer, x1, y1, topBottomU1, topBottomV1, -0.03125F, 0, -1, 0, lightCoords);
|
| 163 | vertex(pose, buffer, x0, y1, topBottomU0, topBottomV1, -0.03125F, 0, -1, 0, lightCoords);
|
| 164 | }
|
| 165 |
|
| 166 | if (segmentX == width - 1) {
|
| 167 | vertex(pose, buffer, x0, y0, leftRightU1, leftRightV0, 0.03125F, -1, 0, 0, lightCoords);
|
| 168 | vertex(pose, buffer, x0, y1, leftRightU1, leftRightV1, 0.03125F, -1, 0, 0, lightCoords);
|
| 169 | vertex(pose, buffer, x0, y1, leftRightU0, leftRightV1, -0.03125F, -1, 0, 0, lightCoords);
|
| 170 | vertex(pose, buffer, x0, y0, leftRightU0, leftRightV0, -0.03125F, -1, 0, 0, lightCoords);
|
| 171 | }
|
| 172 |
|
| 173 | if (segmentX == 0) {
|
| 174 | vertex(pose, buffer, x1, y0, leftRightU1, leftRightV0, -0.03125F, 1, 0, 0, lightCoords);
|
| 175 | vertex(pose, buffer, x1, y1, leftRightU1, leftRightV1, -0.03125F, 1, 0, 0, lightCoords);
|
| 176 | vertex(pose, buffer, x1, y1, leftRightU0, leftRightV1, 0.03125F, 1, 0, 0, lightCoords);
|
| 177 | vertex(pose, buffer, x1, y0, leftRightU0, leftRightV0, 0.03125F, 1, 0, 0, lightCoords);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 | });
|
| 182 | }
|
| 183 |
|
| 184 | private static void vertex(
|
| 185 | final PoseStack.Pose pose,
|
| 186 | final VertexConsumer buffer,
|
| 187 | final float x,
|
| 188 | final float y,
|
| 189 | final float u,
|
| 190 | final float v,
|
| 191 | final float z,
|
| 192 | final int nx,
|
| 193 | final int ny,
|
| 194 | final int nz,
|
| 195 | final int lightCoords
|
| 196 | ) {
|
| 197 | buffer.addVertex(pose, x, y, z).setColor(-1).setUv(u, v).setOverlay(OverlayTexture.NO_OVERLAY).setLight(lightCoords).setNormal(pose, nx, ny, nz);
|
| 198 | }
|
| 199 | }
|
| 200 |
|