| 1 | package net.minecraft.client.renderer.debug;
|
| 2 |
|
| 3 | import java.util.Locale;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.renderer.culling.Frustum;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.gizmos.GizmoStyle;
|
| 9 | import net.minecraft.gizmos.Gizmos;
|
| 10 | import net.minecraft.gizmos.TextGizmo.Style;
|
| 11 | import net.minecraft.util.ARGB;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.util.debug.DebugSubscriptions;
|
| 14 | import net.minecraft.util.debug.DebugValueAccess;
|
| 15 | import net.minecraft.world.level.pathfinder.Node;
|
| 16 | import net.minecraft.world.level.pathfinder.Path;
|
| 17 | import net.minecraft.world.level.pathfinder.Path.DebugData;
|
| 18 | import net.minecraft.world.phys.AABB;
|
| 19 | import net.minecraft.world.phys.Vec3;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class PathfindingRenderer implements DebugRenderer.SimpleDebugRenderer {
|
| 23 | private static final float MAX_RENDER_DIST = 80.0F;
|
| 24 | private static final int MAX_TARGETING_DIST = 8;
|
| 25 | private static final boolean SHOW_ONLY_SELECTED = false;
|
| 26 | private static final boolean SHOW_OPEN_CLOSED = true;
|
| 27 | private static final boolean SHOW_OPEN_CLOSED_COST_MALUS = false;
|
| 28 | private static final boolean SHOW_OPEN_CLOSED_NODE_TYPE_WITH_TEXT = false;
|
| 29 | private static final boolean SHOW_OPEN_CLOSED_NODE_TYPE_WITH_BOX = true;
|
| 30 | private static final boolean SHOW_GROUND_LABELS = true;
|
| 31 | private static final float TEXT_SCALE = 0.32F;
|
| 32 |
|
| 33 | @Override
|
| 34 | public void emitGizmos(
|
| 35 | final double camX, final double camY, final double camZ, final DebugValueAccess debugValues, final Frustum frustum, final float partialTicks
|
| 36 | ) {
|
| 37 | debugValues.forEachEntity(DebugSubscriptions.ENTITY_PATHS, (entity, info) -> renderPath(camX, camY, camZ, info.path(), info.maxNodeDistance()));
|
| 38 | }
|
| 39 |
|
| 40 | private static void renderPath(final double camX, final double camY, final double camZ, final Path path, final float maxNodeDistance) {
|
| 41 | renderPath(path, maxNodeDistance, true, true, camX, camY, camZ);
|
| 42 | }
|
| 43 |
|
| 44 | public static void renderPath(
|
| 45 | final Path path,
|
| 46 | final float maxNodeDistance,
|
| 47 | final boolean renderOpenAndClosedSets,
|
| 48 | final boolean renderGroundLabels,
|
| 49 | final double camX,
|
| 50 | final double camY,
|
| 51 | final double camZ
|
| 52 | ) {
|
| 53 | renderPathLine(path, camX, camY, camZ);
|
| 54 | BlockPos pos = path.getTarget();
|
| 55 | if (distanceToCamera(pos, camX, camY, camZ) <= 80.0F) {
|
| 56 | Gizmos.cuboid(
|
| 57 | new AABB(pos.getX() + 0.25F, pos.getY() + 0.25F, pos.getZ() + 0.25, pos.getX() + 0.75F, pos.getY() + 0.75F, pos.getZ() + 0.75F),
|
| 58 | GizmoStyle.fill(ARGB.colorFromFloat(0.5F, 0.0F, 1.0F, 0.0F))
|
| 59 | );
|
| 60 |
|
| 61 | for (int i = 0; i < path.getNodeCount(); i++) {
|
| 62 | Node n = path.getNode(i);
|
| 63 | if (distanceToCamera(n.asBlockPos(), camX, camY, camZ) <= 80.0F) {
|
| 64 | float r = i == path.getNextNodeIndex() ? 1.0F : 0.0F;
|
| 65 | float b = i == path.getNextNodeIndex() ? 0.0F : 1.0F;
|
| 66 | AABB aabb = new AABB(
|
| 67 | n.x + 0.5F - maxNodeDistance,
|
| 68 | n.y + 0.01F * i,
|
| 69 | n.z + 0.5F - maxNodeDistance,
|
| 70 | n.x + 0.5F + maxNodeDistance,
|
| 71 | n.y + 0.25F + 0.01F * i,
|
| 72 | n.z + 0.5F + maxNodeDistance
|
| 73 | );
|
| 74 | Gizmos.cuboid(aabb, GizmoStyle.fill(ARGB.colorFromFloat(0.5F, r, 0.0F, b)));
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | DebugData debugData = path.debugData();
|
| 80 | if (renderOpenAndClosedSets && debugData != null) {
|
| 81 | for (Node node : debugData.closedSet()) {
|
| 82 | if (distanceToCamera(node.asBlockPos(), camX, camY, camZ) <= 80.0F) {
|
| 83 | Gizmos.cuboid(
|
| 84 | new AABB(
|
| 85 | node.x + 0.5F - maxNodeDistance / 2.0F,
|
| 86 | node.y + 0.01F,
|
| 87 | node.z + 0.5F - maxNodeDistance / 2.0F,
|
| 88 | node.x + 0.5F + maxNodeDistance / 2.0F,
|
| 89 | node.y + 0.1,
|
| 90 | node.z + 0.5F + maxNodeDistance / 2.0F
|
| 91 | ),
|
| 92 | GizmoStyle.fill(ARGB.colorFromFloat(0.5F, 1.0F, 0.8F, 0.8F))
|
| 93 | );
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | for (Node node : debugData.openSet()) {
|
| 98 | if (distanceToCamera(node.asBlockPos(), camX, camY, camZ) <= 80.0F) {
|
| 99 | Gizmos.cuboid(
|
| 100 | new AABB(
|
| 101 | node.x + 0.5F - maxNodeDistance / 2.0F,
|
| 102 | node.y + 0.01F,
|
| 103 | node.z + 0.5F - maxNodeDistance / 2.0F,
|
| 104 | node.x + 0.5F + maxNodeDistance / 2.0F,
|
| 105 | node.y + 0.1,
|
| 106 | node.z + 0.5F + maxNodeDistance / 2.0F
|
| 107 | ),
|
| 108 | GizmoStyle.fill(ARGB.colorFromFloat(0.5F, 0.8F, 1.0F, 1.0F))
|
| 109 | );
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | if (renderGroundLabels) {
|
| 115 | for (int i = 0; i < path.getNodeCount(); i++) {
|
| 116 | Node n = path.getNode(i);
|
| 117 | if (distanceToCamera(n.asBlockPos(), camX, camY, camZ) <= 80.0F) {
|
| 118 | Gizmos.billboardText(String.valueOf(n.type), new Vec3(n.x + 0.5, n.y + 0.75, n.z + 0.5), Style.whiteAndCentered().withScale(0.32F)).setAlwaysOnTop();
|
| 119 | Gizmos.billboardText(
|
| 120 | String.format(Locale.ROOT, "%.2f", n.costMalus), new Vec3(n.x + 0.5, n.y + 0.25, n.z + 0.5), Style.whiteAndCentered().withScale(0.32F)
|
| 121 | )
|
| 122 | .setAlwaysOnTop();
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | public static void renderPathLine(final Path path, final double camX, final double camY, final double camZ) {
|
| 129 | if (path.getNodeCount() >= 2) {
|
| 130 | Vec3 last = path.getNode(0).asVec3();
|
| 131 |
|
| 132 | for (int i = 1; i < path.getNodeCount(); i++) {
|
| 133 | Node n = path.getNode(i);
|
| 134 | if (distanceToCamera(n.asBlockPos(), camX, camY, camZ) > 80.0F) {
|
| 135 | last = n.asVec3();
|
| 136 | } else {
|
| 137 | float hue = (float)i / path.getNodeCount() * 0.33F;
|
| 138 | int color = ARGB.opaque(Mth.hsvToRgb(hue, 0.9F, 0.9F));
|
| 139 | Gizmos.arrow(last.add(0.5, 0.5, 0.5), n.asVec3().add(0.5, 0.5, 0.5), color);
|
| 140 | last = n.asVec3();
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | private static float distanceToCamera(final BlockPos n, final double camX, final double camY, final double camZ) {
|
| 147 | return (float)(Math.abs(n.getX() - camX) + Math.abs(n.getY() - camY) + Math.abs(n.getZ() - camZ));
|
| 148 | }
|
| 149 | }
|
| 150 |
|