From 03973c4dc5927a9f67afdb412c0a920c20977878 Mon Sep 17 00:00:00 2001 From: drouarb Date: Thu, 11 Jul 2024 00:00:27 +0200 Subject: [PATCH] Culling item on belts --- .../content/kinetics/belt/BeltRenderer.java | 24 +++++++++++++++---- .../renderer/SafeBlockEntityRenderer.java | 21 ++++++++++++++++ .../resources/META-INF/accesstransformer.cfg | 3 +++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/kinetics/belt/BeltRenderer.java b/src/main/java/com/simibubi/create/content/kinetics/belt/BeltRenderer.java index c7241ed53..04f366232 100644 --- a/src/main/java/com/simibubi/create/content/kinetics/belt/BeltRenderer.java +++ b/src/main/java/com/simibubi/create/content/kinetics/belt/BeltRenderer.java @@ -198,10 +198,6 @@ public class BeltRenderer extends SafeBlockEntityRenderer { for (TransportedItemStack transported : be.getInventory() .getTransportedItems()) { - ms.pushPose(); - TransformStack.cast(ms) - .nudge(transported.angle); - float offset; float sideOffset; float verticalMovement; @@ -228,6 +224,18 @@ public class BeltRenderer extends SafeBlockEntityRenderer { .getAxis() == Axis.Z); float slopeAngle = onSlope ? tiltForward ? -45 : 45 : 0; + Vec3 itemPos = beltStartOffset.add( + be.getBlockPos().getX(), + be.getBlockPos().getY(), + be.getBlockPos().getZ()) + .add(offsetVec); + + if (this.shouldCullItem(itemPos)) { + continue; + } + + ms.pushPose(); + TransformStack.cast(ms).nudge(transported.angle); ms.translate(offsetVec.x, offsetVec.y, offsetVec.z); boolean alongX = beltFacing @@ -243,7 +251,13 @@ public class BeltRenderer extends SafeBlockEntityRenderer { boolean renderUpright = BeltHelper.isItemUpright(transported.stack); boolean blockItem = itemRenderer.getModel(transported.stack, be.getLevel(), null, 0) .isGui3d(); - int count = (int) (Mth.log2((int) (transported.stack.getCount()))) / 2; + + + int count = 0; + if (Minecraft.getInstance().player.getEyePosition(1.0F).distanceTo(itemPos) < 16) { + count = (int) (Mth.log2((int) (transported.stack.getCount()))) / 2; + } + Random r = new Random(transported.angle); boolean slopeShadowOnly = renderUpright && onSlope; diff --git a/src/main/java/com/simibubi/create/foundation/blockEntity/renderer/SafeBlockEntityRenderer.java b/src/main/java/com/simibubi/create/foundation/blockEntity/renderer/SafeBlockEntityRenderer.java index 1ecb77e29..f26940d4c 100644 --- a/src/main/java/com/simibubi/create/foundation/blockEntity/renderer/SafeBlockEntityRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/blockEntity/renderer/SafeBlockEntityRenderer.java @@ -2,10 +2,14 @@ package com.simibubi.create.foundation.blockEntity.renderer; import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.client.renderer.culling.Frustum; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; public abstract class SafeBlockEntityRenderer implements BlockEntityRenderer { @Override @@ -23,4 +27,21 @@ public abstract class SafeBlockEntityRenderer implements return !be.hasLevel() || be.getBlockState() .getBlock() == Blocks.AIR; } + + public boolean shouldCullItem(Vec3 itemPos) { + Frustum frustum = Minecraft.getInstance().levelRenderer.capturedFrustum != null ? + Minecraft.getInstance().levelRenderer.capturedFrustum : + Minecraft.getInstance().levelRenderer.cullingFrustum; + + AABB itemBB = new AABB( + itemPos.x - 0.25, + itemPos.y - 0.25, + itemPos.z - 0.25, + itemPos.x + 0.25, + itemPos.y + 0.25, + itemPos.z + 0.25 + ); + + return !frustum.isVisible(itemBB); + } } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index a911f646d..1bc660abe 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -42,3 +42,6 @@ public net.minecraft.client.model.AgeableListModel f_102012_ # bodyYOffset public net.minecraft.client.gui.components.CommandSuggestions f_93866_ # suggestions public net.minecraft.client.gui.components.CommandSuggestions$SuggestionsList (Lnet/minecraft/client/gui/components/CommandSuggestions;IIILjava/util/List;Z)V # + +public net.minecraft.client.renderer.LevelRenderer f_172938_ # cullingFrustum +public net.minecraft.client.renderer.LevelRenderer f_109442_ # capturedFrustum