| 1 | package net.minecraft.client.resources.model;
|
| 2 |
|
| 3 | import com.google.common.collect.Interner;
|
| 4 | import com.google.common.collect.Interners;
|
| 5 | import com.google.common.collect.Multimap;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import java.util.concurrent.ConcurrentHashMap;
|
| 12 | import java.util.concurrent.Executor;
|
| 13 | import java.util.function.Function;
|
| 14 | import java.util.stream.Collectors;
|
| 15 | import java.util.stream.IntStream;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.client.model.geom.EntityModelSet;
|
| 19 | import net.minecraft.client.renderer.PlayerSkinRenderCache;
|
| 20 | import net.minecraft.client.renderer.Sheets;
|
| 21 | import net.minecraft.client.renderer.block.FluidModel;
|
| 22 | import net.minecraft.client.renderer.block.dispatch.BlockModelRotation;
|
| 23 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 24 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelPart;
|
| 25 | import net.minecraft.client.renderer.block.dispatch.SingleVariant;
|
| 26 | import net.minecraft.client.renderer.chunk.ChunkSectionLayer;
|
| 27 | import net.minecraft.client.renderer.item.ClientItem;
|
| 28 | import net.minecraft.client.renderer.item.ItemModel;
|
| 29 | import net.minecraft.client.renderer.item.MissingItemModel;
|
| 30 | import net.minecraft.client.renderer.item.ModelRenderProperties;
|
| 31 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 32 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 33 | import net.minecraft.client.resources.model.ModelBakery.MissingModels.1;
|
| 34 | import net.minecraft.client.resources.model.cuboid.ItemTransforms;
|
| 35 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 36 | import net.minecraft.client.resources.model.geometry.QuadCollection;
|
| 37 | import net.minecraft.client.resources.model.sprite.Material;
|
| 38 | import net.minecraft.client.resources.model.sprite.MaterialBaker;
|
| 39 | import net.minecraft.client.resources.model.sprite.SpriteGetter;
|
| 40 | import net.minecraft.client.resources.model.sprite.SpriteId;
|
| 41 | import net.minecraft.client.resources.model.sprite.TextureSlots;
|
| 42 | import net.minecraft.resources.Identifier;
|
| 43 | import net.minecraft.util.thread.ParallelMapTransform;
|
| 44 | import net.minecraft.world.level.block.state.BlockState;
|
| 45 | import org.joml.Matrix4f;
|
| 46 | import org.joml.Matrix4fc;
|
| 47 | import org.joml.Vector3fc;
|
| 48 | import org.slf4j.Logger;
|
| 49 |
|
| 50 | @Environment(EnvType.CLIENT)
|
| 51 | public class ModelBakery {
|
| 52 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 53 | public static final SpriteId FIRE_0 = Sheets.BLOCKS_MAPPER.defaultNamespaceApply("fire_0");
|
| 54 | public static final SpriteId FIRE_1 = Sheets.BLOCKS_MAPPER.defaultNamespaceApply("fire_1");
|
| 55 | private static final int DESTROY_STAGE_COUNT = 10;
|
| 56 | private static final List<Identifier> DESTROY_STAGES = IntStream.range(0, 10)
|
| 57 | .mapToObj(i -> Identifier.withDefaultNamespace("block/destroy_stage_" + i))
|
| 58 | .collect(Collectors.toList());
|
| 59 | private static final List<Identifier> DESTROY_STAGES_OIT = IntStream.range(0, 10)
|
| 60 | .mapToObj(i -> Identifier.withDefaultNamespace("block/destroy_oit_stage_" + i))
|
| 61 | .collect(Collectors.toList());
|
| 62 | private static final List<Identifier> BREAKING_LOCATIONS = DESTROY_STAGES.stream()
|
| 63 | .map(location -> location.withPath(path -> "textures/" + path + ".png"))
|
| 64 | .collect(Collectors.toList());
|
| 65 | private static final List<Identifier> BREAKING_LOCATIONS_OIT = DESTROY_STAGES_OIT.stream()
|
| 66 | .map(location -> location.withPath(path -> "textures/" + path + ".png"))
|
| 67 | .collect(Collectors.toList());
|
| 68 | public static final List<RenderType> DESTROY_TYPES = BREAKING_LOCATIONS.stream().map(RenderTypes::crumbling).collect(Collectors.toList());
|
| 69 | public static final List<RenderType> DESTROY_TYPES_OIT = BREAKING_LOCATIONS_OIT.stream().map(RenderTypes::crumbling).collect(Collectors.toList());
|
| 70 | private static final Matrix4fc IDENTITY = new Matrix4f();
|
| 71 | private final EntityModelSet entityModelSet;
|
| 72 | private final SpriteGetter sprites;
|
| 73 | private final PlayerSkinRenderCache playerSkinRenderCache;
|
| 74 | private final Map<BlockState, BlockStateModel.UnbakedRoot> unbakedBlockStateModels;
|
| 75 | private final Map<Identifier, ClientItem> clientInfos;
|
| 76 | private final Map<Identifier, ResolvedModel> resolvedModels;
|
| 77 | private final ResolvedModel missingModel;
|
| 78 |
|
| 79 | public ModelBakery(
|
| 80 | final EntityModelSet entityModelSet,
|
| 81 | final SpriteGetter sprites,
|
| 82 | final PlayerSkinRenderCache playerSkinRenderCache,
|
| 83 | final Map<BlockState, BlockStateModel.UnbakedRoot> unbakedBlockStateModels,
|
| 84 | final Map<Identifier, ClientItem> clientInfos,
|
| 85 | final Map<Identifier, ResolvedModel> resolvedModels,
|
| 86 | final ResolvedModel missingModel
|
| 87 | ) {
|
| 88 | this.entityModelSet = entityModelSet;
|
| 89 | this.sprites = sprites;
|
| 90 | this.playerSkinRenderCache = playerSkinRenderCache;
|
| 91 | this.unbakedBlockStateModels = unbakedBlockStateModels;
|
| 92 | this.clientInfos = clientInfos;
|
| 93 | this.resolvedModels = resolvedModels;
|
| 94 | this.missingModel = missingModel;
|
| 95 | }
|
| 96 |
|
| 97 | public CompletableFuture<ModelBakery.BakingResult> bakeModels(final MaterialBaker materials, final Executor taskExecutor) {
|
| 98 | ModelBakery.InternerImpl interner = new ModelBakery.InternerImpl();
|
| 99 | ModelBakery.MissingModels missingModels = ModelBakery.MissingModels.bake(this.missingModel, materials, interner);
|
| 100 | ModelBakery.ModelBakerImpl baker = new ModelBakery.ModelBakerImpl(materials, interner, missingModels);
|
| 101 | CompletableFuture<Map<BlockState, BlockStateModel>> bakedBlockStateModelFuture = ParallelMapTransform.schedule(
|
| 102 | this.unbakedBlockStateModels, (blockState, model) -> {
|
| 103 | try {
|
| 104 | return model.bake(blockState, baker);
|
| 105 | } catch (Exception e) {
|
| 106 | LOGGER.warn("Unable to bake model: '{}'", blockState, e);
|
| 107 | return null;
|
| 108 | }
|
| 109 | }, taskExecutor
|
| 110 | );
|
| 111 | CompletableFuture<Map<Identifier, ItemModel>> bakedItemStackModelFuture = ParallelMapTransform.schedule(
|
| 112 | this.clientInfos,
|
| 113 | (location, clientInfo) -> {
|
| 114 | try {
|
| 115 | return clientInfo.model()
|
| 116 | .bake(
|
| 117 | new ItemModel.BakingContext(baker, this.entityModelSet, this.sprites, this.playerSkinRenderCache, missingModels.item, clientInfo.registrySwapper()),
|
| 118 | IDENTITY
|
| 119 | );
|
| 120 | } catch (Exception e) {
|
| 121 | LOGGER.warn("Unable to bake item model: '{}'", location, e);
|
| 122 | return null;
|
| 123 | }
|
| 124 | },
|
| 125 | taskExecutor
|
| 126 | );
|
| 127 | Map<Identifier, ClientItem.Properties> itemStackModelProperties = new HashMap<>(this.clientInfos.size());
|
| 128 | this.clientInfos.forEach((id, clientInfo) -> {
|
| 129 | ClientItem.Properties properties = clientInfo.properties();
|
| 130 | if (!properties.equals(ClientItem.Properties.DEFAULT)) {
|
| 131 | itemStackModelProperties.put(id, properties);
|
| 132 | }
|
| 133 | });
|
| 134 | return bakedBlockStateModelFuture.thenCombine(
|
| 135 | bakedItemStackModelFuture,
|
| 136 | (bakedBlockStateModels, bakedItemStateModels) -> new ModelBakery.BakingResult(
|
| 137 | missingModels, (Map<BlockState, BlockStateModel>)bakedBlockStateModels, (Map<Identifier, ItemModel>)bakedItemStateModels, itemStackModelProperties
|
| 138 | )
|
| 139 | );
|
| 140 | }
|
| 141 |
|
| 142 | @Environment(EnvType.CLIENT)
|
| 143 | public record BakingResult(
|
| 144 | ModelBakery.MissingModels missingModels,
|
| 145 | Map<BlockState, BlockStateModel> blockStateModels,
|
| 146 | Map<Identifier, ItemModel> itemStackModels,
|
| 147 | Map<Identifier, ClientItem.Properties> itemProperties
|
| 148 | ) {
|
| 149 | public BlockStateModel getBlockStateModel(final BlockState blockState) {
|
| 150 | return this.blockStateModels.getOrDefault(blockState, this.missingModels.block);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Environment(EnvType.CLIENT)
|
| 155 | private static class InternerImpl implements ModelBaker.Interner {
|
| 156 | private final Interner<Vector3fc> vectors = Interners.newStrongInterner();
|
| 157 | private final Interner<BakedQuad.MaterialInfo> materialInfos = Interners.newStrongInterner();
|
| 158 |
|
| 159 | @Override
|
| 160 | public Vector3fc vector(final Vector3fc v) {
|
| 161 | return this.vectors.intern(v);
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public BakedQuad.MaterialInfo materialInfo(final BakedQuad.MaterialInfo material) {
|
| 166 | return this.materialInfos.intern(material);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | @Environment(EnvType.CLIENT)
|
| 171 | public record MissingModels(BlockStateModelPart blockPart, BlockStateModel block, MissingItemModel item, FluidModel fluid) {
|
| 172 | public static ModelBakery.MissingModels bake(final ResolvedModel unbaked, final MaterialBaker materials, final ModelBaker.Interner interner) {
|
| 173 | ModelBaker missingModelBakery = new 1(materials, interner);
|
| 174 | TextureSlots textureSlots = unbaked.getTopTextureSlots();
|
| 175 | boolean hasAmbientOcclusion = unbaked.getTopAmbientOcclusion();
|
| 176 | boolean usesBlockLight = unbaked.getTopGuiLight().lightLikeBlock();
|
| 177 | ItemTransforms transforms = unbaked.getTopTransforms();
|
| 178 | QuadCollection geometry = unbaked.bakeTopGeometry(textureSlots, missingModelBakery, BlockModelRotation.IDENTITY);
|
| 179 | Multimap<Identifier, Identifier> forbiddenSprites = SimpleModelWrapper.findNonBlockSprites(geometry);
|
| 180 | if (forbiddenSprites != null) {
|
| 181 | throw new IllegalStateException("Missing block contains sprites from outside of block atlas: " + forbiddenSprites);
|
| 182 | }
|
| 183 |
|
| 184 | Material.Baked particleMaterial = unbaked.resolveParticleMaterial(textureSlots, missingModelBakery);
|
| 185 | SimpleModelWrapper missingModelPart = new SimpleModelWrapper(geometry, hasAmbientOcclusion, particleMaterial);
|
| 186 | BlockStateModel bakedBlockModel = new SingleVariant(missingModelPart);
|
| 187 | MissingItemModel bakedItemModel = new MissingItemModel(geometry.getAll(), new ModelRenderProperties(usesBlockLight, particleMaterial, transforms));
|
| 188 | FluidModel bakedFluidModel = new FluidModel(ChunkSectionLayer.SOLID, particleMaterial, particleMaterial, null, null);
|
| 189 | return new ModelBakery.MissingModels(missingModelPart, bakedBlockModel, bakedItemModel, bakedFluidModel);
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | @Environment(EnvType.CLIENT)
|
| 194 | private class ModelBakerImpl implements ModelBaker {
|
| 195 | private final MaterialBaker materials;
|
| 196 | private final ModelBaker.Interner interner;
|
| 197 | private final ModelBakery.MissingModels missingModels;
|
| 198 | private final Map<ModelBaker.SharedOperationKey<Object>, Object> operationCache = new ConcurrentHashMap<>();
|
| 199 | private final Function<ModelBaker.SharedOperationKey<Object>, Object> cacheComputeFunction = k -> k.compute(this);
|
| 200 |
|
| 201 | private ModelBakerImpl(final MaterialBaker materials, final ModelBaker.Interner interner, final ModelBakery.MissingModels missingModels) {
|
| 202 | this.materials = materials;
|
| 203 | this.interner = interner;
|
| 204 | this.missingModels = missingModels;
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public BlockStateModelPart missingBlockModelPart() {
|
| 209 | return this.missingModels.blockPart;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public MaterialBaker materials() {
|
| 214 | return this.materials;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public ModelBaker.Interner interner() {
|
| 219 | return this.interner;
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public ResolvedModel getModel(final Identifier location) {
|
| 224 | ResolvedModel result = ModelBakery.this.resolvedModels.get(location);
|
| 225 | if (result == null) {
|
| 226 | ModelBakery.LOGGER.warn("Requested a model that was not discovered previously: {}", location);
|
| 227 | return ModelBakery.this.missingModel;
|
| 228 | } else {
|
| 229 | return result;
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public <T> T compute(final ModelBaker.SharedOperationKey<T> key) {
|
| 235 | return (T)this.operationCache.computeIfAbsent(key, this.cacheComputeFunction);
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|