Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 267
src/client/java/dev/kodari/oreglow/OreGlowClient.java
package dev.kodari.oreglow;
import com.mojang.blaze3d.pipeline.BlendFunction;
import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.platform.DepthTestFunction;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.world.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.world.WorldRenderEvents;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.UniformType;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.RenderSetup;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.VertexRendering;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShape;
import dev.kodari.oreglow.mixin.RenderLayerInvoker;
import java.util.ArrayList;
import java.util.List;
public final class OreGlowClient implements ClientModInitializer {
private static final int SCAN_RADIUS = 24;
private static final int MAX_HIGHLIGHTS = 256;
private static final long SCAN_INTERVAL_NANOS = 1_000_000_000L;
private static final RenderLayer ORE_LINES = createOreLineLayer();
private static List<OreMarker> markers = List.of();
private static ClientWorld scannedWorld;
private static long nextScanNanos;
@Override
public void onInitializeClient() {
WorldRenderEvents.AFTER_ENTITIES.register(OreGlowClient::renderOres);
}
private static RenderLayer createOreLineLayer() {
RenderPipeline pipeline = RenderPipeline.builder()
.withLocation(Identifier.of("oreglow", "pipeline/ore_lines"))
.withVertexShader("core/rendertype_lines")
.withFragmentShader("core/rendertype_lines")
.withUniform("DynamicTransforms", UniformType.UNIFORM_BUFFER)
.withUniform("Projection", UniformType.UNIFORM_BUFFER)
.withUniform("Fog", UniformType.UNIFORM_BUFFER)
.withUniform("Globals", UniformType.UNIFORM_BUFFER)
.withBlend(BlendFunction.TRANSLUCENT)
.withCull(false)
.withDepthTestFunction(DepthTestFunction.NO_DEPTH_TEST)
.withDepthWrite(false)
.withVertexFormat(VertexFormats.POSITION_COLOR_NORMAL_LINE_WIDTH, VertexFormat.DrawMode.LINES)
.build();
return RenderLayerInvoker.oreglow$create("oreglow_ore_lines", RenderSetup.builder(pipeline).build());
}
private static void renderOres(WorldRenderContext context) {
MinecraftClient client = MinecraftClient.getInstance();
ClientWorld world = context.world();
if (client.player == null || client.world != world) {
return;
}
long now = System.nanoTime();
if (scannedWorld != world || now >= nextScanNanos) {
scanOres(world, client.player.getBlockPos());
scannedWorld = world;
nextScanNanos = now + SCAN_INTERVAL_NANOS;
}
Vec3d cameraPos = context.camera().getCameraPos();
VertexConsumerProvider consumers = context.consumers();
VertexConsumer lines = consumers.getBuffer(ORE_LINES);
for (OreMarker marker : markers) {
BlockPos pos = marker.pos();
BlockState state = world.getBlockState(pos);
int color = oreColor(state);
if (color == 0) {
continue;
}
VoxelShape shape = state.getOutlineShape(world, pos);
VertexRendering.drawOutline(
context.matrices(),
lines,
shape,
pos.getX() - cameraPos.x,
pos.getY() - cameraPos.y,
pos.getZ() - cameraPos.z,
color,
2.0f
);
}
}
private static void scanOres(ClientWorld world, BlockPos center) {
List<OreMarker> found = new ArrayList<>();
int radiusSquared = SCAN_RADIUS * SCAN_RADIUS;
scan:
for (int y = center.getY() - SCAN_RADIUS; y <= center.getY() + SCAN_RADIUS; y++) {
for (int x = center.getX() - SCAN_RADIUS; x <= center.getX() + SCAN_RADIUS; x++) {
for (int z = center.getZ() - SCAN_RADIUS; z <= center.getZ() + SCAN_RADIUS; z++) {
int dx = x - center.getX();
int dy = y - center.getY();
int dz = z - center.getZ();
if (dx * dx + dy * dy + dz * dz > radiusSquared) {
continue;
}
BlockPos pos = new BlockPos(x, y, z);
int color = oreColor(world.getBlockState(pos));
if (color != 0) {
found.add(new OreMarker(pos, color));
if (found.size() >= MAX_HIGHLIGHTS) {
break scan;
}
}
}
}
}
markers = List.copyOf(found);
}
private static int oreColor(BlockState state) {
if (state.isOf(Blocks.DIAMOND_ORE) || state.isOf(Blocks.DEEPSLATE_DIAMOND_ORE)) return 0xFF55FFFF;
if (state.isOf(Blocks.EMERALD_ORE) || state.isOf(Blocks.DEEPSLATE_EMERALD_ORE)) return 0xFF55FF55;
if (state.isOf(Blocks.GOLD_ORE) || state.isOf(Blocks.DEEPSLATE_GOLD_ORE) || state.isOf(Blocks.NETHER_GOLD_ORE)) return 0xFFFFD700;
if (state.isOf(Blocks.IRON_ORE) || state.isOf(Blocks.DEEPSLATE_IRON_ORE)) return 0xFFFFAA55;
if (state.isOf(Blocks.COPPER_ORE) || state.isOf(Blocks.DEEPSLATE_COPPER_ORE)) return 0xFFFF8844;
if (state.isOf(Blocks.REDSTONE_ORE) || state.isOf(Blocks.DEEPSLATE_REDSTONE_ORE)) return 0xFFFF4444;
if (state.isOf(Blocks.LAPIS_ORE) || state.isOf(Blocks.DEEPSLATE_LAPIS_ORE)) return 0xFF5588FF;
if (state.isOf(Blocks.COAL_ORE) || state.isOf(Blocks.DEEPSLATE_COAL_ORE)) return 0xFFBBBBBB;
if (state.isOf(Blocks.NETHER_QUARTZ_ORE)) return 0xFFFFEEEE;
return 0;
}
private record OreMarker(BlockPos pos, int color) {
}
}