package com.irtimaled.bbor.client;
-import com.irtimaled.bbor.client.renderers.*;
-import com.irtimaled.bbor.common.BoundingBoxCache;
-import com.irtimaled.bbor.common.BoundingBoxType;
-import com.irtimaled.bbor.common.models.*;
-import com.irtimaled.bbor.config.ConfigManager;
-import net.minecraft.client.Minecraft;
-import net.minecraft.util.math.ChunkPos;
-import net.minecraft.util.math.MathHelper;
-import net.minecraft.world.dimension.DimensionType;
-import org.lwjgl.opengl.GL11;
+import com.irtimaled.bbor.client.config.ConfigManager;
+import com.irtimaled.bbor.client.interop.ClientInterop;
+import com.irtimaled.bbor.client.interop.TileEntitiesHelper;
+import com.irtimaled.bbor.client.models.Point;
+import com.irtimaled.bbor.client.providers.BeaconProvider;
+import com.irtimaled.bbor.client.providers.BedrockCeilingProvider;
+import com.irtimaled.bbor.client.providers.BiomeBorderProvider;
+import com.irtimaled.bbor.client.providers.ConduitProvider;
+import com.irtimaled.bbor.client.providers.CustomBeaconProvider;
+import com.irtimaled.bbor.client.providers.CustomBoxProvider;
+import com.irtimaled.bbor.client.providers.CustomLineProvider;
+import com.irtimaled.bbor.client.providers.CustomSphereProvider;
+import com.irtimaled.bbor.client.providers.IBoundingBoxProvider;
+import com.irtimaled.bbor.client.providers.ICachingProvider;
+import com.irtimaled.bbor.client.providers.MobSpawnerProvider;
+import com.irtimaled.bbor.client.providers.SlimeChunkProvider;
+import com.irtimaled.bbor.client.providers.SpawnableBlocksProvider;
+import com.irtimaled.bbor.client.providers.SpawningSphereProvider;
+import com.irtimaled.bbor.client.providers.WorldSpawnProvider;
+import com.irtimaled.bbor.client.renderers.AbstractRenderer;
+import com.irtimaled.bbor.client.renderers.RenderHelper;
+import com.irtimaled.bbor.client.renderers.RenderQueue;
+import com.irtimaled.bbor.common.MathHelper;
+import com.irtimaled.bbor.common.TypeHelper;
+import com.irtimaled.bbor.common.models.AbstractBoundingBox;
+import com.irtimaled.bbor.common.models.DimensionId;
+import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectArrayList;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.loader.api.FabricLoader;
+import net.minecraft.client.util.math.MatrixStack;
+
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
-import java.util.*;
+public class ClientRenderer {
+ private static final int CHUNK_SIZE = 16;
+ private static final Map<Class<? extends AbstractBoundingBox>, AbstractRenderer> boundingBoxRendererMap = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
-import static com.irtimaled.bbor.client.Constants.CHUNK_SIZE;
+ private static boolean active;
+ private static final Set<IBoundingBoxProvider> providers = new HashSet<>();
-public class ClientRenderer {
- private final GetCache getCache;
- private static final Map<Class<? extends BoundingBox>, Renderer> boundingBoxRendererMap = new HashMap<>();
- private long seed;
- private Set<BoundingBox> spawnChunkBoundingBoxes = new HashSet<>();
-
- ClientRenderer(GetCache getCache) {
- this.getCache = getCache;
- boundingBoxRendererMap.put(BoundingBoxVillage.class, new VillageRenderer());
- boundingBoxRendererMap.put(BoundingBoxSlimeChunk.class, new SlimeChunkRenderer());
- boundingBoxRendererMap.put(BoundingBoxWorldSpawn.class, new WorldSpawnRenderer());
- boundingBoxRendererMap.put(BoundingBoxStructure.class, new StructureRenderer());
- boundingBoxRendererMap.put(BoundingBoxMobSpawner.class, new MobSpawnerRenderer());
+ private static AtomicLong lastDurationNanos = new AtomicLong(0L);
+
+ public static boolean getActive() {
+ return active;
}
- private boolean isWithinRenderDistance(Coords minCoords, Coords maxCoords) {
- int renderDistanceBlocks = Minecraft.getInstance().gameSettings.renderDistanceChunks * CHUNK_SIZE;
- int minX = MathHelper.floor(PlayerCoords.getX() - renderDistanceBlocks);
- int maxX = MathHelper.floor(PlayerCoords.getX() + renderDistanceBlocks);
- int minZ = MathHelper.floor(PlayerCoords.getZ() - renderDistanceBlocks);
- int maxZ = MathHelper.floor(PlayerCoords.getZ() + renderDistanceBlocks);
-
- return maxCoords.getX() >= minX &&
- maxCoords.getZ() >= minZ &&
- minCoords.getX() <= maxX &&
- minCoords.getZ() <= maxZ;
+ public static void toggleActive() {
+ active = !active;
+ if (!active) return;
+
+ Player.setActiveY();
}
- private boolean isWithinRenderDistance(BoundingBox boundingBox) {
- return isWithinRenderDistance(boundingBox.getMinCoords(), boundingBox.getMaxCoords());
+ static void deactivate() {
+ active = false;
}
- public void render(DimensionType dimensionType, Boolean outerBoxesOnly) {
- Map<BoundingBox, Set<BoundingBox>> boundingBoxes = getBoundingBoxes(dimensionType);
+ static {
+ registerProvider(new SlimeChunkProvider());
+ registerProvider(new WorldSpawnProvider());
+ registerProvider(new SpawningSphereProvider());
+ registerProvider(new BeaconProvider());
+ registerProvider(new CustomBoxProvider());
+ registerProvider(new CustomBeaconProvider());
+ registerProvider(new BiomeBorderProvider());
+ registerProvider(new MobSpawnerProvider());
+ registerProvider(new ConduitProvider());
+ registerProvider(new SpawnableBlocksProvider());
+ registerProvider(new CustomLineProvider());
+ registerProvider(new CustomSphereProvider());
+// registerProvider(new FlowerForestProvider());
+ registerProvider(new BedrockCeilingProvider());
+ }
- GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- GL11.glLineWidth(2.0f);
- GL11.glDisable(GL11.GL_TEXTURE_2D);
- GL11.glDisable(GL11.GL_CULL_FACE);
+ public static <T extends AbstractBoundingBox> void registerProvider(IBoundingBoxProvider<T> provider) {
+ providers.add(provider);
+ }
- if (ConfigManager.alwaysVisible.get()) {
- GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
- }
- for (Map.Entry<BoundingBox, Set<BoundingBox>> entry : boundingBoxes.entrySet()) {
- BoundingBox key = entry.getKey();
- if (!key.shouldRender()) continue;
-
- Renderer renderer = boundingBoxRendererMap.get(key.getClass());
- if (renderer == null) continue;
-
- if (!outerBoxesOnly) {
- Set<BoundingBox> children = entry.getValue();
- if (children != null) {
- children.forEach(renderer::render);
- continue;
- }
- }
- renderer.render(key);
- }
+ public static <T extends AbstractBoundingBox> AbstractRenderer<T> registerRenderer(Class<? extends T> type, Supplier<AbstractRenderer<T>> renderer) {
+ if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) return null;
+ final AbstractRenderer<T> renderer1 = renderer.get();
+ boundingBoxRendererMap.put(type, renderer1);
+ return renderer1;
+ }
- GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
- GL11.glEnable(GL11.GL_CULL_FACE);
- GL11.glEnable(GL11.GL_TEXTURE_2D);
+ public static AbstractRenderer getRenderer(Class<? extends AbstractBoundingBox> clazz) {
+ return boundingBoxRendererMap.get(clazz);
}
- private Map<BoundingBox, Set<BoundingBox>> getBoundingBoxes(DimensionType dimensionType) {
- Map<BoundingBox, Set<BoundingBox>> boundingBoxes = new HashMap<>();
- if (dimensionType == DimensionType.OVERWORLD) {
- if (BoundingBoxType.SlimeChunks.shouldRender()) {
- addSlimeChunks(boundingBoxes);
- }
+ private static boolean isWithinRenderDistance(AbstractBoundingBox boundingBox) {
+ int renderDistanceBlocks = ClientInterop.getRenderDistanceChunks() * CHUNK_SIZE;
+ int minX = MathHelper.floor(Player.getX() - renderDistanceBlocks);
+ int maxX = MathHelper.floor(Player.getX() + renderDistanceBlocks);
+ int minZ = MathHelper.floor(Player.getZ() - renderDistanceBlocks);
+ int maxZ = MathHelper.floor(Player.getZ() + renderDistanceBlocks);
- for (BoundingBox boundingBox : spawnChunkBoundingBoxes) {
- if (boundingBox.shouldRender() && isWithinRenderDistance(boundingBox)) {
- boundingBoxes.put(boundingBox, null);
- }
- }
- }
+ return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
+ }
- BoundingBoxCache cache = getCache.apply(dimensionType);
- if (cache != null) {
- for (Map.Entry<BoundingBox, Set<BoundingBox>> entry : cache.getBoundingBoxes().entrySet()) {
- BoundingBox key = entry.getKey();
- if (key.shouldRender() && isWithinRenderDistance(key)) {
- boundingBoxes.put(key, entry.getValue());
- }
- }
+ public static void render(MatrixStack matrixStack, DimensionId dimensionId) {
+ if (!active) return;
+
+ long startTime = System.nanoTime();
+ matrixStack.push();
+ RenderHelper.beforeRender();
+ TileEntitiesHelper.clearCache();
+
+ final List<AbstractBoundingBox> boundingBoxes = getBoundingBoxes(dimensionId);
+ RenderCulling.flushPreRendering();
+ for (AbstractBoundingBox key : boundingBoxes) {
+ AbstractRenderer renderer = key.getRenderer();
+ if (renderer != null) renderer.render(matrixStack, key);
}
- return boundingBoxes;
+
+ RenderQueue.renderDeferred();
+
+ RenderHelper.afterRender();
+ RenderCulling.flushRendering();
+ matrixStack.pop();
+ lastDurationNanos.set(System.nanoTime() - startTime);
}
- private void addSlimeChunks(Map<BoundingBox, Set<BoundingBox>> boundingBoxes) {
- Minecraft minecraft = Minecraft.getInstance();
- int renderDistanceChunks = minecraft.gameSettings.renderDistanceChunks;
- int playerChunkX = MathHelper.floor(PlayerCoords.getX() / 16.0D);
- int playerChunkZ = MathHelper.floor(PlayerCoords.getZ() / 16.0D);
- for (int chunkX = playerChunkX - renderDistanceChunks; chunkX <= playerChunkX + renderDistanceChunks; ++chunkX) {
- for (int chunkZ = playerChunkZ - renderDistanceChunks; chunkZ <= playerChunkZ + renderDistanceChunks; ++chunkZ) {
- if (isSlimeChunk(chunkX, chunkZ)) {
- ChunkPos chunk = new ChunkPos(chunkX, chunkZ);
- Coords minCoords = new Coords(chunk.getXStart(), 1, chunk.getZStart());
- Coords maxCoords = new Coords(chunk.getXEnd(), 38, chunk.getZEnd());
- boundingBoxes.put(BoundingBoxSlimeChunk.from(minCoords, maxCoords), null);
+ private static final ObjectArrayList<AbstractBoundingBox> listForRendering = new ObjectArrayList<>();
+
+ public static List<AbstractBoundingBox> getBoundingBoxes(DimensionId dimensionId) {
+ listForRendering.clear();
+ final boolean doPreCulling = ConfigManager.fastRender.get() >= 2;
+ for (IBoundingBoxProvider<?> provider : providers) {
+ if (provider.canProvide(dimensionId)) {
+ for (AbstractBoundingBox boundingBox : provider.get(dimensionId)) {
+ if (isWithinRenderDistance(boundingBox)) {
+ if (doPreCulling && !boundingBox.isVisibleCulling()) continue;
+ listForRendering.add(boundingBox);
+ }
}
}
}
- }
- private boolean isSlimeChunk(int chunkX, int chunkZ) {
- Random r = new Random(seed +
- (long) (chunkX * chunkX * 4987142) +
- (long) (chunkX * 5947611) +
- (long) (chunkZ * chunkZ) * 4392871L +
- (long) (chunkZ * 389711) ^ 987234911L);
- return r.nextInt(10) == 0;
- }
+ Point point = Player.getPoint();
+ listForRendering.sort(Comparator.comparingDouble((AbstractBoundingBox boundingBox) -> boundingBox.getDistance(point.getX(), point.getY(), point.getZ())).reversed());
- void setWorldData(long seed, int spawnX, int spawnZ) {
- this.seed = seed;
- spawnChunkBoundingBoxes = getSpawnChunkBoundingBoxes(spawnX, spawnZ);
+ return listForRendering;
}
- private Set<BoundingBox> getSpawnChunkBoundingBoxes(int spawnX, int spawnZ) {
- Set<BoundingBox> boundingBoxes = new HashSet<>();
- boundingBoxes.add(getWorldSpawnBoundingBox(spawnX, spawnZ));
- boundingBoxes.add(buildSpawnChunksBoundingBox(spawnX, spawnZ, 12, BoundingBoxType.SpawnChunks));
- boundingBoxes.add(buildSpawnChunksBoundingBox(spawnX, spawnZ, 16, BoundingBoxType.LazySpawnChunks));
- return boundingBoxes;
+ public static void clear() {
+ for(IBoundingBoxProvider<?> provider : providers) {
+ TypeHelper.doIfType(provider, ICachingProvider.class, ICachingProvider::clearCache);
+ }
}
- private BoundingBox getWorldSpawnBoundingBox(int spawnX, int spawnZ) {
- Coords minCoords = new Coords(spawnX - 10, 0, spawnZ - 10);
- Coords maxCoords = new Coords(spawnX + 10, 0, spawnZ + 10);
-
- return BoundingBoxWorldSpawn.from(minCoords, maxCoords, BoundingBoxType.WorldSpawn);
+ public static long getLastDurationNanos() {
+ return lastDurationNanos.get();
}
- private BoundingBox buildSpawnChunksBoundingBox(int spawnX, int spawnZ, int size, BoundingBoxType type) {
- double midOffset = CHUNK_SIZE * (size / 2.0);
- double midX = Math.round((float) (spawnX / (double) CHUNK_SIZE)) * (double) CHUNK_SIZE;
- double midZ = Math.round((float) (spawnZ / (double) CHUNK_SIZE)) * (double) CHUNK_SIZE;
- Coords minCoords = new Coords(midX - midOffset, 0, midZ - midOffset);
- if (spawnX / (double) CHUNK_SIZE % 0.5D == 0.0D && spawnZ / (double) CHUNK_SIZE % 0.5D == 0.0D) {
- midX += (double) CHUNK_SIZE;
- midZ += (double) CHUNK_SIZE;
- }
- Coords maxCoords = new Coords(midX + midOffset, 0, midZ + midOffset);
- return BoundingBoxWorldSpawn.from(minCoords, maxCoords, type);
- }
+
}