Skip to content

Commit 5dddc5a

Browse files
committed
accurate culling
# Conflicts: # src/main/java/com/simibubi/create/content/kinetics/chainConveyor/ChainConveyorBlockEntity.java # src/main/java/com/simibubi/create/content/kinetics/chainConveyor/ChainConveyorRenderer.java # src/main/resources/create.mixins.json
1 parent d667786 commit 5dddc5a

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

src/main/java/com/simibubi/create/content/kinetics/chainConveyor/ChainConveyorBlockEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public ChainConveyorBlockEntity(BlockEntityType<?> typeIn, BlockPos pos, BlockSt
8888

8989
@Override
9090
protected AABB createRenderBoundingBox() {
91-
return new AABB(worldPosition).inflate(connections.isEmpty() ? 3 : 64);
91+
return AABB.INFINITE;
9292
}
9393

9494
@Override

src/main/java/com/simibubi/create/content/kinetics/chainConveyor/ChainConveyorRenderer.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.Map.Entry;
55

6+
import org.joml.FrustumIntersection;
67
import org.joml.Matrix4f;
78

89
import com.mojang.blaze3d.vertex.PoseStack;
@@ -52,25 +53,33 @@ protected void renderSafe(ChainConveyorBlockEntity be, float partialTicks, PoseS
5253
super.renderSafe(be, partialTicks, ms, buffer, light, overlay);
5354
BlockPos pos = be.getBlockPos();
5455

55-
renderChains(be, ms, buffer, light, overlay);
56+
FrustumIntersection frustum = null;
57+
Vec3 camPos = null;
58+
if(Minecraft.getInstance().level == be.getLevel())
59+
{
60+
frustum = getFrustumIntersection();
61+
camPos = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();
62+
}
63+
renderChains(be, ms, buffer, light, overlay, frustum, camPos);
5664

5765
if (VisualizationManager.supportsVisualization(be.getLevel()))
5866
return;
5967

60-
CachedBuffers.partial(AllPartialModels.CHAIN_CONVEYOR_WHEEL, be.getBlockState())
61-
.light(light)
62-
.overlay(overlay)
63-
.renderInto(ms, buffer.getBuffer(RenderType.cutoutMipped()));
68+
if (frustum != null && frustum.testAab(pos.getX() - 2 - (float) camPos.x, pos.getY() - (float) camPos.y, pos.getZ() - 2 - (float) camPos.z, pos.getX() + 2 - (float) camPos.x, pos.getY() + 1 - (float) camPos.y, pos.getZ() + 2 - (float) camPos.z))
69+
CachedBuffers.partial(AllPartialModels.CHAIN_CONVEYOR_WHEEL, be.getBlockState())
70+
.light(light)
71+
.overlay(overlay)
72+
.renderInto(ms, buffer.getBuffer(RenderType.cutoutMipped()));
6473

6574
for (ChainConveyorPackage box : be.loopingPackages)
66-
renderBox(be, ms, buffer, overlay, pos, box, partialTicks);
75+
renderBox(be, ms, buffer, overlay, pos, box, partialTicks, frustum, camPos);
6776
for (Entry<BlockPos, List<ChainConveyorPackage>> entry : be.travellingPackages.entrySet())
6877
for (ChainConveyorPackage box : entry.getValue())
69-
renderBox(be, ms, buffer, overlay, pos, box, partialTicks);
78+
renderBox(be, ms, buffer, overlay, pos, box, partialTicks, frustum, camPos);
7079
}
7180

7281
private void renderBox(ChainConveyorBlockEntity be, PoseStack ms, MultiBufferSource buffer, int overlay,
73-
BlockPos pos, ChainConveyorPackage box, float partialTicks) {
82+
BlockPos pos, ChainConveyorPackage box, float partialTicks, FrustumIntersection frustum, Vec3 camPos) {
7483
if (box.worldPosition == null)
7584
return;
7685
if (box.item == null || box.item.isEmpty())
@@ -82,6 +91,9 @@ private void renderBox(ChainConveyorBlockEntity be, PoseStack ms, MultiBufferSou
8291

8392
Vec3 position = physicsData.prevPos.lerp(physicsData.pos, partialTicks);
8493
Vec3 targetPosition = physicsData.prevTargetPos.lerp(physicsData.targetPos, partialTicks);
94+
if (frustum != null && !frustum.testSphere((float) (targetPosition.x - camPos.x), (float) (targetPosition.y - camPos.y), (float) (targetPosition.z - camPos.z), 1))
95+
return;
96+
8597
float yaw = AngleHelper.angleLerp(partialTicks, physicsData.prevYaw, physicsData.yaw);
8698
Vec3 offset =
8799
new Vec3(targetPosition.x - pos.getX(), targetPosition.y - pos.getY(), targetPosition.z - pos.getZ());
@@ -141,7 +153,7 @@ private static Vec3 getClosestPointOnChain(Vec3 cam, Vec3 start, Vec3 end) {
141153
}
142154

143155
private void renderChains(ChainConveyorBlockEntity be, PoseStack ms, MultiBufferSource buffer, int light,
144-
int overlay) {
156+
int overlay, FrustumIntersection frustum, Vec3 camPos) {
145157
float time = AnimationTickHolder.getRenderTime(be.getLevel()) / (360f / Math.abs(be.getSpeed()));
146158
time %= 1;
147159
if (time < 0)
@@ -154,6 +166,10 @@ private void renderChains(ChainConveyorBlockEntity be, PoseStack ms, MultiBuffer
154166
if (stats == null)
155167
continue;
156168

169+
if (frustum != null && !frustum.testLineSegment((float) (stats.start().x - camPos.x), (float) (stats.start().y - camPos.y), (float) (stats.start().z - camPos.z),
170+
(float) (stats.end().x - camPos.x), (float) (stats.end().y - camPos.y), (float) (stats.end().z - camPos.z))) {
171+
continue;
172+
}
157173
Vec3 diff = stats.end()
158174
.subtract(stats.start());
159175
double yaw = (float) Mth.RAD_TO_DEG * Mth.atan2(diff.x, diff.z);
@@ -193,9 +209,7 @@ private void renderChains(ChainConveyorBlockEntity be, PoseStack ms, MultiBuffer
193209
level.getBrightness(LightLayer.SKY, tilePos.offset(blockPos)));
194210

195211
boolean far = false;
196-
if (Minecraft.getInstance().level == be.getLevel()) {
197-
Vec3 camPos = Minecraft.getInstance()
198-
.getBlockEntityRenderDispatcher().camera.getPosition();
212+
if (frustum != null) {
199213
Vec3 closest = getClosestPointOnChain(camPos, stats.start(), stats.end());
200214
if (closest.distanceToSqr(camPos) > MIP_DISTANCE_SQR)
201215
far = true;

src/main/java/com/simibubi/create/foundation/blockEntity/renderer/SafeBlockEntityRenderer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.simibubi.create.foundation.blockEntity.CachedRenderBBBlockEntity;
77
import com.simibubi.create.foundation.mixin.accessor.LevelRendererAccessor;
88

9+
import com.simibubi.create.foundation.mixin.accessor.FrustumAccessor;
910
import com.simibubi.create.foundation.mixin.accessor.LevelRendererAccessor;
1011

1112
import net.createmod.ponder.api.level.PonderLevel;
@@ -19,6 +20,8 @@
1920
import net.minecraft.world.phys.AABB;
2021
import net.minecraft.world.phys.Vec3;
2122

23+
import org.joml.FrustumIntersection;
24+
2225
public abstract class SafeBlockEntityRenderer<T extends BlockEntity> implements BlockEntityRenderer<T> {
2326
@Override
2427
public final void render(T be, float partialTicks, PoseStack ms, MultiBufferSource bufferSource, int light,
@@ -36,14 +39,20 @@ public boolean isInvalid(T be) {
3639
.getBlock() == Blocks.AIR;
3740
}
3841

42+
public Frustum getFrustum() {
43+
LevelRendererAccessor accessor = (LevelRendererAccessor) Minecraft.getInstance().levelRenderer;
44+
return accessor.create$getCapturedFrustum() != null ? accessor.create$getCapturedFrustum() : accessor.create$getCullingFrustum();
45+
}
46+
47+
public FrustumIntersection getFrustumIntersection() {
48+
return ((FrustumAccessor) getFrustum()).create$getFrustumIntersection();
49+
}
50+
3951
public boolean shouldCullItem(Vec3 itemPos, Level level) {
4052
if (level instanceof PonderLevel)
4153
return false;
4254

43-
LevelRendererAccessor accessor = (LevelRendererAccessor) Minecraft.getInstance().levelRenderer;
44-
Frustum frustum = accessor.create$getCapturedFrustum() != null ?
45-
accessor.create$getCapturedFrustum() :
46-
accessor.create$getCullingFrustum();
55+
Frustum frustum = getFrustum();
4756

4857
AABB itemBB = new AABB(
4958
itemPos.x - 0.25,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.simibubi.create.foundation.mixin.accessor;
2+
3+
import net.minecraft.client.renderer.culling.Frustum;
4+
5+
import org.joml.FrustumIntersection;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.gen.Accessor;
8+
9+
@Mixin(Frustum.class)
10+
public interface FrustumAccessor {
11+
@Accessor("intersection")
12+
FrustumIntersection create$getFrustumIntersection();
13+
}

src/main/resources/create.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"accessor.AgeableListModelAccessor",
5454
"accessor.EntityRenderDispatcherAccessor",
5555
"accessor.FontAccessor",
56+
"accessor.FrustumAccessor",
5657
"accessor.GuiAccessor",
5758
"accessor.HumanoidArmorLayerAccessor",
5859
"accessor.LevelRendererAccessor",

0 commit comments

Comments
 (0)