From 6fd482bdbb98e186326ea9ab05965abd094f7a6e Mon Sep 17 00:00:00 2001 From: Robert <11903511+Roadhog360@users.noreply.github.com> Date: Sun, 28 Dec 2025 02:31:32 -0600 Subject: [PATCH] Misc changes, and a few fixes (#236) --- .../gtnewhorizon/gtnhlib/GTNHLibConfig.java | 5 +- .../gtnhlib/client/model/ModelISBRH.java | 73 +++++++++------- .../client/model/baked/PileOfQuads.java | 2 - .../model/loading/ModelDeserializer.java | 11 ++- .../client/model/unbaked/JSONModel.java | 56 +++++++++--- .../client/model/unbaked/MissingModel.java | 87 ++++++++++++++++++- .../renderer/cel/model/quad/ModelQuad.java | 22 +++++ .../cel/model/quad/ModelQuadView.java | 16 +++- .../cel/model/quad/ModelQuadViewMutable.java | 10 +++ .../quad/properties/ModelQuadFacing.java | 53 ++++++++++- .../gtnewhorizon/gtnhlib/mixins/Mixins.java | 7 +- .../early/models/MixinBlockParticle.java | 48 ---------- .../early/models/MixinBlock_IconWrapper.java | 71 +++++++++++++++ 13 files changed, 355 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlockParticle.java create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlock_IconWrapper.java diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java b/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java index d3a3ed69..c6bdb35c 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLibConfig.java @@ -24,6 +24,10 @@ public class GTNHLibConfig { @Config.RequiresMcRestart public static boolean autoTextureLoading; + @Config.Comment("Ensures that blocks always return a valid icon for JSON model blocks, by overriding the block icon functions and passing the particle icon.") + @Config.DefaultBoolean(true) + public static boolean modelIconWrapperMixin; + @Config.Comment("Enables various mixins that allow blocks to dynamically change their sound.") @Config.DefaultBoolean(true) @Config.RequiresMcRestart @@ -37,5 +41,4 @@ public class GTNHLibConfig { @Config.DefaultBoolean(false) @Config.RequiresMcRestart public static boolean enableTestBlocks; - } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelISBRH.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelISBRH.java index eec7ee66..211b195e 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelISBRH.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/ModelISBRH.java @@ -1,9 +1,7 @@ package com.gtnewhorizon.gtnhlib.client.model; -import static com.gtnewhorizon.gtnhlib.client.renderer.cel.api.util.NormI8.unpackX; -import static com.gtnewhorizon.gtnhlib.client.renderer.cel.api.util.NormI8.unpackY; -import static com.gtnewhorizon.gtnhlib.client.renderer.cel.api.util.NormI8.unpackZ; -import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing.DIRECTIONS; +import static com.gtnewhorizon.gtnhlib.client.renderer.cel.api.util.NormI8.*; +import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing.*; import static java.lang.Math.max; import static net.minecraftforge.client.IItemRenderer.ItemRenderType.ENTITY; import static net.minecraftforge.client.IItemRenderer.ItemRenderType.EQUIPPED; @@ -54,7 +52,7 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { /// Override this if you want programmatic model selection @SuppressWarnings("unused") - public BakedModel getModel(IBlockAccess world, Block block, int meta, int x, int y, int z) { + public BakedModel getModel(@Nullable IBlockAccess world, Block block, int meta, int x, int y, int z) { return ModelRegistry.getBakedModel(new BlockState(block, meta)); } @@ -74,11 +72,19 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { int color = model.getColor(world, x, y, z, block, meta, random); var rendered = false; - for (var dir : DIRECTIONS) { - // TODO: face culling - + for (var dir : VALUES) { final var quads = model.getQuads(world, x, y, z, block, meta, dir, random, color, null); if (quads.isEmpty()) continue; + if (dir.isDirection() && !renderer.renderAllFaces + && !shouldSideBeRendered( + world, + x + dir.getStepX(), + y + dir.getStepY(), + z + dir.getStepZ(), + dir.toForgeDir().ordinal(), + block)) { + continue; + } // iterates over the quads and dumps em into the tesselator, nothing special rendered = true; @@ -94,12 +100,12 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { final float g = (quadColor >> 8 & 255) / 255f; final float b = (quadColor >> 16 & 255) / 255f; - final int lm = getLightMap(block, quad, dir, world, x, y, z, renderer); + final int lm = getLightMap(block, quad, quad.getLightFace(), world, x, y, z, renderer); tesselator.setBrightness(lm); - final float shade = diffuseLight(quad.getComputedFaceNormal()); + final float shade = quad.hasDirectionalShading() ? diffuseLight(quad.getComputedFaceNormal()) : 1; tesselator.setColorOpaque_F(r * shade, g * shade, b * shade); - renderQuad(quad, x, y, z, tesselator, null); + renderQuad(quad, x, y, z, tesselator, renderer.overrideBlockTexture); } } @@ -108,6 +114,7 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { public void renderQuad(ModelQuadView quad, float x, float y, float z, Tessellator tessellator, @Nullable IIcon overrideIcon) { + // TODO: Respect overrideIcon for (int i = 0; i < 4; ++i) { tessellator.addVertexWithUV( quad.getX(i) + x, @@ -128,7 +135,7 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { final int lx = x + dir.getStepX(); final int ly = y + dir.getStepY(); final int lz = z + dir.getStepZ(); - return block.getMixedBrightnessForBlock(world, lx, ly, lz); + return getBrightness(block, quad, world, lx, ly, lz); } } case NEG_X, NEG_Y, NEG_Z -> { @@ -136,29 +143,39 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { final int lx = x + dir.getStepX(); final int ly = y + dir.getStepY(); final int lz = z + dir.getStepZ(); - return block.getMixedBrightnessForBlock(world, lx, ly, lz); + return getBrightness(block, quad, world, lx, ly, lz); } } } // The face is inset to some degree, pick self light (if transparent) if (block.getLightOpacity(world, x, y, z) != 0) { - return block.getMixedBrightnessForBlock(world, x, y, z); + return getBrightness(block, quad, world, x, y, z); } // ...or greatest among neighbors otherwise - int lm = block.getMixedBrightnessForBlock(world, x, y, z);; + int lm = getBrightness(block, quad, world, x, y, z); for (int i = 0; i < 6; i++) { final var neighbor = DIRECTIONS[i]; final int lx = x + neighbor.getStepX(); final int ly = y + neighbor.getStepY(); final int lz = z + neighbor.getStepZ(); - lm = max(lm, block.getMixedBrightnessForBlock(world, lx, ly, lz)); + lm = max(lm, getBrightness(block, quad, world, lx, ly, lz)); } return lm; } + private int getBrightness(Block block, ModelQuadView quad, IBlockAccess world, int x, int y, int z) { + int brightness = block.getMixedBrightnessForBlock(world, x, y, z); + if (quad.getEmissiveness() > 0) { + int blockLight = Math.max(quad.getEmissiveness(), (brightness & 0xF0) >> 4); + int skyLight = Math.max(quad.getEmissiveness(), (brightness & 0xF00000) >> 20); + return skyLight << 20 | blockLight << 4; + } + return brightness; + } + private float getAveragePos(ModelQuadView quad, ModelQuadFacing dir) { float avg = 0; for (int i = 0; i < 4; i++) { @@ -215,7 +232,7 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { int color = model.getColor(null, 0, 0, 0, block, meta, RAND); - for (ModelQuadFacing dir : DIRECTIONS) { + for (ModelQuadFacing dir : VALUES) { final var quads = model.getQuads(null, 0, 0, 0, block, meta, dir, RAND, color, null); if (quads.isEmpty()) { @@ -249,6 +266,15 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { GL11.glPopMatrix(); } + /** + * TODO: We need to find a good way to make it so the bound fields are not accounted for but I still want to use + * {@link Block#shouldSideBeRendered}. This is so blocks can define custom culling behavior when needed, but the + * bound fields are not relevant here because JSON models don't listen to them. + */ + protected boolean shouldSideBeRendered(IBlockAccess world, int x, int y, int z, int side, Block block) { + return block.shouldSideBeRendered(world, x, y, z, side); + } + private static final Vector3f rotated = new Vector3f(0f, 0f, 0f); private static final Vector3f translated = new Vector3f(0f, 0f, 0f); private static final Vector3f scaled = new Vector3f(1f, 1f, 1f); @@ -371,19 +397,8 @@ public class ModelISBRH implements ISimpleBlockRenderingHandler, IItemRenderer { } } - public IIcon getParticleIcon(Block block, IBlockAccess world, int x, int y, int z, int meta) { + public IIcon getParticleIcon(Block block, @Nullable IBlockAccess world, int x, int y, int z, int meta) { final var model = getModel(world, block, meta, x, y, z); return model.getParticle(meta, RAND); } - - public boolean isMissingIcon(IIcon icon) { - if (icon == null) return false; - - String name = icon.getIconName(); - if (name == null) return false; - - if (name.equals("missingno")) return false; - - return !name.startsWith("MISSING_ICON_BLOCK_"); - } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/baked/PileOfQuads.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/baked/PileOfQuads.java index 2abf7957..b2d8f953 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/baked/PileOfQuads.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/baked/PileOfQuads.java @@ -9,7 +9,6 @@ import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing.UNASSIGNED; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; @@ -30,7 +29,6 @@ import it.unimi.dsi.fastutil.objects.ObjectImmutableList; public final class PileOfQuads implements BakedModel { - public static final PileOfQuads BLANK = new PileOfQuads(new HashMap<>(), new HashMap<>(), null); private final SidedQuadStore sidedQuadStore; private final Map display; private final IIcon particle; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/loading/ModelDeserializer.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/loading/ModelDeserializer.java index fec2a739..1b7fbad6 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/loading/ModelDeserializer.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/loading/ModelDeserializer.java @@ -194,9 +194,10 @@ public class ModelDeserializer implements JsonDeserializer { final Vector3f to = loadVec3(json, "to").div(16); final ModelElement.Rotation rotation = loadRotation(json); final boolean shade = JsonUtil.loadBool(json, "shade", true); + final int lightEmission = JsonUtil.loadInt(json, "light_emission", 0); final List faces = loadFaces(json); - ret.add(new ModelElement(from, to, rotation, shade, faces)); + ret.add(new ModelElement(from, to, rotation, shade, lightEmission, faces)); } } @@ -288,14 +289,16 @@ public class ModelDeserializer implements JsonDeserializer { private final Vector3f to; private final @Nullable Rotation rotation; private final boolean shade; + private final int lightEmission; private final List faces; public ModelElement(Vector3f from, Vector3f to, @Nullable ModelDeserializer.ModelElement.Rotation rotation, - boolean shade, List faces) { + boolean shade, int lightEmission, List faces) { this.from = from; this.to = to; this.rotation = rotation; this.shade = shade; + this.lightEmission = lightEmission; this.faces = faces; } @@ -315,6 +318,10 @@ public class ModelDeserializer implements JsonDeserializer { return shade; } + public int lightEmission() { + return lightEmission; + } + public List faces() { return faces; } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/JSONModel.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/JSONModel.java index bf3cdb0f..f81bb2bf 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/JSONModel.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/JSONModel.java @@ -4,7 +4,6 @@ import static com.gtnewhorizon.gtnhlib.client.model.loading.ModelDeserializer.Mo import static com.gtnewhorizon.gtnhlib.client.model.loading.ModelRegistry.MODEL_LOGGER; import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing.NEG_Y; import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing.POS_Y; -import static com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing.UNASSIGNED; import static java.lang.Math.max; import static java.lang.Math.min; import static org.joml.Math.fma; @@ -40,6 +39,7 @@ import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuad; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadView; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadViewMutable; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing; +import com.gtnewhorizon.gtnhlib.client.renderer.cel.util.MathUtil; import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; @@ -57,7 +57,7 @@ public class JSONModel implements UnbakedModel { private final Object2ObjectMap textures; private List elements; - private static final Vector4f DEFAULT_UV = new Vector4f(0, 0, 16, 16); + protected static final Vector4f DEFAULT_UV = new Vector4f(0, 0, 16, 16); public JSONModel(@Nullable ModelLoc parentId, boolean useAO, Map display, @NotNull Object2ObjectMap textures, List elements) { @@ -81,7 +81,7 @@ public class JSONModel implements UnbakedModel { this.elements = og.elements; } - private static void setUV(ModelQuadViewMutable q, int i, float u, float v) { + protected static void setUV(ModelQuadViewMutable q, int i, float u, float v) { q.setTexU(i, u); q.setTexV(i, v); } @@ -91,7 +91,7 @@ public class JSONModel implements UnbakedModel { * Note: still doesn't fix AO. Whoops. */ @NotNull - private static Vector3f mapSideToVertex(Vector3f from, Vector3f to, int index, ForgeDirection side) { + protected static Vector3f mapSideToVertex(Vector3f from, Vector3f to, int index, ForgeDirection side) { return switch (side) { case DOWN -> switch (index) { case 0 -> new Vector3f(from.x, from.y, to.z); @@ -165,7 +165,6 @@ public class JSONModel implements UnbakedModel { // Assign vertexes final var quad = new ModelQuad(); for (int i = 0; i < 4; ++i) { - final Vector3f vert = mapSideToVertex(from, to, i, f.name()).mulPosition(rot).mulPosition(vRot); quad.setX(i, vert.x); quad.setY(i, vert.y); @@ -179,9 +178,11 @@ public class JSONModel implements UnbakedModel { Z = max(Z, vert.z); } - // Set culling and nominal faces - final var normFace = quad.getNormalFace(); - quad.setLightFace(normFace != UNASSIGNED ? normFace : POS_Y); + // Set shading and lighting + quad.setEmissiveness(e.lightEmission()); + quad.setDirectionalShading(e.shade()); + quad.setHasAmbientOcclusion(this.useAO); + quad.setLightFace(ModelQuadFacing.fromForgeDir(f.name())); // Set UV Vector4f uv = Objects.firstNonNull(f.uv(), DEFAULT_UV); @@ -189,6 +190,7 @@ public class JSONModel implements UnbakedModel { new Vector2f(uv.z, uv.w), new Vector2f(uv.z, uv.y) }; if (data.uvLock()) { + final var normFace = quad.getNormalFace(); int angle = 0; if (normFace == POS_Y) { angle = 360 - data.y(); @@ -222,11 +224,36 @@ public class JSONModel implements UnbakedModel { // Set the tint index quad.setColorIndex(f.tintIndex()); - // Set AO - quad.setHasAmbientOcclusion(this.useAO); - // Bake and add it - sidedQuadStore.computeIfAbsent(quad.getNormalFace(), d -> new ArrayList<>()).add(quad); + + ModelQuadFacing cullFace = ModelQuadFacing.fromForgeDir(f.cullFace()); + if (cullFace.isDirection()) { + // If cullface is not unassigned, we rotate it by the affine matrix, so that way the direction we + // check for culling also is rotated. + Vector3f facing = new Vector3f(cullFace.getStepX(), cullFace.getStepY(), cullFace.getStepZ()) + .mulDirection(vRot); + // Only one of these three vector coordinates should be a value other than 0. Error handling should + // be done where the vRot's data is serialized. + if (MathUtil.roughlyEqual(facing.x, 1)) { + cullFace = ModelQuadFacing.POS_X; + } + if (MathUtil.roughlyEqual(facing.x, -1)) { + cullFace = ModelQuadFacing.NEG_X; + } + if (MathUtil.roughlyEqual(facing.y, 1)) { + cullFace = ModelQuadFacing.POS_Y; + } + if (MathUtil.roughlyEqual(facing.y, -1)) { + cullFace = ModelQuadFacing.NEG_Y; + } + if (MathUtil.roughlyEqual(facing.z, 1)) { + cullFace = ModelQuadFacing.POS_Z; + } + if (MathUtil.roughlyEqual(facing.z, -1)) { + cullFace = ModelQuadFacing.NEG_Z; + } + } + sidedQuadStore.computeIfAbsent(cullFace, d -> new ArrayList<>()).add(quad); } } @@ -234,14 +261,15 @@ public class JSONModel implements UnbakedModel { return new PileOfQuads(sidedQuadStore, this.display, this.getParticle()); } - // TODO fix - private void bakeSprite(ModelQuadViewMutable quad, String name) { + // TODO Doesn't account for UV rotation settings atm + protected void bakeSprite(ModelQuadViewMutable quad, String name) { name = name.replaceFirst("^minecraft:", ""); final var icon = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(name); final float minU = icon.getMinU(); final float minV = icon.getMinV(); final float dU = icon.getMaxU() - minU; final float dV = icon.getMaxV() - minV; + quad.setSprite(icon); for (int i = 0; i < 4; ++i) { quad.setTexU(i, fma(dU, quad.getTexU(i) / 16, minU)); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/MissingModel.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/MissingModel.java index 1e514343..adbbac91 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/MissingModel.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/model/unbaked/MissingModel.java @@ -1,9 +1,25 @@ package com.gtnewhorizon.gtnhlib.client.model.unbaked; -import static com.gtnewhorizon.gtnhlib.client.model.baked.PileOfQuads.BLANK; +import static com.gtnewhorizon.gtnhlib.client.model.loading.ModelDeserializer.ModelElement.Rotation.NOOP; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.IIcon; +import net.minecraftforge.common.util.ForgeDirection; + +import org.joml.Matrix4f; +import org.joml.Vector3f; +import org.joml.Vector4f; import com.gtnewhorizon.gtnhlib.client.model.BakeData; import com.gtnewhorizon.gtnhlib.client.model.baked.BakedModel; +import com.gtnewhorizon.gtnhlib.client.model.baked.PileOfQuads; +import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuad; +import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadView; +import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties.ModelQuadFacing; import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; @@ -15,8 +31,75 @@ public class MissingModel extends JSONModel { super(null, false, null, Object2ObjectMaps.emptyMap(), null); } + @Override + public BakedModel bake() { + return super.bake(); + } + + /** + * Overrides the default baking logic to bake a hardcoded cube. Used when a state not specified in the blockstate + * file is chosen to render. Also appears when the model provided is missing. + *

+ * TODO: Possible memory leak since a new one is baked for every missing state. Perhaps we could store a singleton + * that's cached on first bake, that is reset on resource reload. Would that need a threadlocal? + */ @Override public BakedModel bake(BakeData data) { - return BLANK; + final var vRot = data.getAffineMatrix(); + final var sidedQuadStore = new HashMap>(7); + for (ForgeDirection facing : ForgeDirection.VALID_DIRECTIONS) { + ModelQuadFacing quadFacing = ModelQuadFacing.fromForgeDir(facing); + final Matrix4f rot = NOOP.getAffineMatrix(); + + // The from vector is the minimum corner of the face's element + final Vector3f from = switch (quadFacing) { + case POS_X -> new Vector3f(1, 0, 0); + case POS_Y -> new Vector3f(0, 1, 0); + case POS_Z -> new Vector3f(0, 0, 1); + case NEG_X, NEG_Y, NEG_Z -> new Vector3f(0, 0, 0); + case UNASSIGNED -> null; + }; + + // The to vector is the maximum corner of the face's element + final Vector3f to = switch (quadFacing) { + // POS_X (Fixed X=1, Y/Z span 0->1) + case POS_X, POS_Y, POS_Z -> new Vector3f(1, 1, 1); + case NEG_X -> new Vector3f(0, 1, 1); + case NEG_Y -> new Vector3f(1, 0, 1); + case NEG_Z -> new Vector3f(1, 1, 0); + case UNASSIGNED -> null; + }; + + // Assign vertexes + final var quad = new ModelQuad(); + for (int i = 0; i < 4; ++i) { + final Vector3f vert = mapSideToVertex(from, to, i, facing).mulPosition(rot).mulPosition(vRot); + quad.setX(i, vert.x); + quad.setY(i, vert.y); + quad.setZ(i, vert.z); + } + + // Set culling and nominal faces + quad.setLightFace(quadFacing); + quad.setColorIndex(-1); + quad.setDirectionalShading(true); + quad.setEmissiveness(0); + + // Set UV + final Vector4f uv = DEFAULT_UV; + setUV(quad, 0, uv.x, uv.y); + setUV(quad, 1, uv.x, uv.w); + setUV(quad, 2, uv.z, uv.w); + setUV(quad, 3, uv.z, uv.y); + + bakeSprite(quad, "missingno"); + + // Bake and add it + sidedQuadStore.computeIfAbsent(quadFacing, d -> new ArrayList<>()).add(quad); + } + + final IIcon missing = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite("missingno"); + // Add them to the model + return new PileOfQuads(sidedQuadStore, Collections.emptyMap(), missing); } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuad.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuad.java index 1f149742..7ea0e834 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuad.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuad.java @@ -32,6 +32,8 @@ public class ModelQuad implements ModelQuadViewMutable { private boolean hasAmbientOcclusion = true; private int shaderBlockId; + private int emissiveness; + private boolean dirShading; public ModelQuad() {} @@ -92,6 +94,16 @@ public class ModelQuad implements ModelQuadViewMutable { this.colorIdx = index; } + @Override + public boolean setDirectionalShading(boolean dirShading) { + return this.dirShading = dirShading; + } + + @Override + public int setEmissiveness(int emissiveness) { + return this.emissiveness = emissiveness; + } + @Override public void setLightFace(ModelQuadFacing face) { if (!face.isDirection()) { @@ -120,6 +132,16 @@ public class ModelQuad implements ModelQuadViewMutable { return this.colorIdx; } + @Override + public boolean hasDirectionalShading() { + return dirShading; + } + + @Override + public int getEmissiveness() { + return emissiveness; + } + @Override public float getX(int idx) { return Float.intBitsToFloat(this.data[vertexOffset(idx) + POSITION_INDEX]); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadView.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadView.java index 9fbde75d..315f8df1 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadView.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadView.java @@ -62,6 +62,18 @@ public interface ModelQuadView extends ModelPrimitiveView { */ int getColorIndex(); + /** + * Not to be confused with ambient occlusion AKA smooth lighting When true, allows this quad to multiply its + * brightness based on the direction it's facing. When this is false, each face derives its brightness directly from + * the light value it's facing without multiplying it. + */ + boolean hasDirectionalShading(); + + /** + * The emissiveness of this face, effectively the minimum brightness value it will be rendered with. + */ + int getEmissiveness(); + /** * @return The sprite texture used by this quad, or null if none is attached */ @@ -97,9 +109,5 @@ public interface ModelQuadView extends ModelPrimitiveView { return true; } - default int getVanillaLightEmission() { - return 0; - } - int getShaderBlockId(); } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadViewMutable.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadViewMutable.java index 1a2d6477..938ee174 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadViewMutable.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/ModelQuadViewMutable.java @@ -58,6 +58,16 @@ public interface ModelQuadViewMutable extends ModelQuadView { */ void setColorIndex(int index); + /** + * See {@link ModelQuadView#hasDirectionalShading()} for what this value does + */ + boolean setDirectionalShading(boolean dirShading); + + /** + * See {@link ModelQuadView#getEmissiveness()} for what this value does. + */ + int setEmissiveness(int emissiveness); + /** * Sets the face used by this quad for lighting effects */ diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/properties/ModelQuadFacing.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/properties/ModelQuadFacing.java index d91643d2..f727f3d4 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/properties/ModelQuadFacing.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/cel/model/quad/properties/ModelQuadFacing.java @@ -2,6 +2,9 @@ package com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.properties; import java.util.Arrays; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.common.util.ForgeDirection; + import com.gtnewhorizon.gtnhlib.client.renderer.cel.api.util.NormI8; import lombok.Getter; @@ -17,7 +20,7 @@ public enum ModelQuadFacing { UNASSIGNED(0, 0, 0, null); public static final ModelQuadFacing[] VALUES = ModelQuadFacing.values(); - public static final ModelQuadFacing[] DIRECTIONS = Arrays.stream(VALUES).filter(facing -> facing != UNASSIGNED) + public static final ModelQuadFacing[] DIRECTIONS = Arrays.stream(VALUES).filter(ModelQuadFacing::isDirection) .toArray(ModelQuadFacing[]::new); public static final ModelQuadFacing[] HORIZONTAL_DIRECTIONS = Arrays.stream(DIRECTIONS) .filter(facing -> facing.axis != Axis.Y).toArray(ModelQuadFacing[]::new); @@ -75,4 +78,52 @@ public enum ModelQuadFacing { }; } } + + public static ModelQuadFacing fromForgeDir(ForgeDirection dir) { + return switch (dir) { + case UP -> POS_Y; + case DOWN -> NEG_Y; + case EAST -> POS_X; + case WEST -> NEG_X; + case SOUTH -> POS_Z; + case NORTH -> NEG_Z; + case UNKNOWN -> UNASSIGNED; + }; + } + + public static ModelQuadFacing fromEnumFacing(EnumFacing dir) { + return switch (dir) { + case UP -> POS_Y; + case DOWN -> NEG_Y; + case WEST -> POS_X; + case EAST -> NEG_X; + case NORTH -> NEG_Z; + case SOUTH -> POS_Z; + }; + } + + public ForgeDirection toForgeDir() { + return switch (this) { + case POS_Y -> ForgeDirection.UP; + case NEG_Y -> ForgeDirection.DOWN; + case POS_X -> ForgeDirection.EAST; + case NEG_X -> ForgeDirection.WEST; + case POS_Z -> ForgeDirection.SOUTH; + case NEG_Z -> ForgeDirection.NORTH; + case UNASSIGNED -> ForgeDirection.UNKNOWN; + }; + } + + public EnumFacing toEnumFacing() { + return switch (this) { + case POS_Y -> EnumFacing.UP; + case NEG_Y -> EnumFacing.DOWN; + case POS_X -> EnumFacing.WEST; + case NEG_X -> EnumFacing.EAST; + case POS_Z -> EnumFacing.SOUTH; + case NEG_Z -> EnumFacing.NORTH; + case UNASSIGNED -> throw new IllegalArgumentException( + "Cannot convert UNASSIGNED ModelQuadFacing to EnumFacing!"); + }; + } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java index 38022c23..efcfdf19 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java @@ -31,9 +31,10 @@ public enum Mixins implements IMixins { "models.MixinFolderResourcePack", "models.SRRMAccessor") .setPhase(Phase.EARLY).setApplyIf(() -> GTNHLibConfig.autoTextureLoading)), - BLOCK_PARTICLE( - new MixinBuilder("Automatically override model block particle").addClientMixins("models.MixinBlockParticle") - .setPhase(Phase.EARLY).setApplyIf(() -> GTNHLibConfig.autoTextureLoading)), + MODEL_ICON_WRAPPER(new MixinBuilder( + "Ensures that blocks always return a valid icon for JSON model blocks, by using the particle icon.") + .addClientMixins("models.MixinBlock_IconWrapper").setPhase(Phase.EARLY) + .setApplyIf(() -> GTNHLibConfig.modelIconWrapperMixin)), MODEL_ITEM_RENDERER(new MixinBuilder("Restore origin pivot before modifier").addClientMixins("models.MixinModelFHC") .setPhase(Phase.EARLY)), DYNAMIC_BLOCK_SOUNDS(new MixinBuilder("Dynamic block sounds") diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlockParticle.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlockParticle.java deleted file mode 100644 index bbbb1cda..00000000 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlockParticle.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.gtnewhorizon.gtnhlib.mixins.early.models; - -import net.minecraft.block.Block; -import net.minecraft.util.IIcon; -import net.minecraft.world.IBlockAccess; - -import org.spongepowered.asm.mixin.Mixin; - -import com.gtnewhorizon.gtnhlib.client.model.ModelISBRH; -import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; - -@Mixin(Block.class) -public class MixinBlockParticle { - - @WrapMethod(method = "getIcon(Lnet/minecraft/world/IBlockAccess;IIII)Lnet/minecraft/util/IIcon;") - private IIcon nhlib$wrapGetIcon(IBlockAccess world, int x, int y, int z, int side, Operation original) { - Block block = (Block) (Object) this; - - IIcon icon = original.call(world, x, y, z, side); - if (ModelISBRH.INSTANCE.isMissingIcon(icon)) { - return icon; - } - - if (block.getRenderType() == ModelISBRH.JSON_ISBRH_ID) { - int meta = world.getBlockMetadata(x, y, z); - return ModelISBRH.INSTANCE.getParticleIcon(block, world, x, y, z, meta); - } - - return icon; - } - - @WrapMethod(method = "getIcon(II)Lnet/minecraft/util/IIcon;") - private IIcon nhlib$wrapGetIcon(int side, int meta, Operation original) { - Block block = (Block) (Object) this; - - IIcon icon = original.call(meta, side); - if (ModelISBRH.INSTANCE.isMissingIcon(icon)) { - return icon; - } - - if (block.getRenderType() == ModelISBRH.JSON_ISBRH_ID) { - return ModelISBRH.INSTANCE.getParticleIcon(block, null, 0, 0, 0, meta); - } - - return icon; - } -} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlock_IconWrapper.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlock_IconWrapper.java new file mode 100644 index 00000000..ada66450 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/models/MixinBlock_IconWrapper.java @@ -0,0 +1,71 @@ +package com.gtnewhorizon.gtnhlib.mixins.early.models; + +import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.util.IIcon; +import net.minecraft.world.IBlockAccess; + +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; + +import com.gtnewhorizon.gtnhlib.client.model.ModelISBRH; +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; + +import cpw.mods.fml.client.FMLClientHandler; + +@Mixin(Block.class) +public abstract class MixinBlock_IconWrapper { + + @Shadow + public abstract int getRenderType(); + + @Shadow + protected IIcon blockIcon; + + @WrapMethod(method = "getIcon(Lnet/minecraft/world/IBlockAccess;IIII)Lnet/minecraft/util/IIcon;") + private IIcon nhlib$wrapGetIcon(IBlockAccess world, int x, int y, int z, int side, Operation original) { + return getRenderType() == ModelISBRH.JSON_ISBRH_ID + ? nhlib$getParticleIcon(world, x, y, z, world.getBlockMetadata(x, y, z)) + : original.call(world, x, y, z, side); + } + + @WrapMethod(method = "getIcon(II)Lnet/minecraft/util/IIcon;") + private IIcon nhlib$wrapGetIcon(int side, int meta, Operation original) { + return getRenderType() == ModelISBRH.JSON_ISBRH_ID ? nhlib$getParticleIcon(null, 0, 0, 0, meta) + : original.call(side, meta); + } + + @WrapMethod(method = "func_149735_b") + private IIcon nhlib$wrapGetIconObf(int side, int meta, Operation original) { + return getRenderType() == ModelISBRH.JSON_ISBRH_ID ? nhlib$getParticleIcon(null, 0, 0, 0, meta) + : original.call(side, meta); + } + + @WrapMethod(method = "getBlockTextureFromSide") + private IIcon nhlib$wrapGetIcon(int side, Operation original) { + // Set the blockIcon here so anything outside accessing it with ATs still gets the correct icon. + // Can't do it in registericons because the icon isn't baked or somethimg, it crashes. + return getRenderType() == ModelISBRH.JSON_ISBRH_ID ? nhlib$getParticleIcon(null, 0, 0, 0, 0) + : original.call(side); + } + + @WrapMethod(method = "registerBlockIcons") + private void nhlib$setField(IIconRegister reg, Operation original) { + original.call(reg); + if (getRenderType() == ModelISBRH.JSON_ISBRH_ID && blockIcon == null) { + blockIcon = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite("missingno"); + } + } + + @Unique + private IIcon nhlib$getParticleIcon(@Nullable IBlockAccess world, int x, int y, int z, int meta) { + IIcon particle = ModelISBRH.INSTANCE.getParticleIcon((Block) (Object) this, world, x, y, z, meta); + return particle == null + ? FMLClientHandler.instance().getClient().getTextureMapBlocks().registerIcon("missingno") + : particle; + } +}