| 1 | package net.minecraft.client.renderer.debug;
|
| 2 |
|
| 3 | import java.time.Duration;
|
| 4 | import java.time.Instant;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.renderer.culling.Frustum;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.SectionPos;
|
| 11 | import net.minecraft.gizmos.GizmoStyle;
|
| 12 | import net.minecraft.gizmos.Gizmos;
|
| 13 | import net.minecraft.util.ARGB;
|
| 14 | import net.minecraft.util.debug.DebugValueAccess;
|
| 15 | import net.minecraft.world.level.LightLayer;
|
| 16 | import net.minecraft.world.level.lighting.LevelLightEngine;
|
| 17 | import net.minecraft.world.level.lighting.LayerLightSectionStorage.SectionType;
|
| 18 | import net.minecraft.world.phys.Vec3;
|
| 19 | import net.minecraft.world.phys.shapes.BitSetDiscreteVoxelShape;
|
| 20 | import net.minecraft.world.phys.shapes.DiscreteVoxelShape;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class LightSectionDebugRenderer implements DebugRenderer.SimpleDebugRenderer {
|
| 25 | private static final Duration REFRESH_INTERVAL = Duration.ofMillis(500L);
|
| 26 | private static final int RADIUS = 10;
|
| 27 | private static final int LIGHT_AND_BLOCKS_COLOR = ARGB.colorFromFloat(0.25F, 1.0F, 1.0F, 0.0F);
|
| 28 | private static final int LIGHT_ONLY_COLOR = ARGB.colorFromFloat(0.125F, 0.25F, 0.125F, 0.0F);
|
| 29 | private final Minecraft minecraft;
|
| 30 | private final LightLayer lightLayer;
|
| 31 | private Instant lastUpdateTime = Instant.now();
|
| 32 | private LightSectionDebugRenderer.@Nullable SectionData data;
|
| 33 |
|
| 34 | public LightSectionDebugRenderer(final Minecraft minecraft, final LightLayer lightLayer) {
|
| 35 | this.minecraft = minecraft;
|
| 36 | this.lightLayer = lightLayer;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void emitGizmos(
|
| 41 | final double camX, final double camY, final double camZ, final DebugValueAccess debugValues, final Frustum frustum, final float partialTicks
|
| 42 | ) {
|
| 43 | Instant time = Instant.now();
|
| 44 | if (this.data == null || Duration.between(this.lastUpdateTime, time).compareTo(REFRESH_INTERVAL) > 0) {
|
| 45 | this.lastUpdateTime = time;
|
| 46 | this.data = new LightSectionDebugRenderer.SectionData(
|
| 47 | this.minecraft.level.getLightEngine(), SectionPos.of(this.minecraft.player.blockPosition()), 10, this.lightLayer
|
| 48 | );
|
| 49 | }
|
| 50 |
|
| 51 | renderEdges(this.data.lightAndBlocksShape, this.data.minPos, LIGHT_AND_BLOCKS_COLOR);
|
| 52 | renderEdges(this.data.lightShape, this.data.minPos, LIGHT_ONLY_COLOR);
|
| 53 | renderFaces(this.data.lightAndBlocksShape, this.data.minPos, LIGHT_AND_BLOCKS_COLOR);
|
| 54 | renderFaces(this.data.lightShape, this.data.minPos, LIGHT_ONLY_COLOR);
|
| 55 | }
|
| 56 |
|
| 57 | private static void renderFaces(final DiscreteVoxelShape shape, final SectionPos minSection, final int color) {
|
| 58 | shape.forAllFaces((direction, x, y, z) -> {
|
| 59 | int sectionX = x + minSection.getX();
|
| 60 | int sectionY = y + minSection.getY();
|
| 61 | int sectionZ = z + minSection.getZ();
|
| 62 | renderFace(direction, sectionX, sectionY, sectionZ, color);
|
| 63 | });
|
| 64 | }
|
| 65 |
|
| 66 | private static void renderEdges(final DiscreteVoxelShape shape, final SectionPos minSection, final int color) {
|
| 67 | shape.forAllEdges((x0, y0, z0, x1, y1, z1) -> {
|
| 68 | int sectionX0 = x0 + minSection.getX();
|
| 69 | int sectionY0 = y0 + minSection.getY();
|
| 70 | int sectionZ0 = z0 + minSection.getZ();
|
| 71 | int sectionX1 = x1 + minSection.getX();
|
| 72 | int sectionY1 = y1 + minSection.getY();
|
| 73 | int sectionZ1 = z1 + minSection.getZ();
|
| 74 | renderEdge(sectionX0, sectionY0, sectionZ0, sectionX1, sectionY1, sectionZ1, color);
|
| 75 | }, true);
|
| 76 | }
|
| 77 |
|
| 78 | private static void renderFace(final Direction direction, final int sectionX, final int sectionY, final int sectionZ, final int color) {
|
| 79 | Vec3 cuboidCornerA = new Vec3(SectionPos.sectionToBlockCoord(sectionX), SectionPos.sectionToBlockCoord(sectionY), SectionPos.sectionToBlockCoord(sectionZ));
|
| 80 | Vec3 cuboidCornerB = cuboidCornerA.add(16.0, 16.0, 16.0);
|
| 81 | Gizmos.rect(cuboidCornerA, cuboidCornerB, direction, GizmoStyle.fill(color));
|
| 82 | }
|
| 83 |
|
| 84 | private static void renderEdge(
|
| 85 | final int sectionX0, final int sectionY0, final int sectionZ0, final int sectionX1, final int sectionY1, final int sectionZ1, final int color
|
| 86 | ) {
|
| 87 | double x0 = SectionPos.sectionToBlockCoord(sectionX0);
|
| 88 | double y0 = SectionPos.sectionToBlockCoord(sectionY0);
|
| 89 | double z0 = SectionPos.sectionToBlockCoord(sectionZ0);
|
| 90 | double x1 = SectionPos.sectionToBlockCoord(sectionX1);
|
| 91 | double y1 = SectionPos.sectionToBlockCoord(sectionY1);
|
| 92 | double z1 = SectionPos.sectionToBlockCoord(sectionZ1);
|
| 93 | int opaqueColor = ARGB.opaque(color);
|
| 94 | Gizmos.line(new Vec3(x0, y0, z0), new Vec3(x1, y1, z1), opaqueColor);
|
| 95 | }
|
| 96 |
|
| 97 | @Environment(EnvType.CLIENT)
|
| 98 | private static final class SectionData {
|
| 99 | private final DiscreteVoxelShape lightAndBlocksShape;
|
| 100 | private final DiscreteVoxelShape lightShape;
|
| 101 | private final SectionPos minPos;
|
| 102 |
|
| 103 | private SectionData(final LevelLightEngine engine, final SectionPos centerPos, final int radius, final LightLayer lightLayer) {
|
| 104 | int size = radius * 2 + 1;
|
| 105 | this.lightAndBlocksShape = new BitSetDiscreteVoxelShape(size, size, size);
|
| 106 | this.lightShape = new BitSetDiscreteVoxelShape(size, size, size);
|
| 107 |
|
| 108 | for (int z = 0; z < size; z++) {
|
| 109 | for (int y = 0; y < size; y++) {
|
| 110 | for (int x = 0; x < size; x++) {
|
| 111 | SectionPos pos = SectionPos.of(centerPos.x() + x - radius, centerPos.y() + y - radius, centerPos.z() + z - radius);
|
| 112 | SectionType type = engine.getDebugSectionType(lightLayer, pos);
|
| 113 | if (type == SectionType.LIGHT_AND_DATA) {
|
| 114 | this.lightAndBlocksShape.fill(x, y, z);
|
| 115 | this.lightShape.fill(x, y, z);
|
| 116 | } else if (type == SectionType.LIGHT_ONLY) {
|
| 117 | this.lightShape.fill(x, y, z);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | this.minPos = SectionPos.of(centerPos.x() - radius, centerPos.y() - radius, centerPos.z() - radius);
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|