| 1 | package net.minecraft.client.renderer.item;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.mojang.math.Transformation;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 8 | import java.util.HashSet;
|
| 9 | import java.util.Iterator;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.function.Supplier;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.color.item.ItemTintSource;
|
| 17 | import net.minecraft.client.color.item.ItemTintSources;
|
| 18 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 19 | import net.minecraft.client.renderer.block.dispatch.BlockModelRotation;
|
| 20 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 21 | import net.minecraft.client.resources.model.ModelBaker;
|
| 22 | import net.minecraft.client.resources.model.ResolvableModel;
|
| 23 | import net.minecraft.client.resources.model.ResolvedModel;
|
| 24 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 25 | import net.minecraft.client.resources.model.geometry.ItemQuads;
|
| 26 | import net.minecraft.client.resources.model.geometry.QuadCollection;
|
| 27 | import net.minecraft.client.resources.model.sprite.TextureSlots;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.tags.ItemTags;
|
| 30 | import net.minecraft.world.entity.ItemOwner;
|
| 31 | import net.minecraft.world.item.ItemDisplayContext;
|
| 32 | import net.minecraft.world.item.ItemStack;
|
| 33 | import net.minecraft.world.item.Items;
|
| 34 | import org.joml.Matrix4fc;
|
| 35 | import org.joml.Vector3fc;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | @Environment(EnvType.CLIENT)
|
| 39 | public class CuboidItemModelWrapper implements ItemModel {
|
| 40 | private final List<ItemTintSource> tints;
|
| 41 | private final boolean animated;
|
| 42 | private final ItemQuads itemQuads;
|
| 43 | private final Supplier<Vector3fc[]> extents;
|
| 44 | private final ModelRenderProperties properties;
|
| 45 | private final Matrix4fc transformation;
|
| 46 |
|
| 47 | private CuboidItemModelWrapper(
|
| 48 | final List<ItemTintSource> tints, final QuadCollection quads, final ModelRenderProperties properties, final Matrix4fc transformation
|
| 49 | ) {
|
| 50 | this.tints = tints;
|
| 51 | this.animated = quads.hasMaterialFlag(2);
|
| 52 | this.itemQuads = ItemQuads.split(quads.getAll());
|
| 53 | this.properties = properties;
|
| 54 | this.transformation = transformation;
|
| 55 | this.extents = Suppliers.memoize(() -> computeExtents(quads.getAll()));
|
| 56 | }
|
| 57 |
|
| 58 | public static Vector3fc[] computeExtents(final List<BakedQuad> quads) {
|
| 59 | Set<Vector3fc> result = new HashSet<>();
|
| 60 |
|
| 61 | for (BakedQuad quad : quads) {
|
| 62 | for (int vertex = 0; vertex < 4; vertex++) {
|
| 63 | result.add(quad.position(vertex));
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | return result.toArray(Vector3fc[]::new);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void update(
|
| 72 | final ItemStackRenderState output,
|
| 73 | final ItemStack item,
|
| 74 | final ItemModelResolver resolver,
|
| 75 | final ItemDisplayContext displayContext,
|
| 76 | final @Nullable ClientLevel level,
|
| 77 | final @Nullable ItemOwner owner,
|
| 78 | final int seed
|
| 79 | ) {
|
| 80 | output.appendModelIdentityElement(this);
|
| 81 | ItemStackRenderState.LayerRenderState layer = output.newLayer();
|
| 82 | if (item.hasFoil()) {
|
| 83 | ItemStackRenderState.FoilType foilType = hasSpecialAnimatedTexture(item) ? ItemStackRenderState.FoilType.SPECIAL : ItemStackRenderState.FoilType.STANDARD;
|
| 84 | layer.setFoilType(foilType);
|
| 85 | output.setAnimated();
|
| 86 | output.appendModelIdentityElement(foilType);
|
| 87 | }
|
| 88 |
|
| 89 | if (!this.tints.isEmpty()) {
|
| 90 | IntList tintLayers = layer.tintLayers();
|
| 91 |
|
| 92 | for (ItemTintSource tintSource : this.tints) {
|
| 93 | int tint = tintSource.calculate(item, level, owner == null ? null : owner.asLivingEntity());
|
| 94 | tintLayers.add(tint);
|
| 95 | output.appendModelIdentityElement(tint);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | layer.setExtents(this.extents);
|
| 100 | layer.setLocalTransform(this.transformation);
|
| 101 | this.properties.applyToLayer(layer, displayContext);
|
| 102 | layer.setQuads(this.itemQuads);
|
| 103 | if (this.animated) {
|
| 104 | output.setAnimated();
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private static void validateAtlasUsage(final List<BakedQuad> quads) {
|
| 109 | Iterator<BakedQuad> quadIterator = quads.iterator();
|
| 110 | if (quadIterator.hasNext()) {
|
| 111 | Identifier expectedAtlas = quadIterator.next().materialInfo().sprite().atlasLocation();
|
| 112 |
|
| 113 | while (quadIterator.hasNext()) {
|
| 114 | BakedQuad quad = quadIterator.next();
|
| 115 | Identifier quadAtlas = quad.materialInfo().sprite().atlasLocation();
|
| 116 | if (!quadAtlas.equals(expectedAtlas)) {
|
| 117 | throw new IllegalStateException("Multiple atlases used in model, expected " + expectedAtlas + ", but also got " + quadAtlas);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | if (!expectedAtlas.equals(TextureAtlas.LOCATION_ITEMS) && !expectedAtlas.equals(TextureAtlas.LOCATION_BLOCKS)) {
|
| 122 | throw new IllegalArgumentException("Atlas " + expectedAtlas + " can't be usef for item models");
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | private static boolean hasSpecialAnimatedTexture(final ItemStack itemStack) {
|
| 128 | return itemStack.is(ItemTags.COMPASSES) || itemStack.is(Items.CLOCK);
|
| 129 | }
|
| 130 |
|
| 131 | @Environment(EnvType.CLIENT)
|
| 132 | public record Unbaked(Identifier model, Optional<Transformation> transformation, List<ItemTintSource> tints) implements ItemModel.Unbaked {
|
| 133 | public static final MapCodec<CuboidItemModelWrapper.Unbaked> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 134 | i -> i.group(
|
| 135 | Identifier.CODEC.fieldOf("model").forGetter(CuboidItemModelWrapper.Unbaked::model),
|
| 136 | Transformation.EXTENDED_CODEC.optionalFieldOf("transformation").forGetter(CuboidItemModelWrapper.Unbaked::transformation),
|
| 137 | ItemTintSources.CODEC.listOf().optionalFieldOf("tints", List.of()).forGetter(CuboidItemModelWrapper.Unbaked::tints)
|
| 138 | )
|
| 139 | .apply(i, CuboidItemModelWrapper.Unbaked::new)
|
| 140 | );
|
| 141 |
|
| 142 | @Override
|
| 143 | public void resolveDependencies(final ResolvableModel.Resolver resolver) {
|
| 144 | resolver.markDependency(this.model);
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public ItemModel bake(final ItemModel.BakingContext context, final Matrix4fc transformation) {
|
| 149 | ModelBaker baker = context.blockModelBaker();
|
| 150 | ResolvedModel resolvedModel = baker.getModel(this.model);
|
| 151 | TextureSlots textureSlots = resolvedModel.getTopTextureSlots();
|
| 152 | QuadCollection quads = resolvedModel.bakeTopGeometry(textureSlots, baker, BlockModelRotation.IDENTITY);
|
| 153 | ModelRenderProperties properties = ModelRenderProperties.fromResolvedModel(baker, resolvedModel, textureSlots);
|
| 154 | CuboidItemModelWrapper.validateAtlasUsage(quads.getAll());
|
| 155 | Matrix4fc modelTransform = Transformation.compose(transformation, this.transformation);
|
| 156 | return new CuboidItemModelWrapper(this.tints, quads, properties, modelTransform);
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public MapCodec<CuboidItemModelWrapper.Unbaked> type() {
|
| 161 | return MAP_CODEC;
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|