Skip to content

Instantly share code, notes, and snippets.

@UltraBlackLinux
Last active November 2, 2024 20:42
Show Gist options
  • Save UltraBlackLinux/2eeade645031ed1e96ee432b306e8358 to your computer and use it in GitHub Desktop.
Save UltraBlackLinux/2eeade645031ed1e96ee432b306e8358 to your computer and use it in GitHub Desktop.
fabric baritone renderer on 1.21
/*
* This file was taken from Baritone and has been modified. Thanks!
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.ShaderProgramKeys;
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
public class Renderer {
Tessellator tessellator;
BufferBuilder builder;
boolean ignoreDepth = false;
MatrixStack stack;
VertexConsumer consumer;
public Renderer(MatrixStack stack, VertexConsumerProvider consumers, float lineWidth, boolean ignoreDepth) {
tessellator = Tessellator.getInstance();
this.ignoreDepth = ignoreDepth;
consumer = consumers.getBuffer(RenderLayer.LINES);
this.stack = stack;
RenderSystem.enableBlend();
RenderSystem.setShader(ShaderProgramKeys.POSITION_COLOR);
RenderSystem.blendFuncSeparate(
GlStateManager.SrcFactor.SRC_ALPHA,
GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA,
GlStateManager.SrcFactor.ONE,
GlStateManager.DstFactor.ZERO
);
RenderSystem.lineWidth(lineWidth);
RenderSystem.depthMask(false);
RenderSystem.disableCull();
if (ignoreDepth) {
RenderSystem.disableDepthTest();
}
RenderSystem.setShader(ShaderProgramKeys.RENDERTYPE_LINES);
builder = tessellator.begin(VertexFormat.DrawMode.LINES, VertexFormats.POSITION_COLOR);
}
public void render() {
BuiltBuffer built = builder.end();
if (built != null) {
BufferRenderer.drawWithGlobalProgram(built);
}
if (ignoreDepth) {
RenderSystem.enableDepthTest();
}
RenderSystem.enableCull();
RenderSystem.depthMask(true);
RenderSystem.disableBlend();
}
//public void drawBox(Box box, Color color) {
// VertexRendering.drawBox(stack, consumer,
// box.minX, box.minY, box.minZ,
// box.maxX, box.maxY, box.maxZ,
// color.red, color.green, color.blue, color.alpha);
//}
public void emitLine(Color color,
double x1, double y1, double z1,
double x2, double y2, double z2,
double nx, double ny, double nz) {
MatrixStack.Entry pose = stack.peek();
builder.vertex(pose, (float) x1, (float) y1, (float) z1).color(color.red, color.green, color.blue, color.alpha).normal(pose, (float) nx, (float) ny, (float) nz);
builder.vertex(pose, (float) x2, (float) y2, (float) z2).color(color.red, color.green, color.blue, color.alpha).normal(pose, (float) nx, (float) ny, (float) nz);
}
public void emitLine(Color color, Vec3d start, Vec3d end) {
var vec = MinecraftClient.getInstance().player.getCameraPosVec(0);
emitLine(color, start.x - vec.x, start.y - vec.y, start.z - vec.z,
end.x - vec.x, end.y - vec.y, end.z - vec.z);
}
public void emitLine(Color color, double x1, double y1, double z1, double x2, double y2, double z2) {
final double dx = x2 - x1;
final double dy = y2 - y1;
final double dz = z2 - z1;
final double invMag = 1.0 / Math.sqrt(dx * dx + dy * dy + dz * dz);
final double nx = dx * invMag;
final double ny = dy * invMag;
final double nz = dz * invMag;
emitLine(color, x1, y1, z1, x2, y2, z2, nx, ny, nz);
}
public void drawBox(Box toDraw, Color color) {
var vec = MinecraftClient.getInstance().player.getClientCameraPosVec(0);
toDraw = new Box(toDraw.getMinPos().subtract(vec), toDraw.getMaxPos().subtract(vec));
// bottom
emitLine(color, toDraw.minX, toDraw.minY, toDraw.minZ, toDraw.maxX, toDraw.minY, toDraw.minZ, 1.0, 0.0, 0.0);
emitLine(color, toDraw.maxX, toDraw.minY, toDraw.maxZ, toDraw.minX, toDraw.minY, toDraw.maxZ, -1.0, 0.0, 0.0);
emitLine(color, toDraw.maxX, toDraw.minY, toDraw.minZ, toDraw.maxX, toDraw.minY, toDraw.maxZ, 0.0, 0.0, 1.0);
emitLine(color, toDraw.minX, toDraw.minY, toDraw.maxZ, toDraw.minX, toDraw.minY, toDraw.minZ, 0.0, 0.0, -1.0);
// top
emitLine(color, toDraw.minX, toDraw.maxY, toDraw.minZ, toDraw.maxX, toDraw.maxY, toDraw.minZ, 1.0, 0.0, 0.0);
emitLine(color, toDraw.maxX, toDraw.maxY, toDraw.minZ, toDraw.maxX, toDraw.maxY, toDraw.maxZ, 0.0, 0.0, 1.0);
emitLine(color, toDraw.maxX, toDraw.maxY, toDraw.maxZ, toDraw.minX, toDraw.maxY, toDraw.maxZ, -1.0, 0.0, 0.0);
emitLine(color, toDraw.minX, toDraw.maxY, toDraw.maxZ, toDraw.minX, toDraw.maxY, toDraw.minZ, 0.0, 0.0, -1.0);
// corners
emitLine(color, toDraw.minX, toDraw.minY, toDraw.minZ, toDraw.minX, toDraw.maxY, toDraw.minZ, 0.0, 1.0, 0.0);
emitLine(color, toDraw.maxX, toDraw.minY, toDraw.minZ, toDraw.maxX, toDraw.maxY, toDraw.minZ, 0.0, 1.0, 0.0);
emitLine(color, toDraw.maxX, toDraw.minY, toDraw.maxZ, toDraw.maxX, toDraw.maxY, toDraw.maxZ, 0.0, 1.0, 0.0);
emitLine(color, toDraw.minX, toDraw.minY, toDraw.maxZ, toDraw.minX, toDraw.maxY, toDraw.maxZ, 0.0, 1.0, 0.0);
}
}
-----------------------------
public class Color {
public final int red;
public final int green;
public final int blue;
public final int alpha;
public Color(int color) {
alpha = color >> 24;
red = color >> 16 & 0xFF;
green = color >> 8 & 0xFF;
blue = color & 0xFF;
}
public Color(int r, int g, int b, int a) {
red = r;
green = g;
blue = b;
alpha = a;
}
public int toInt() {
int out = alpha;
out = out << 8 | red;
out = out << 8 | green;
out = out << 8 | blue;
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment