| 1 | package net.minecraft.client.resources.model.cuboid;
|
| 2 |
|
| 3 | import com.mojang.math.Quadrant;
|
| 4 | import java.util.Collection;
|
| 5 | import java.util.HashSet;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Set;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.renderer.block.dispatch.ModelState;
|
| 11 | import net.minecraft.client.renderer.texture.SpriteContents;
|
| 12 | import net.minecraft.client.resources.model.ModelBaker;
|
| 13 | import net.minecraft.client.resources.model.ModelDebugName;
|
| 14 | import net.minecraft.client.resources.model.UnbakedModel;
|
| 15 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 16 | import net.minecraft.client.resources.model.geometry.QuadCollection;
|
| 17 | import net.minecraft.client.resources.model.geometry.UnbakedGeometry;
|
| 18 | import net.minecraft.client.resources.model.sprite.Material;
|
| 19 | import net.minecraft.client.resources.model.sprite.TextureSlots;
|
| 20 | import net.minecraft.client.resources.model.sprite.TextureSlots.Data.Builder;
|
| 21 | import net.minecraft.core.Direction;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import org.joml.Vector3f;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class ItemModelGenerator implements UnbakedModel {
|
| 28 | public static final Identifier GENERATED_ITEM_MODEL_ID = Identifier.withDefaultNamespace("builtin/generated");
|
| 29 | public static final List<String> LAYERS = List.of("layer0", "layer1", "layer2", "layer3", "layer4");
|
| 30 | private static final float MIN_Z = 7.5F;
|
| 31 | private static final float MAX_Z = 8.5F;
|
| 32 | private static final TextureSlots.Data TEXTURE_SLOTS = new Builder().addReference("particle", "layer0").build();
|
| 33 | private static final CuboidFace.UVs SOUTH_FACE_UVS = new CuboidFace.UVs(0.0F, 0.0F, 16.0F, 16.0F);
|
| 34 | private static final CuboidFace.UVs NORTH_FACE_UVS = new CuboidFace.UVs(16.0F, 0.0F, 0.0F, 16.0F);
|
| 35 | private static final float UV_SHRINK = 0.1F;
|
| 36 |
|
| 37 | @Override
|
| 38 | public TextureSlots.Data textureSlots() {
|
| 39 | return TEXTURE_SLOTS;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public UnbakedGeometry geometry() {
|
| 44 | return ItemModelGenerator::bake;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public UnbakedModel.@Nullable GuiLight guiLight() {
|
| 49 | return UnbakedModel.GuiLight.FRONT;
|
| 50 | }
|
| 51 |
|
| 52 | private static QuadCollection bake(final TextureSlots textureSlots, final ModelBaker modelBaker, final ModelState modelState, final ModelDebugName name) {
|
| 53 | QuadCollection singleResult = null;
|
| 54 | QuadCollection.Builder builder = null;
|
| 55 |
|
| 56 | for (int layerIndex = 0; layerIndex < LAYERS.size(); layerIndex++) {
|
| 57 | String textureReference = LAYERS.get(layerIndex);
|
| 58 | Material material = textureSlots.getMaterial(textureReference);
|
| 59 | if (material == null) {
|
| 60 | break;
|
| 61 | }
|
| 62 |
|
| 63 | Material.Baked bakedMaterial = modelBaker.materials().get(material, name);
|
| 64 | QuadCollection bakedLayer = modelBaker.compute(new ItemModelGenerator.ItemLayerKey(bakedMaterial, modelState, layerIndex));
|
| 65 | if (builder != null) {
|
| 66 | builder.addAll(bakedLayer);
|
| 67 | } else if (singleResult != null) {
|
| 68 | builder = new QuadCollection.Builder();
|
| 69 | builder.addAll(singleResult);
|
| 70 | builder.addAll(bakedLayer);
|
| 71 | singleResult = null;
|
| 72 | } else {
|
| 73 | singleResult = bakedLayer;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | if (builder != null) {
|
| 78 | return builder.build();
|
| 79 | } else {
|
| 80 | return singleResult != null ? singleResult : QuadCollection.EMPTY;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | private static void bakeExtrudedSprite(
|
| 85 | final QuadCollection.Builder builder, final ModelBaker.Interner interner, final ModelState modelState, final BakedQuad.MaterialInfo materialInfo
|
| 86 | ) {
|
| 87 | Vector3f from = new Vector3f(0.0F, 0.0F, 7.5F);
|
| 88 | Vector3f to = new Vector3f(16.0F, 16.0F, 8.5F);
|
| 89 | builder.addUnculledFace(FaceBakery.bakeQuad(interner, from, to, SOUTH_FACE_UVS, Quadrant.R0, materialInfo, Direction.SOUTH, modelState, null));
|
| 90 | builder.addUnculledFace(FaceBakery.bakeQuad(interner, from, to, NORTH_FACE_UVS, Quadrant.R0, materialInfo, Direction.NORTH, modelState, null));
|
| 91 | bakeSideFaces(builder, interner, modelState, materialInfo);
|
| 92 | }
|
| 93 |
|
| 94 | private static void bakeSideFaces(
|
| 95 | final QuadCollection.Builder builder, final ModelBaker.Interner interner, final ModelState modelState, final BakedQuad.MaterialInfo materialInfo
|
| 96 | ) {
|
| 97 | SpriteContents sprite = materialInfo.sprite().contents();
|
| 98 | float xScale = 16.0F / sprite.width();
|
| 99 | float yScale = 16.0F / sprite.height();
|
| 100 | Vector3f from = new Vector3f();
|
| 101 | Vector3f to = new Vector3f();
|
| 102 |
|
| 103 | for (ItemModelGenerator.SideFace sideFace : getSideFaces(sprite)) {
|
| 104 | float x = sideFace.x();
|
| 105 | float y = sideFace.y();
|
| 106 | ItemModelGenerator.SideDirection sideDirection = sideFace.facing();
|
| 107 | float u0 = x + 0.1F;
|
| 108 | float u1 = x + 1.0F - 0.1F;
|
| 109 | float v0;
|
| 110 | float v1;
|
| 111 | if (sideDirection.isHorizontal()) {
|
| 112 | v0 = y + 0.1F;
|
| 113 | v1 = y + 1.0F - 0.1F;
|
| 114 | } else {
|
| 115 | v0 = y + 1.0F - 0.1F;
|
| 116 | v1 = y + 0.1F;
|
| 117 | }
|
| 118 |
|
| 119 | float startX = x;
|
| 120 | float startY = y;
|
| 121 | float endX = x;
|
| 122 | float endY = y;
|
| 123 | switch (sideDirection) {
|
| 124 | case UP:
|
| 125 | endX++;
|
| 126 | break;
|
| 127 | case DOWN:
|
| 128 | endX++;
|
| 129 | startY++;
|
| 130 | endY++;
|
| 131 | break;
|
| 132 | case LEFT:
|
| 133 | endY++;
|
| 134 | break;
|
| 135 | case RIGHT:
|
| 136 | startX++;
|
| 137 | endX++;
|
| 138 | endY++;
|
| 139 | }
|
| 140 |
|
| 141 | startX *= xScale;
|
| 142 | endX *= xScale;
|
| 143 | startY *= yScale;
|
| 144 | endY *= yScale;
|
| 145 | startY = 16.0F - startY;
|
| 146 | endY = 16.0F - endY;
|
| 147 | switch (sideDirection) {
|
| 148 | case UP:
|
| 149 | from.set(startX, startY, 7.5F);
|
| 150 | to.set(endX, startY, 8.5F);
|
| 151 | break;
|
| 152 | case DOWN:
|
| 153 | from.set(startX, endY, 7.5F);
|
| 154 | to.set(endX, endY, 8.5F);
|
| 155 | break;
|
| 156 | case LEFT:
|
| 157 | from.set(startX, startY, 7.5F);
|
| 158 | to.set(startX, endY, 8.5F);
|
| 159 | break;
|
| 160 | case RIGHT:
|
| 161 | from.set(endX, startY, 7.5F);
|
| 162 | to.set(endX, endY, 8.5F);
|
| 163 | break;
|
| 164 | default:
|
| 165 | throw new UnsupportedOperationException();
|
| 166 | }
|
| 167 |
|
| 168 | CuboidFace.UVs uvs = new CuboidFace.UVs(u0 * xScale, v0 * yScale, u1 * xScale, v1 * yScale);
|
| 169 | builder.addUnculledFace(FaceBakery.bakeQuad(interner, from, to, uvs, Quadrant.R0, materialInfo, sideDirection.getDirection(), modelState, null));
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private static Collection<ItemModelGenerator.SideFace> getSideFaces(final SpriteContents sprite) {
|
| 174 | int width = sprite.width();
|
| 175 | int height = sprite.height();
|
| 176 | Set<ItemModelGenerator.SideFace> sideFaces = new HashSet<>();
|
| 177 | sprite.getUniqueFrames().forEach(frame -> {
|
| 178 | for (int y = 0; y < height; y++) {
|
| 179 | for (int x = 0; x < width; x++) {
|
| 180 | boolean thisOpaque = !isTransparent(sprite, frame, x, y, width, height);
|
| 181 | if (thisOpaque) {
|
| 182 | checkTransition(ItemModelGenerator.SideDirection.UP, sideFaces, sprite, frame, x, y, width, height);
|
| 183 | checkTransition(ItemModelGenerator.SideDirection.DOWN, sideFaces, sprite, frame, x, y, width, height);
|
| 184 | checkTransition(ItemModelGenerator.SideDirection.LEFT, sideFaces, sprite, frame, x, y, width, height);
|
| 185 | checkTransition(ItemModelGenerator.SideDirection.RIGHT, sideFaces, sprite, frame, x, y, width, height);
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 | });
|
| 190 | return sideFaces;
|
| 191 | }
|
| 192 |
|
| 193 | private static void checkTransition(
|
| 194 | final ItemModelGenerator.SideDirection facing,
|
| 195 | final Set<ItemModelGenerator.SideFace> sideFaces,
|
| 196 | final SpriteContents sprite,
|
| 197 | final int frame,
|
| 198 | final int x,
|
| 199 | final int y,
|
| 200 | final int width,
|
| 201 | final int height
|
| 202 | ) {
|
| 203 | if (isTransparent(sprite, frame, x - facing.direction.getStepX(), y - facing.direction.getStepY(), width, height)) {
|
| 204 | sideFaces.add(new ItemModelGenerator.SideFace(facing, x, y));
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | private static boolean isTransparent(final SpriteContents sprite, final int frame, final int x, final int y, final int width, final int height) {
|
| 209 | return x >= 0 && y >= 0 && x < width && y < height ? sprite.isTransparent(frame, x, y) : true;
|
| 210 | }
|
| 211 |
|
| 212 | @Environment(EnvType.CLIENT)
|
| 213 | private record ItemLayerKey(Material.Baked material, ModelState modelState, int layerIndex) implements ModelBaker.SharedOperationKey<QuadCollection> {
|
| 214 | public QuadCollection compute(final ModelBaker modelBakery) {
|
| 215 | QuadCollection.Builder builder = new QuadCollection.Builder();
|
| 216 | BakedQuad.MaterialInfo materialInfo = modelBakery.interner()
|
| 217 | .materialInfo(BakedQuad.MaterialInfo.of(this.material, this.material.sprite().transparency(), this.layerIndex, null, 0));
|
| 218 | ItemModelGenerator.bakeExtrudedSprite(builder, modelBakery.interner(), this.modelState, materialInfo);
|
| 219 | return builder.build();
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | @Environment(EnvType.CLIENT)
|
| 224 | private enum SideDirection {
|
| 225 | UP(Direction.UP),
|
| 226 | DOWN(Direction.DOWN),
|
| 227 | LEFT(Direction.EAST),
|
| 228 | RIGHT(Direction.WEST);
|
| 229 |
|
| 230 | private final Direction direction;
|
| 231 |
|
| 232 | SideDirection(final Direction direction) {
|
| 233 | this.direction = direction;
|
| 234 | }
|
| 235 |
|
| 236 | public Direction getDirection() {
|
| 237 | return this.direction;
|
| 238 | }
|
| 239 |
|
| 240 | private boolean isHorizontal() {
|
| 241 | return this == DOWN || this == UP;
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | @Environment(EnvType.CLIENT)
|
| 246 | private record SideFace(ItemModelGenerator.SideDirection facing, int x, int y) {
|
| 247 | }
|
| 248 | }
|
| 249 |
|