Skip to content

Commit

Permalink
Finish light level render
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Jan 3, 2024
1 parent d73ef88 commit e69f141
Showing 1 changed file with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,84 @@
import net.minecraft.block.Blocks;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.LightType;

import java.util.HashSet;
import java.util.Optional;

import static com.connorcode.sigmautils.SigmaUtils.client;
import static com.connorcode.sigmautils.misc.util.WorldRenderUtils.getMatrices;

@ModuleInfo(description = "Shows the light level the top of blocks")
public class LightLevel extends Module {
EnumSetting<LightSource> source = new EnumSetting<LightSource>(LightLevel.class, "Light Source", LightSource.class).value(LightSource.All).build();
EnumSetting<Coloring> coloring = new EnumSetting<Coloring>(LightLevel.class, "Coloring", Coloring.class).value(Coloring.Hue).build();
NumberSetting scale = new NumberSetting(LightLevel.class, "Scale", 0.5, 5).value(2).build();
EnumSetting<LightSource> source = new EnumSetting<>(LightLevel.class, "Light Source", LightSource.class).value(LightSource.All).build();
EnumSetting<Coloring> coloring = new EnumSetting<>(LightLevel.class, "Coloring", Coloring.class).value(Coloring.Hue).build();
NumberSetting scale = new NumberSetting(LightLevel.class, "Scale", 0.5, 3).value(2).build();
NumberSetting rangeH = new NumberSetting(LightLevel.class, "Horizontal Range", 5, 25).value(5).precision(0).build();
NumberSetting rangeV = new NumberSetting(LightLevel.class, "Vertical Range", 0, 10).value(5).precision(0).build();

BlockPos centerPos;
HashSet<BlockPos> positions = new HashSet<>();

@EventHandler
void onRender(WorldRender.PostWorldRenderEvent event) {
if (!enabled || client.player == null || client.world == null) return;

var profiler = client.getProfiler();
profiler.push("SigmaUtils::LightLevel::Render");

var blockPos = client.player.getBlockPos();
var pos = blockPos.toCenterPos().subtract(new Vec3d(0, .5 - .01, 0));
updatePositions();

var immediate = client.getBufferBuilders().getEntityVertexConsumers();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();

for (var pos : positions) {
var light = lightLevel(pos);
var color = getColor(light);
if (color.isEmpty()) continue;

var width = client.textRenderer.getWidth(Integer.toString(light));
var text = Integer.toString(light);
var matrix = getFloorMatrices(pos.toCenterPos().subtract(0, .5 - 0.01, 0));
client.textRenderer.draw(text,
width / -2f,
client.textRenderer.fontHeight / -2f,
color.get(),
false,
matrix.peek().getPositionMatrix(),
immediate,
TextRenderer.TextLayerType.NORMAL,
0,
255,
false);
}

immediate.draw();
RenderSystem.disableBlend();
profiler.pop();
}

void updatePositions() {
assert client.player != null;
var currentPos = client.player.getBlockPos();
if (centerPos.equals(currentPos)) return;
centerPos = currentPos;
positions.clear();

for (var x = -rangeH.intValue(); x <= rangeH.intValue(); x++) {
for (var z = -rangeH.intValue(); z <= rangeH.intValue(); z++) {
for (var y = -rangeV.intValue(); y <= rangeV.intValue(); y++) {
var thisPos = blockPos.add(x, y, z);
var thisPos = currentPos.add(x, y, z);
if (isTransparent(thisPos.down()) || !isTransparent(thisPos)) continue;

var light = lightLevel(thisPos);
var color = getColor(light);
if (color.isEmpty()) continue;

var width = client.textRenderer.getWidth(Integer.toString(light));
var text = Integer.toString(light);
var matrix = getFloorMatrices(pos.add(x, y, z));
client.textRenderer.draw(text, width / -2f, 0f, color.get(), false, matrix.peek().getPositionMatrix(), immediate, TextRenderer.TextLayerType.NORMAL, 0, 255, false);
positions.add(thisPos);
}
}
}

immediate.draw();
RenderSystem.disableBlend();
profiler.pop();
}

boolean isTransparent(BlockPos pos) {
Expand All @@ -74,9 +98,10 @@ boolean isTransparent(BlockPos pos) {

MatrixStack getFloorMatrices(Vec3d pos) {
var _scale = (float) scale.value();
var matrix = getMatrices(pos); // TODO: Remove this
var camYaw = client.gameRenderer.getCamera().getYaw() + 45;
var matrix = getMatrices(pos);
matrix.multiply(RotationAxis.POSITIVE_X.rotationDegrees(90));
matrix.multiply(RotationAxis.POSITIVE_Z.rotationDegrees((int) (client.gameRenderer.getCamera().getYaw() / 90) * 90));
matrix.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(Math.round(camYaw / 90f) * 90));
matrix.scale(-0.025f * _scale, -0.025f * _scale, 1);
return matrix;
}
Expand All @@ -95,10 +120,16 @@ Optional<Integer> getColor(int light) {
var lightf = MathHelper.clamp(light / 15f, 0, 1);
return switch (coloring.value()) {
case Hue -> Optional.of(MathHelper.hsvToRgb(lightf, 1, 1));
case HostileSpawn -> Optional.ofNullable(light == 1 ? 0xff0000 : null);
case HostileSpawn -> Optional.ofNullable(light <= 1 ? 0xff0000 : null);
};
}

@Override
public NbtCompound saveConfig() {
centerPos = null;
return super.saveConfig();
}

enum Coloring {
Hue, HostileSpawn
}
Expand Down

0 comments on commit e69f141

Please sign in to comment.