| 1 | package net.minecraft.client.renderer.blockentity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Transformation;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.model.Model;
|
| 10 | import net.minecraft.client.model.geom.EntityModelSet;
|
| 11 | import net.minecraft.client.model.geom.ModelLayers;
|
| 12 | import net.minecraft.client.model.geom.ModelPart;
|
| 13 | import net.minecraft.client.renderer.Sheets;
|
| 14 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 15 | import net.minecraft.client.renderer.blockentity.state.ShulkerBoxRenderState;
|
| 16 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 17 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 18 | import net.minecraft.client.renderer.special.SpecialModelRenderer;
|
| 19 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 20 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 21 | import net.minecraft.client.resources.model.sprite.SpriteGetter;
|
| 22 | import net.minecraft.client.resources.model.sprite.SpriteId;
|
| 23 | import net.minecraft.core.Direction;
|
| 24 | import net.minecraft.util.Util;
|
| 25 | import net.minecraft.world.item.DyeColor;
|
| 26 | import net.minecraft.world.level.block.ShulkerBoxBlock;
|
| 27 | import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
|
| 28 | import net.minecraft.world.phys.Vec3;
|
| 29 | import org.joml.Matrix4f;
|
| 30 | import org.joml.Vector3fc;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class ShulkerBoxRenderer implements BlockEntityRenderer<ShulkerBoxBlockEntity, ShulkerBoxRenderState> {
|
| 35 | private static final Map<Direction, Transformation> TRANSFORMATIONS = Util.makeEnumMap(Direction.class, ShulkerBoxRenderer::createModelTransform);
|
| 36 | private final SpriteGetter sprites;
|
| 37 | private final ShulkerBoxRenderer.ShulkerBoxModel model;
|
| 38 |
|
| 39 | public ShulkerBoxRenderer(final BlockEntityRendererProvider.Context context) {
|
| 40 | this(context.entityModelSet(), context.sprites());
|
| 41 | }
|
| 42 |
|
| 43 | public ShulkerBoxRenderer(final SpecialModelRenderer.BakingContext context) {
|
| 44 | this(context.entityModelSet(), context.sprites());
|
| 45 | }
|
| 46 |
|
| 47 | public ShulkerBoxRenderer(final EntityModelSet context, final SpriteGetter sprites) {
|
| 48 | this.sprites = sprites;
|
| 49 | this.model = new ShulkerBoxRenderer.ShulkerBoxModel(context.bakeLayer(ModelLayers.SHULKER_BOX));
|
| 50 | }
|
| 51 |
|
| 52 | public ShulkerBoxRenderState createRenderState() {
|
| 53 | return new ShulkerBoxRenderState();
|
| 54 | }
|
| 55 |
|
| 56 | public void extractRenderState(
|
| 57 | final ShulkerBoxBlockEntity blockEntity,
|
| 58 | final ShulkerBoxRenderState state,
|
| 59 | final float partialTicks,
|
| 60 | final Vec3 cameraPosition,
|
| 61 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 62 | ) {
|
| 63 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 64 | state.direction = blockEntity.getBlockState().getValueOrElse(ShulkerBoxBlock.FACING, Direction.UP);
|
| 65 | state.color = blockEntity.getColor();
|
| 66 | state.progress = blockEntity.getProgress(partialTicks);
|
| 67 | }
|
| 68 |
|
| 69 | public void submit(final ShulkerBoxRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 70 | DyeColor color = state.color;
|
| 71 | SpriteId sprite;
|
| 72 | if (color == null) {
|
| 73 | sprite = Sheets.DEFAULT_SHULKER_TEXTURE_LOCATION;
|
| 74 | } else {
|
| 75 | sprite = Sheets.getShulkerBoxSprite(color);
|
| 76 | }
|
| 77 |
|
| 78 | this.submit(poseStack, submitNodeCollector, state.lightCoords, OverlayTexture.NO_OVERLAY, state.direction, state.progress, state.breakProgress, sprite, 0);
|
| 79 | }
|
| 80 |
|
| 81 | private void submit(
|
| 82 | final PoseStack poseStack,
|
| 83 | final SubmitNodeCollector submitNodeCollector,
|
| 84 | final int lightCoords,
|
| 85 | final int overlayCoords,
|
| 86 | final Direction direction,
|
| 87 | final float progress,
|
| 88 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress,
|
| 89 | final SpriteId sprite,
|
| 90 | final int outlineColor
|
| 91 | ) {
|
| 92 | poseStack.pushPose();
|
| 93 | poseStack.mulPose(modelTransform(direction));
|
| 94 | this.submit(poseStack, submitNodeCollector, lightCoords, overlayCoords, progress, breakProgress, sprite, outlineColor);
|
| 95 | poseStack.popPose();
|
| 96 | }
|
| 97 |
|
| 98 | public void submit(
|
| 99 | final PoseStack poseStack,
|
| 100 | final SubmitNodeCollector submitNodeCollector,
|
| 101 | final int lightCoords,
|
| 102 | final int overlayCoords,
|
| 103 | final float progress,
|
| 104 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress,
|
| 105 | final SpriteId sprite,
|
| 106 | final int outlineColor
|
| 107 | ) {
|
| 108 | this.model.setupAnim(progress);
|
| 109 | submitNodeCollector.submitModel(this.model, progress, poseStack, lightCoords, overlayCoords, -1, sprite, this.sprites, outlineColor);
|
| 110 | if (breakProgress != null) {
|
| 111 | submitNodeCollector.order(1)
|
| 112 | .submitCrumblingOverlay(this.model, progress, poseStack, sprite.renderType(this.model.renderType()), lightCoords, overlayCoords, -1, breakProgress);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private static Transformation createModelTransform(final Direction direction) {
|
| 117 | float scale = 0.9995F;
|
| 118 | return new Transformation(
|
| 119 | new Matrix4f()
|
| 120 | .translation(0.5F, 0.5F, 0.5F)
|
| 121 | .scale(0.9995F, 0.9995F, 0.9995F)
|
| 122 | .rotate(direction.getRotation())
|
| 123 | .scale(1.0F, -1.0F, -1.0F)
|
| 124 | .translate(0.0F, -1.0F, 0.0F)
|
| 125 | );
|
| 126 | }
|
| 127 |
|
| 128 | public static Transformation modelTransform(final Direction direction) {
|
| 129 | return TRANSFORMATIONS.get(direction);
|
| 130 | }
|
| 131 |
|
| 132 | public void getExtents(final float progress, final Consumer<Vector3fc> output) {
|
| 133 | PoseStack poseStack = new PoseStack();
|
| 134 | this.model.setupAnim(progress);
|
| 135 | this.model.root().getExtentsForGui(poseStack, output);
|
| 136 | }
|
| 137 |
|
| 138 | @Environment(EnvType.CLIENT)
|
| 139 | private static class ShulkerBoxModel extends Model<Float> {
|
| 140 | private final ModelPart lid;
|
| 141 |
|
| 142 | public ShulkerBoxModel(final ModelPart root) {
|
| 143 | super(root, RenderTypes::entityCutout);
|
| 144 | this.lid = root.getChild("lid");
|
| 145 | }
|
| 146 |
|
| 147 | public void setupAnim(final Float progress) {
|
| 148 | super.setupAnim(progress);
|
| 149 | this.lid.setPos(0.0F, 24.0F - progress * 0.5F * 16.0F, 0.0F);
|
| 150 | this.lid.yRot = 270.0F * progress * (float) (Math.PI / 180.0);
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|