Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.client.renderer.SubmitNodeCollector;
import net.minecraft.client.renderer.rendertype.RenderType;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Vector3f;

import java.util.ArrayList;
Expand All @@ -26,6 +27,15 @@ private enum Primitive {
QUADS
}

/**
* Minimum length of a subdivided line piece, in blocks.
*/
private static final double MIN_SEGMENT_LENGTH = 0.5;
/**
* Maximum length of a line piece, relative to its distance from the camera.
*/
private static final double SEGMENT_DISTANCE_FACTOR = 0.15;

private final TypeFactory types;
private @Nullable SubmitNodeCollector collector;
private @Nullable PoseStack poseStack;
Expand Down Expand Up @@ -87,9 +97,7 @@ public RenderSink vertex(final double x, final double y, final double z) {

if (this.activePrimitive == Primitive.LINE_LOOP) {
if (this.canLoop) {
final Vector3f normal = this.activeTarget.hasNormals() ? this.computeNormal(this.loopX, this.loopY, this.loopZ, x, y, z) : null;
this.addVertex(this.loopX, this.loopY, this.loopZ, normal);
this.addVertex(x, y, z, normal);
this.addLineSegment(this.loopX, this.loopY, this.loopZ, x, y, z);
} else {
this.loopFirstX = (float) x;
this.loopFirstY = (float) y;
Expand All @@ -101,9 +109,7 @@ public RenderSink vertex(final double x, final double y, final double z) {
this.canLoop = true;
} else if (this.activePrimitive == Primitive.LINES) {
if (this.canLoop) {
final Vector3f normal = this.activeTarget.hasNormals() ? this.computeNormal(this.loopX, this.loopY, this.loopZ, x, y, z) : null;
this.addVertex(this.loopX, this.loopY, this.loopZ, normal);
this.addVertex(x, y, z, normal);
this.addLineSegment(this.loopX, this.loopY, this.loopZ, x, y, z);
this.canLoop = false;
} else {
this.loopX = (float) x;
Expand All @@ -126,10 +132,68 @@ private void addVertex(final double x, final double y, final double z, @Nullable
(float) x, (float) y, (float) z,
this.r, this.g, this.b, this.a,
this.currentLineWidth,
normal == null ? null : new Vector3f(normal)
// normals are never mutated, so pieces of a subdivided line can share one instance
normal
));
}

/**
* Emits a line segment, split into pieces whose length is proportional to their distance from the camera.
*
* <p>The vanilla line shader expands every segment into a quad by offsetting each endpoint
* perpendicular to the line in NDC space and multiplying that offset back by the endpoint's own
* {@code w}. When one endpoint lies behind the camera ({@code w < 0}), its offset flips sign in clip
* space, so after near-plane clipping the quad widens by roughly {@code |w_behind| / w}. For long
* segments that pass close to the camera (e.g. tall vertical grid lines while looking up or down)
* this turns a thin line into a wide band.</p>
*
* <p>Limiting each piece to a fraction of its distance from the camera keeps that factor below one
* for any piece that is actually on screen, so pieces crossing the camera plane end up entirely
* outside the view frustum. Pieces are never shorter than {@link #MIN_SEGMENT_LENGTH}.</p>
*/
private void addLineSegment(final double x0, final double y0, final double z0, final double x1, final double y1, final double z1) {
final Vector3f normal = activeTargetOrThrow().hasNormals() ? this.computeNormal(x0, y0, z0, x1, y1, z1) : null;

// Measure distances in camera space: some shapes translate the pose stack instead of
// submitting camera-relative coordinates (e.g. chunk boundaries, ellipsoids).
final Vector3f start = new Vector3f((float) x0, (float) y0, (float) z0);
final Vector3f end = new Vector3f((float) x1, (float) y1, (float) z1);
if (this.poseStack != null) {
final Matrix4f pose = this.poseStack.last().pose();
pose.transformPosition(start);
pose.transformPosition(end);
}
final double length = start.distance(end);
if (!(length > MIN_SEGMENT_LENGTH)) {
this.addVertex(x0, y0, z0, normal);
this.addVertex(x1, y1, z1, normal);
return;
}

// The pose is affine, so walking the segment by a parameter in [0, 1] is valid in both spaces.
double t = 0.0;
double px = x0;
double py = y0;
double pz = z0;
while (t < 1.0) {
final double cx = start.x() + (end.x() - start.x()) * t;
final double cy = start.y() + (end.y() - start.y()) * t;
final double cz = start.z() + (end.z() - start.z()) * t;
final double distance = Math.sqrt(cx * cx + cy * cy + cz * cz);
final double step = Math.max(MIN_SEGMENT_LENGTH, distance * SEGMENT_DISTANCE_FACTOR);
final double next = Math.min(1.0, t + step / length);
final double nx = next >= 1.0 ? x1 : x0 + (x1 - x0) * next;
final double ny = next >= 1.0 ? y1 : y0 + (y1 - y0) * next;
final double nz = next >= 1.0 ? z1 : z0 + (z1 - z0) * next;
this.addVertex(px, py, pz, normal);
this.addVertex(nx, ny, nz, normal);
px = nx;
py = ny;
pz = nz;
t = next;
}
}

private Vector3f computeNormal(final double x0, final double y0, final double z0, final double x1, final double y1, final double z1) {
final double dX = (x1 - x0);
final double dY = (y1 - y0);
Expand All @@ -149,11 +213,7 @@ public RenderSink endLineLoop() {
this.end(currentVariants().lineLoop(), Primitive.LINE_LOOP);
if (this.canLoop) {
this.canLoop = false;
final Vector3f normal = activeTargetOrThrow().hasNormals()
? this.computeNormal(this.loopX, this.loopY, this.loopZ, this.loopFirstX, this.loopFirstY, this.loopFirstZ)
: null;
this.addVertex(this.loopX, this.loopY, this.loopZ, normal);
this.addVertex(this.loopFirstX, this.loopFirstY, this.loopFirstZ, normal);
this.addLineSegment(this.loopX, this.loopY, this.loopZ, this.loopFirstX, this.loopFirstY, this.loopFirstZ);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public void render(CUIRenderContext ctx)
.vertex(x2, y, z2);
}

// Cull the vertical extent of lines spanning Y as well, so they don't extend far beyond view range
final double vy1 = Math.max(y1, -cullAt);
final double vy2 = Math.min(y2, cullAt);
final boolean drawVertical = vy1 < vy2;

final double xEnd = Math.min(x2, cullAtX);
for (double x = Math.max(x1, -cullAtX); x <= xEnd; x += this.spacing)
{
Expand All @@ -155,11 +160,14 @@ public void render(CUIRenderContext ctx)
continue;
}

ctx.vertex(x, y1, z1)
.vertex(x, y2, z1)
.vertex(x, y1, z2)
.vertex(x, y2, z2)
.vertex(x, y2, z1)
if (drawVertical)
{
ctx.vertex(x, vy1, z1)
.vertex(x, vy2, z1)
.vertex(x, vy1, z2)
.vertex(x, vy2, z2);
}
ctx.vertex(x, y2, z1)
.vertex(x, y2, z2)
.vertex(x, y1, z1)
.vertex(x, y1, z2);
Expand All @@ -174,13 +182,16 @@ public void render(CUIRenderContext ctx)
}

ctx.vertex(x1, y1, z)
.vertex(x2, y1, z)
.vertex(x1, y2, z)
.vertex(x2, y2, z)
.vertex(x2, y1, z)
.vertex(x2, y2, z)
.vertex(x1, y1, z)
.vertex(x1, y2, z);
.vertex(x2, y1, z);
if (drawVertical)
{
ctx.vertex(x1, vy1, z)
.vertex(x1, vy2, z)
.vertex(x2, vy1, z)
.vertex(x2, vy2, z);
}
ctx.vertex(x1, y2, z)
.vertex(x2, y2, z);
}

ctx.endLines();
Expand Down
Loading