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;
11 import java.util.function.Supplier;
13 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
14 private static final double TAU = 6.283185307179586D;
15 public static final double PHI_SEGMENT = TAU / 90D;
16 private static final double PI = TAU / 2D;
17 public static final double THETA_SEGMENT = PHI_SEGMENT / 2D;
19 public abstract void render(T boundingBox);
21 void renderCuboid(OffsetBox bb, Color color) {
22 OffsetBox nudge = bb.nudge();
23 renderOutlinedCuboid(nudge, color);
24 renderFilledFaces(nudge.getMin(), nudge.getMax(), color);
27 void renderOutlinedCuboid(OffsetBox bb, Color color) {
28 RenderHelper.polygonModeLine();
29 OffsetPoint min = bb.getMin();
30 OffsetPoint max = bb.getMax();
31 renderFaces(min, max, color, 255, min.getY() == max.getY() ? Renderer::startLineLoop : Renderer::startLines);
34 private void renderFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha, Supplier<Renderer> rendererSupplier) {
35 double minX = min.getX();
36 double minY = min.getY();
37 double minZ = min.getZ();
39 double maxX = max.getX();
40 double maxY = max.getY();
41 double maxZ = max.getZ();
43 if (ConfigManager.invertBoxColorPlayerInside.get() &&
44 playerInsideBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)) {
45 color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
48 Renderer renderer = rendererSupplier.get()
52 if (minX != maxX && minZ != maxZ) {
53 renderer.addPoint(minX, minY, minZ)
54 .addPoint(maxX, minY, minZ)
55 .addPoint(maxX, minY, maxZ)
56 .addPoint(minX, minY, maxZ);
59 renderer.addPoint(minX, maxY, minZ)
60 .addPoint(maxX, maxY, minZ)
61 .addPoint(maxX, maxY, maxZ)
62 .addPoint(minX, maxY, maxZ);
66 if (minX != maxX && minY != maxY) {
67 renderer.addPoint(minX, minY, maxZ)
68 .addPoint(minX, maxY, maxZ)
69 .addPoint(maxX, maxY, maxZ)
70 .addPoint(maxX, minY, maxZ);
73 renderer.addPoint(minX, minY, minZ)
74 .addPoint(minX, maxY, minZ)
75 .addPoint(maxX, maxY, minZ)
76 .addPoint(maxX, minY, minZ);
79 if (minY != maxY && minZ != maxZ) {
80 renderer.addPoint(minX, minY, minZ)
81 .addPoint(minX, minY, maxZ)
82 .addPoint(minX, maxY, maxZ)
83 .addPoint(minX, maxY, minZ);
86 renderer.addPoint(maxX, minY, minZ)
87 .addPoint(maxX, minY, maxZ)
88 .addPoint(maxX, maxY, maxZ)
89 .addPoint(maxX, maxY, minZ);
95 private boolean playerInsideBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
96 return minX < 0 && maxX > 0 && minY < 0 && maxY > 0 && minZ < 0 && maxZ > 0;
99 void renderLine(OffsetPoint startPoint, OffsetPoint endPoint, Color color) {
100 RenderHelper.polygonModeLine();
101 Renderer.startLines()
103 .addPoint(startPoint)
108 void renderFilledFaces(OffsetPoint min, OffsetPoint max, Color color) {
109 renderFilledFaces(min, max, color, 30);
112 void renderFilledFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha) {
113 if (!ConfigManager.fill.get()) return;
114 RenderQueue.deferRendering(() -> renderFaces(min, max, color, alpha, Renderer::startQuads));
117 void renderText(OffsetPoint offsetPoint, String... texts) {
118 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.getStringWidth(text) / 2f;
124 fontRenderer.draw(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)));