-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from robotman2412/lines
Line rendering improvements
- Loading branch information
Showing
10 changed files
with
142 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/mods/railcraft/client/util/LineRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mods.railcraft.client.util; | ||
|
||
import org.joml.Vector3f; | ||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.world.phys.Vec3; | ||
|
||
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), | ||
(float) from.x, (float) from.y, (float) from.z, | ||
(float) to.x, (float) to.y, (float) to.z | ||
); | ||
} | ||
|
||
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); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/mods/railcraft/client/util/SimpleLineRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mods.railcraft.client.util; | ||
|
||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import com.mojang.blaze3d.vertex.VertexConsumer; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.client.renderer.RenderType; | ||
|
||
public class SimpleLineRenderer implements LineRenderer { | ||
private final VertexConsumer consumer; | ||
|
||
public SimpleLineRenderer(MultiBufferSource bufferSource) { | ||
consumer = bufferSource.getBuffer(RenderType.lines()); | ||
} | ||
|
||
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(); | ||
|
||
// 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
package mods.railcraft.util; | ||
|
||
import java.util.Collection; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Vec3i; | ||
import net.minecraft.world.phys.Vec3; | ||
|
||
public final class MathUtil { | ||
|
||
public static boolean nearZero(double f) { | ||
return Math.abs(f) < 0.001; | ||
} | ||
|
||
public static BlockPos centroid(Collection<? extends Vec3i> points) { | ||
public static Vec3 centroid(Collection<? extends Vec3i> points) { | ||
if (points.isEmpty()) { | ||
return BlockPos.ZERO; | ||
return Vec3.ZERO; | ||
} | ||
double x = 0; | ||
double y = 0; | ||
double z = 0; | ||
for (var pos : points) { | ||
x += pos.getX(); | ||
y += pos.getY(); | ||
z += pos.getZ(); | ||
x += pos.getX() + 0.5; | ||
y += pos.getY() + 0.5; | ||
z += pos.getZ() + 0.5; | ||
} | ||
int size = points.size(); | ||
x /= size; | ||
y /= size; | ||
z /= size; | ||
return BlockPos.containing(x, y, z); | ||
return new Vec3(x, y, z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.