From b740df84063e9e7b8f0527bafc5fd13629a4ee0d Mon Sep 17 00:00:00 2001 From: comp500 Date: Mon, 17 Jun 2024 00:24:00 +0100 Subject: [PATCH] Fix glint rendering issue (Indigo PR port) See https://github.com/FabricMC/fabric/issues/3853 and https://github.com/PepperCode1/Continuity/issues/429 Co-authored-by: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> --- gradle.properties | 2 +- .../mixin/renderer/ItemRendererAccessor.java | 30 +++++++++++++++++ .../renderer/render/ItemRenderContext.java | 33 ++++++++++++++++++- src/main/resources/indium.mixins.json | 1 + 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/main/java/link/infra/indium/mixin/renderer/ItemRendererAccessor.java diff --git a/gradle.properties b/gradle.properties index 339144d..cd45c41 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.21 yarn_mappings=1.21+build.1 loader_version=0.15.11 # Mod Properties -mod_version=1.0.32 +mod_version=1.0.33 maven_group=link.infra archives_base_name=indium # Dependencies diff --git a/src/main/java/link/infra/indium/mixin/renderer/ItemRendererAccessor.java b/src/main/java/link/infra/indium/mixin/renderer/ItemRendererAccessor.java new file mode 100644 index 0000000..5ca1d60 --- /dev/null +++ b/src/main/java/link/infra/indium/mixin/renderer/ItemRendererAccessor.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package link.infra.indium.mixin.renderer; + +import net.minecraft.client.render.item.ItemRenderer; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(ItemRenderer.class) +public interface ItemRendererAccessor { + @Invoker("usesDynamicDisplay") + static boolean indium$usesDynamicDisplay(ItemStack stack) { + throw new AssertionError(); + } +} diff --git a/src/main/java/link/infra/indium/renderer/render/ItemRenderContext.java b/src/main/java/link/infra/indium/renderer/render/ItemRenderContext.java index 8710836..2db9f8e 100644 --- a/src/main/java/link/infra/indium/renderer/render/ItemRenderContext.java +++ b/src/main/java/link/infra/indium/renderer/render/ItemRenderContext.java @@ -16,6 +16,7 @@ package link.infra.indium.renderer.render; +import link.infra.indium.mixin.renderer.ItemRendererAccessor; import link.infra.indium.renderer.helper.ColorHelper; import link.infra.indium.renderer.mesh.EncodingFormat; import link.infra.indium.renderer.mesh.MutableQuadViewImpl; @@ -36,6 +37,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.math.Direction; +import net.minecraft.util.math.MatrixUtil; import net.minecraft.util.math.random.LocalRandom; import net.minecraft.util.math.random.Random; import org.jetbrains.annotations.Nullable; @@ -82,13 +84,16 @@ public ModelTransformationMode itemTransformationMode() { private ItemStack itemStack; private ModelTransformationMode transformMode; + private MatrixStack matrixStack; private VertexConsumerProvider vertexConsumerProvider; private int lightmap; private boolean isDefaultTranslucent; private boolean isTranslucentDirect; private boolean isDefaultGlint; + private boolean isGlintDynamicDisplay; + private MatrixStack.Entry dynamicDisplayGlintEntry; private VertexConsumer translucentVertexConsumer; private VertexConsumer cutoutVertexConsumer; private VertexConsumer translucentGlintVertexConsumer; @@ -112,6 +117,7 @@ public BakedModelConsumer bakedModelConsumer() { public void renderModel(ItemStack itemStack, ModelTransformationMode transformMode, boolean invert, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int lightmap, int overlay, BakedModel model) { this.itemStack = itemStack; this.transformMode = transformMode; + this.matrixStack = matrixStack; this.vertexConsumerProvider = vertexConsumerProvider; this.lightmap = lightmap; this.overlay = overlay; @@ -123,8 +129,10 @@ public void renderModel(ItemStack itemStack, ModelTransformationMode transformMo model.emitItemQuads(itemStack, randomSupplier, this); this.itemStack = null; + this.matrixStack = null; this.vertexConsumerProvider = null; + dynamicDisplayGlintEntry = null; translucentVertexConsumer = null; cutoutVertexConsumer = null; translucentGlintVertexConsumer = null; @@ -151,6 +159,7 @@ private void computeOutputInfo() { } isDefaultGlint = itemStack.hasGlint(); + isGlintDynamicDisplay = ItemRendererAccessor.indium$usesDynamicDisplay(itemStack); } private void renderQuad(MutableQuadViewImpl quad) { @@ -170,7 +179,7 @@ private void renderQuad(MutableQuadViewImpl quad) { private void colorizeQuad(MutableQuadViewImpl quad, int colorIndex) { if (colorIndex != -1) { - final int itemColor = 0xFF000000 | colorMap.getColor(itemStack, colorIndex); + final int itemColor = colorMap.getColor(itemStack, colorIndex); for (int i = 0; i < 4; i++) { quad.color(i, ColorHelper.multiplyColor(itemColor, quad.color(i))); @@ -245,6 +254,10 @@ private VertexConsumer getVertexConsumer(BlendMode blendMode, TriState glintMode } private VertexConsumer createTranslucentVertexConsumer(boolean glint) { + if (glint && isGlintDynamicDisplay) { + return createDynamicDisplayGlintVertexConsumer(MinecraftClient.isFabulousGraphicsOrBetter() && !isTranslucentDirect ? TexturedRenderLayers.getItemEntityTranslucentCull() : TexturedRenderLayers.getEntityTranslucentCull()); + } + if (isTranslucentDirect) { return ItemRenderer.getDirectItemGlintConsumer(vertexConsumerProvider, TexturedRenderLayers.getEntityTranslucentCull(), true, glint); } else if (MinecraftClient.isFabulousGraphicsOrBetter()) { @@ -255,9 +268,27 @@ private VertexConsumer createTranslucentVertexConsumer(boolean glint) { } private VertexConsumer createCutoutVertexConsumer(boolean glint) { + if (glint && isGlintDynamicDisplay) { + return createDynamicDisplayGlintVertexConsumer(TexturedRenderLayers.getEntityCutout()); + } + return ItemRenderer.getDirectItemGlintConsumer(vertexConsumerProvider, TexturedRenderLayers.getEntityCutout(), true, glint); } + private VertexConsumer createDynamicDisplayGlintVertexConsumer(RenderLayer layer) { + if (dynamicDisplayGlintEntry == null) { + dynamicDisplayGlintEntry = matrixStack.peek().copy(); + + if (transformMode == ModelTransformationMode.GUI) { + MatrixUtil.scale(dynamicDisplayGlintEntry.getPositionMatrix(), 0.5F); + } else if (transformMode.isFirstPerson()) { + MatrixUtil.scale(dynamicDisplayGlintEntry.getPositionMatrix(), 0.75F); + } + } + + return ItemRenderer.getDynamicDisplayGlintConsumer(vertexConsumerProvider, layer, dynamicDisplayGlintEntry); + } + private class BakedModelConsumerImpl implements BakedModelConsumer { @Override public void accept(BakedModel model) { diff --git a/src/main/resources/indium.mixins.json b/src/main/resources/indium.mixins.json index 1e8c006..615f984 100644 --- a/src/main/resources/indium.mixins.json +++ b/src/main/resources/indium.mixins.json @@ -7,6 +7,7 @@ "renderer.MixinBlockModelRenderer", "renderer.MixinItemRenderer", "renderer.MixinBakedModel", + "renderer.ItemRendererAccessor", "sodium.AccessBlockRenderer", "sodium.MixinBlockRenderCache", "sodium.MixinChunkBuilderMeshingTask"