diff --git a/build.gradle b/build.gradle index 7ffac19..e746ab5 100644 --- a/build.gradle +++ b/build.gradle @@ -111,8 +111,8 @@ publishing { } // TODO: infer from fabric.mod.json?! -def supportedVersions = ["${project.minecraft_version}"] -def verName = "Indium ${project.mod_version} for Minecraft ${project.minecraft_version}/Sodium ${project.sodium_version}" +def supportedVersions = ["${project.minecraft_version}", "1.20.2"] +def verName = "Indium ${project.mod_version} for Minecraft 1.20.x/Sodium ${project.sodium_version}" // Check version is as expected if (System.getenv("EXPECTED_VERSION") !== null) { diff --git a/gradle.properties b/gradle.properties index db5645f..3a98e34 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,13 +6,13 @@ minecraft_version=1.20.1 yarn_mappings=1.20.1+build.8 loader_version=0.14.21 # Mod Properties -mod_version=1.0.25 +mod_version=1.0.26 maven_group=link.infra archives_base_name=indium # Dependencies # check this on https://modmuss50.me/fabric.html -fabric_version=0.84.0+1.20.1 -sodium_version=0.5.2 +fabric_version=0.89.0+1.20.1 +sodium_version=0.5.3 sodium_minecraft_version=1.20.1 # Publishing metadata curseforge_id=459496 diff --git a/src/main/java/link/infra/indium/mixin/renderer/MixinBakedModel.java b/src/main/java/link/infra/indium/mixin/renderer/MixinBakedModel.java new file mode 100644 index 0000000..92d8141 --- /dev/null +++ b/src/main/java/link/infra/indium/mixin/renderer/MixinBakedModel.java @@ -0,0 +1,41 @@ +/* + * 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 link.infra.indium.renderer.render.AbstractBlockRenderContext; +import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel; +import net.fabricmc.fabric.api.renderer.v1.render.RenderContext; +import net.fabricmc.fabric.impl.renderer.VanillaModelEncoder; +import net.minecraft.block.BlockState; +import net.minecraft.client.render.model.BakedModel; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.random.Random; +import net.minecraft.world.BlockRenderView; +import org.spongepowered.asm.mixin.Mixin; + +import java.util.function.Supplier; + +@Mixin(BakedModel.class) +public interface MixinBakedModel extends FabricBakedModel { + /** + * Override the fallback path to shade vanilla quads differently. + */ + @Override + default void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier randomSupplier, RenderContext context) { + VanillaModelEncoder.emitBlockQuads((BakedModel) this, state, randomSupplier, context, ((AbstractBlockRenderContext) context).getVanillaModelEmitter()); + } +} \ No newline at end of file diff --git a/src/main/java/link/infra/indium/mixin/renderer/MixinItemRenderer.java b/src/main/java/link/infra/indium/mixin/renderer/MixinItemRenderer.java index 89b7c78..9080220 100644 --- a/src/main/java/link/infra/indium/mixin/renderer/MixinItemRenderer.java +++ b/src/main/java/link/infra/indium/mixin/renderer/MixinItemRenderer.java @@ -16,6 +16,7 @@ package link.infra.indium.mixin.renderer; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -24,7 +25,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import link.infra.indium.renderer.accessor.AccessItemRenderer; -import link.infra.indium.renderer.render.IndiumQuadHandler; import link.infra.indium.renderer.render.ItemRenderContext; import net.minecraft.client.color.item.ItemColors; import net.minecraft.client.render.VertexConsumer; @@ -37,22 +37,19 @@ @Mixin(ItemRenderer.class) public abstract class MixinItemRenderer implements AccessItemRenderer { - @Shadow - protected ItemColors colors; + @Shadow @Final + private ItemColors colors; @Unique private final ThreadLocal indium_contexts = ThreadLocal.withInitial(() -> new ItemRenderContext(colors)); - @Unique - private final ItemRenderContext.VanillaQuadHandler indium_vanillaHandler = new IndiumQuadHandler(this); - @Shadow protected abstract void renderBakedItemModel(BakedModel model, ItemStack stack, int light, int overlay, MatrixStack matrixStack, VertexConsumer buffer); @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/BakedModel;isBuiltin()Z"), method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformationMode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", cancellable = true) public void hook_renderItem(ItemStack stack, ModelTransformationMode transformMode, boolean invert, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int light, int overlay, BakedModel model, CallbackInfo ci) { if (!model.isVanillaAdapter()) { - indium_contexts.get().renderModel(stack, transformMode, invert, matrixStack, vertexConsumerProvider, light, overlay, model, indium_vanillaHandler); + indium_contexts.get().renderModel(stack, transformMode, invert, matrixStack, vertexConsumerProvider, light, overlay, model); matrixStack.pop(); ci.cancel(); } diff --git a/src/main/java/link/infra/indium/renderer/render/AbstractBlockRenderContext.java b/src/main/java/link/infra/indium/renderer/render/AbstractBlockRenderContext.java index 0293456..38daacc 100644 --- a/src/main/java/link/infra/indium/renderer/render/AbstractBlockRenderContext.java +++ b/src/main/java/link/infra/indium/renderer/render/AbstractBlockRenderContext.java @@ -16,16 +16,7 @@ package link.infra.indium.renderer.render; -import static link.infra.indium.renderer.helper.GeometryHelper.AXIS_ALIGNED_FLAG; -import static link.infra.indium.renderer.helper.GeometryHelper.LIGHT_FACE_FLAG; - -import java.util.List; - -import org.jetbrains.annotations.Nullable; -import org.joml.Vector3f; - import link.infra.indium.Indium; -import link.infra.indium.renderer.IndiumRenderer; import link.infra.indium.renderer.aocalc.AoCalculator; import link.infra.indium.renderer.aocalc.AoConfig; import link.infra.indium.renderer.helper.ColorHelper; @@ -36,17 +27,22 @@ import me.jellysquid.mods.sodium.client.render.chunk.terrain.material.Material; import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial; import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter; -import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper; import net.fabricmc.fabric.api.util.TriState; +import net.fabricmc.fabric.impl.renderer.VanillaModelEncoder; import net.minecraft.block.BlockState; import net.minecraft.client.render.LightmapTextureManager; import net.minecraft.client.render.RenderLayer; import net.minecraft.client.render.WorldRenderer; import net.minecraft.client.render.model.BakedModel; -import net.minecraft.client.render.model.BakedQuad; +import net.minecraft.client.render.model.json.ModelTransformationMode; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.BlockRenderView; +import org.jetbrains.annotations.Nullable; +import org.joml.Vector3f; + +import static link.infra.indium.renderer.helper.GeometryHelper.AXIS_ALIGNED_FLAG; +import static link.infra.indium.renderer.helper.GeometryHelper.LIGHT_FACE_FLAG; /** * Subclasses must set the {@link #blockInfo} and {@link #aoCalc} fields in their constructor. @@ -67,6 +63,18 @@ public void emitDirectly() { } }; + private final MutableQuadViewImpl vanillaModelEditorQuad = new MutableQuadViewImpl() { + { + data = new int[EncodingFormat.TOTAL_STRIDE]; + clear(); + } + + @Override + public void emitDirectly() { + renderQuad(this, true); + } + }; + private final BakedModelConsumerImpl vanillaModelConsumer = new BakedModelConsumerImpl(); protected abstract LightDataAccess getLightCache(); @@ -79,6 +87,21 @@ public QuadEmitter getEmitter() { return editorQuad; } + public QuadEmitter getVanillaModelEmitter() { + // Do not clear the editorQuad since it is not accessible to API users. + return vanillaModelEditorQuad; + } + + @Override + public boolean isFaceCulled(@Nullable Direction face) { + return !blockInfo.shouldDrawFace(face); + } + + @Override + public ModelTransformationMode itemTransformationMode() { + throw new IllegalStateException("itemTransformationMode() can only be called on an item render context."); + } + @Override public BakedModelConsumer bakedModelConsumer() { return vanillaModelConsumer; @@ -89,7 +112,7 @@ private void renderQuad(MutableQuadViewImpl quad, boolean isVanilla) { return; } - if (!blockInfo.shouldDrawFace(quad.cullFace())) { + if (isFaceCulled(quad.cullFace())) { return; } @@ -296,21 +319,6 @@ private static int getOffsetLightmap(LightDataAccess lightCache, BlockPos pos, D * them through vanilla logic would require additional hooks. */ private class BakedModelConsumerImpl implements BakedModelConsumer { - private static final RenderMaterial MATERIAL_SHADED = IndiumRenderer.INSTANCE.materialFinder().find(); - private static final RenderMaterial MATERIAL_FLAT = IndiumRenderer.INSTANCE.materialFinder().ambientOcclusion(TriState.FALSE).find(); - - private final MutableQuadViewImpl editorQuad = new MutableQuadViewImpl() { - { - data = new int[EncodingFormat.TOTAL_STRIDE]; - clear(); - } - - @Override - public void emitDirectly() { - renderQuad(this, true); - } - }; - @Override public void accept(BakedModel model) { accept(model, blockInfo.blockState); @@ -318,23 +326,7 @@ public void accept(BakedModel model) { @Override public void accept(BakedModel model, @Nullable BlockState state) { - MutableQuadViewImpl editorQuad = this.editorQuad; - final RenderMaterial defaultMaterial = model.useAmbientOcclusion() ? MATERIAL_SHADED : MATERIAL_FLAT; - - for (int i = 0; i <= ModelHelper.NULL_FACE_ID; i++) { - final Direction cullFace = ModelHelper.faceFromIndex(i); - final List quads = model.getQuads(state, cullFace, blockInfo.randomSupplier.get()); - final int count = quads.size(); - - for (int j = 0; j < count; j++) { - final BakedQuad q = quads.get(j); - editorQuad.fromVanilla(q, defaultMaterial, cullFace); - // Call renderQuad directly instead of emit for efficiency - renderQuad(editorQuad, true); - } - } - - // Do not clear the editorQuad since it is not accessible to API users. + VanillaModelEncoder.emitBlockQuads(model, state, blockInfo.randomSupplier, AbstractBlockRenderContext.this, vanillaModelEditorQuad); } } } diff --git a/src/main/java/link/infra/indium/renderer/render/AbstractRenderContext.java b/src/main/java/link/infra/indium/renderer/render/AbstractRenderContext.java index a9702d9..ee083e7 100644 --- a/src/main/java/link/infra/indium/renderer/render/AbstractRenderContext.java +++ b/src/main/java/link/infra/indium/renderer/render/AbstractRenderContext.java @@ -63,7 +63,8 @@ protected final boolean transform(MutableQuadView q) { return activeTransform.transform(q); } - protected boolean hasTransform() { + @Override + public boolean hasTransform() { return activeTransform != NO_TRANSFORM; } diff --git a/src/main/java/link/infra/indium/renderer/render/IndiumQuadHandler.java b/src/main/java/link/infra/indium/renderer/render/IndiumQuadHandler.java deleted file mode 100644 index 0097ed9..0000000 --- a/src/main/java/link/infra/indium/renderer/render/IndiumQuadHandler.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.renderer.render; - -import link.infra.indium.renderer.accessor.AccessItemRenderer; -import net.minecraft.client.render.VertexConsumer; -import net.minecraft.client.render.model.BakedModel; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.item.ItemStack; - -public class IndiumQuadHandler implements ItemRenderContext.VanillaQuadHandler { - private final AccessItemRenderer itemRenderer; - - public IndiumQuadHandler(AccessItemRenderer itemRenderer) { - this.itemRenderer = itemRenderer; - } - - @Override - public void accept(BakedModel model, ItemStack stack, int color, int overlay, MatrixStack matrixStack, VertexConsumer buffer) { - itemRenderer.indium$renderBakedItemModel(model, stack, color, overlay, matrixStack, buffer); - } -} 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 322dae1..8710836 100644 --- a/src/main/java/link/infra/indium/renderer/render/ItemRenderContext.java +++ b/src/main/java/link/infra/indium/renderer/render/ItemRenderContext.java @@ -16,32 +16,20 @@ package link.infra.indium.renderer.render; -import java.util.List; -import java.util.function.Supplier; - -import org.jetbrains.annotations.Nullable; - -import link.infra.indium.renderer.IndiumRenderer; import link.infra.indium.renderer.helper.ColorHelper; import link.infra.indium.renderer.mesh.EncodingFormat; import link.infra.indium.renderer.mesh.MutableQuadViewImpl; import net.fabricmc.fabric.api.renderer.v1.material.BlendMode; import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial; import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter; -import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper; import net.fabricmc.fabric.api.util.TriState; +import net.fabricmc.fabric.impl.renderer.VanillaModelEncoder; import net.minecraft.block.BlockState; import net.minecraft.client.MinecraftClient; import net.minecraft.client.color.item.ItemColors; -import net.minecraft.client.render.LightmapTextureManager; -import net.minecraft.client.render.RenderLayer; -import net.minecraft.client.render.RenderLayers; -import net.minecraft.client.render.TexturedRenderLayers; -import net.minecraft.client.render.VertexConsumer; -import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.*; import net.minecraft.client.render.item.ItemRenderer; import net.minecraft.client.render.model.BakedModel; -import net.minecraft.client.render.model.BakedQuad; import net.minecraft.client.render.model.json.ModelTransformationMode; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.BlockItem; @@ -50,6 +38,9 @@ import net.minecraft.util.math.Direction; import net.minecraft.util.math.random.LocalRandom; import net.minecraft.util.math.random.Random; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Supplier; /** * The render context used for item rendering. @@ -77,14 +68,22 @@ public void emitDirectly() { } }; + @Override + public boolean isFaceCulled(@Nullable Direction face) { + throw new IllegalStateException("isFaceCulled can only be called on a block render context."); + } + + @Override + public ModelTransformationMode itemTransformationMode() { + return transformMode; + } + private final BakedModelConsumerImpl vanillaModelConsumer = new BakedModelConsumerImpl(); private ItemStack itemStack; private ModelTransformationMode transformMode; - private MatrixStack matrixStack; private VertexConsumerProvider vertexConsumerProvider; private int lightmap; - private VanillaQuadHandler vanillaHandler; private boolean isDefaultTranslucent; private boolean isTranslucentDirect; @@ -94,7 +93,6 @@ public void emitDirectly() { private VertexConsumer cutoutVertexConsumer; private VertexConsumer translucentGlintVertexConsumer; private VertexConsumer cutoutGlintVertexConsumer; - private VertexConsumer defaultVertexConsumer; public ItemRenderContext(ItemColors colorMap) { this.colorMap = colorMap; @@ -111,14 +109,12 @@ public BakedModelConsumer bakedModelConsumer() { return vanillaModelConsumer; } - public void renderModel(ItemStack itemStack, ModelTransformationMode transformMode, boolean invert, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int lightmap, int overlay, BakedModel model, VanillaQuadHandler vanillaHandler) { + 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; - this.vanillaHandler = vanillaHandler; computeOutputInfo(); matrix = matrixStack.peek().getPositionMatrix(); @@ -127,15 +123,12 @@ public void renderModel(ItemStack itemStack, ModelTransformationMode transformMo model.emitItemQuads(itemStack, randomSupplier, this); this.itemStack = null; - this.matrixStack = null; this.vertexConsumerProvider = null; - this.vanillaHandler = null; translucentVertexConsumer = null; cutoutVertexConsumer = null; translucentGlintVertexConsumer = null; cutoutGlintVertexConsumer = null; - defaultVertexConsumer = null; } private void computeOutputInfo() { @@ -158,8 +151,6 @@ private void computeOutputInfo() { } isDefaultGlint = itemStack.hasGlint(); - - defaultVertexConsumer = getVertexConsumer(BlendMode.DEFAULT, TriState.DEFAULT); } private void renderQuad(MutableQuadViewImpl quad) { @@ -275,34 +266,7 @@ public void accept(BakedModel model) { @Override public void accept(BakedModel model, @Nullable BlockState state) { - if (hasTransform()) { - MutableQuadViewImpl editorQuad = ItemRenderContext.this.editorQuad; - - // if there's a transform in effect, convert to mesh-based quads so that we can apply it - for (int i = 0; i <= ModelHelper.NULL_FACE_ID; i++) { - final Direction cullFace = ModelHelper.faceFromIndex(i); - random.setSeed(ITEM_RANDOM_SEED); - final List quads = model.getQuads(state, cullFace, random); - final int count = quads.size(); - - for (int j = 0; j < count; j++) { - final BakedQuad q = quads.get(j); - editorQuad.fromVanilla(q, IndiumRenderer.MATERIAL_STANDARD, cullFace); - // Call renderQuad directly instead of emit for efficiency - renderQuad(editorQuad); - } - } - - editorQuad.clear(); - } else { - vanillaHandler.accept(model, itemStack, lightmap, overlay, matrixStack, defaultVertexConsumer); - } + VanillaModelEncoder.emitItemQuads(model, state, randomSupplier, ItemRenderContext.this); } } - - /** used to accept a method reference from the ItemRenderer. */ - @FunctionalInterface - public interface VanillaQuadHandler { - void accept(BakedModel model, ItemStack stack, int color, int overlay, MatrixStack matrixStack, VertexConsumer buffer); - } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 056ed0e..080a425 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -29,8 +29,8 @@ "depends": { "fabricloader": ">=0.8.0", "minecraft": "~1.20.1", - "sodium": "0.5.2", - "fabric-renderer-api-v1": ">=3.1.0", + "sodium": "0.5.3", + "fabric-renderer-api-v1": ">=3.2.0", "fabric-resource-loader-v0": ">=0.4.0" } } diff --git a/src/main/resources/indium.mixins.json b/src/main/resources/indium.mixins.json index 0cd08ec..62a5a42 100644 --- a/src/main/resources/indium.mixins.json +++ b/src/main/resources/indium.mixins.json @@ -6,6 +6,7 @@ "renderer.AccessAmbientOcclusionCalculator", "renderer.MixinBlockModelRenderer", "renderer.MixinItemRenderer", + "renderer.MixinBakedModel", "sodium.AccessBlockRenderer", "sodium.MixinBlockRenderCache", "sodium.MixinChunkBuilderMeshingTask"