mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 19:39:22 +00:00
Allow faces of an "elements" model to be made emissive (#8890)
This commit is contained in:
1 parent
f9d0daa91c
commit
326e0329b9
13 files changed
+284
-63
No files matched your search
@@ -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; }
|
||||
|
||||
Reference in new issue
Block a user