| 1 | package net.minecraft.client.renderer.debug;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.gui.components.debug.DebugScreenEntries;
|
| 11 | import net.minecraft.client.renderer.culling.Frustum;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.util.debug.DebugValueAccess;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.EntitySelector;
|
| 16 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 17 | import net.minecraft.world.level.LightLayer;
|
| 18 | import net.minecraft.world.phys.AABB;
|
| 19 | import net.minecraft.world.phys.EntityHitResult;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class DebugRenderer {
|
| 25 | private final List<DebugRenderer.SimpleDebugRenderer> renderers = new ArrayList<>();
|
| 26 | private long lastDebugEntriesVersion;
|
| 27 |
|
| 28 | public DebugRenderer() {
|
| 29 | this.refreshRendererList();
|
| 30 | }
|
| 31 |
|
| 32 | public void refreshRendererList() {
|
| 33 | Minecraft minecraft = Minecraft.getInstance();
|
| 34 | this.renderers.clear();
|
| 35 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.CHUNK_BORDERS)) {
|
| 36 | this.renderers.add(new ChunkBorderRenderer(minecraft));
|
| 37 | }
|
| 38 |
|
| 39 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.CHUNK_SECTION_OCTREE)) {
|
| 40 | this.renderers.add(new OctreeDebugRenderer(minecraft));
|
| 41 | }
|
| 42 |
|
| 43 | if (SharedConstants.DEBUG_PATHFINDING) {
|
| 44 | this.renderers.add(new PathfindingRenderer());
|
| 45 | }
|
| 46 |
|
| 47 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_WATER_LEVELS)) {
|
| 48 | this.renderers.add(new WaterDebugRenderer(minecraft));
|
| 49 | }
|
| 50 |
|
| 51 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_HEIGHTMAP)) {
|
| 52 | this.renderers.add(new HeightMapRenderer(minecraft));
|
| 53 | }
|
| 54 |
|
| 55 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_COLLISION_BOXES)) {
|
| 56 | this.renderers.add(new CollisionBoxRenderer(minecraft));
|
| 57 | }
|
| 58 |
|
| 59 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_ENTITY_SUPPORTING_BLOCKS)) {
|
| 60 | this.renderers.add(new SupportBlockRenderer(minecraft));
|
| 61 | }
|
| 62 |
|
| 63 | if (SharedConstants.DEBUG_NEIGHBORSUPDATE) {
|
| 64 | this.renderers.add(new NeighborsUpdateRenderer());
|
| 65 | }
|
| 66 |
|
| 67 | if (SharedConstants.DEBUG_EXPERIMENTAL_REDSTONEWIRE_UPDATE_ORDER) {
|
| 68 | this.renderers.add(new RedstoneWireOrientationsRenderer());
|
| 69 | }
|
| 70 |
|
| 71 | if (SharedConstants.DEBUG_STRUCTURES) {
|
| 72 | this.renderers.add(new StructureRenderer());
|
| 73 | }
|
| 74 |
|
| 75 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_BLOCK_LIGHT_LEVELS)
|
| 76 | || minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_SKY_LIGHT_LEVELS)) {
|
| 77 | this.renderers
|
| 78 | .add(
|
| 79 | new LightDebugRenderer(
|
| 80 | minecraft,
|
| 81 | minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_BLOCK_LIGHT_LEVELS),
|
| 82 | minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_SKY_LIGHT_LEVELS)
|
| 83 | )
|
| 84 | );
|
| 85 | }
|
| 86 |
|
| 87 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_SOLID_FACES)) {
|
| 88 | this.renderers.add(new SolidFaceRenderer(minecraft));
|
| 89 | }
|
| 90 |
|
| 91 | if (SharedConstants.DEBUG_VILLAGE_SECTIONS) {
|
| 92 | this.renderers.add(new VillageSectionsDebugRenderer());
|
| 93 | }
|
| 94 |
|
| 95 | if (SharedConstants.DEBUG_BRAIN) {
|
| 96 | this.renderers.add(new BrainDebugRenderer(minecraft));
|
| 97 | }
|
| 98 |
|
| 99 | if (SharedConstants.DEBUG_POI) {
|
| 100 | this.renderers.add(new PoiDebugRenderer(new BrainDebugRenderer(minecraft)));
|
| 101 | }
|
| 102 |
|
| 103 | if (SharedConstants.DEBUG_BEES) {
|
| 104 | this.renderers.add(new BeeDebugRenderer(minecraft));
|
| 105 | }
|
| 106 |
|
| 107 | if (SharedConstants.DEBUG_RAIDS) {
|
| 108 | this.renderers.add(new RaidDebugRenderer(minecraft));
|
| 109 | }
|
| 110 |
|
| 111 | if (SharedConstants.DEBUG_GOAL_SELECTOR) {
|
| 112 | this.renderers.add(new GoalSelectorDebugRenderer(minecraft));
|
| 113 | }
|
| 114 |
|
| 115 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_CHUNKS_ON_SERVER)) {
|
| 116 | this.renderers.add(new ChunkDebugRenderer(minecraft));
|
| 117 | }
|
| 118 |
|
| 119 | if (SharedConstants.DEBUG_GAME_EVENT_LISTENERS) {
|
| 120 | this.renderers.add(new GameEventListenerRenderer());
|
| 121 | }
|
| 122 |
|
| 123 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_SKY_LIGHT_SECTIONS)) {
|
| 124 | this.renderers.add(new LightSectionDebugRenderer(minecraft, LightLayer.SKY));
|
| 125 | }
|
| 126 |
|
| 127 | if (SharedConstants.DEBUG_BREEZE_MOB) {
|
| 128 | this.renderers.add(new BreezeDebugRenderer(minecraft));
|
| 129 | }
|
| 130 |
|
| 131 | if (SharedConstants.DEBUG_ENTITY_BLOCK_INTERSECTION) {
|
| 132 | this.renderers.add(new EntityBlockIntersectionDebugRenderer());
|
| 133 | }
|
| 134 |
|
| 135 | if (minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.ENTITY_HITBOXES)) {
|
| 136 | this.renderers.add(new EntityHitboxDebugRenderer(minecraft));
|
| 137 | }
|
| 138 |
|
| 139 | this.renderers.add(new ChunkCullingDebugRenderer(minecraft));
|
| 140 | }
|
| 141 |
|
| 142 | public void emitGizmos(final Frustum frustum, final double camX, final double camY, final double camZ, final float partialTicks) {
|
| 143 | Minecraft minecraft = Minecraft.getInstance();
|
| 144 | DebugValueAccess debugValues = minecraft.getConnection().createDebugValueAccess();
|
| 145 | if (minecraft.debugEntries.getCurrentlyEnabledVersion() != this.lastDebugEntriesVersion) {
|
| 146 | this.lastDebugEntriesVersion = minecraft.debugEntries.getCurrentlyEnabledVersion();
|
| 147 | this.refreshRendererList();
|
| 148 | }
|
| 149 |
|
| 150 | for (DebugRenderer.SimpleDebugRenderer renderer : this.renderers) {
|
| 151 | renderer.emitGizmos(camX, camY, camZ, debugValues, frustum, partialTicks);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | public static Optional<Entity> getTargetedEntity(final @Nullable Entity cameraEntity, final int maxTargetingRange) {
|
| 156 | if (cameraEntity == null) {
|
| 157 | return Optional.empty();
|
| 158 | } else {
|
| 159 | Vec3 from = cameraEntity.getEyePosition();
|
| 160 | Vec3 pick = cameraEntity.getViewVector(1.0F).scale(maxTargetingRange);
|
| 161 | Vec3 to = from.add(pick);
|
| 162 | AABB box = cameraEntity.getBoundingBox().expandTowards(pick).inflate(1.0);
|
| 163 | int rangeSquared = maxTargetingRange * maxTargetingRange;
|
| 164 | EntityHitResult hitResult = ProjectileUtil.getEntityHitResult(cameraEntity, from, to, box, EntitySelector.CAN_BE_PICKED, rangeSquared);
|
| 165 | if (hitResult == null) {
|
| 166 | return Optional.empty();
|
| 167 | } else {
|
| 168 | return from.distanceToSqr(hitResult.getLocation()) > rangeSquared ? Optional.empty() : Optional.of(hitResult.getEntity());
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private static Vec3 mixColor(final float hueShift) {
|
| 174 | float regions = 5.99999F;
|
| 175 | int region = (int)(Mth.clamp(hueShift, 0.0F, 1.0F) * 5.99999F);
|
| 176 | float progress = hueShift * 5.99999F - region;
|
| 177 |
|
| 178 | return switch (region) {
|
| 179 | case 0 -> new Vec3(1.0, progress, 0.0);
|
| 180 | case 1 -> new Vec3(1.0F - progress, 1.0, 0.0);
|
| 181 | case 2 -> new Vec3(0.0, 1.0, progress);
|
| 182 | case 3 -> new Vec3(0.0, 1.0 - progress, 1.0);
|
| 183 | case 4 -> new Vec3(progress, 0.0, 1.0);
|
| 184 | case 5 -> new Vec3(1.0, 0.0, 1.0 - progress);
|
| 185 | default -> throw new IllegalStateException("Unexpected value: " + region);
|
| 186 | };
|
| 187 | }
|
| 188 |
|
| 189 | private static Vec3 shiftHue(final float r, final float g, final float b, final float hs) {
|
| 190 | Vec3 rshifted = mixColor(hs).scale(r);
|
| 191 | Vec3 gshifted = mixColor((hs + 0.33333334F) % 1.0F).scale(g);
|
| 192 | Vec3 bshifted = mixColor((hs + 0.6666667F) % 1.0F).scale(b);
|
| 193 | Vec3 combined = rshifted.add(gshifted).add(bshifted);
|
| 194 | double max = Math.max(Math.max(1.0, combined.x), Math.max(combined.y, combined.z));
|
| 195 | return new Vec3(combined.x / max, combined.y / max, combined.z / max);
|
| 196 | }
|
| 197 |
|
| 198 | @Environment(EnvType.CLIENT)
|
| 199 | public interface SimpleDebugRenderer {
|
| 200 | void emitGizmos(double camX, double camY, double camZ, DebugValueAccess debugValues, final Frustum frustum, final float partialTicks);
|
| 201 | }
|
| 202 | }
|
| 203 |
|