1 package com.irtimaled.bbor.client.renderers;
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;
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;
25 private static final Box ORIGIN_BOX = new Box(BlockPos.ORIGIN);
27 public abstract void render(MatrixStack matrixStack, T boundingBox);
29 void renderCuboid(MatrixStack matrixStack, OffsetBox bb, Color color, boolean fillOnly, int fillAlpha) {
30 OffsetBox nudge = bb.nudge();
33 renderCuboid0(matrixStack, nudge, color, fillOnly, fillAlpha, false);
38 private void renderCuboid0(MatrixStack stack, OffsetBox nudge, Color color, boolean fillOnly, int fillAlpha, boolean mask) {
39 if (!RenderCulling.isVisibleCulling(nudge.toBox())) return;
40 if (ConfigManager.invertBoxColorPlayerInside.get() &&
41 playerInsideBoundingBox(nudge)) {
42 color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
44 final MatrixStack.Entry lastStack = stack.peek();
46 int regionX = (((int) Camera.getX()) >> 9) << 9;
47 int regionZ = (((int) Camera.getZ()) >> 9) << 9;
48 RenderHelper.applyRegionalRenderOffset(stack);
49 final double minX = nudge.getMin().getX();
50 final double minY = nudge.getMin().getY();
51 final double minZ = nudge.getMin().getZ();
52 final double maxX = nudge.getMax().getX();
53 final double maxY = nudge.getMax().getY();
54 final double maxZ = nudge.getMax().getZ();
55 stack.translate(minX - regionX, minY, minZ - regionZ);
56 stack.scale((float) (maxX - minX),
57 (float) (maxY - minY),
58 (float) (maxZ - minZ));
60 if (fillOnly || ConfigManager.fill.get()) {
61 RenderBatch.drawSolidBox(stack.peek(), ORIGIN_BOX, color, fillAlpha, mask);
65 stack.peek().getModel().load(lastStack.getModel());
66 stack.peek().getNormal().load(lastStack.getNormal());
67 renderLine(stack, new OffsetPoint(minX, minY, minZ), new OffsetPoint(maxX, minY, minZ), color);
68 renderLine(stack, new OffsetPoint(maxX, minY, minZ), new OffsetPoint(maxX, minY, maxZ), color);
69 renderLine(stack, new OffsetPoint(maxX, minY, maxZ), new OffsetPoint(minX, minY, maxZ), color);
70 renderLine(stack, new OffsetPoint(minX, minY, maxZ), new OffsetPoint(minX, minY, minZ), color);
71 renderLine(stack, new OffsetPoint(minX, minY, minZ), new OffsetPoint(minX, maxY, minZ), color);
72 renderLine(stack, new OffsetPoint(maxX, minY, minZ), new OffsetPoint(maxX, maxY, minZ), color);
73 renderLine(stack, new OffsetPoint(maxX, minY, maxZ), new OffsetPoint(maxX, maxY, maxZ), color);
74 renderLine(stack, new OffsetPoint(minX, minY, maxZ), new OffsetPoint(minX, maxY, maxZ), color);
75 renderLine(stack, new OffsetPoint(minX, maxY, minZ), new OffsetPoint(maxX, maxY, minZ), color);
76 renderLine(stack, new OffsetPoint(maxX, maxY, minZ), new OffsetPoint(maxX, maxY, maxZ), color);
77 renderLine(stack, new OffsetPoint(maxX, maxY, maxZ), new OffsetPoint(minX, maxY, maxZ), color);
78 renderLine(stack, new OffsetPoint(minX, maxY, maxZ), new OffsetPoint(minX, maxY, minZ), color);
85 private boolean playerInsideBoundingBox(OffsetBox nudge) {
86 return nudge.getMin().getX() < 0 && nudge.getMax().getX() > 0 &&
87 nudge.getMin().getY() < 0 && nudge.getMax().getY() > 0 &&
88 nudge.getMin().getZ() < 0 && nudge.getMax().getZ() > 0;
92 void renderLine(MatrixStack matrixStack, OffsetPoint startPoint, OffsetPoint endPoint, Color color) {
93 // if ((startPoint.getY() == endPoint.getY() && startPoint.getZ() == endPoint.getZ()) ||
94 // (startPoint.getX() == endPoint.getX() && startPoint.getZ() == endPoint.getZ()) ||
95 // (startPoint.getX() == endPoint.getX() && startPoint.getY() == endPoint.getY())) {
96 // renderCuboid0(matrixStack, new OffsetBox(startPoint.offset(-getLineWidth(), -getLineWidth(), -getLineWidth()), endPoint.offset(getLineWidth(), getLineWidth(), getLineWidth())), color, true, 255, true);
100 if (!RenderCulling.isVisibleCulling(new OffsetBox(startPoint, endPoint).toBox())) return; // TODO better culling
104 RenderHelper.applyRegionalRenderOffset(matrixStack);
106 RenderBatch.drawLine(matrixStack.peek(), startPoint.getPoint(), endPoint.getPoint(), color, 255);
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;
120 RenderHelper.afterRenderFont(matrixStack);
123 void renderSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
124 if (ConfigManager.renderSphereAsDots.get()) {
125 renderDotSphere(matrixStack, center, radius, color);
127 renderLineSphere(matrixStack, center, radius, color);
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;
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);
143 private void renderCircle(MatrixStack matrixStack, Point center, double radius, Color color, double dy) {
146 RenderHelper.applyRegionalRenderOffset(matrixStack);
147 RenderSystem.setShader(GameRenderer::getPositionShader);
149 Point firstPoint = null;
150 Point lastPoint = null;
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) {
159 RenderBatch.drawLine(matrixStack.peek(), lastPoint, point, color, 255);
162 RenderBatch.drawLine(matrixStack.peek(), lastPoint, firstPoint, color, 255);
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;
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);