Skip to content

Commit

Permalink
Make lines slightly easier to see from all angles
Browse files Browse the repository at this point in the history
  • Loading branch information
robotman2412 committed Feb 24, 2024
1 parent 7624d89 commit e4ecdcf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mods.railcraft.client.util.LineRenderer;
import mods.railcraft.client.util.RenderUtil;
import mods.railcraft.client.util.SimpleLineRenderer;
import mods.railcraft.client.util.VolumetricLineRenderer;
import mods.railcraft.network.play.LinkedCartsMessage;
import mods.railcraft.world.item.GogglesItem;
import mods.railcraft.world.item.RailcraftItems;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void render(PoseStack poseStack, Camera mainCamera, float partialTick) {
continue;
}

var renderer = new SimpleLineRenderer(bufferSource);
var renderer = LineRenderer.create(bufferSource);
final int color = linkedCart.trainId().hashCode();
final var cartPosition = cart.getPosition(partialTick);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void renderSignalAuraf(
float endZ = (float) target.z() - blockEntity.getBlockPos().getZ();

renderer.renderLine(poseStack,
red ,green, blue, 1,
red ,green, blue,
0.5F, 0.5F, 0.5F,
endX, endY, endZ
);
Expand All @@ -97,9 +97,9 @@ private static void renderSignalAura(
float endZ = 0.5F + target.getZ() - blockEntity.getBlockPos().getZ();

renderer.renderLine(poseStack,
red ,green, blue, 1,
0.5F, 0.5F, 0.5F,
endX, endY, endZ
red, green, blue,
0.5F, 0.5F, 0.5F,
endX, endY, endZ
);
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/mods/railcraft/client/util/LineRenderer.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package mods.railcraft.client.util;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;

public interface LineRenderer {
static LineRenderer create(MultiBufferSource bufferSource) {
return new SimpleLineRenderer(bufferSource);
}

default void renderLine(PoseStack poseStack, int color, Vec3 from, Vec3 to) {
renderLine(poseStack,
RenderUtil.getRed(color), RenderUtil.getGreen(color), RenderUtil.getBlue(color), 1,
RenderUtil.getRed(color), RenderUtil.getGreen(color), RenderUtil.getBlue(color),
(float) from.x, (float) from.y, (float) from.z,
(float) to.x, (float) to.y, (float) to.z
);
}

void renderLine(PoseStack poseStack, float r, float g, float b, float a, float x0, float y0, float z0, float x1, float y1, float z1);
default void renderLine(PoseStack poseStack, int color, Vector3f from, Vector3f to) {
renderLine(poseStack,
RenderUtil.getRed(color), RenderUtil.getGreen(color), RenderUtil.getBlue(color),
from.x, from.y, from.z,
to.x, to.y, to.z
);
}

void renderLine(PoseStack poseStack, float r, float g, float b, float x0, float y0, float z0, float x1, float y1, float z1);
}
31 changes: 20 additions & 11 deletions src/main/java/mods/railcraft/client/util/SimpleLineRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import org.joml.Matrix3f;
import org.joml.Vector3f;

public class SimpleLineRenderer implements LineRenderer {
private final VertexConsumer consumer;
Expand All @@ -12,20 +14,27 @@ public SimpleLineRenderer(MultiBufferSource bufferSource) {
consumer = bufferSource.getBuffer(RenderType.lines());
}

public void renderLine(PoseStack poseStack, float r, float g, float b, float a, float x0, float y0, float z0, float x1, float y1, float z1) {
public void renderLine(PoseStack poseStack, float r, float g, float b, float x0, float y0, float z0, float x1, float y1, float z1) {
poseStack.pushPose();
var matrix = poseStack.last().pose();
var normal = poseStack.last().normal();
consumer
.vertex(matrix, x0, y0, z0)
.color(r, g, b, a)
.normal(normal, 0, 0, 0)
.endVertex();
consumer
.vertex(matrix, x1, y1, z1)
.color(r, g, b, a)
.normal(normal, 0, 0, 0)
.endVertex();

// Draw a copy with each UV value to make the line visible from all angles.
for (int i = 0; i < 3; i++) {
int nx = i == 0 ? 1 : 0;
int ny = i == 1 ? 1 : 0;
int nz = i == 2 ? 1 : 0;
consumer
.vertex(matrix, x0, y0, z0)
.color(r, g, b, 1)
.normal(normal, nx, ny, nz)
.endVertex();
consumer
.vertex(matrix, x1, y1, z1)
.color(r, g, b, 1)
.normal(normal, nx, ny, nz)
.endVertex();
}
poseStack.popPose();
}
}

0 comments on commit e4ecdcf

Please sign in to comment.