Skip to content

Commit

Permalink
Add/fix tile culling, fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Dec 29, 2021
1 parent f00793b commit 2c79640
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ public class Config {

public int configVersion = 4;
public boolean renderNametagsThroughWalls = true;
public Set<String> blockEntityWhitelist = new HashSet<>(Arrays.asList("minecraft:beacon", "create:rope_pulley", "create:hose_pulley", "betterend:eternal_pedestal"));
public Set<String> blockEntityWhitelist = new HashSet<>(Arrays.asList("tile.beacon"));
public int tracingDistance = 128;
public boolean debugMode = false;
public int sleepDelay = 10;
public int hitboxLimit = 50;
public boolean skipMarkerArmorStands = true;
public boolean tickCulling = true;
public Set<String> tickCullingWhitelist = new HashSet<>(Arrays.asList("minecraft:firework_rocket", "minecraft:boat"));

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,7 @@ public class ConfigUpgrader {

public static boolean upgradeConfig(Config config) {
boolean changed = false;
if(config.configVersion <= 1) {
config.blockEntityWhitelist.add("betterend:eternal_pedestal");
config.configVersion = 2;
changed = true;
}
if(config.configVersion <= 3) { // added tickCulling config
config.configVersion = 3;
changed = true;
}
if(config.configVersion < 4){
config.configVersion = 4;
config.skipMarkerArmorStands = true;
config.tickCullingWhitelist.add("minecraft:boat");
changed = true;
}

// check for more changes here

return changed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.chunk.Chunk;

Expand All @@ -43,7 +41,7 @@ public CullTask(OcclusionCullingInstance culling, Set<String> unCullable) {

@Override
public void run() {
while (client != null) { //FIXME
while (client != null) { // not correct, but the running field is hidden
try {
Thread.sleep(sleepDelay);

Expand All @@ -61,36 +59,30 @@ public void run() {
Vec3d camera = lastPos;
culling.resetCache();
boolean noCulling = client.thePlayer.isSpectator() || client.gameSettings.thirdPersonView != 0;
for (int x = -8; x <= 8; x++) {
for (int z = -8; z <= 8; z++) {
Chunk chunk = client.theWorld.getChunkFromChunkCoords((int)client.thePlayer.posX >> 4 + x,
(int)client.thePlayer.posZ >> 4 + z);
Iterator<Entry<BlockPos, TileEntity>> iterator = chunk.getTileEntityMap().entrySet().iterator();
Entry<BlockPos, TileEntity> entry;
while(iterator.hasNext()) {
try {
entry = iterator.next();
}catch(NullPointerException | ConcurrentModificationException ex) {
break; // We are not synced to the main thread, so NPE's/CME are allowed here and way less
// overhead probably than trying to sync stuff up for no really good reason
}
if(unCullable.contains(entry.getValue().getBlockType().getUnlocalizedName())) { //FIXME?
continue;
}
Cullable cullable = (Cullable) entry.getValue();
if (!cullable.isForcedVisible()) {
if (noCulling) {
cullable.setCulled(false);
continue;
}
BlockPos pos = entry.getKey();
if(pos.distanceSq(cameraMC.xCoord, cameraMC.yCoord, cameraMC.zCoord) < 64*64) { // 64 is the fixed max tile view distance
aabbMin.set(pos.getX(), pos.getY(), pos.getZ());
aabbMax.set(pos.getX()+1d, pos.getY()+1d, pos.getZ()+1d);
boolean visible = culling.isAABBVisible(aabbMin, aabbMax, camera);
cullable.setCulled(!visible);
}
}
Iterator<TileEntity> iterator = client.theWorld.loadedTileEntityList.iterator();
TileEntity entry;
while(iterator.hasNext()) {
try {
entry = iterator.next();
}catch(NullPointerException | ConcurrentModificationException ex) {
break; // We are not synced to the main thread, so NPE's/CME are allowed here and way less
// overhead probably than trying to sync stuff up for no really good reason
}
if(unCullable.contains(entry.getBlockType().getUnlocalizedName())) {
continue;
}
Cullable cullable = (Cullable) entry;
if (!cullable.isForcedVisible()) {
if (noCulling) {
cullable.setCulled(false);
continue;
}
BlockPos pos = entry.getPos();
if(pos.distanceSq(cameraMC.xCoord, cameraMC.yCoord, cameraMC.zCoord) < 64*64) { // 64 is the fixed max tile view distance
aabbMin.set(pos.getX(), pos.getY(), pos.getZ());
aabbMax.set(pos.getX()+1d, pos.getY()+1d, pos.getZ()+1d);
boolean visible = culling.isAABBVisible(aabbMin, aabbMax, camera);
cullable.setCulled(!visible);
}

}
Expand Down Expand Up @@ -118,10 +110,10 @@ public void run() {
continue;
}
AxisAlignedBB boundingBox = entity.getEntityBoundingBox();
/*if(boundingBox.x() > hitboxLimit || boundingBox.getYsize() > hitboxLimit || boundingBox.getZsize() > hitboxLimit) {
if(boundingBox.maxX - boundingBox.minX > hitboxLimit || boundingBox.maxY - boundingBox.minY > hitboxLimit || boundingBox.maxZ - boundingBox.minZ > hitboxLimit) {
cullable.setCulled(false); // To big to bother to cull
continue;
}*/
}
aabbMin.set(boundingBox.minX, boundingBox.minY, boundingBox.minZ);
aabbMax.set(boundingBox.maxX, boundingBox.maxY, boundingBox.maxZ);
boolean visible = culling.isAABBVisible(aabbMin, aabbMax, camera);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;

public abstract class EntityCullingModBase {

public static EntityCullingModBase instance = new EntityCullingMod();
public OcclusionCullingInstance culling;
public Set<String> unCullable = new HashSet<>();
public Set<String> tickCullWhistelist = new HashSet<>();
public boolean debugHitboxes = false;
public static boolean enabled = true; // public static to make it faster for the jvm
public CullTask cullTask;
private Thread cullThread;
protected KeyBinding keybind = new KeyBinding("key.entityculling.toggle", -1, "EntityCulling");
protected boolean pressed = false;
private boolean configKeysLoaded = false;

public Config config;
private final File settingsFile = new File("config", "entityculling.json");
Expand Down Expand Up @@ -61,7 +60,7 @@ public void onInitialize() {
}
}
culling = new OcclusionCullingInstance(config.tracingDistance, new Provider());
cullTask = new CullTask(culling, unCullable);
cullTask = new CullTask(culling, config.blockEntityWhitelist);

cullThread = new Thread(cullTask, "CullThread");
cullThread.setUncaughtExceptionHandler((thread, ex) -> {
Expand All @@ -87,20 +86,6 @@ public void worldTick() {
}

public void clientTick() {
/*if(!configKeysLoaded) {
for(String blockId : config.blockEntityWhitelist) {
Optional<BlockEntityType<?>> block = Registry.BLOCK_ENTITY_TYPE.getOptional(new ResourceLocation(blockId));
block.ifPresent(b -> {
unCullable.add(b);
});
}
for(String entityType : config.tickCullingWhitelist) {
Optional<EntityType<?>> entity = Registry.ENTITY_TYPE.getOptional(new ResourceLocation(entityType));
entity.ifPresent(e -> {
tickCullWhistelist.add(e);
});
}
}*/
if (keybind.isKeyDown()) {
if (pressed)
return;
Expand All @@ -109,11 +94,11 @@ public void clientTick() {
EntityPlayerSP player = Minecraft.getMinecraft().thePlayer;
if(enabled) {
if (player != null) {
player.sendChatMessage("Culling on");
player.addChatMessage(new ChatComponentText("Culling on"));
}
} else {
if (player != null) {
player.sendChatMessage("Culling off");
player.addChatMessage(new ChatComponentText("Culling off"));
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
@Mixin(TileEntityRendererDispatcher.class)
public class BlockEntityRenderDispatcherMixin {

@Inject(method = "renderTileEntityAt", at = @At("HEAD"), cancellable = true)
@Inject(method = "Lnet/minecraft/client/renderer/tileentity/TileEntityRendererDispatcher;renderTileEntityAt(Lnet/minecraft/tileentity/TileEntity;DDDFI)V", at = @At("HEAD"), cancellable = true)
public void renderTileEntityAt(TileEntity blockEntity, double p_renderTileEntityAt_2_, double d1,
double d2, float f1, CallbackInfo info) {
double d2, float f1, int p_renderTileEntityAt_9_, CallbackInfo info) {
if (!((Cullable) blockEntity).isForcedVisible() && ((Cullable) blockEntity).isCulled()) {
EntityCullingModBase.instance.skippedBlockEntities++;
info.cancel();
Expand Down

This file was deleted.

0 comments on commit 2c79640

Please sign in to comment.