| 1 | package net.minecraft.client.renderer.feature;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.QuadInstance;
|
| 5 | import com.mojang.math.MatrixUtil;
|
| 6 | import java.util.List;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.renderer.feature.submit.TranslucentSubmit;
|
| 10 | import net.minecraft.client.renderer.item.ItemStackRenderState;
|
| 11 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 12 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.world.item.ItemDisplayContext;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class ItemFeatureRenderer extends RenderTypeFeatureRenderer<ItemFeatureRenderer.Submit> {
|
| 18 | public static final FeatureRendererType<ItemFeatureRenderer.Submit> TYPE = FeatureRendererType.create("Item");
|
| 19 | public static final Identifier ENCHANTED_GLINT_ARMOR = Identifier.withDefaultNamespace("textures/misc/enchanted_glint_armor.png");
|
| 20 | public static final Identifier ENCHANTED_GLINT_ITEM = Identifier.withDefaultNamespace("textures/misc/enchanted_glint_item.png");
|
| 21 | private static final float SPECIAL_FOIL_UI_SCALE = 0.5F;
|
| 22 | private static final float SPECIAL_FOIL_FIRST_PERSON_SCALE = 0.75F;
|
| 23 | public static final float SPECIAL_FOIL_TEXTURE_SCALE = 0.0078125F;
|
| 24 | public static final int NO_TINT = -1;
|
| 25 | private final QuadInstance quadInstance = new QuadInstance();
|
| 26 |
|
| 27 | @Override
|
| 28 | protected void buildGroup(final FeatureFrameContext context, final List<ItemFeatureRenderer.Submit> submits) {
|
| 29 | for (ItemFeatureRenderer.Submit submit : submits) {
|
| 30 | this.prepareSubmit(submit);
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | private void prepareSubmit(final ItemFeatureRenderer.Submit submit) {
|
| 35 | if (submit.outlineColor() != 0) {
|
| 36 | this.prepareOutlineSubmit(submit);
|
| 37 | } else {
|
| 38 | this.prepareMainSubmit(submit);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | private void prepareMainSubmit(final ItemFeatureRenderer.Submit submit) {
|
| 43 | this.quadInstance.setLightCoords(submit.lightCoords());
|
| 44 | this.quadInstance.setOverlayCoords(submit.overlayCoords());
|
| 45 | ItemStackRenderState.FoilType foilType = submit.foilType();
|
| 46 | PoseStack.Pose foilDecalPose = foilType == ItemStackRenderState.FoilType.SPECIAL ? computeFoilDecalPose(submit.displayContext(), submit.pose()) : null;
|
| 47 |
|
| 48 | for (BakedQuad quad : submit.quads()) {
|
| 49 | BakedQuad.MaterialInfo material = quad.materialInfo();
|
| 50 |
|
| 51 | RenderType renderType = switch (foilType) {
|
| 52 | case NONE -> material.itemRenderType();
|
| 53 | case STANDARD -> material.itemGlintRenderType();
|
| 54 | case SPECIAL -> material.itemGlintSpecialRenderType();
|
| 55 | };
|
| 56 | this.quadInstance.setColor(getLayerColorSafe(submit.tintLayers(), material));
|
| 57 | if (foilType == ItemStackRenderState.FoilType.SPECIAL) {
|
| 58 | this.getVertexBuilder(renderType).putBakedQuadWithGlint(submit.pose(), quad, this.quadInstance, foilDecalPose);
|
| 59 | } else {
|
| 60 | this.getVertexBuilder(renderType).putBakedQuad(submit.pose(), quad, this.quadInstance);
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | private void prepareOutlineSubmit(final ItemFeatureRenderer.Submit submit) {
|
| 66 | for (BakedQuad quad : submit.quads()) {
|
| 67 | BakedQuad.MaterialInfo material = quad.materialInfo();
|
| 68 | RenderType renderType = material.itemRenderType().outline().orElse(null);
|
| 69 | if (renderType != null) {
|
| 70 | this.quadInstance.setColor(submit.outlineColor());
|
| 71 | this.getVertexBuilder(renderType).putBakedQuad(submit.pose(), quad, this.quadInstance);
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static PoseStack.Pose computeFoilDecalPose(final ItemDisplayContext type, final PoseStack.Pose pose) {
|
| 77 | PoseStack.Pose foilDecalPose = pose.copy();
|
| 78 | if (type == ItemDisplayContext.GUI) {
|
| 79 | MatrixUtil.mulComponentWise(foilDecalPose.pose(), 0.5F);
|
| 80 | } else if (type.firstPerson()) {
|
| 81 | MatrixUtil.mulComponentWise(foilDecalPose.pose(), 0.75F);
|
| 82 | }
|
| 83 |
|
| 84 | return foilDecalPose;
|
| 85 | }
|
| 86 |
|
| 87 | private static int getLayerColorSafe(final int[] layers, final int layer) {
|
| 88 | return layer >= 0 && layer < layers.length ? layers[layer] : -1;
|
| 89 | }
|
| 90 |
|
| 91 | private static int getLayerColorSafe(final int[] tintLayers, final BakedQuad.MaterialInfo material) {
|
| 92 | return material.isTinted() ? getLayerColorSafe(tintLayers, material.tintIndex()) : -1;
|
| 93 | }
|
| 94 |
|
| 95 | @Environment(EnvType.CLIENT)
|
| 96 | public record Submit(
|
| 97 | PoseStack.Pose pose,
|
| 98 | ItemDisplayContext displayContext,
|
| 99 | int lightCoords,
|
| 100 | int overlayCoords,
|
| 101 | int outlineColor,
|
| 102 | int[] tintLayers,
|
| 103 | List<BakedQuad> quads,
|
| 104 | ItemStackRenderState.FoilType foilType
|
| 105 | ) implements TranslucentSubmit {
|
| 106 | @Override
|
| 107 | public float distanceToCameraSq() {
|
| 108 | return TranslucentSubmit.computeDistanceToCameraSq(this.pose.pose());
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public FeatureRendererType<ItemFeatureRenderer.Submit> featureType() {
|
| 113 | return ItemFeatureRenderer.TYPE;
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|