EntityHitboxDebugRenderer .java

net.minecraft.client.renderer.debug.EntityHitboxDebugRenderer (class, 119 lines): members: frustum, emitGizmos, minecraft. imports: CameraType, GizmoStyle.

net.minecraft.client.renderer.debugclass119 linesclient4.8 KB

Implements

API Surface (1)

void emitGizmos(final double camX, final double camY, final double camZ, final DebugValueAccess debugValues, final Frustum frustum, final float partialTicks)

Fields & Constants (2)

minecraft
frustum

String literals (1)

"Missing Server Entity"

Full Source

EntityHitboxDebugRenderer.java119 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.debug;
2
3
import net.fabricmc.api.EnvType;
4
import net.fabricmc.api.Environment;
5
import net.minecraft.SharedConstants;
6
import net.minecraft.client.CameraType;
7
import net.minecraft.client.Minecraft;
8
import net.minecraft.client.renderer.culling.Frustum;
9
import net.minecraft.client.server.IntegratedServer;
10
import net.minecraft.gizmos.GizmoStyle;
11
import net.minecraft.gizmos.Gizmos;
12
import net.minecraft.gizmos.TextGizmo.Style;
13
import net.minecraft.server.level.ServerLevel;
14
import net.minecraft.util.ARGB;
15
import net.minecraft.util.Util;
16
import net.minecraft.util.debug.DebugValueAccess;
17
import net.minecraft.world.entity.Entity;
18
import net.minecraft.world.entity.LivingEntity;
19
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
20
import net.minecraft.world.entity.boss.enderdragon.EnderDragonPart;
21
import net.minecraft.world.phys.AABB;
22
import net.minecraft.world.phys.Vec3;
23
import org.jspecify.annotations.Nullable;
24
25
@Environment(EnvType.CLIENT)
26
public class EntityHitboxDebugRenderer implements DebugRenderer.SimpleDebugRenderer {
27
	private final Minecraft minecraft;
28
29
	public EntityHitboxDebugRenderer(final Minecraft minecraft) {
30
		this.minecraft = minecraft;
31
	}
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
		if (this.minecraft.level != null) {
38
			long chunkFadeDuration = Util.toMillis(this.minecraft.options.chunkSectionFadeInTime().get());
39
40
			for (Entity entity : this.minecraft.level.entitiesForRendering()) {
41
				if (!entity.isInvisible()) {
42
					float entityPartialTicks = this.minecraft.getDeltaTracker().getGameTimeDeltaPartialTick(!this.minecraft.level.tickRateManager().isEntityFrozen(entity));
43
					if (this.minecraft.levelExtractor.isEntityVisible(entity, frustum, camX, camY, camZ, entityPartialTicks, chunkFadeDuration)
44
						&& (entity != this.minecraft.getCameraEntity() || this.minecraft.options.getCameraType() != CameraType.FIRST_PERSON)) {
45
						this.showHitboxes(entity, entityPartialTicks, false);
46
						if (SharedConstants.DEBUG_SHOW_LOCAL_SERVER_ENTITY_HIT_BOXES) {
47
							Entity serverEntity = this.getServerEntity(entity);
48
							if (serverEntity != null) {
49
								this.showHitboxes(entity, entityPartialTicks, true);
50
							} else {
51
								Gizmos.billboardText(
52
									"Missing Server Entity",
53
									entity.getPosition(entityPartialTicks).add(0.0, entity.getBoundingBox().getYsize() + 1.5, 0.0),
54
									Style.forColorAndCentered(-65536)
55
								);
56
							}
57
						}
58
					}
59
				}
60
			}
61
		}
62
	}
63
64
	private @Nullable Entity getServerEntity(final Entity entity) {
65
		IntegratedServer server = this.minecraft.getSingleplayerServer();
66
		if (server != null) {
67
			ServerLevel level = server.getLevel(entity.level().dimension());
68
			if (level != null) {
69
				return level.getEntity(entity.getId());
70
			}
71
		}
72
73
		return null;
74
	}
75
76
	private void showHitboxes(final Entity entity, final float partialTicks, final boolean isServerEntity) {
77
		Vec3 latestPosition = entity.position();
78
		Vec3 currentPosition = entity.getPosition(partialTicks);
79
		Vec3 offset = currentPosition.subtract(latestPosition);
80
		int mainColor = isServerEntity ? -16711936 : -1;
81
		Gizmos.cuboid(entity.getBoundingBox().move(offset), GizmoStyle.stroke(mainColor));
82
		Gizmos.point(currentPosition, mainColor, 2.0F);
83
		Entity vehicle = entity.getVehicle();
84
		if (vehicle != null) {
85
			float width = Math.min(vehicle.getBbWidth(), entity.getBbWidth()) / 2.0F;
86
			float height = 0.0625F;
87
			Vec3 position = vehicle.getPassengerRidingPosition(entity).add(offset);
88
			Gizmos.cuboid(
89
				new AABB(position.x - width, position.y, position.z - width, position.x + width, position.y + 0.0625, position.z + width), GizmoStyle.stroke(-256)
90
			);
91
		}
92
93
		if (entity instanceof LivingEntity) {
94
			AABB bb = entity.getBoundingBox().move(offset);
95
			float padding = 0.01F;
96
			Gizmos.cuboid(
97
				new AABB(bb.minX, bb.minY + entity.getEyeHeight() - 0.01F, bb.minZ, bb.maxX, bb.minY + entity.getEyeHeight() + 0.01F, bb.maxZ), GizmoStyle.stroke(-65536)
98
			);
99
		}
100
101
		if (entity instanceof EnderDragon dragon) {
102
			for (EnderDragonPart subEntity : dragon.getSubEntities()) {
103
				Vec3 latestSubPosition = subEntity.position();
104
				Vec3 currentSubPosition = subEntity.getPosition(partialTicks);
105
				Vec3 subOffset = currentSubPosition.subtract(latestSubPosition);
106
				Gizmos.cuboid(subEntity.getBoundingBox().move(subOffset), GizmoStyle.stroke(ARGB.colorFromFloat(1.0F, 0.25F, 1.0F, 0.0F)));
107
			}
108
		}
109
110
		Vec3 eyePosition = currentPosition.add(0.0, entity.getEyeHeight(), 0.0);
111
		Vec3 viewVector = entity.getViewVector(partialTicks);
112
		Gizmos.arrow(eyePosition, eyePosition.add(viewVector.scale(2.0)), -16776961);
113
		if (isServerEntity) {
114
			Vec3 deltaMovement = entity.getDeltaMovement();
115
			Gizmos.arrow(currentPosition, currentPosition.add(deltaMovement), -256);
116
		}
117
	}
118
}
119

Decompiled Minecraft 26.3 source of EntityHitboxDebugRenderer (net/minecraft/client/renderer/debug/EntityHitboxDebugRenderer.java) with Mojang official mappings, stored verbatim on the site.