]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/renderers/AbstractRenderer.java
General performance improvements and serverside fixes
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / renderers / AbstractRenderer.java
1 package com.irtimaled.bbor.client.renderers;
2
3 import com.irtimaled.bbor.client.Camera;
4 import com.irtimaled.bbor.client.RenderCulling;
5 import com.irtimaled.bbor.client.config.ConfigManager;
6 import com.irtimaled.bbor.client.models.Point;
7 import com.irtimaled.bbor.common.MathHelper;
8 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
9 import com.mojang.blaze3d.systems.RenderSystem;
10 import net.minecraft.client.MinecraftClient;
11 import net.minecraft.client.font.TextRenderer;
12 import net.minecraft.client.render.GameRenderer;
13 import net.minecraft.client.util.math.MatrixStack;
14 import net.minecraft.util.math.BlockPos;
15 import net.minecraft.util.math.Box;
16
17 import java.awt.*;
18
19 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
20     private static final double TAU = 6.283185307179586D;
21     public static final double PHI_SEGMENT = TAU / 90D;
22     private static final double PI = TAU / 2D;
23     public static final double THETA_SEGMENT = PHI_SEGMENT / 2D;
24
25     private static final Box ORIGIN_BOX = new Box(BlockPos.ORIGIN);
26
27     public abstract void render(MatrixStack matrixStack, T boundingBox);
28
29     void renderCuboid(MatrixStack matrixStack, OffsetBox bb, Color color, boolean fillOnly, int fillAlpha) {
30         matrixStack.push();
31
32         renderCuboid0(matrixStack, bb, color, fillOnly, fillAlpha, false);
33
34         matrixStack.pop();
35     }
36
37     private void renderCuboid0(MatrixStack stack, OffsetBox nudge, Color color, boolean fillOnly, int fillAlpha, boolean mask) {
38         if (!RenderCulling.isVisibleCulling(nudge.toBox())) return;
39         if (ConfigManager.invertBoxColorPlayerInside.get() &&
40                 playerInsideBoundingBox(nudge)) {
41             color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
42         }
43         final MatrixStack.Entry lastStack = stack.peek();
44         stack.push();
45         int regionX = (((int) Camera.getX()) >> 9) << 9;
46         int regionZ = (((int) Camera.getZ()) >> 9) << 9;
47         RenderHelper.applyRegionalRenderOffset(stack);
48         final double minX = nudge.getMin().getX();
49         final double minY = nudge.getMin().getY();
50         final double minZ = nudge.getMin().getZ();
51         final double maxX = nudge.getMax().getX();
52         final double maxY = nudge.getMax().getY();
53         final double maxZ = nudge.getMax().getZ();
54         stack.translate(minX - regionX, minY, minZ - regionZ);
55         stack.scale((float) (maxX - minX),
56                 (float) (maxY - minY),
57                 (float) (maxZ - minZ));
58
59         if (fillOnly || ConfigManager.fill.get()) {
60             RenderBatch.drawSolidBox(stack.peek(), ORIGIN_BOX, color, fillAlpha, mask, minX == maxX, minY == maxY, minZ == maxZ);
61         }
62         if (!fillOnly) {
63             stack.push();
64             stack.peek().getModel().load(lastStack.getModel());
65             stack.peek().getNormal().load(lastStack.getNormal());
66             renderLine(stack, new OffsetPoint(minX, minY, minZ), new OffsetPoint(maxX, minY, minZ), color, true);
67             renderLine(stack, new OffsetPoint(maxX, minY, minZ), new OffsetPoint(maxX, minY, maxZ), color, true);
68             renderLine(stack, new OffsetPoint(maxX, minY, maxZ), new OffsetPoint(minX, minY, maxZ), color, true);
69             renderLine(stack, new OffsetPoint(minX, minY, maxZ), new OffsetPoint(minX, minY, minZ), color, true);
70             renderLine(stack, new OffsetPoint(minX, minY, minZ), new OffsetPoint(minX, maxY, minZ), color, true);
71             renderLine(stack, new OffsetPoint(maxX, minY, minZ), new OffsetPoint(maxX, maxY, minZ), color, true);
72             renderLine(stack, new OffsetPoint(maxX, minY, maxZ), new OffsetPoint(maxX, maxY, maxZ), color, true);
73             renderLine(stack, new OffsetPoint(minX, minY, maxZ), new OffsetPoint(minX, maxY, maxZ), color, true);
74             renderLine(stack, new OffsetPoint(minX, maxY, minZ), new OffsetPoint(maxX, maxY, minZ), color, true);
75             renderLine(stack, new OffsetPoint(maxX, maxY, minZ), new OffsetPoint(maxX, maxY, maxZ), color, true);
76             renderLine(stack, new OffsetPoint(maxX, maxY, maxZ), new OffsetPoint(minX, maxY, maxZ), color, true);
77             renderLine(stack, new OffsetPoint(minX, maxY, maxZ), new OffsetPoint(minX, maxY, minZ), color, true);
78             stack.pop();
79         }
80
81         stack.pop();
82     }
83
84     private boolean playerInsideBoundingBox(OffsetBox nudge) {
85         return nudge.getMin().getX() < 0 && nudge.getMax().getX() > 0 &&
86                 nudge.getMin().getY() < 0 && nudge.getMax().getY() > 0 &&
87                 nudge.getMin().getZ() < 0 && nudge.getMax().getZ() > 0;
88     }
89
90
91     void renderLine(MatrixStack matrixStack, OffsetPoint startPoint, OffsetPoint endPoint, Color color, boolean cullIfEmpty) {
92 //        if ((startPoint.getY() == endPoint.getY() && startPoint.getZ() == endPoint.getZ()) ||
93 //                (startPoint.getX() == endPoint.getX() && startPoint.getZ() == endPoint.getZ()) ||
94 //                (startPoint.getX() == endPoint.getX() && startPoint.getY() == endPoint.getY())) {
95 //            renderCuboid0(matrixStack, new OffsetBox(startPoint.offset(-getLineWidth(), -getLineWidth(), -getLineWidth()), endPoint.offset(getLineWidth(), getLineWidth(), getLineWidth())), color, true, 255, true);
96 //            return;
97 //        }
98
99         if (cullIfEmpty && startPoint.equals(endPoint)) return;
100         if (!RenderCulling.isVisibleCulling(new OffsetBox(startPoint, endPoint).toBox())) return; // TODO better culling
101
102         matrixStack.push();
103
104         RenderHelper.applyRegionalRenderOffset(matrixStack);
105
106         RenderBatch.drawLine(matrixStack.peek(), startPoint.getPoint(), endPoint.getPoint(), color, 255);
107
108         matrixStack.pop();
109     }
110
111     void renderText(MatrixStack matrixStack, OffsetPoint offsetPoint, String... texts) {
112         TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
113         RenderHelper.beforeRenderFont(matrixStack, offsetPoint);
114         float top = -(fontRenderer.fontHeight * texts.length) / 2f;
115         for (String text : texts) {
116             float left = fontRenderer.getWidth(text) / 2f;
117             fontRenderer.draw(new MatrixStack(), text, -left, top, -1);
118             top += fontRenderer.fontHeight;
119         }
120         RenderHelper.afterRenderFont(matrixStack);
121     }
122
123     void renderSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
124         if (ConfigManager.renderSphereAsDots.get()) {
125             renderDotSphere(matrixStack, center, radius, color);
126         } else {
127             renderLineSphere(matrixStack, center, radius, color);
128         }
129     }
130
131     private void renderLineSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
132         if (!RenderCulling.isVisibleCulling(new Box(new BlockPos(center.getX(), center.getY(), center.getZ())).expand(radius))) return;
133
134         double offset = ((radius - (int) radius) == 0) ? center.getY() - (int) center.getY() : 0;
135         int dyStep = radius < 64 ? 1 : MathHelper.floor(radius / 32);
136         for (double dy = offset - radius; dy <= radius + 1; dy += dyStep) {
137             double circleRadius = Math.sqrt((radius * radius) - (dy * dy));
138             if (circleRadius == 0) circleRadius = Math.sqrt(2) / 2;
139             renderCircle(matrixStack, center, circleRadius, color, dy + 0.001F);
140         }
141     }
142
143     private void renderCircle(MatrixStack matrixStack, Point center, double radius, Color color, double dy) {
144         matrixStack.push();
145
146         RenderHelper.applyRegionalRenderOffset(matrixStack);
147         RenderSystem.setShader(GameRenderer::getPositionShader);
148
149         Point firstPoint = null;
150         Point lastPoint = null;
151
152         for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
153             final Point point = center.offset(Math.cos(phi) * radius, dy, Math.sin(phi) * radius);
154             if (firstPoint == null) firstPoint = point;
155             if (lastPoint == null) {
156                 lastPoint = point;
157                 continue;
158             }
159             RenderBatch.drawLine(matrixStack.peek(), lastPoint, point, color, 255);
160             lastPoint = point;
161         }
162         RenderBatch.drawLine(matrixStack.peek(), lastPoint, firstPoint, color, 255);
163
164         matrixStack.pop();
165     }
166
167     private void renderDotSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
168         if (!RenderCulling.isVisibleCulling(new Box(new BlockPos(center.getX(), center.getY(), center.getZ())).expand(radius))) return;
169         matrixStack.push();
170
171         for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
172             double dy = radius * Math.cos(phi);
173             double radiusBySinPhi = radius * Math.sin(phi);
174             for (double theta = 0.0D; theta < PI; theta += THETA_SEGMENT) {
175                 double dx = radiusBySinPhi * Math.cos(theta);
176                 double dz = radiusBySinPhi * Math.sin(theta);
177                 final Point point = center.offset(dx, dy, dz);
178                 renderCuboid0(matrixStack, new OffsetBox(point.offset(-0.0025f, -0.0025f, -0.0025f), point.offset(0.0025f, 0.0025f, 0.0025f)), color, true, 255, true);
179             }
180         }
181         matrixStack.pop();
182     }
183 }