[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

@@ -0,0 +1,34 @@
--- a/net/minecraft/client/renderer/block/model/BlockElement.java
+++ b/net/minecraft/client/renderer/block/model/BlockElement.java
@@ -28,14 +_,21 @@
public final Map<Direction, BlockElementFace> f_111310_;
public final BlockElementRotation f_111311_;
public final boolean f_111312_;
+ public net.minecraftforge.client.model.ForgeFaceData faceData;
public BlockElement(Vector3f p_253626_, Vector3f p_254426_, Map<Direction, BlockElementFace> p_254454_, @Nullable BlockElementRotation p_254229_, boolean p_253661_) {
+ this(p_253626_, p_254426_, p_254454_, p_254229_, p_253661_, net.minecraftforge.client.model.ForgeFaceData.DEFAULT);
+ }
+
+ public BlockElement(Vector3f p_253626_, Vector3f p_254426_, Map<Direction, BlockElementFace> p_254454_, @Nullable BlockElementRotation p_254229_, boolean p_253661_, net.minecraftforge.client.model.ForgeFaceData faceData) {
this.f_111308_ = p_253626_;
this.f_111309_ = p_254426_;
this.f_111310_ = p_254454_;
this.f_111311_ = p_254229_;
this.f_111312_ = p_253661_;
this.m_111319_();
+ this.faceData = faceData;
+ this.f_111310_.values().forEach(face -> face.parent = this);
}
private void m_111319_() {
@@ -78,7 +_,8 @@
throw new JsonParseException("Expected shade to be a Boolean");
} else {
boolean flag = GsonHelper.m_13855_(jsonobject, "shade", true);
- return new BlockElement(vector3f, vector3f1, map, blockelementrotation, flag);
+ var faceData = net.minecraftforge.client.model.ForgeFaceData.read(jsonobject.get("forge_data"), net.minecraftforge.client.model.ForgeFaceData.DEFAULT);
+ return new BlockElement(vector3f, vector3f1, map, blockelementrotation, flag, faceData);
}
}
@@ -1,40 +1,43 @@
--- a/net/minecraft/client/renderer/block/model/BlockElementFace.java
+++ b/net/minecraft/client/renderer/block/model/BlockElementFace.java
@@ -19,12 +_,24 @@
@@ -19,12 +_,30 @@
public final int f_111355_;
public final String f_111356_;
public final BlockFaceUV f_111357_;
+ public final int emissivity;
+ public final boolean hasAmbientOcclusion;
+ @Nullable
+ private final net.minecraftforge.client.model.ForgeFaceData faceData; // If null, we defer to the parent BlockElement's ForgeFaceData, which is not nullable.
+ @Nullable
+ BlockElement parent; // Parent canot be set by the constructor due to instantiation ordering. This shouldn't really ever be null, but it could theoretically be.
public BlockElementFace(@Nullable Direction p_111359_, int p_111360_, String p_111361_, BlockFaceUV p_111362_) {
+ this(p_111359_, p_111360_, p_111361_, p_111362_, 0, true);
+ this(p_111359_, p_111360_, p_111361_, p_111362_, null);
+ }
+
+ public BlockElementFace(@Nullable Direction p_111359_, int p_111360_, String p_111361_, BlockFaceUV p_111362_, int emissivity) {
+ this(p_111359_, p_111360_, p_111361_, p_111362_, emissivity, true);
+ }
+
+ public BlockElementFace(@Nullable Direction p_111359_, int p_111360_, String p_111361_, BlockFaceUV p_111362_, int emissivity, boolean hasAmbientOcclusion) {
+ public BlockElementFace(@Nullable Direction p_111359_, int p_111360_, String p_111361_, BlockFaceUV p_111362_, @Nullable net.minecraftforge.client.model.ForgeFaceData faceData) {
this.f_111354_ = p_111359_;
this.f_111355_ = p_111360_;
this.f_111356_ = p_111361_;
this.f_111357_ = p_111362_;
+ this.emissivity = emissivity;
+ this.hasAmbientOcclusion = hasAmbientOcclusion;
+ this.faceData = faceData;
+ }
+
+ public net.minecraftforge.client.model.ForgeFaceData getFaceData() {
+ if(this.faceData != null) {
+ return this.faceData;
+ } else if(this.parent != null) {
+ return this.parent.faceData;
+ }
+ return net.minecraftforge.client.model.ForgeFaceData.DEFAULT;
}
@OnlyIn(Dist.CLIENT)
@@ -37,7 +_,11 @@
@@ -37,7 +_,8 @@
int i = this.m_111368_(jsonobject);
String s = this.m_111370_(jsonobject);
BlockFaceUV blockfaceuv = p_111367_.deserialize(jsonobject, BlockFaceUV.class);
- return new BlockElementFace(direction, i, s, blockfaceuv);
+ int emissivity = GsonHelper.m_13824_(jsonobject, "emissivity", 0);
+ if (emissivity != net.minecraft.util.Mth.m_14045_(emissivity, 0, 15))
+ throw new JsonParseException("The emissivity value must be between 0 and 15. Found: " + emissivity);
+ boolean hasAmbientOcclusion = GsonHelper.m_13855_(jsonobject, "ambientocclusion", true);
+ return new BlockElementFace(direction, i, s, blockfaceuv, emissivity, hasAmbientOcclusion);
+ var faceData = net.minecraftforge.client.model.ForgeFaceData.read(jsonobject.get("forge_data"), null);
+ return new BlockElementFace(direction, i, s, blockfaceuv, faceData);
}
protected int m_111368_(JsonObject p_111369_) {
@@ -1,13 +1,17 @@
--- a/net/minecraft/client/renderer/block/model/FaceBakery.java
+++ b/net/minecraft/client/renderer/block/model/FaceBakery.java
@@ -50,7 +_,10 @@
@@ -50,7 +_,14 @@
this.m_111630_(aint, direction);
}
- return new BakedQuad(aint, p_111603_.f_111355_, direction, p_111604_, p_111608_);
+ net.minecraftforge.client.ForgeHooksClient.fillNormal(aint, direction);
+ var quad = new BakedQuad(aint, p_111603_.f_111355_, direction, p_111604_, p_111608_, p_111603_.hasAmbientOcclusion);
+ net.minecraftforge.client.model.QuadTransformers.settingEmissivity(p_111603_.emissivity).processInPlace(quad);
+ var data = p_111603_.getFaceData();
+ var quad = new BakedQuad(aint, p_111603_.f_111355_, direction, p_111604_, p_111608_, data.ambientOcclusion());
+ if (!net.minecraftforge.client.model.ForgeFaceData.DEFAULT.equals(data)) {
+ net.minecraftforge.client.model.QuadTransformers.applyingLightmap(data.blockLight(), data.skyLight()).processInPlace(quad);
+ net.minecraftforge.client.model.QuadTransformers.applyingColor(data.color()).processInPlace(quad);
+ }
+ return quad;
}
@@ -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();
+3 -2
View File
@@ -18,7 +18,6 @@ net/minecraft/client/renderer/block/ModelBlockRenderer.tesselateBlock(Lnet/minec
net/minecraft/client/renderer/block/ModelBlockRenderer.tesselateWithAO(Lnet/minecraft/world/level/BlockAndTintGetter;Lnet/minecraft/client/resources/model/BakedModel;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/BlockPos;Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;ZLnet/minecraft/util/RandomSource;JILnet/minecraftforge/client/model/data/ModelData;Lnet/minecraft/client/renderer/RenderType;)V=|p_111079_,p_111080_,p_111081_,p_111082_,p_111083_,p_111084_,p_111085_,p_111086_,p_111087_,p_111088_,modelData,renderType
net/minecraft/client/renderer/block/ModelBlockRenderer.tesselateWithoutAO(Lnet/minecraft/world/level/BlockAndTintGetter;Lnet/minecraft/client/resources/model/BakedModel;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/BlockPos;Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;ZLnet/minecraft/util/RandomSource;JILnet/minecraftforge/client/model/data/ModelData;Lnet/minecraft/client/renderer/RenderType;)V=|p_111091_,p_111092_,p_111093_,p_111094_,p_111095_,p_111096_,p_111097_,p_111098_,p_111099_,p_111100_,modelData,renderType
net/minecraft/client/renderer/block/model/BakedQuad.<init>([IILnet/minecraft/core/Direction;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;ZZ)V=|p_111298_,p_111299_,p_111300_,p_111301_,p_111302_,hasAmbientOcclusion
net/minecraft/client/renderer/block/model/BlockElementFace.<init>(Lnet/minecraft/core/Direction;ILjava/lang/String;Lnet/minecraft/client/renderer/block/model/BlockFaceUV;IZ)V=|p_111359_,p_111360_,p_111361_,p_111362_,emissivity,hasAmbientOcclusion
net/minecraft/client/renderer/block/model/BlockModel.bakeVanilla(Lnet/minecraft/client/resources/model/ModelBaker;Lnet/minecraft/client/renderer/block/model/BlockModel;Ljava/util/function/Function;Lnet/minecraft/client/resources/model/ModelState;Lnet/minecraft/resources/ResourceLocation;ZLnet/minecraftforge/client/RenderTypeGroup;)Lnet/minecraft/client/resources/model/BakedModel;=|p_249720_,p_111451_,p_111452_,p_111453_,p_111454_,p_111455_,renderTypes
net/minecraft/client/renderer/block/model/ItemTransform.<init>(Lorg/joml/Vector3f;Lorg/joml/Vector3f;Lorg/joml/Vector3f;Lorg/joml/Vector3f;)V=|p_254427_,p_254496_,p_254022_,rightRotation
net/minecraft/client/renderer/block/model/ItemTransforms.<init>(Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lnet/minecraft/client/renderer/block/model/ItemTransform;Lcom/google/common/collect/ImmutableMap;)V=|p_111798_,p_111799_,p_111800_,p_111801_,p_111802_,p_111803_,p_111804_,p_111805_,moddedTransforms
@@ -89,4 +88,6 @@ net/minecraft/world/level/block/LiquidBlock.<init>(Ljava/util/function/Supplier;
net/minecraft/world/level/block/PoweredRailBlock.<init>(Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;Z)V=|p_55218_,isPoweredRail
net/minecraft/world/level/entity/PersistentEntitySectionManager.addEntityWithoutEvent(Lnet/minecraft/world/level/entity/EntityAccess;Z)Z=|p_157539_,p_157540_
net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate.processEntityInfos(Lnet/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate;Lnet/minecraft/world/level/LevelAccessor;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/levelgen/structure/templatesystem/StructurePlaceSettings;Ljava/util/List;)Ljava/util/List;=|template,p_215387_0_,p_215387_1_,p_215387_2_,p_215387_3_
net/minecraft/world/level/storage/loot/LootPool.<init>([Lnet/minecraft/world/level/storage/loot/entries/LootPoolEntryContainer;[Lnet/minecraft/world/level/storage/loot/predicates/LootItemCondition;[Lnet/minecraft/world/level/storage/loot/functions/LootItemFunction;Lnet/minecraft/world/level/storage/loot/providers/number/NumberProvider;Lnet/minecraft/world/level/storage/loot/providers/number/NumberProvider;Ljava/lang/String;)V=|p_165128_,p_165129_,p_165130_,p_165131_,p_165132_,name
net/minecraft/world/level/storage/loot/LootPool.<init>([Lnet/minecraft/world/level/storage/loot/entries/LootPoolEntryContainer;[Lnet/minecraft/world/level/storage/loot/predicates/LootItemCondition;[Lnet/minecraft/world/level/storage/loot/functions/LootItemFunction;Lnet/minecraft/world/level/storage/loot/providers/number/NumberProvider;Lnet/minecraft/world/level/storage/loot/providers/number/NumberProvider;Ljava/lang/String;)V=|p_165128_,p_165129_,p_165130_,p_165131_,p_165132_,name
net/minecraft/client/renderer/block/model/BlockElement.<init>(Lorg/joml/Vector3f;Lorg/joml/Vector3f;Ljava/util/Map;Lnet/minecraft/client/renderer/block/model/BlockElementRotation;ZLnet/minecraftforge/client/model/ForgeFaceData;)V=|p_253626_,p_254426_,p_254454_,p_254229_,p_253661_,faceData
net/minecraft/client/renderer/block/model/BlockElementFace.<init>(Lnet/minecraft/core/Direction;ILjava/lang/String;Lnet/minecraft/client/renderer/block/model/BlockFaceUV;Lnet/minecraftforge/client/model/ForgeFaceData;)V=|p_111359_,p_111360_,p_111361_,p_111362_,faceData
@@ -33,7 +33,7 @@ import net.minecraftforge.client.model.generators.BlockModelBuilder;
import net.minecraftforge.client.model.generators.BlockStateProvider;
import net.minecraftforge.client.model.generators.ConfiguredModel;
import net.minecraftforge.client.model.generators.ItemModelProvider;
import net.minecraftforge.client.model.generators.loaders.ItemLayersModelBuilder;
import net.minecraftforge.client.model.generators.loaders.ItemLayerModelBuilder;
import net.minecraftforge.client.model.generators.loaders.ObjModelBuilder;
import net.minecraftforge.client.model.generators.loaders.SeparateTransformsModelBuilder;
import net.minecraftforge.client.model.geometry.SimpleUnbakedGeometry;
@@ -203,8 +203,8 @@ public class NewModelLoaderTest
withExistingParent(NewModelLoaderTest.item_layers.getId().getPath(), "forge:item/default")
.texture("layer0", "minecraft:item/coal")
.texture("layer1", "minecraft:item/stick")
.customLoader(ItemLayersModelBuilder::begin)
.emissive(1)
.customLoader(ItemLayerModelBuilder::begin)
.emissive(15, 15, 1)
.end();
withExistingParent(NewModelLoaderTest.separate_perspective.getId().getPath(), "forge:item/default")
.customLoader(SeparateTransformsModelBuilder::begin)