1 package com.irtimaled.bbor.client.renderers;
3 import com.irtimaled.bbor.client.config.ConfigManager;
4 import com.irtimaled.bbor.client.models.Point;
5 import com.irtimaled.bbor.common.MathHelper;
6 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
7 import net.minecraft.client.MinecraftClient;
8 import net.minecraft.client.font.TextRenderer;
9 import net.minecraft.client.util.math.MatrixStack;
12 import java.util.function.Supplier;
14 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
15 private static final double TAU = 6.283185307179586D;
16 public static final double PHI_SEGMENT = TAU / 90D;
17 private static final double PI = TAU / 2D;
18 public static final double THETA_SEGMENT = PHI_SEGMENT / 2D;
20 public abstract void render(T boundingBox);
22 void renderCuboid(OffsetBox bb, Color color) {
23 OffsetBox nudge = bb.nudge();
24 renderOutlinedCuboid(nudge, color);
25 renderFilledFaces(nudge.getMin(), nudge.getMax(), color);
28 void renderOutlinedCuboid(OffsetBox bb, Color color) {
29 RenderHelper.polygonModeLine();
30 OffsetPoint min = bb.getMin();
31 OffsetPoint max = bb.getMax();
32 renderFaces(min, max, color, 255, min.getY() == max.getY() ? Renderer::startLineLoop : Renderer::startLines);
35 private void renderFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha, Supplier<Renderer> rendererSupplier) {
36 double minX = min.getX();
37 double minY = min.getY();
38 double minZ = min.getZ();
40 double maxX = max.getX();
41 double maxY = max.getY();
42 double maxZ = max.getZ();
44 if (ConfigManager.invertBoxColorPlayerInside.get() &&
45 playerInsideBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)) {
46 color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
49 Renderer renderer = rendererSupplier.get()
53 if (minX != maxX && minZ != maxZ) {
54 renderer.addPoint(minX, minY, minZ)
55 .addPoint(maxX, minY, minZ)
56 .addPoint(maxX, minY, maxZ)
57 .addPoint(minX, minY, maxZ);
60 renderer.addPoint(minX, maxY, minZ)
61 .addPoint(maxX, maxY, minZ)
62 .addPoint(maxX, maxY, maxZ)
63 .addPoint(minX, maxY, maxZ);
67 if (minX != maxX && minY != maxY) {
68 renderer.addPoint(minX, minY, maxZ)
69 .addPoint(minX, maxY, maxZ)
70 .addPoint(maxX, maxY, maxZ)
71 .addPoint(maxX, minY, maxZ);
74 renderer.addPoint(minX, minY, minZ)
75 .addPoint(minX, maxY, minZ)
76 .addPoint(maxX, maxY, minZ)
77 .addPoint(maxX, minY, minZ);
80 if (minY != maxY && minZ != maxZ) {
81 renderer.addPoint(minX, minY, minZ)
82 .addPoint(minX, minY, maxZ)
83 .addPoint(minX, maxY, maxZ)
84 .addPoint(minX, maxY, minZ);
87 renderer.addPoint(maxX, minY, minZ)
88 .addPoint(maxX, minY, maxZ)
89 .addPoint(maxX, maxY, maxZ)
90 .addPoint(maxX, maxY, minZ);
96 private boolean playerInsideBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
97 return minX < 0 && maxX > 0 && minY < 0 && maxY > 0 && minZ < 0 && maxZ > 0;
100 void renderLine(OffsetPoint startPoint, OffsetPoint endPoint, Color color) {
101 RenderHelper.polygonModeLine();
102 Renderer.startLines()
104 .addPoint(startPoint)
109 void renderFilledFaces(OffsetPoint min, OffsetPoint max, Color color) {
110 renderFilledFaces(min, max, color, 30);
113 void renderFilledFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha) {
114 if (!ConfigManager.fill.get()) return;
115 RenderQueue.deferRendering(() -> renderFaces(min, max, color, alpha, Renderer::startQuads));
118 void renderText(OffsetPoint offsetPoint, String... texts) {
119 TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
120 RenderHelper.beforeRenderFont(offsetPoint);
121 float top = -(fontRenderer.fontHeight * texts.length) / 2f;
122 for (String text : texts) {
123 float left = fontRenderer.getWidth(text) / 2f;
124 fontRenderer.draw(new MatrixStack(), text, -left, top, -1);
125 top += fontRenderer.fontHeight;
127 RenderHelper.afterRenderFont();
130 void renderSphere(Point center, double radius, Color color) {
131 if (ConfigManager.renderSphereAsDots.get()) {
132 renderDotSphere(center, radius, color);
134 renderLineSphere(center, radius, color);
138 private void renderLineSphere(Point center, double radius, Color color) {
139 RenderHelper.lineWidth2();
141 double offset = ((radius - (int) radius) == 0) ? center.getY() - (int) center.getY() : 0;
142 int dyStep = radius < 64 ? 1 : MathHelper.floor(radius / 32);
143 for (double dy = offset - radius; dy <= radius + 1; dy += dyStep) {
144 double circleRadius = Math.sqrt((radius * radius) - (dy * dy));
145 if (circleRadius == 0) circleRadius = Math.sqrt(2) / 2;
146 renderCircle(center, circleRadius, color, dy + 0.001F);
150 private void renderCircle(Point center, double radius, Color color, double dy) {
151 Renderer renderer = Renderer.startLineLoop()
154 for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
155 renderer.addPoint(new OffsetPoint(center.offset(Math.cos(phi) * radius, dy, Math.sin(phi) * radius)));
161 private void renderDotSphere(Point center, double radius, Color color) {
162 RenderHelper.enablePointSmooth();
163 RenderHelper.pointSize5();
164 Renderer renderer = Renderer.startPoints()
167 for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
168 double dy = radius * Math.cos(phi);
169 double radiusBySinPhi = radius * Math.sin(phi);
170 for (double theta = 0.0D; theta < PI; theta += THETA_SEGMENT) {
171 double dx = radiusBySinPhi * Math.cos(theta);
172 double dz = radiusBySinPhi * Math.sin(theta);
174 renderer.addPoint(new OffsetPoint(center.offset(dx, dy, dz)));