183 lines
8.3 KiB
Java
183 lines
8.3 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* org.joml.Matrix4f
|
|
* org.joml.Matrix4fc
|
|
* org.joml.Quaternionfc
|
|
* org.joml.Vector4f
|
|
* org.joml.Vector4fc
|
|
*/
|
|
package net.minecraft.client.renderer.gizmos;
|
|
|
|
import com.mojang.blaze3d.vertex.PoseStack;
|
|
import com.mojang.blaze3d.vertex.VertexConsumer;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.gui.Font;
|
|
import net.minecraft.client.renderer.MultiBufferSource;
|
|
import net.minecraft.client.renderer.rendertype.RenderTypes;
|
|
import net.minecraft.client.renderer.state.CameraRenderState;
|
|
import net.minecraft.gizmos.GizmoPrimitives;
|
|
import net.minecraft.gizmos.TextGizmo;
|
|
import net.minecraft.util.Mth;
|
|
import net.minecraft.world.phys.Vec3;
|
|
import org.joml.Matrix4f;
|
|
import org.joml.Matrix4fc;
|
|
import org.joml.Quaternionfc;
|
|
import org.joml.Vector4f;
|
|
import org.joml.Vector4fc;
|
|
|
|
public class DrawableGizmoPrimitives
|
|
implements GizmoPrimitives {
|
|
private final List<Line> lines = new ArrayList<Line>();
|
|
private final List<Quad> quads = new ArrayList<Quad>();
|
|
private final List<TriangleFan> triangleFans = new ArrayList<TriangleFan>();
|
|
private final List<Text> texts = new ArrayList<Text>();
|
|
private final List<Point> points = new ArrayList<Point>();
|
|
|
|
@Override
|
|
public void addPoint(Vec3 pos, int color, float size) {
|
|
this.points.add(new Point(pos, color, size));
|
|
}
|
|
|
|
@Override
|
|
public void addLine(Vec3 start, Vec3 end, int color, float width) {
|
|
this.lines.add(new Line(start, end, color, width));
|
|
}
|
|
|
|
@Override
|
|
public void addTriangleFan(Vec3[] points, int color) {
|
|
this.triangleFans.add(new TriangleFan(points, color));
|
|
}
|
|
|
|
@Override
|
|
public void addQuad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color) {
|
|
this.quads.add(new Quad(a, b, c, d, color));
|
|
}
|
|
|
|
@Override
|
|
public void addText(Vec3 pos, String text, TextGizmo.Style style) {
|
|
this.texts.add(new Text(pos, text, style));
|
|
}
|
|
|
|
public void render(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera, Matrix4f modelViewMatrix) {
|
|
this.renderQuads(poseStack, bufferSource, camera);
|
|
this.renderTriangleFans(poseStack, bufferSource, camera);
|
|
this.renderLines(poseStack, bufferSource, camera, modelViewMatrix);
|
|
this.renderTexts(poseStack, bufferSource, camera);
|
|
this.renderPoints(poseStack, bufferSource, camera);
|
|
}
|
|
|
|
private void renderTexts(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
|
|
Minecraft minecraft = Minecraft.getInstance();
|
|
Font font = minecraft.font;
|
|
if (!camera.initialized) {
|
|
return;
|
|
}
|
|
double camX = camera.pos.x();
|
|
double camY = camera.pos.y();
|
|
double camZ = camera.pos.z();
|
|
for (Text text : this.texts) {
|
|
poseStack.pushPose();
|
|
poseStack.translate((float)(text.pos().x() - camX), (float)(text.pos().y() - camY), (float)(text.pos().z() - camZ));
|
|
poseStack.mulPose((Quaternionfc)camera.orientation);
|
|
poseStack.scale(text.style.scale() / 16.0f, -text.style.scale() / 16.0f, text.style.scale() / 16.0f);
|
|
float fontX = text.style.adjustLeft().isEmpty() ? (float)(-font.width(text.text)) / 2.0f : (float)(-text.style.adjustLeft().getAsDouble()) / text.style.scale();
|
|
font.drawInBatch(text.text, fontX, 0.0f, text.style.color(), false, poseStack.last().pose(), bufferSource, Font.DisplayMode.NORMAL, 0, 0xF000F0);
|
|
poseStack.popPose();
|
|
}
|
|
}
|
|
|
|
private void renderLines(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera, Matrix4f modelViewMatrix) {
|
|
VertexConsumer builder = bufferSource.getBuffer(RenderTypes.lines());
|
|
PoseStack.Pose pose = poseStack.last();
|
|
Vector4f start = new Vector4f();
|
|
Vector4f end = new Vector4f();
|
|
Vector4f startViewSpace = new Vector4f();
|
|
Vector4f endViewSpace = new Vector4f();
|
|
Vector4f intersectionInWorld = new Vector4f();
|
|
double camX = camera.pos.x();
|
|
double camY = camera.pos.y();
|
|
double camZ = camera.pos.z();
|
|
for (Line line : this.lines) {
|
|
boolean endIsBehindCamera;
|
|
start.set(line.start().x() - camX, line.start().y() - camY, line.start().z() - camZ, 1.0);
|
|
end.set(line.end().x() - camX, line.end().y() - camY, line.end().z() - camZ, 1.0);
|
|
start.mul((Matrix4fc)modelViewMatrix, startViewSpace);
|
|
end.mul((Matrix4fc)modelViewMatrix, endViewSpace);
|
|
boolean startIsBehindCamera = startViewSpace.z > -0.05f;
|
|
boolean bl = endIsBehindCamera = endViewSpace.z > -0.05f;
|
|
if (startIsBehindCamera && endIsBehindCamera) continue;
|
|
if (startIsBehindCamera || endIsBehindCamera) {
|
|
float denom = endViewSpace.z - startViewSpace.z;
|
|
if (Math.abs(denom) < 1.0E-9f) continue;
|
|
float intersection = Mth.clamp((-0.05f - startViewSpace.z) / denom, 0.0f, 1.0f);
|
|
start.lerp((Vector4fc)end, intersection, intersectionInWorld);
|
|
if (startIsBehindCamera) {
|
|
start.set((Vector4fc)intersectionInWorld);
|
|
} else {
|
|
end.set((Vector4fc)intersectionInWorld);
|
|
}
|
|
}
|
|
builder.addVertex(pose, start.x, start.y, start.z).setNormal(pose, end.x - start.x, end.y - start.y, end.z - start.z).setColor(line.color()).setLineWidth(line.width());
|
|
builder.addVertex(pose, end.x, end.y, end.z).setNormal(pose, end.x - start.x, end.y - start.y, end.z - start.z).setColor(line.color()).setLineWidth(line.width());
|
|
}
|
|
}
|
|
|
|
private void renderTriangleFans(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
|
|
PoseStack.Pose pose = poseStack.last();
|
|
double camX = camera.pos.x();
|
|
double camY = camera.pos.y();
|
|
double camZ = camera.pos.z();
|
|
for (TriangleFan triangleFan : this.triangleFans) {
|
|
VertexConsumer builder = bufferSource.getBuffer(RenderTypes.debugTriangleFan());
|
|
for (Vec3 point : triangleFan.points()) {
|
|
builder.addVertex(pose, (float)(point.x() - camX), (float)(point.y() - camY), (float)(point.z() - camZ)).setColor(triangleFan.color());
|
|
}
|
|
}
|
|
}
|
|
|
|
private void renderQuads(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
|
|
VertexConsumer builder = bufferSource.getBuffer(RenderTypes.debugFilledBox());
|
|
PoseStack.Pose pose = poseStack.last();
|
|
double camX = camera.pos.x();
|
|
double camY = camera.pos.y();
|
|
double camZ = camera.pos.z();
|
|
for (Quad quad : this.quads) {
|
|
builder.addVertex(pose, (float)(quad.a().x() - camX), (float)(quad.a().y() - camY), (float)(quad.a().z() - camZ)).setColor(quad.color());
|
|
builder.addVertex(pose, (float)(quad.b().x() - camX), (float)(quad.b().y() - camY), (float)(quad.b().z() - camZ)).setColor(quad.color());
|
|
builder.addVertex(pose, (float)(quad.c().x() - camX), (float)(quad.c().y() - camY), (float)(quad.c().z() - camZ)).setColor(quad.color());
|
|
builder.addVertex(pose, (float)(quad.d().x() - camX), (float)(quad.d().y() - camY), (float)(quad.d().z() - camZ)).setColor(quad.color());
|
|
}
|
|
}
|
|
|
|
private void renderPoints(PoseStack poseStack, MultiBufferSource bufferSource, CameraRenderState camera) {
|
|
VertexConsumer builder = bufferSource.getBuffer(RenderTypes.debugPoint());
|
|
PoseStack.Pose pose = poseStack.last();
|
|
double camX = camera.pos.x();
|
|
double camY = camera.pos.y();
|
|
double camZ = camera.pos.z();
|
|
for (Point point : this.points) {
|
|
builder.addVertex(pose, (float)(point.pos.x() - camX), (float)(point.pos.y() - camY), (float)(point.pos.z() - camZ)).setColor(point.color()).setLineWidth(point.size());
|
|
}
|
|
}
|
|
|
|
private record Point(Vec3 pos, int color, float size) {
|
|
}
|
|
|
|
private record Line(Vec3 start, Vec3 end, int color, float width) {
|
|
}
|
|
|
|
private record TriangleFan(Vec3[] points, int color) {
|
|
}
|
|
|
|
private record Quad(Vec3 a, Vec3 b, Vec3 c, Vec3 d, int color) {
|
|
}
|
|
|
|
private record Text(Vec3 pos, String text, TextGizmo.Style style) {
|
|
}
|
|
}
|
|
|