| 1 | package net.minecraft.client.renderer.blockentity;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 5 | import com.mojang.math.Axis;
|
| 6 | import com.mojang.math.Transformation;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.model.geom.EntityModelSet;
|
| 12 | import net.minecraft.client.model.geom.ModelLayers;
|
| 13 | import net.minecraft.client.model.object.skull.DragonHeadModel;
|
| 14 | import net.minecraft.client.model.object.skull.PiglinHeadModel;
|
| 15 | import net.minecraft.client.model.object.skull.SkullModel;
|
| 16 | import net.minecraft.client.model.object.skull.SkullModelBase;
|
| 17 | import net.minecraft.client.renderer.PlayerSkinRenderCache;
|
| 18 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 19 | import net.minecraft.client.renderer.blockentity.state.SkullBlockRenderState;
|
| 20 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 21 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 22 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 23 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 24 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 25 | import net.minecraft.client.resources.DefaultPlayerSkin;
|
| 26 | import net.minecraft.core.Direction;
|
| 27 | import net.minecraft.resources.Identifier;
|
| 28 | import net.minecraft.util.Util;
|
| 29 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 30 | import net.minecraft.world.level.block.AbstractSkullBlock;
|
| 31 | import net.minecraft.world.level.block.SkullBlock;
|
| 32 | import net.minecraft.world.level.block.WallSkullBlock;
|
| 33 | import net.minecraft.world.level.block.SkullBlock.Type;
|
| 34 | import net.minecraft.world.level.block.SkullBlock.Types;
|
| 35 | import net.minecraft.world.level.block.entity.SkullBlockEntity;
|
| 36 | import net.minecraft.world.level.block.state.BlockState;
|
| 37 | import net.minecraft.world.level.block.state.properties.RotationSegment;
|
| 38 | import net.minecraft.world.phys.Vec3;
|
| 39 | import org.joml.Matrix4f;
|
| 40 | import org.joml.Vector3f;
|
| 41 | import org.jspecify.annotations.Nullable;
|
| 42 |
|
| 43 | @Environment(EnvType.CLIENT)
|
| 44 | public class SkullBlockRenderer implements BlockEntityRenderer<SkullBlockEntity, SkullBlockRenderState> {
|
| 45 | public static final WallAndGroundTransformations<Transformation> TRANSFORMATIONS = new WallAndGroundTransformations<>(
|
| 46 | SkullBlockRenderer::createWallTransformation, SkullBlockRenderer::createGroundTransformation, 16
|
| 47 | );
|
| 48 | private final Function<Type, SkullModelBase> modelByType;
|
| 49 | private static final Map<Type, Identifier> SKIN_BY_TYPE = Util.make(Maps.newHashMap(), map -> {
|
| 50 | map.put(Types.SKELETON, Identifier.withDefaultNamespace("textures/entity/skeleton/skeleton.png"));
|
| 51 | map.put(Types.WITHER_SKELETON, Identifier.withDefaultNamespace("textures/entity/skeleton/wither_skeleton.png"));
|
| 52 | map.put(Types.ZOMBIE, Identifier.withDefaultNamespace("textures/entity/zombie/zombie.png"));
|
| 53 | map.put(Types.CREEPER, Identifier.withDefaultNamespace("textures/entity/creeper/creeper.png"));
|
| 54 | map.put(Types.DRAGON, Identifier.withDefaultNamespace("textures/entity/enderdragon/dragon.png"));
|
| 55 | map.put(Types.PIGLIN, Identifier.withDefaultNamespace("textures/entity/piglin/piglin.png"));
|
| 56 | map.put(Types.PLAYER, DefaultPlayerSkin.getDefaultTexture());
|
| 57 | });
|
| 58 | private final PlayerSkinRenderCache playerSkinRenderCache;
|
| 59 |
|
| 60 | public static @Nullable SkullModelBase createModel(final EntityModelSet modelSet, final Type type) {
|
| 61 | if (type instanceof Types vanillaType) {
|
| 62 | return switch (vanillaType) {
|
| 63 | case SKELETON -> new SkullModel(modelSet.bakeLayer(ModelLayers.SKELETON_SKULL));
|
| 64 | case WITHER_SKELETON -> new SkullModel(modelSet.bakeLayer(ModelLayers.WITHER_SKELETON_SKULL));
|
| 65 | case PLAYER -> new SkullModel(modelSet.bakeLayer(ModelLayers.PLAYER_HEAD));
|
| 66 | case ZOMBIE -> new SkullModel(modelSet.bakeLayer(ModelLayers.ZOMBIE_HEAD));
|
| 67 | case CREEPER -> new SkullModel(modelSet.bakeLayer(ModelLayers.CREEPER_HEAD));
|
| 68 | case DRAGON -> new DragonHeadModel(modelSet.bakeLayer(ModelLayers.DRAGON_SKULL));
|
| 69 | case PIGLIN -> new PiglinHeadModel(modelSet.bakeLayer(ModelLayers.PIGLIN_HEAD));
|
| 70 | };
|
| 71 | } else {
|
| 72 | return null;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public SkullBlockRenderer(final BlockEntityRendererProvider.Context context) {
|
| 77 | EntityModelSet modelSet = context.entityModelSet();
|
| 78 | this.playerSkinRenderCache = context.playerSkinRenderCache();
|
| 79 | this.modelByType = Util.memoize(type -> createModel(modelSet, type));
|
| 80 | }
|
| 81 |
|
| 82 | public SkullBlockRenderState createRenderState() {
|
| 83 | return new SkullBlockRenderState();
|
| 84 | }
|
| 85 |
|
| 86 | public void extractRenderState(
|
| 87 | final SkullBlockEntity blockEntity,
|
| 88 | final SkullBlockRenderState state,
|
| 89 | final float partialTicks,
|
| 90 | final Vec3 cameraPosition,
|
| 91 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 92 | ) {
|
| 93 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 94 | state.animationProgress = blockEntity.getAnimation(partialTicks);
|
| 95 | BlockState blockState = blockEntity.getBlockState();
|
| 96 | if (blockState.getBlock() instanceof WallSkullBlock) {
|
| 97 | Direction facing = blockState.getValue(WallSkullBlock.FACING);
|
| 98 | state.transformation = TRANSFORMATIONS.wallTransformation(facing);
|
| 99 | } else {
|
| 100 | state.transformation = TRANSFORMATIONS.freeTransformations(blockState.getValue(SkullBlock.ROTATION));
|
| 101 | }
|
| 102 |
|
| 103 | state.skullType = ((AbstractSkullBlock)blockState.getBlock()).getType();
|
| 104 | state.renderType = this.resolveSkullRenderType(state.skullType, blockEntity);
|
| 105 | }
|
| 106 |
|
| 107 | public void submit(final SkullBlockRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 108 | SkullModelBase model = this.modelByType.apply(state.skullType);
|
| 109 | poseStack.pushPose();
|
| 110 | poseStack.mulPose(state.transformation);
|
| 111 | submitSkull(state.animationProgress, poseStack, submitNodeCollector, state.lightCoords, model, state.renderType, 0, state.breakProgress);
|
| 112 | poseStack.popPose();
|
| 113 | }
|
| 114 |
|
| 115 | public static void submitSkull(
|
| 116 | final float animationValue,
|
| 117 | final PoseStack poseStack,
|
| 118 | final SubmitNodeCollector submitNodeCollector,
|
| 119 | final int lightCoords,
|
| 120 | final SkullModelBase model,
|
| 121 | final RenderType renderType,
|
| 122 | final int outlineColor,
|
| 123 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 124 | ) {
|
| 125 | SkullModelBase.State modelState = new SkullModelBase.State();
|
| 126 | modelState.animationPos = animationValue;
|
| 127 | submitNodeCollector.submitModel(model, modelState, poseStack, renderType, lightCoords, OverlayTexture.NO_OVERLAY, outlineColor);
|
| 128 | if (breakProgress != null) {
|
| 129 | submitNodeCollector.order(1).submitCrumblingOverlay(model, modelState, poseStack, renderType, lightCoords, OverlayTexture.NO_OVERLAY, -1, breakProgress);
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | private RenderType resolveSkullRenderType(final Type type, final SkullBlockEntity entity) {
|
| 134 | if (type == Types.PLAYER) {
|
| 135 | ResolvableProfile ownerProfile = entity.getOwnerProfile();
|
| 136 | if (ownerProfile != null) {
|
| 137 | return this.playerSkinRenderCache.getOrDefault(ownerProfile).renderType();
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | return getSkullRenderType(type, null);
|
| 142 | }
|
| 143 |
|
| 144 | public static RenderType getSkullRenderType(final Type type, final @Nullable Identifier texture) {
|
| 145 | return RenderTypes.entityCutoutZOffset(texture != null ? texture : SKIN_BY_TYPE.get(type));
|
| 146 | }
|
| 147 |
|
| 148 | public static RenderType getPlayerSkinRenderType(final Identifier texture) {
|
| 149 | return RenderTypes.entityTranslucent(texture);
|
| 150 | }
|
| 151 |
|
| 152 | public static RenderType getPlayerSkinRenderTypeCutout(final Identifier texture) {
|
| 153 | return RenderTypes.entityCutout(texture);
|
| 154 | }
|
| 155 |
|
| 156 | private static Transformation createWallTransformation(final Direction wallDirection) {
|
| 157 | float offset = 0.25F;
|
| 158 | return new Transformation(
|
| 159 | new Vector3f(0.5F - wallDirection.getStepX() * 0.25F, 0.25F, 0.5F - wallDirection.getStepZ() * 0.25F),
|
| 160 | Axis.YP.rotationDegrees(-wallDirection.getOpposite().toYRot()),
|
| 161 | new Vector3f(-1.0F, -1.0F, 1.0F),
|
| 162 | null
|
| 163 | );
|
| 164 | }
|
| 165 |
|
| 166 | private static Transformation createGroundTransformation(final int segment) {
|
| 167 | return new Transformation(
|
| 168 | new Matrix4f().translation(0.5F, 0.0F, 0.5F).rotate(Axis.YP.rotationDegrees(-RotationSegment.convertToDegrees(segment))).scale(-1.0F, -1.0F, 1.0F)
|
| 169 | );
|
| 170 | }
|
| 171 | }
|
| 172 |
|