]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/renderers/RenderBatch.java
173273dae90ad8e4b53d1e8a4d843d8c9dccc19f
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / renderers / RenderBatch.java
1 package com.irtimaled.bbor.client.renderers;
2
3 import com.irtimaled.bbor.client.Camera;
4 import com.irtimaled.bbor.client.models.Point;
5 import com.mojang.blaze3d.systems.RenderSystem;
6 import net.minecraft.client.render.BufferBuilder;
7 import net.minecraft.client.render.BufferRenderer;
8 import net.minecraft.client.render.GameRenderer;
9 import net.minecraft.client.render.VertexFormat;
10 import net.minecraft.client.render.VertexFormats;
11 import net.minecraft.client.util.math.MatrixStack;
12 import net.minecraft.util.math.Box;
13
14 import java.awt.*;
15 import java.util.concurrent.atomic.AtomicLong;
16
17 public class RenderBatch {
18
19     private static final BufferBuilder quadBufferBuilderNonMasked = new BufferBuilder(2097152);
20     private static final BufferBuilder quadBufferBuilderMasked = new BufferBuilder(2097152);
21     private static final BufferBuilder lineBufferBuilder = new BufferBuilder(2097152);
22
23     private static final Object mutex = new Object();
24     private static final AtomicLong quadNonMaskedCount = new AtomicLong(0L);
25     private static final AtomicLong quadMaskedCount = new AtomicLong(0L);
26     private static final AtomicLong lineCount = new AtomicLong(0L);
27     private static final AtomicLong quadNonMaskedCountLast = new AtomicLong(0L);
28     private static final AtomicLong quadMaskedCountLast = new AtomicLong(0L);
29     private static final AtomicLong lineCountLast = new AtomicLong(0L);
30
31     private static final AtomicLong lastDurationNanos = new AtomicLong(0L);
32
33     static void beginBatch() {
34         quadBufferBuilderMasked.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
35         quadBufferBuilderNonMasked.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
36         lineBufferBuilder.begin(VertexFormat.DrawMode.DEBUG_LINES, VertexFormats.POSITION_COLOR);
37     }
38
39     public static void drawSolidBox(MatrixStack.Entry matrixEntry, Box box, Color color, int alpha, boolean mask) {
40         final float minX = (float) box.minX;
41         final float minY = (float) box.minY;
42         final float minZ = (float) box.minZ;
43         final float maxX = (float) box.maxX;
44         final float maxY = (float) box.maxY;
45         final float maxZ = (float) box.maxZ;
46         final int red = color.getRed();
47         final int green = color.getGreen();
48         final int blue = color.getBlue();
49
50         if (mask) quadMaskedCount.getAndIncrement();
51         else quadNonMaskedCount.getAndIncrement();
52
53         final BufferBuilder bufferBuilder = mask ? RenderBatch.quadBufferBuilderMasked : RenderBatch.quadBufferBuilderNonMasked;
54         bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, minZ).color(red, green, blue, alpha).next();
55         bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, minZ).color(red, green, blue, alpha).next();
56         bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, maxZ).color(red, green, blue, alpha).next();
57         bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, maxZ).color(red, green, blue, alpha).next();
58
59         bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, minZ).color(red, green, blue, alpha).next();
60         bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, maxZ).color(red, green, blue, alpha).next();
61         bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, maxZ).color(red, green, blue, alpha).next();
62         bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, minZ).color(red, green, blue, alpha).next();
63
64         bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, minZ).color(red, green, blue, alpha).next();
65         bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, minZ).color(red, green, blue, alpha).next();
66         bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, minZ).color(red, green, blue, alpha).next();
67         bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, minZ).color(red, green, blue, alpha).next();
68
69         bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, minZ).color(red, green, blue, alpha).next();
70         bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, minZ).color(red, green, blue, alpha).next();
71         bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, maxZ).color(red, green, blue, alpha).next();
72         bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, maxZ).color(red, green, blue, alpha).next();
73
74         bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, maxZ).color(red, green, blue, alpha).next();
75         bufferBuilder.vertex(matrixEntry.getModel(), maxX, minY, maxZ).color(red, green, blue, alpha).next();
76         bufferBuilder.vertex(matrixEntry.getModel(), maxX, maxY, maxZ).color(red, green, blue, alpha).next();
77         bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, maxZ).color(red, green, blue, alpha).next();
78
79         bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, minZ).color(red, green, blue, alpha).next();
80         bufferBuilder.vertex(matrixEntry.getModel(), minX, minY, maxZ).color(red, green, blue, alpha).next();
81         bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, maxZ).color(red, green, blue, alpha).next();
82         bufferBuilder.vertex(matrixEntry.getModel(), minX, maxY, minZ).color(red, green, blue, alpha).next();
83     }
84
85     static void drawLine(MatrixStack.Entry matrixEntry, Point startPoint, Point endPoint, Color color, int alpha) {
86         int regionX = (((int) Camera.getX()) >> 9) * 512;
87         int regionZ = (((int) Camera.getZ()) >> 9) * 512;
88
89         lineCount.getAndIncrement();
90
91         lineBufferBuilder
92                 .vertex(matrixEntry.getModel(),
93                         (float) startPoint.getX() - regionX,
94                         (float) startPoint.getY(),
95                         (float) startPoint.getZ() - regionZ)
96                 .color(color.getRed(), color.getGreen(), color.getBlue(), alpha)
97                 .next();
98         lineBufferBuilder
99                 .vertex(matrixEntry.getModel(),
100                         (float) endPoint.getX() - regionX,
101                         (float) endPoint.getY(),
102                         (float) endPoint.getZ() - regionZ)
103                 .color(color.getRed(), color.getGreen(), color.getBlue(), alpha)
104                 .next();
105     }
106
107     static void endBatch() {
108         RenderSystem.setShader(GameRenderer::getPositionColorShader);
109         long startTime = System.nanoTime();
110         quadBufferBuilderMasked.end();
111         quadBufferBuilderNonMasked.end();
112         lineBufferBuilder.end();
113
114         synchronized (mutex) {
115             quadMaskedCountLast.set(quadMaskedCount.get());
116             quadNonMaskedCountLast.set(quadNonMaskedCount.get());
117             lineCountLast.set(lineCount.get());
118             quadMaskedCount.set(0);
119             quadNonMaskedCount.set(0);
120             lineCount.set(0);
121         }
122
123         RenderSystem.depthMask(true);
124         if (quadMaskedCount.get() != 0) BufferRenderer.draw(quadBufferBuilderMasked);
125         if (lineCountLast.get() != 0) BufferRenderer.draw(lineBufferBuilder);
126
127         RenderSystem.depthMask(false);
128         if (quadNonMaskedCountLast.get() != 0) BufferRenderer.draw(quadBufferBuilderNonMasked);
129         lastDurationNanos.set(System.nanoTime() - startTime);
130
131         RenderSystem.depthMask(true);
132     }
133
134     public static String debugString() {
135         return String.format("[BBOR] Statistics: Boxes: %d,%d Lines: %d @ %.2fms", quadMaskedCountLast.get(), quadNonMaskedCountLast.get(), lineCountLast.get(), lastDurationNanos.get() / 1_000_000.0);
136     }
137
138 }