Backport new multi-layer model from 1.15. Added as a separate class and loader ID to avoid breaking changes.

This commit is contained in:
David Quintana committed 2020-05-28 17:05:33 +02:00
1 parent d544c31b72
commit 3ba3a6649e
9 files changed
+289 -84

No files matched your search

@@ -71,6 +71,7 @@ public class ModelLoaderRegistry2
registerLoader(new ResourceLocation("forge","obj"), OBJLoader2.INSTANCE);
registerLoader(new ResourceLocation("forge","bucket"), ModelDynBucket.Loader.INSTANCE);
registerLoader(new ResourceLocation("forge","composite"), CompositeModel.Loader.INSTANCE);
registerLoader(new ResourceLocation("forge","new-multi-layer"), NewMultiLayerModel.Loader.INSTANCE);
// Model adapters from the old loading system
registerLoader(new ResourceLocation("forge","b3d"), new ModelLoaderAdapter(B3DLoader.INSTANCE));
@@ -58,6 +58,7 @@ import com.google.gson.JsonParser;
/**
* A model that can be rendered in multiple {@link BlockRenderLayer}.
*/
@Deprecated // Switch to the NewMultiLayerModel, which is what becomes MultiLayerModel in 1.15+
public final class MultiLayerModel implements IUnbakedModel
{
private static final Logger LOGGER = LogManager.getLogger();
@@ -0,0 +1,222 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.client.model;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.model.*;
import net.minecraft.client.renderer.texture.ISprite;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.resources.IResourceManager;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.Direction;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.client.model.geometry.IModelGeometry;
import net.minecraftforge.common.model.TRSRTransformation;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.vecmath.Matrix4f;
import java.util.*;
import java.util.function.Function;
public class NewMultiLayerModel implements IModelGeometry<NewMultiLayerModel>
{
private static final Logger LOGGER = LogManager.getLogger();
private final ImmutableMap<BlockRenderLayer, IUnbakedModel> models;
public NewMultiLayerModel(ImmutableMap<BlockRenderLayer, IUnbakedModel> models)
{
this.models = models;
}
@Override
public Collection<ResourceLocation> getTextureDependencies(IModelConfiguration owner, Function<ResourceLocation, IUnbakedModel> modelGetter, Set<String> missingTextureErrors)
{
Set<ResourceLocation> materials = Sets.newHashSet();
materials.add(new ResourceLocation(owner.resolveTexture("particle")));
for (IUnbakedModel m : models.values())
materials.addAll(m.getTextures(modelGetter, missingTextureErrors));
return materials;
}
private static ImmutableMap<BlockRenderLayer, IBakedModel> buildModels(ImmutableMap<BlockRenderLayer, IUnbakedModel> models, ISprite sprite, ModelBakery bakery, Function<ResourceLocation, TextureAtlasSprite> spriteGetter, VertexFormat format)
{
ImmutableMap.Builder<BlockRenderLayer, IBakedModel> builder = ImmutableMap.builder();
for(Map.Entry<BlockRenderLayer, IUnbakedModel> entry : models.entrySet())
{
builder.put(entry.getKey(), entry.getValue().bake(bakery, spriteGetter, sprite, format));
}
return builder.build();
}
@Override
public IBakedModel bake(IModelConfiguration owner, ModelBakery bakery, Function<ResourceLocation, TextureAtlasSprite> spriteGetter, ISprite sprite, VertexFormat format, ItemOverrideList overrides)
{
@SuppressWarnings("deprecation")
IUnbakedModel missing = ModelLoaderRegistry.getMissingModel();
return new NewMultiLayerModel.MultiLayerBakedModel(
owner.useSmoothLighting(), owner.isShadedInGui(),
spriteGetter.apply(new ResourceLocation(owner.resolveTexture("particle"))), overrides,
buildModels(models, sprite, bakery, spriteGetter, format),
missing.bake(bakery, spriteGetter, sprite, format),
PerspectiveMapWrapper.getTransforms(new ModelStateComposition(owner.getCombinedState(), sprite.getState())));
}
private static final class MultiLayerBakedModel implements IBakedModel
{
private final ImmutableMap<BlockRenderLayer, IBakedModel> models;
private final ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> cameraTransforms;
protected final boolean ambientOcclusion;
protected final boolean gui3d;
protected final TextureAtlasSprite particle;
protected final ItemOverrideList overrides;
private final IBakedModel missing;
public MultiLayerBakedModel(
boolean ambientOcclusion, boolean isGui3d, TextureAtlasSprite particle, ItemOverrideList overrides,
ImmutableMap<BlockRenderLayer, IBakedModel> models, IBakedModel missing, ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> cameraTransforms)
{
this.models = models;
this.cameraTransforms = cameraTransforms;
this.missing = missing;
this.ambientOcclusion = ambientOcclusion;
this.gui3d = isGui3d;
this.particle = particle;
this.overrides = overrides;
}
@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand)
{
return getQuads(state, side, rand, EmptyModelData.INSTANCE);
}
@Nonnull
@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData)
{
BlockRenderLayer layer = MinecraftForgeClient.getRenderLayer();
if (layer == null)
{
ImmutableList.Builder<BakedQuad> builder = ImmutableList.builder();
for (IBakedModel model : models.values())
{
builder.addAll(model.getQuads(state, side, rand));
}
return builder.build();
}
// assumes that child model will handle this state properly. FIXME?
return models.getOrDefault(layer, missing).getQuads(state, side, rand, extraData);
}
@Override
public boolean isAmbientOcclusion()
{
return ambientOcclusion;
}
@Override
public boolean isAmbientOcclusion(BlockState state)
{
return ambientOcclusion;
}
@Override
public boolean isGui3d()
{
return gui3d;
}
@Override
public boolean isBuiltInRenderer()
{
return false;
}
@Override
public TextureAtlasSprite getParticleTexture()
{
return particle;
}
@Override
public boolean doesHandlePerspectives()
{
return true;
}
@Override
public Pair<? extends IBakedModel, Matrix4f> handlePerspective(ItemCameraTransforms.TransformType cameraTransformType)
{
return PerspectiveMapWrapper.handlePerspective(this, cameraTransforms, cameraTransformType);
}
@Override
public ItemOverrideList getOverrides()
{
return ItemOverrideList.EMPTY;
}
}
public static final class Loader implements IModelLoader<NewMultiLayerModel>
{
public static final NewMultiLayerModel.Loader INSTANCE = new NewMultiLayerModel.Loader();
private Loader() {}
@Override
public void onResourceManagerReload(IResourceManager resourceManager)
{
}
@Override
public NewMultiLayerModel read(JsonDeserializationContext deserializationContext, JsonObject modelContents)
{
ImmutableMap.Builder<BlockRenderLayer, IUnbakedModel> builder = ImmutableMap.builder();
JsonObject layersObject = JSONUtils.getJsonObject(modelContents, "layers");
for(BlockRenderLayer layer : BlockRenderLayer.values()) // block layers
{
String layerName = layer.toString(); // mc overrides toString to return the ID for the layer
if(layersObject.has(layerName))
{
builder.put(layer, deserializationContext.deserialize(JSONUtils.getJsonObject(layersObject, layerName), BlockModel.class));
}
}
ImmutableMap<BlockRenderLayer, IUnbakedModel> models = builder.build();
return new NewMultiLayerModel(models);
}
}
}
@@ -17,90 +17,71 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
package net.minecraftforge.debug.client.model;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ObjectHolder;
//@Mod(modid = MultiLayerModelTest.MODID, name = "ForgeDebugMultiLayerModel", version = MultiLayerModelTest.VERSION, acceptableRemoteVersions = "*")
@Mod(MultiLayerModelTest.MODID)
public class MultiLayerModelTest
{
private static final boolean ENABLED = true;
public static final String MODID = "forgedebugmultilayermodel";
public static final String VERSION = "0.0";
private static final String blockName = "test_layer_block";
private static final ResourceLocation blockId = new ResourceLocation(MODID, blockName);
@ObjectHolder(blockName)
public static final Block TEST_BLOCK = null;
@ObjectHolder("test_layer_block_new")
public static final Block TEST_BLOCK_NEW = null;
public MultiLayerModelTest()
{
if (!ENABLED)
return;
IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus();
modBus.register(Registration.class);
}
private static class MultiLayerBlock extends Block
{
public MultiLayerBlock()
{
super(Properties.create(Material.CLAY));
}
@Override
public boolean canRenderInLayer(BlockState state, BlockRenderLayer layer)
{
return layer == BlockRenderLayer.SOLID || layer == BlockRenderLayer.TRANSLUCENT;
}
}
//@Mod.EventBusSubscriber(modid = MODID)
public static class Registration
{
@net.minecraftforge.eventbus.api.SubscribeEvent
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event)
{
if (!ENABLED)
return;
event.getRegistry().register(
new Block(Material.WOOD)
{
{
setCreativeTab(CreativeTabs.BUILDING_BLOCKS);
setUnlocalizedName(MODID + "." + blockName);
setRegistryName(blockId);
}
@Override
public boolean isOpaqueCube(IBlockState state)
{
return false;
}
@Override
public boolean isFullCube(IBlockState state)
{
return false;
}
@Override
public boolean canRenderInLayer(IBlockState state, BlockRenderLayer layer)
{
return layer == BlockRenderLayer.SOLID || layer == BlockRenderLayer.TRANSLUCENT;
}
}
event.getRegistry().registerAll(
new MultiLayerBlock().setRegistryName(new ResourceLocation(MODID, "test_layer_block_new"))
);
}
@net.minecraftforge.eventbus.api.SubscribeEvent
@SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event)
{
event.getRegistry().register(new ItemBlock(TEST_BLOCK).setRegistryName(TEST_BLOCK.getRegistryName()));
}
@SubscribeEvent
public static void registerModels(ModelRegistryEvent event)
{
if (!ENABLED)
return;
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(TEST_BLOCK), 0, new ModelResourceLocation(blockId, "inventory"));
event.getRegistry().registerAll(
new BlockItem(TEST_BLOCK_NEW, new Item.Properties().group(ItemGroup.MISC)).setRegistryName(TEST_BLOCK_NEW.getRegistryName())
);
}
}
}
*/
+2
View File
@@ -67,3 +67,5 @@ loaderVersion="[28,)"
modId="global_loot_test"
[[mods]]
modId="custom_plant_type_test"
[[mods]]
modId="forgedebugmultilayermodel"
@@ -1,26 +0,0 @@
{
"forge_marker": 1,
"defaults": {
"model": "forge:multi-layer",
"custom": {
// base is used for model properties - camera transforms, isGui3d, e.t.c.
"base": "forgedebugmultilayermodel:test_layer_block#aux",
// per-layer models
"Solid": "forgedebugmultilayermodel:test_layer_block#aux",
"Translucent": "forgedebugmultilayermodel:test_layer_block#pane"
},
"transform": "forge:default-block"
},
"variants": {
"normal": [{}],
"inventory": [{}],
"pane": [{
"model": "minecraft:pink_stained_glass_pane_noside"
}],
"aux": [{
"model": "cube_all",
"textures": { "all": "blocks/slime" },
"transform": { "scale": 0.5 }
}]
}
}
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "forgedebugmultilayermodel:block/test_layer_block_new"}
}
}
@@ -0,0 +1,16 @@
{
"parent": "block/block",
"loader": "forge:new-multi-layer",
"layers": {
"Solid": {
"parent": "block/cube_all",
"textures": { "all": "block/slime_block" },
"transform": { "scale": 0.5, "translation": [ -0.25, -0.25, -0.25 ] }
},
"Translucent": {
"parent": "block/cube_all",
"textures": { "all": "block/slime_block" }
}
},
"textures": { "particle": "block/slime_block" }
}
@@ -0,0 +1,3 @@
{
"parent": "forgedebugmultilayermodel:block/test_layer_block_new"
}