[1.19.3] Allow Item and Elements models to specify static color, sky light, and block light values. (#9106)

This commit is contained in:
Brennan Ward authored and GitHub committed 2022-12-31 10:36:56 +01:00
1 parent 15214a6e9a
commit f225d1f3fb
12 files changed
+361 -116

No files matched your search

@@ -36,7 +36,6 @@ public class ClientForgeMod
// TODO: Deprecated. To be removed in 1.20
event.register("bucket", DynamicFluidContainerModel.Loader.INSTANCE_DEPRECATED);
event.register("item-layers", ItemLayerModel.Loader.INSTANCE_DEPRECATED);
event.register("separate-perspective", SeparateTransformsModel.Loader.INSTANCE_DEPRECATED);
}
@@ -0,0 +1,64 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.client.model;
import java.util.function.Function;
import javax.annotation.Nullable;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.renderer.block.model.BlockElement;
import net.minecraft.client.renderer.block.model.BlockElementFace;
import net.minecraft.util.ExtraCodecs;
/**
* Holds extra data that may be injected into a face.<p>
* Used by {@link ItemLayerModel}, {@link BlockElement} and {@link BlockElementFace}
*
* @param color Color in ARGB format
* @param blockLight Block Light for this face from 0-15 (inclusive)
* @param skyLight Sky Light for this face from 0-15 (inclusive)
* @param ambientOcclusion If this face has AO
*/
public record ForgeFaceData(int color, int blockLight, int skyLight, boolean ambientOcclusion)
{
public static final ForgeFaceData DEFAULT = new ForgeFaceData(0xFFFFFFFF, 0, 0, true);
public static final Codec<Integer> COLOR = new ExtraCodecs.EitherCodec<>(Codec.INT, Codec.STRING).xmap(
either -> either.map(Function.identity(), str -> (int) Long.parseLong(str, 16)),
color -> Either.right(Integer.toHexString(color)));
public static final Codec<ForgeFaceData> CODEC = RecordCodecBuilder.create(builder -> builder.group(
COLOR.optionalFieldOf("color", 0xFFFFFFFF).forGetter(ForgeFaceData::color),
Codec.intRange(0, 15).optionalFieldOf("block_light", 0).forGetter(ForgeFaceData::blockLight),
Codec.intRange(0, 15).optionalFieldOf("sky_light", 0).forGetter(ForgeFaceData::skyLight),
Codec.BOOL.optionalFieldOf("ambient_occlusion", true).forGetter(ForgeFaceData::ambientOcclusion))
.apply(builder, ForgeFaceData::new));
/**
* Parses a ForgeFaceData from JSON
* @param obj The JsonObject to parse from, weakly-typed to JsonElement to reduce logic complexity.
* @param fallback What to return if the first parameter is null.
* @return The parsed ForgeFaceData, or the fallback parameter if the first parmeter is null.
* @throws JsonParseException
*/
@Nullable
public static ForgeFaceData read(@Nullable JsonElement obj, @Nullable ForgeFaceData fallback) throws JsonParseException
{
if(obj == null)
{
return fallback;
}
return CODEC.parse(JsonOps.INSTANCE, obj).getOrThrow(false, JsonParseException::new);
}
}
@@ -10,11 +10,9 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.ItemModelGenerator;
import net.minecraft.client.renderer.block.model.ItemOverrides;
@@ -23,7 +21,6 @@ import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBaker;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.ForgeRenderTypes;
import net.minecraftforge.client.RenderTypeGroup;
@@ -31,13 +28,9 @@ import net.minecraftforge.client.model.geometry.IGeometryBakingContext;
import net.minecraftforge.client.model.geometry.IGeometryLoader;
import net.minecraftforge.client.model.geometry.IUnbakedGeometry;
import net.minecraftforge.client.model.geometry.UnbakedGeometryHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
/**
@@ -48,26 +41,16 @@ import java.util.function.Function;
*/
public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
{
private static final Logger LOGGER = LogManager.getLogger();
@Nullable
private ImmutableList<Material> textures;
private final IntSet emissiveLayers;
private final Int2ObjectMap<ForgeFaceData> layerData;
private final Int2ObjectMap<ResourceLocation> renderTypeNames;
private final boolean deprecatedLoader, logWarning;
public ItemLayerModel(@Nullable ImmutableList<Material> textures, IntSet emissiveLayers, Int2ObjectMap<ResourceLocation> renderTypeNames)
{
this(textures, emissiveLayers, renderTypeNames, false, false);
}
private ItemLayerModel(@Nullable ImmutableList<Material> textures, IntSet emissiveLayers, Int2ObjectMap<ResourceLocation> renderTypeNames, boolean deprecatedLoader, boolean logWarning)
private ItemLayerModel(@Nullable ImmutableList<Material> textures, Int2ObjectMap<ForgeFaceData> layerData, Int2ObjectMap<ResourceLocation> renderTypeNames)
{
this.textures = textures;
this.emissiveLayers = emissiveLayers;
this.layerData = layerData;
this.renderTypeNames = renderTypeNames;
this.deprecatedLoader = deprecatedLoader;
this.logWarning = logWarning;
}
@Override
@@ -84,11 +67,6 @@ public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
textures = builder.build();
}
if (deprecatedLoader)
LOGGER.warn("Model \"" + modelLocation + "\" is using the deprecated loader \"forge:item-layers\" instead of \"forge:item_layers\". This loader will be removed in 1.20.");
if (logWarning)
LOGGER.warn("Model \"" + modelLocation + "\" is using the deprecated \"fullbright_layers\" field in its item layer model instead of \"emissive_layers\". This field will be removed in 1.20.");
TextureAtlasSprite particle = spriteGetter.apply(
context.hasMaterial("particle") ? context.getMaterial("particle") : textures.get(0)
);
@@ -101,9 +79,8 @@ public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
for (int i = 0; i < textures.size(); i++)
{
TextureAtlasSprite sprite = spriteGetter.apply(textures.get(i));
var unbaked = UnbakedGeometryHelper.createUnbakedItemElements(i, sprite.contents());
var unbaked = UnbakedGeometryHelper.createUnbakedItemElements(i, sprite.contents(), this.layerData.get(i));
var quads = UnbakedGeometryHelper.bakeElements(unbaked, $ -> sprite, modelState, modelLocation);
if (emissiveLayers.contains(i)) QuadTransformers.settingMaxEmissivity().processInPlace(quads);
var renderTypeName = renderTypeNames.get(i);
var renderTypes = renderTypeName != null ? context.getRenderType(renderTypeName) : null;
builder.addQuads(renderTypes != null ? renderTypes : normalRenderTypes, quads);
@@ -114,16 +91,7 @@ public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
public static final class Loader implements IGeometryLoader<ItemLayerModel>
{
public static final Loader INSTANCE = new Loader(false);
@Deprecated(forRemoval = true, since = "1.19")
public static final Loader INSTANCE_DEPRECATED = new Loader(true);
private final boolean deprecated;
private Loader(boolean deprecated)
{
this.deprecated = deprecated;
}
public static final Loader INSTANCE = new Loader();
@Override
public ItemLayerModel read(JsonObject jsonObject, JsonDeserializationContext deserializationContext)
@@ -141,25 +109,28 @@ public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
}
}
var emissiveLayers = new IntOpenHashSet();
readUnlit(jsonObject, "emissive_layers", renderTypeNames, emissiveLayers, false);
boolean logWarning = readUnlit(jsonObject, "fullbright_layers", renderTypeNames, emissiveLayers, true); // TODO: Deprecated name. To be removed in 1.20
return new ItemLayerModel(null, emissiveLayers, renderTypeNames, deprecated, logWarning);
var emissiveLayers = new Int2ObjectArrayMap<ForgeFaceData>();
if(jsonObject.has("forge_data"))
{
JsonObject forgeData = jsonObject.get("forge_data").getAsJsonObject();
readLayerData(forgeData, "layers", renderTypeNames, emissiveLayers, false);
}
return new ItemLayerModel(null, emissiveLayers, renderTypeNames);
}
private boolean readUnlit(JsonObject jsonObject, String name, Int2ObjectOpenHashMap<ResourceLocation> renderTypeNames, IntOpenHashSet litLayers, boolean logWarning)
protected void readLayerData(JsonObject jsonObject, String name, Int2ObjectOpenHashMap<ResourceLocation> renderTypeNames, Int2ObjectMap<ForgeFaceData> layerData, boolean logWarning)
{
if (!jsonObject.has(name))
return false;
var fullbrightLayers = jsonObject.getAsJsonArray(name);
var renderType = new ResourceLocation("forge", "item_unlit");
for (var layer : fullbrightLayers)
{
litLayers.add(layer.getAsInt());
renderTypeNames.putIfAbsent(layer.getAsInt(), renderType);
return;
}
var fullbrightLayers = jsonObject.getAsJsonObject(name);
for (var entry : fullbrightLayers.entrySet())
{
int layer = Integer.parseInt(entry.getKey());
var data = ForgeFaceData.read(entry.getValue(), ForgeFaceData.DEFAULT);
layerData.put(layer, data);
}
return logWarning && !fullbrightLayers.isEmpty();
}
}
}
@@ -83,19 +83,27 @@ public final class QuadTransformers {
}
/**
* {@return a new {@link BakedQuad} transformer that applies the specified lightmap}
* @return A new {@link BakedQuad} transformer that applies the specified packed light value.
*/
public static IQuadTransformer applyingLightmap(int lightmap)
public static IQuadTransformer applyingLightmap(int packedLight)
{
return quad -> {
var vertices = quad.getVertices();
for (int i = 0; i < 4; i++)
vertices[i * IQuadTransformer.STRIDE + IQuadTransformer.UV2] = lightmap;
vertices[i * IQuadTransformer.STRIDE + IQuadTransformer.UV2] = packedLight;
};
}
/**
* {@return a {@link BakedQuad} transformer that sets the lightmap to the given emissivity (0-15)}
* @return A new {@link BakedQuad} transformer that applies the specified block and sky light values.
*/
public static IQuadTransformer applyingLightmap(int blockLight, int skyLight)
{
return applyingLightmap(LightTexture.pack(blockLight, skyLight));
}
/**
* @return A {@link BakedQuad} transformer that sets the lightmap to the given emissivity (0-15)
*/
public static IQuadTransformer settingEmissivity(int emissivity)
{
@@ -104,13 +112,64 @@ public final class QuadTransformers {
}
/**
* {@return a {@link BakedQuad} transformer that sets the lightmap to its max value}
* @return A {@link BakedQuad} transformer that sets the lightmap to its max value
*/
public static IQuadTransformer settingMaxEmissivity()
{
return EMISSIVE_TRANSFORMERS[15];
}
/**
* @param color The color in ARGB format.
* @return A {@link BakedQuad} transformer that sets the color to the specified value.
*/
public static IQuadTransformer applyingColor(int color)
{
final int fixedColor = toABGR(color);
return quad -> {
var vertices = quad.getVertices();
for (int i = 0; i < 4; i++)
vertices[i * IQuadTransformer.STRIDE + IQuadTransformer.COLOR] = fixedColor;
};
}
/**
* This method supplies a default alpha value of 255 (no transparency)
* @param red The red value (0-255)
* @param green The green value (0-255)
* @param blue The blue value (0-255)
* @return A {@link BakedQuad} transformer that sets the color to the specified value.
*/
public static IQuadTransformer applyingColor(int red, int green, int blue)
{
return applyingColor(255, red, green, blue);
}
/**
* @param alpha The alpha value (0-255)
* @param red The red value (0-255)
* @param green The green value (0-255)
* @param blue The blue value (0-255)
* @return A {@link BakedQuad} transformer that sets the color to the specified value.
*/
public static IQuadTransformer applyingColor(int alpha, int red, int green, int blue)
{
return applyingColor(alpha << 24 | red << 16 | green << 8 | blue);
}
/**
* Converts an ARGB color to an ABGR color, as the commonly used color format is not the format colors end up packed into.
* This function doubles as its own inverse.
* @param color ARGB color
* @return ABGR color
*/
public static int toABGR(int argb)
{
return (argb & 0xFF00FF00) // alpha and green same spot
| ((argb >> 16) & 0x000000FF) // red moves to blue
| ((argb << 16) & 0x00FF0000); // blue moves to red
}
private QuadTransformers()
{
}
@@ -20,6 +20,7 @@ import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import net.minecraft.client.renderer.block.model.BlockFaceUV;
import net.minecraft.client.renderer.block.model.BlockModel.GuiLight;
@@ -32,6 +33,7 @@ import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraftforge.client.model.ForgeFaceData;
import net.minecraftforge.common.data.ExistingFileHelper;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;
@@ -60,7 +62,7 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
protected final List<ElementBuilder> elements = new ArrayList<>();
protected CustomLoaderBuilder customLoader = null;
protected CustomLoaderBuilder<T> customLoader = null;
protected ModelBuilder(ResourceLocation outputLocation, ExistingFileHelper existingFileHelper) {
super(outputLocation);
@@ -317,11 +319,8 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
if (face.tintIndex != -1) {
faceObj.addProperty("tintindex", face.tintIndex);
}
if (face.emissivity > 0) {
faceObj.addProperty("emissivity", face.emissivity);
}
if (!face.hasAmbientOcclusion) {
faceObj.addProperty("ambientocclusion", face.hasAmbientOcclusion);
if (!face.getFaceData().equals(ForgeFaceData.DEFAULT)) {
faceObj.add("forge_data", ForgeFaceData.CODEC.encodeStart(JsonOps.INSTANCE, face.getFaceData()).result().get());
}
faces.add(dir.getSerializedName(), faceObj);
}
@@ -368,6 +367,9 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
private final Map<Direction, FaceBuilder> faces = new LinkedHashMap<>();
private RotationBuilder rotation;
private boolean shade = true;
private int color = 0xFFFFFFFF;
private int blockLight = 0, skyLight = 0;
private boolean hasAmbientOcclusion = true;
private void validateCoordinate(float coord, char name) {
Preconditions.checkArgument(!(coord < -16.0F) && !(coord > 32.0F), "Position " + name + " out of range, must be within [-16, 32]. Found: %d", coord);
@@ -499,6 +501,42 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
public ElementBuilder cube(String texture) {
return allFaces(addTexture(texture).andThen((dir, f) -> f.cullface(dir)));
}
/**
* Set the block and sky light of the element (0-15).
* Traditional "emissivity" values were set both of these to the same value.
*
* @param blockLight the block light
* @param skyLight the sky light
* @return this builder
*/
public ElementBuilder emissivity(int blockLight, int skyLight) {
this.blockLight = blockLight;
this.skyLight = skyLight;
return this;
}
/**
* Sets the color of the element.
*
* @param color the color in ARGB format.
* @return this builder
*/
public ElementBuilder color(int color) {
this.color = color;
return this;
}
/**
* Set the ambient occlusion of the element.
*
* @param ao the ambient occlusion
* @return this builder
*/
public ElementBuilder ao(boolean ao) {
this.hasAmbientOcclusion = ao;
return this;
}
private BiConsumer<Direction, FaceBuilder> addTexture(String texture) {
return ($, f) -> f.texture(texture);
@@ -507,7 +545,7 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
BlockElement build() {
Map<Direction, BlockElementFace> faces = this.faces.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().build(), (k1, k2) -> { throw new IllegalArgumentException(); }, LinkedHashMap::new));
return new BlockElement(from, to, faces, rotation == null ? null : rotation.build(), shade);
return new BlockElement(from, to, faces, rotation == null ? null : rotation.build(), shade, new ForgeFaceData(this.color, this.blockLight, this.skyLight, this.hasAmbientOcclusion));
}
public T end() { return self(); }
@@ -519,7 +557,8 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
private String texture = MissingTextureAtlasSprite.getLocation().toString();
private float[] uvs;
private FaceRotation rotation = FaceRotation.ZERO;
private int emissivity = 0;
private int color = 0xFFFFFFFF;
private int blockLight = 0, skyLight = 0;
private boolean hasAmbientOcclusion = true;
FaceBuilder(Direction dir) {
@@ -568,23 +607,28 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
}
/**
* Set the emissivity of the face (0-15).
* Set the block and sky light of the face (0-15).
* Traditional "emissivity" values set both of these to the same value.
*
* @param emissivity the emissivity
* @param blockLight the block light
* @param skyLight the sky light
* @return this builder
*/
public FaceBuilder emissivity(int emissivity) {
this.emissivity = emissivity;
public FaceBuilder emissivity(int blockLight, int skyLight) {
this.blockLight = blockLight;
this.skyLight = skyLight;
return this;
}
/**
* Make the face emissive (emissivity = 15).
* Sets the color of the face.
*
* @param color the color in ARGB format.
* @return this builder
*/
public FaceBuilder emissive() {
return emissivity(15);
public FaceBuilder color(int color) {
this.color = color;
return this;
}
/**
@@ -602,7 +646,7 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
if (this.texture == null) {
throw new IllegalStateException("A model face must have a texture");
}
return new BlockElementFace(cullface, tintindex, texture, new BlockFaceUV(uvs, rotation.rotation), emissivity, hasAmbientOcclusion);
return new BlockElementFace(cullface, tintindex, texture, new BlockFaceUV(uvs, rotation.rotation), new ForgeFaceData(this.color, this.blockLight, this.skyLight, this.hasAmbientOcclusion));
}
public ElementBuilder end() { return ElementBuilder.this; }
@@ -5,30 +5,37 @@
package net.minecraftforge.client.model.generators.loaders;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.model.ForgeFaceData;
import net.minecraftforge.client.model.generators.CustomLoaderBuilder;
import net.minecraftforge.client.model.generators.ModelBuilder;
import net.minecraftforge.common.data.ExistingFileHelper;
import java.util.*;
public class ItemLayersModelBuilder<T extends ModelBuilder<T>> extends CustomLoaderBuilder<T>
public class ItemLayerModelBuilder<T extends ModelBuilder<T>> extends CustomLoaderBuilder<T>
{
public static <T extends ModelBuilder<T>> ItemLayersModelBuilder<T> begin(T parent, ExistingFileHelper existingFileHelper)
public static <T extends ModelBuilder<T>> ItemLayerModelBuilder<T> begin(T parent, ExistingFileHelper existingFileHelper)
{
return new ItemLayersModelBuilder<>(parent, existingFileHelper);
return new ItemLayerModelBuilder<>(parent, existingFileHelper);
}
private final IntSet emissiveLayers = new IntOpenHashSet();
private final Int2ObjectMap<ForgeFaceData> faceData = new Int2ObjectOpenHashMap<>();
private final Map<ResourceLocation, IntSet> renderTypes = new LinkedHashMap<>();
private final IntSet layersWithRenderTypes = new IntOpenHashSet();
protected ItemLayersModelBuilder(T parent, ExistingFileHelper existingFileHelper)
protected ItemLayerModelBuilder(T parent, ExistingFileHelper existingFileHelper)
{
super(new ResourceLocation("forge:item_layers"), parent, existingFileHelper);
}
@@ -36,18 +43,51 @@ public class ItemLayersModelBuilder<T extends ModelBuilder<T>> extends CustomLoa
/**
* Marks a set of layers to be rendered emissively.
*
* @param blockLight The block light (0-15)
* @param skyLight The sky light (0-15)
* @param layers the layers that will render unlit
* @return this builder
* @throws NullPointerException if {@code layers} is {@code null}
* @throws IllegalArgumentException if {@code layers} is empty
* @throws IllegalArgumentException if any entry in {@code layers} is smaller than 0
*/
public ItemLayersModelBuilder<T> emissive(int... layers)
public ItemLayerModelBuilder<T> emissive(int blockLight, int skyLight, int... layers)
{
Preconditions.checkNotNull(layers, "Layers must not be null");
Preconditions.checkArgument(layers.length > 0, "At least one layer must be specified");
Preconditions.checkArgument(Arrays.stream(layers).allMatch(i -> i >= 0), "All layers must be >= 0");
Arrays.stream(layers).forEach(emissiveLayers::add);
for(int i : layers)
{
faceData.compute(i, (key, value) -> {
ForgeFaceData fallback = value == null ? ForgeFaceData.DEFAULT : value;
return new ForgeFaceData(fallback.color(), blockLight, skyLight, fallback.ambientOcclusion());
});
}
return this;
}
/**
* Marks a set of layers to be rendered with a specific color.
*
* @param color The color, in ARGB.
* @param layers the layers that will render with color
* @return this builder
* @throws NullPointerException if {@code layers} is {@code null}
* @throws IllegalArgumentException if {@code layers} is empty
* @throws IllegalArgumentException if any entry in {@code layers} is smaller than 0
*/
public ItemLayerModelBuilder<T> color(int color, int... layers)
{
Preconditions.checkNotNull(layers, "Layers must not be null");
Preconditions.checkArgument(layers.length > 0, "At least one layer must be specified");
Preconditions.checkArgument(Arrays.stream(layers).allMatch(i -> i >= 0), "All layers must be >= 0");
for(int i : layers)
{
faceData.compute(i, (key, value) -> {
ForgeFaceData fallback = value == null ? ForgeFaceData.DEFAULT : value;
return new ForgeFaceData(color, fallback.blockLight(), fallback.skyLight(), fallback.ambientOcclusion());
});
}
return this;
}
@@ -64,7 +104,7 @@ public class ItemLayersModelBuilder<T extends ModelBuilder<T>> extends CustomLoa
* @throws IllegalArgumentException if any entry in {@code layers} is smaller than 0
* @throws IllegalArgumentException if any entry in {@code layers} already has a render type
*/
public ItemLayersModelBuilder<T> renderType(String renderType, int... layers)
public ItemLayerModelBuilder<T> renderType(String renderType, int... layers)
{
Preconditions.checkNotNull(renderType, "Render type must not be null");
ResourceLocation asLoc;
@@ -88,7 +128,7 @@ public class ItemLayersModelBuilder<T extends ModelBuilder<T>> extends CustomLoa
* @throws IllegalArgumentException if any entry in {@code layers} is smaller than 0
* @throws IllegalArgumentException if any entry in {@code layers} already has a render type
*/
public ItemLayersModelBuilder<T> renderType(ResourceLocation renderType, int... layers)
public ItemLayerModelBuilder<T> renderType(ResourceLocation renderType, int... layers)
{
Preconditions.checkNotNull(renderType, "Render type must not be null");
Preconditions.checkNotNull(layers, "Layers must not be null");
@@ -109,9 +149,16 @@ public class ItemLayersModelBuilder<T extends ModelBuilder<T>> extends CustomLoa
{
json = super.toJson(json);
JsonArray unlitLayers = new JsonArray();
this.emissiveLayers.intStream().sorted().forEach(unlitLayers::add);
json.add("emissive_layers", unlitLayers);
JsonObject forgeData = new JsonObject();
JsonObject layerObj = new JsonObject();
for(Int2ObjectMap.Entry<ForgeFaceData> entry : this.faceData.int2ObjectEntrySet())
{
layerObj.add(String.valueOf(entry.getIntKey()), ForgeFaceData.CODEC.encodeStart(JsonOps.INSTANCE, entry.getValue()).getOrThrow(false, s -> {}));
}
forgeData.add("layers", layerObj);
json.add("forge_data", forgeData);
JsonObject renderTypes = new JsonObject();
this.renderTypes.forEach((renderType, layers) -> {
@@ -27,6 +27,7 @@ import net.minecraft.client.resources.model.ModelState;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.model.ElementsModel;
import net.minecraftforge.client.model.ForgeFaceData;
import net.minecraftforge.client.model.IModelBuilder;
import net.minecraftforge.client.model.SimpleModelState;
import org.jetbrains.annotations.ApiStatus;
@@ -127,15 +128,33 @@ public class UnbakedGeometryHelper
return elementsModel.bake(blockModel.customData, modelBaker, spriteGetter, modelState, blockModel.getOverrides(modelBaker, owner, spriteGetter), modelLocation);
}
/**
* @see #createUnbakedItemElements(int, SpriteContents, ForgeFaceData)
*/
public static List<BlockElement> createUnbakedItemElements(int layerIndex, SpriteContents spriteContents)
{
return createUnbakedItemElements(layerIndex, spriteContents, null);
}
/**
* Creates a list of {@linkplain BlockElement block elements} in the shape of the specified sprite contents.
* These can later be baked using the same, or another texture.
* <p>
* The {@link Direction#NORTH} and {@link Direction#SOUTH} faces take up the whole surface.
*/
public static List<BlockElement> createUnbakedItemElements(int layerIndex, SpriteContents spriteContents)
public static List<BlockElement> createUnbakedItemElements(int layerIndex, SpriteContents spriteContents, @Nullable ForgeFaceData faceData)
{
return ITEM_MODEL_GENERATOR.processFrames(layerIndex, "layer" + layerIndex, spriteContents);
var elements = ITEM_MODEL_GENERATOR.processFrames(layerIndex, "layer" + layerIndex, spriteContents);
elements.forEach(element -> element.faceData = faceData);
return elements;
}
/**
* @see #createUnbakedItemMaskElements(int, SpriteContents, ForgeFaceData)
*/
public static List<BlockElement> createUnbakedItemMaskElements(int layerIndex, SpriteContents spriteContents)
{
return createUnbakedItemMaskElements(layerIndex, spriteContents, null);
}
/**
@@ -144,9 +163,9 @@ public class UnbakedGeometryHelper
* <p>
* The {@link Direction#NORTH} and {@link Direction#SOUTH} faces take up only the pixels the texture uses.
*/
public static List<BlockElement> createUnbakedItemMaskElements(int layerIndex, SpriteContents spriteContents)
public static List<BlockElement> createUnbakedItemMaskElements(int layerIndex, SpriteContents spriteContents, @Nullable ForgeFaceData faceData)
{
var elements = createUnbakedItemElements(layerIndex, spriteContents);
var elements = createUnbakedItemElements(layerIndex, spriteContents, faceData);
elements.remove(0); // Remove north and south faces
int width = spriteContents.width(), height = spriteContents.height();