]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/RenderCulling.java
Add "Fast Render" option to configure culling behaviors
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / RenderCulling.java
1 package com.irtimaled.bbor.client;
2
3 import com.irtimaled.bbor.client.config.ConfigManager;
4 import net.minecraft.client.render.Frustum;
5 import net.minecraft.util.math.Box;
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.concurrent.atomic.AtomicInteger;
10
11 public class RenderCulling {
12
13     private static volatile Frustum frustum = null;
14     private static final Object mutex = new Object();
15     private static final AtomicInteger culledCount = new AtomicInteger();
16     private static final AtomicInteger totalCount = new AtomicInteger();
17     private static final AtomicInteger culledCountLast = new AtomicInteger();
18     private static final AtomicInteger totalCountLast = new AtomicInteger();
19     private static final AtomicInteger preCulledCountLast = new AtomicInteger();
20     private static final AtomicInteger preTotalCountLast = new AtomicInteger();
21
22     public static void setFrustum(Frustum frustum) {
23         RenderCulling.frustum = frustum;
24     }
25
26     public static void flushRendering() {
27         synchronized (mutex) {
28             culledCountLast.set(culledCount.get());
29             totalCountLast.set(totalCount.get());
30             culledCount.set(0);
31             totalCount.set(0);
32         }
33     }
34
35     public static void flushPreRendering() {
36         synchronized (mutex) {
37             preCulledCountLast.set(culledCount.get());
38             preTotalCountLast.set(totalCount.get());
39             culledCount.set(0);
40             totalCount.set(0);
41         }
42     }
43
44     public static List<String> debugStrings() {
45         if (!ClientRenderer.getActive()) return List.of("[BBOR] Rendering not enabled");
46         final ArrayList<String> list = new ArrayList<>(2);
47         if (ConfigManager.fastRender.get() >= 2) {
48             final int preCulledCountLast;
49             final int preTotalCountLast;
50             synchronized (mutex) {
51                 preCulledCountLast = RenderCulling.preCulledCountLast.get();
52                 preTotalCountLast = RenderCulling.preTotalCountLast.get();
53             }
54             list.add(String.format("[BBOR] Pre-culling: %d / %d (%.1f%%)", preCulledCountLast, preTotalCountLast, (preCulledCountLast / (float) preTotalCountLast) * 100.0));
55         }
56         if (ConfigManager.fastRender.get() >= 1) {
57             final int culledCountLast;
58             final int totalCountLast;
59             synchronized (mutex) {
60                 culledCountLast = RenderCulling.culledCountLast.get();
61                 totalCountLast = RenderCulling.totalCountLast.get();
62             }
63             list.add(String.format("[BBOR] Rendering culling: %d / %d (%.1f%%)", culledCountLast, totalCountLast, (culledCountLast / (float) totalCountLast) * 100.0));
64         }
65         return list;
66     }
67
68     private static boolean cullFrustum(Box box) {
69         final Frustum frustum = RenderCulling.frustum;
70         if (frustum != null) {
71             return frustum.isVisible(box);
72         } else {
73             return true;
74         }
75     }
76
77     public static boolean isVisibleCulling(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
78         return isVisibleCulling(new Box(minX, minY, minZ, maxX, maxY, maxZ));
79     }
80
81     public static boolean isVisibleCulling(Box box) {
82         final boolean cullResult = cullFrustum(box);
83         totalCount.incrementAndGet();
84         if (!cullResult) culledCount.incrementAndGet();
85         return cullResult;
86     }
87
88     public static void incrementCulling() {
89         totalCount.incrementAndGet();
90     }
91
92 }