Allow faces of an "elements" model to be made emissive (#8890)

This commit is contained in:
Néstor Amador authored and GitHub committed 2022-07-26 15:01:29 -07:00
1 parent f9d0daa91c
commit 326e0329b9
13 files changed
+284 -63

No files matched your search

@@ -0,0 +1,33 @@
--- a/net/minecraft/client/renderer/block/model/BlockElementFace.java
+++ b/net/minecraft/client/renderer/block/model/BlockElementFace.java
@@ -19,12 +_,18 @@
public final int f_111355_;
public final String f_111356_;
public final BlockFaceUV f_111357_;
+ public final int emissivity;
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);
+ }
+
+ public BlockElementFace(@Nullable Direction p_111359_, int p_111360_, String p_111361_, BlockFaceUV p_111362_, int emissivity) {
this.f_111354_ = p_111359_;
this.f_111355_ = p_111360_;
this.f_111356_ = p_111361_;
this.f_111357_ = p_111362_;
+ this.emissivity = emissivity;
}
@OnlyIn(Dist.CLIENT)
@@ -37,7 +_,10 @@
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);
+ return new BlockElementFace(direction, i, s, blockfaceuv, emissivity);
}
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
@@ -49,6 +_,7 @@
@@ -49,7 +_,10 @@
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);
return new BakedQuad(aint, p_111603_.f_111355_, direction, p_111604_, p_111608_);
+ var quad = new BakedQuad(aint, p_111603_.f_111355_, direction, p_111604_, p_111608_);
+ net.minecraftforge.client.model.QuadTransformers.settingEmissivity(p_111603_.emissivity).processInPlace(quad);
+ return quad;
}
public static BlockFaceUV m_111581_(BlockFaceUV p_111582_, Direction p_111583_, Transformation p_111584_, ResourceLocation p_111585_) {
@@ -131,8 +_,8 @@
p_111615_[i + 1] = Float.floatToRawIntBits(p_111617_.m_122260_());
p_111615_[i + 2] = Float.floatToRawIntBits(p_111617_.m_122269_());
@@ -69,8 +69,6 @@ public class DynamicFluidContainerModel implements IUnbakedGeometry<DynamicFluid
// Depth offsets to prevent Z-fighting
private static final Transformation FLUID_TRANSFORM = new Transformation(Vector3f.ZERO, Quaternion.ONE, new Vector3f(1, 1, 1.002f), Quaternion.ONE);
private static final Transformation COVER_TRANSFORM = new Transformation(Vector3f.ZERO, Quaternion.ONE, new Vector3f(1, 1, 1.004f), Quaternion.ONE);
// Transformer to set quads to max brightness
private static final IQuadTransformer MAX_LIGHTMAP_TRANSFORMER = IQuadTransformer.applyingLightmap(0x00F000F0);
private final Fluid fluid;
private final boolean flipGas;
@@ -163,9 +161,9 @@ public class DynamicFluidContainerModel implements IUnbakedGeometry<DynamicFluid
var unbaked = UnbakedGeometryHelper.createUnbakedItemMaskElements(1, templateSprite); // Use template as mask
var quads = UnbakedGeometryHelper.bakeElements(unbaked, $ -> fluidSprite, transformedState, modelLocation); // Bake with fluid texture
var unlit = applyFluidLuminosity && fluid.getFluidType().getLightLevel() > 0;
var renderTypes = getLayerRenderTypes(unlit);
if (unlit) MAX_LIGHTMAP_TRANSFORMER.processInPlace(quads);
var emissive = applyFluidLuminosity && fluid.getFluidType().getLightLevel() > 0;
var renderTypes = getLayerRenderTypes(emissive);
if (emissive) QuadTransformers.settingMaxEmissivity().processInPlace(quads);
modelBuilder.addQuads(renderTypes, quads);
}
@@ -8,8 +8,6 @@ package net.minecraftforge.client.model;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormatElement;
import com.mojang.math.Transformation;
import com.mojang.math.Vector3f;
import com.mojang.math.Vector4f;
import net.minecraft.client.renderer.block.model.BakedQuad;
import java.util.Arrays;
@@ -18,8 +16,7 @@ import java.util.List;
/**
* Transformer for {@link BakedQuad baked quads}.
*
* @see #applying(Transformation)
* @see #applyingLightmap(int)
* @see QuadTransformers
*/
public interface IQuadTransformer
{
@@ -61,70 +58,35 @@ public interface IQuadTransformer
/**
* Creates a {@link BakedQuad} transformer that does nothing.
*
* @deprecated Use {@link QuadTransformers#empty()}
*/
@Deprecated(forRemoval = true, since = "1.19")
static IQuadTransformer empty()
{
return quad -> {};
return QuadTransformers.empty();
}
/**
* Creates a {@link BakedQuad} transformer that applies the specified {@link Transformation}.
*
* @deprecated Use {@link QuadTransformers#applying(Transformation)}
*/
@Deprecated(forRemoval = true, since = "1.19")
static IQuadTransformer applying(Transformation transform)
{
if (transform.isIdentity())
return empty();
return quad -> {
var vertices = quad.getVertices();
for (int i = 0; i < 4; i++)
{
int offset = i * STRIDE + POSITION;
float x = Float.intBitsToFloat(vertices[offset]);
float y = Float.intBitsToFloat(vertices[offset + 1]);
float z = Float.intBitsToFloat(vertices[offset + 2]);
Vector4f pos = new Vector4f(x, y, z, 1);
transform.transformPosition(pos);
pos.perspectiveDivide();
vertices[offset] = Float.floatToRawIntBits(pos.x());
vertices[offset + 1] = Float.floatToRawIntBits(pos.y());
vertices[offset + 2] = Float.floatToRawIntBits(pos.z());
}
for (int i = 0; i < 4; i++)
{
int offset = i * STRIDE + NORMAL;
int normalIn = vertices[offset];
if ((normalIn >> 8) != 0)
{
float x = ((byte) (normalIn & 0xFF)) / 127.0f;
float y = ((byte) ((normalIn >> 8) & 0xFF)) / 127.0f;
float z = ((byte) ((normalIn >> 16) & 0xFF)) / 127.0f;
Vector3f pos = new Vector3f(x, y, z);
transform.transformNormal(pos);
pos.normalize();
vertices[offset] = (((byte) (x * 127.0f)) & 0xFF) |
((((byte) (y * 127.0f)) & 0xFF) << 8) |
((((byte) (z * 127.0f)) & 0xFF) << 16) |
(normalIn & 0xFF000000);
}
}
};
return QuadTransformers.applying(transform);
}
/**
* Creates a {@link BakedQuad} transformer that applies the specified lightmap.
*
* @deprecated Use {@link QuadTransformers#applyingLightmap(int)}
*/
@Deprecated(forRemoval = true, since = "1.19")
static IQuadTransformer applyingLightmap(int lightmap)
{
return quad -> {
var vertices = quad.getVertices();
for (int i = 0; i < 4; i++)
vertices[i * STRIDE + UV2] = lightmap;
};
return QuadTransformers.applyingLightmap(lightmap);
}
private static BakedQuad copy(BakedQuad quad)
@@ -50,9 +50,6 @@ public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
{
private static final Logger LOGGER = LogManager.getLogger();
// Transformer to set quads to max brightness
private static final IQuadTransformer MAX_LIGHTMAP_TRANSFORMER = IQuadTransformer.applyingLightmap(0x00F000F0);
@Nullable
private ImmutableList<Material> textures;
private final IntSet emissiveLayers;
@@ -98,7 +95,7 @@ public class ItemLayerModel implements IUnbakedGeometry<ItemLayerModel>
TextureAtlasSprite sprite = spriteGetter.apply(textures.get(i));
var unbaked = UnbakedGeometryHelper.createUnbakedItemElements(i, sprite);
var quads = UnbakedGeometryHelper.bakeElements(unbaked, $ -> sprite, modelState, modelLocation);
if (emissiveLayers.contains(i)) MAX_LIGHTMAP_TRANSFORMER.processInPlace(quads);
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);
@@ -0,0 +1,118 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.client.model;
import com.google.common.base.Preconditions;
import com.mojang.math.Transformation;
import com.mojang.math.Vector3f;
import com.mojang.math.Vector4f;
import net.minecraft.Util;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.block.model.BakedQuad;
import java.util.Arrays;
/**
* A collection of {@link IQuadTransformer} implementations.
*
* @see IQuadTransformer
*/
public final class QuadTransformers {
private static final IQuadTransformer EMPTY = quad -> {};
private static final IQuadTransformer[] EMISSIVE_TRANSFORMERS = Util.make(new IQuadTransformer[16], array -> {
Arrays.setAll(array, i -> applyingLightmap(LightTexture.pack(i, i)));
});
/**
* {@return a {@link BakedQuad} transformer that does nothing}
*/
public static IQuadTransformer empty()
{
return EMPTY;
}
/**
* {@return a new {@link BakedQuad} transformer that applies the specified {@link Transformation}}
*/
public static IQuadTransformer applying(Transformation transform)
{
if (transform.isIdentity())
return empty();
return quad -> {
var vertices = quad.getVertices();
for (int i = 0; i < 4; i++)
{
int offset = i * IQuadTransformer.STRIDE + IQuadTransformer.POSITION;
float x = Float.intBitsToFloat(vertices[offset]);
float y = Float.intBitsToFloat(vertices[offset + 1]);
float z = Float.intBitsToFloat(vertices[offset + 2]);
Vector4f pos = new Vector4f(x, y, z, 1);
transform.transformPosition(pos);
pos.perspectiveDivide();
vertices[offset] = Float.floatToRawIntBits(pos.x());
vertices[offset + 1] = Float.floatToRawIntBits(pos.y());
vertices[offset + 2] = Float.floatToRawIntBits(pos.z());
}
for (int i = 0; i < 4; i++)
{
int offset = i * IQuadTransformer.STRIDE + IQuadTransformer.NORMAL;
int normalIn = vertices[offset];
if ((normalIn >> 8) != 0)
{
float x = ((byte) (normalIn & 0xFF)) / 127.0f;
float y = ((byte) ((normalIn >> 8) & 0xFF)) / 127.0f;
float z = ((byte) ((normalIn >> 16) & 0xFF)) / 127.0f;
Vector3f pos = new Vector3f(x, y, z);
transform.transformNormal(pos);
pos.normalize();
vertices[offset] = (((byte) (x * 127.0f)) & 0xFF) |
((((byte) (y * 127.0f)) & 0xFF) << 8) |
((((byte) (z * 127.0f)) & 0xFF) << 16) |
(normalIn & 0xFF000000);
}
}
};
}
/**
* {@return a new {@link BakedQuad} transformer that applies the specified lightmap}
*/
public static IQuadTransformer applyingLightmap(int lightmap)
{
return quad -> {
var vertices = quad.getVertices();
for (int i = 0; i < 4; i++)
vertices[i * IQuadTransformer.STRIDE + IQuadTransformer.UV2] = lightmap;
};
}
/**
* {@return a {@link BakedQuad} transformer that sets the lightmap to the given emissivity (0-15)}
*/
public static IQuadTransformer settingEmissivity(int emissivity)
{
Preconditions.checkArgument(emissivity >= 0 && emissivity < 16, "Emissivity must be between 0 and 15.");
return EMISSIVE_TRANSFORMERS[emissivity];
}
/**
* {@return a {@link BakedQuad} transformer that sets the lightmap to its max value}
*/
public static IQuadTransformer settingMaxEmissivity()
{
return EMISSIVE_TRANSFORMERS[15];
}
private QuadTransformers()
{
}
}
@@ -317,6 +317,9 @@ 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);
}
faces.add(dir.getSerializedName(), faceObj);
}
if (!part.faces.isEmpty()) {
@@ -513,6 +516,7 @@ 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;
FaceBuilder(Direction dir) {
// param unused for functional match
@@ -559,11 +563,31 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
return this;
}
/**
* Set the emissivity of the face (0-15).
*
* @param emissivity the emissivity
* @return this builder
*/
public FaceBuilder emissivity(int emissivity) {
this.emissivity = emissivity;
return this;
}
/**
* Make the face emissive (emissivity = 15).
*
* @return this builder
*/
public FaceBuilder emissive() {
return emissivity(15);
}
BlockElementFace build() {
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));
return new BlockElementFace(cullface, tintindex, texture, new BlockFaceUV(uvs, rotation.rotation), emissivity);
}
public ElementBuilder end() { return ElementBuilder.this; }
+1
View File
@@ -14,6 +14,7 @@ net/minecraft/client/renderer/block/ModelBlockRenderer.renderModel(Lcom/mojang/b
net/minecraft/client/renderer/block/ModelBlockRenderer.tesselateBlock(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_111048_,p_111049_,p_111050_,p_111051_,p_111052_,p_111053_,p_111054_,p_111055_,p_111056_,p_111057_,modelData,renderType
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/BlockElementFace.<init>(Lnet/minecraft/core/Direction;ILjava/lang/String;Lnet/minecraft/client/renderer/block/model/BlockFaceUV;Z)V=|p_111359_,p_111360_,p_111361_,p_111362_,emissive
net/minecraft/client/renderer/block/model/BlockModel.bakeVanilla(Lnet/minecraft/client/resources/model/ModelBakery;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_111450_,p_111451_,p_111452_,p_111453_,p_111454_,p_111455_,renderTypes
net/minecraft/client/renderer/block/model/ItemTransform.<init>(Lcom/mojang/math/Vector3f;Lcom/mojang/math/Vector3f;Lcom/mojang/math/Vector3f;Lcom/mojang/math/Vector3f;)V=|p_111760_,p_111761_,p_111762_,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
@@ -0,0 +1,40 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.client;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
/**
* Test mod that demos emissivity on "elements" models.
*/
@Mod(EmissiveElementsTest.MOD_ID)
public class EmissiveElementsTest
{
public static final String MOD_ID = "emissive_elements_test";
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID);
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
public static final RegistryObject<Block> TEST_BLOCK = BLOCKS.register("emissive", () -> new Block(BlockBehaviour.Properties.of(Material.STONE)));
public static final RegistryObject<Item> TEST_BLOCK_ITEM = ITEMS.register("emissive", () -> new BlockItem(TEST_BLOCK.get(), new Item.Properties().tab(CreativeModeTab.TAB_DECORATIONS)));
public EmissiveElementsTest()
{
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
BLOCKS.register(modEventBus);
ITEMS.register(modEventBus);
}
}
+2
View File
@@ -238,6 +238,8 @@ modId="mod_mismatch_test"
modId="custom_transformtype_test"
[[mods]]
modId="mega_model_test"
[[mods]]
modId="emissive_elements_test"
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "emissive_elements_test:block/emissive" }
}
}
@@ -0,0 +1,34 @@
{
"parent": "block/block",
"render_type": "cutout",
"textures": {
"all": "minecraft:block/cobblestone",
"overlay": "minecraft:item/redstone"
},
"elements": [
{
"from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"down": { "texture": "#all", "cullface": "down" },
"up": { "texture": "#all", "cullface": "up" },
"north": { "texture": "#all", "cullface": "north" },
"south": { "texture": "#all", "cullface": "south" },
"west": { "texture": "#all", "cullface": "west" },
"east": { "texture": "#all", "cullface": "east" }
}
},
{
"from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"down": { "texture": "#overlay", "emissivity": 8, "cullface": "down" },
"up": { "texture": "#overlay", "emissivity": 4, "cullface": "up" },
"north": { "texture": "#overlay", "emissivity": 15, "cullface": "north" },
"south": { "texture": "#overlay", "emissivity": 15, "cullface": "south" },
"west": { "texture": "#overlay", "emissivity": 15, "cullface": "west" },
"east": { "texture": "#overlay", "emissivity": 15, "cullface": "east" }
}
}
]
}
@@ -0,0 +1,3 @@
{
"parent": "emissive_elements_test:block/emissive"
}