| 1 | package net.minecraft.client.renderer.entity;
|
| 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.renderer.SubmitNodeCollector;
|
| 8 | import net.minecraft.client.renderer.entity.state.ItemClusterRenderState;
|
| 9 | import net.minecraft.client.renderer.entity.state.ItemEntityRenderState;
|
| 10 | import net.minecraft.client.renderer.item.ItemModelResolver;
|
| 11 | import net.minecraft.client.renderer.item.ItemStackRenderState;
|
| 12 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 13 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.world.entity.item.ItemEntity;
|
| 17 | import net.minecraft.world.phys.AABB;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class ItemEntityRenderer extends EntityRenderer<ItemEntity, ItemEntityRenderState> {
|
| 21 | private static final float ITEM_MIN_HOVER_HEIGHT = 0.0625F;
|
| 22 | private static final float ITEM_BUNDLE_OFFSET_SCALE = 0.15F;
|
| 23 | private static final float FLAT_ITEM_DEPTH_THRESHOLD = 0.0625F;
|
| 24 | private final ItemModelResolver itemModelResolver;
|
| 25 | private final RandomSource random = RandomSource.create();
|
| 26 |
|
| 27 | public ItemEntityRenderer(final EntityRendererProvider.Context context) {
|
| 28 | super(context);
|
| 29 | this.itemModelResolver = context.getItemModelResolver();
|
| 30 | this.shadowRadius = 0.15F;
|
| 31 | this.shadowStrength = 0.75F;
|
| 32 | }
|
| 33 |
|
| 34 | public ItemEntityRenderState createRenderState() {
|
| 35 | return new ItemEntityRenderState();
|
| 36 | }
|
| 37 |
|
| 38 | public void extractRenderState(final ItemEntity entity, final ItemEntityRenderState state, final float partialTicks) {
|
| 39 | super.extractRenderState(entity, state, partialTicks);
|
| 40 | state.bobOffset = entity.bobOffs;
|
| 41 | state.extractItemGroupRenderState(entity, entity.getItem(), this.itemModelResolver);
|
| 42 | }
|
| 43 |
|
| 44 | public void submit(final ItemEntityRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 45 | if (!state.item.isEmpty()) {
|
| 46 | poseStack.pushPose();
|
| 47 | AABB boundingBox = state.item.getModelBoundingBox();
|
| 48 | float minOffsetY = -((float)boundingBox.minY) + 0.0625F;
|
| 49 | float bob = Mth.sin(state.ageInTicks / 10.0F + state.bobOffset) * 0.1F + 0.1F;
|
| 50 | poseStack.translate(0.0F, bob + minOffsetY, 0.0F);
|
| 51 | float spin = ItemEntity.getSpin(state.ageInTicks, state.bobOffset);
|
| 52 | poseStack.rotate(Axis.YP, spin);
|
| 53 | submitMultipleFromCount(poseStack, submitNodeCollector, state.lightCoords, state, this.random, boundingBox);
|
| 54 | poseStack.popPose();
|
| 55 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | public static void submitMultipleFromCount(
|
| 60 | final PoseStack poseStack,
|
| 61 | final SubmitNodeCollector submitNodeCollector,
|
| 62 | final int lightCoords,
|
| 63 | final ItemClusterRenderState state,
|
| 64 | final RandomSource random
|
| 65 | ) {
|
| 66 | submitMultipleFromCount(poseStack, submitNodeCollector, lightCoords, state, random, state.item.getModelBoundingBox());
|
| 67 | }
|
| 68 |
|
| 69 | public static void submitMultipleFromCount(
|
| 70 | final PoseStack poseStack,
|
| 71 | final SubmitNodeCollector submitNodeCollector,
|
| 72 | final int lightCoords,
|
| 73 | final ItemClusterRenderState state,
|
| 74 | final RandomSource random,
|
| 75 | final AABB modelBoundingBox
|
| 76 | ) {
|
| 77 | int amount = state.count;
|
| 78 | if (amount != 0) {
|
| 79 | random.setSeed(state.seed);
|
| 80 | ItemStackRenderState item = state.item;
|
| 81 | float modelDepth = (float)modelBoundingBox.getZsize();
|
| 82 | if (modelDepth > 0.0625F) {
|
| 83 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 84 |
|
| 85 | for (int i = 1; i < amount; i++) {
|
| 86 | poseStack.pushPose();
|
| 87 | float xo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F;
|
| 88 | float yo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F;
|
| 89 | float zo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F;
|
| 90 | poseStack.translate(xo, yo, zo);
|
| 91 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 92 | poseStack.popPose();
|
| 93 | }
|
| 94 | } else {
|
| 95 | float offsetZ = modelDepth * 1.5F;
|
| 96 | poseStack.translate(0.0F, 0.0F, -(offsetZ * (amount - 1) / 2.0F));
|
| 97 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 98 | poseStack.translate(0.0F, 0.0F, offsetZ);
|
| 99 |
|
| 100 | for (int i = 1; i < amount; i++) {
|
| 101 | poseStack.pushPose();
|
| 102 | float xo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F * 0.5F;
|
| 103 | float yo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F * 0.5F;
|
| 104 | poseStack.translate(xo, yo, 0.0F);
|
| 105 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 106 | poseStack.popPose();
|
| 107 | poseStack.translate(0.0F, 0.0F, offsetZ);
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | public static void renderMultipleFromCount(
|
| 114 | final PoseStack poseStack,
|
| 115 | final SubmitNodeCollector submitNodeCollector,
|
| 116 | final int lightCoords,
|
| 117 | final ItemClusterRenderState state,
|
| 118 | final RandomSource random
|
| 119 | ) {
|
| 120 | AABB modelBoundingBox = state.item.getModelBoundingBox();
|
| 121 | int amount = state.count;
|
| 122 | if (amount != 0) {
|
| 123 | random.setSeed(state.seed);
|
| 124 | ItemStackRenderState item = state.item;
|
| 125 | float modelDepth = (float)modelBoundingBox.getZsize();
|
| 126 | if (modelDepth > 0.0625F) {
|
| 127 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 128 |
|
| 129 | for (int i = 1; i < amount; i++) {
|
| 130 | poseStack.pushPose();
|
| 131 | float xo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F;
|
| 132 | float yo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F;
|
| 133 | float zo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F;
|
| 134 | poseStack.translate(xo, yo, zo);
|
| 135 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 136 | poseStack.popPose();
|
| 137 | }
|
| 138 | } else {
|
| 139 | float offsetZ = modelDepth * 1.5F;
|
| 140 | poseStack.translate(0.0F, 0.0F, -(offsetZ * (amount - 1) / 2.0F));
|
| 141 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 142 | poseStack.translate(0.0F, 0.0F, offsetZ);
|
| 143 |
|
| 144 | for (int i = 1; i < amount; i++) {
|
| 145 | poseStack.pushPose();
|
| 146 | float xo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F * 0.5F;
|
| 147 | float yo = (random.nextFloat() * 2.0F - 1.0F) * 0.15F * 0.5F;
|
| 148 | poseStack.translate(xo, yo, 0.0F);
|
| 149 | item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 150 | poseStack.popPose();
|
| 151 | poseStack.translate(0.0F, 0.0F, offsetZ);
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|