LivingEntityRenderer .java

net.minecraft.client.renderer.entity.LivingEntityRenderer (class): members: getOverlayCoords, isUpsideDownName, EYE_BED_OFFSET, addLayer, extractNameTags.

net.minecraft.client.renderer.entityclass324 linesclientextends LivingEntity12.0 KB

Implements

API Surface (19)

boolean addLayer(final RenderLayer<S, M> layer)
M getModel()
AABB getBoundingBoxForCulling(final T entity, final float partialTicks)
void submit(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera)
boolean shouldRenderLayers(final S state)
int getModelTint(final S state)
int getOverlayCoords(final LivingEntityRenderState state, final float whiteOverlayProgress)
boolean isBodyVisible(final S state)
boolean isShaking(final S state)
void setupRotations(final S state, final PoseStack poseStack, float bodyRot, final float entityScale)
float getFlipDegrees()
float getWhiteOverlayProgress(final S state)
void scale(final S state, final PoseStack poseStack)
boolean shouldShowName(final T entity, final double distanceToCameraSq)
boolean isEntityUpsideDown(final T mob)
boolean isUpsideDownName(final String name)
float getShadowRadius(final S state)
void extractRenderState(final T entity, final S state, final float partialTicks)
void extractNameTags(final T entity, final S state, final float partialTicks)

Fields & Constants (4)

EYE_BED_OFFSET
model
itemModelResolver
layers

String literals (2)

"Dinnerbone"
"Grumm"

Full Source

LivingEntityRenderer.java324 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.entity;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.blaze3d.vertex.PoseStack;
5
import com.mojang.math.Axis;
6
import java.util.List;
7
import net.fabricmc.api.EnvType;
8
import net.fabricmc.api.Environment;
9
import net.minecraft.client.Minecraft;
10
import net.minecraft.client.model.EntityModel;
11
import net.minecraft.client.player.LocalPlayer;
12
import net.minecraft.client.renderer.SubmitNodeCollector;
13
import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
14
import net.minecraft.client.renderer.entity.layers.RenderLayer;
15
import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
16
import net.minecraft.client.renderer.item.ItemModelResolver;
17
import net.minecraft.client.renderer.rendertype.RenderType;
18
import net.minecraft.client.renderer.rendertype.RenderTypes;
19
import net.minecraft.client.renderer.state.level.CameraRenderState;
20
import net.minecraft.client.renderer.texture.OverlayTexture;
21
import net.minecraft.core.Direction;
22
import net.minecraft.core.component.DataComponents;
23
import net.minecraft.network.chat.Component;
24
import net.minecraft.resources.Identifier;
25
import net.minecraft.util.ARGB;
26
import net.minecraft.util.Mth;
27
import net.minecraft.world.entity.EquipmentSlot;
28
import net.minecraft.world.entity.LivingEntity;
29
import net.minecraft.world.entity.Pose;
30
import net.minecraft.world.entity.ai.attributes.Attributes;
31
import net.minecraft.world.item.BlockItem;
32
import net.minecraft.world.item.ItemDisplayContext;
33
import net.minecraft.world.item.ItemStack;
34
import net.minecraft.world.item.Items;
35
import net.minecraft.world.level.block.AbstractSkullBlock;
36
import net.minecraft.world.phys.AABB;
37
import net.minecraft.world.scores.Team;
38
import net.minecraft.world.scores.Team.Visibility;
39
import org.jspecify.annotations.Nullable;
40
41
@Environment(EnvType.CLIENT)
42
public abstract class LivingEntityRenderer<T extends LivingEntity, S extends LivingEntityRenderState, M extends EntityModel<? super S>>
43
	extends EntityRenderer<T, S>
44
	implements RenderLayerParent<S, M> {
45
	private static final float EYE_BED_OFFSET = 0.1F;
46
	protected M model;
47
	protected final ItemModelResolver itemModelResolver;
48
	protected final List<RenderLayer<S, M>> layers = Lists.newArrayList();
49
50
	public LivingEntityRenderer(final EntityRendererProvider.Context context, final M model, final float shadow) {
51
		super(context);
52
		this.itemModelResolver = context.getItemModelResolver();
53
		this.model = model;
54
		this.shadowRadius = shadow;
55
	}
56
57
	protected final boolean addLayer(final RenderLayer<S, M> layer) {
58
		return this.layers.add(layer);
59
	}
60
61
	@Override
62
	public M getModel() {
63
		return this.model;
64
	}
65
66
	protected AABB getBoundingBoxForCulling(final T entity, final float partialTicks) {
67
		AABB aabb = super.getBoundingBoxForCulling(entity, partialTicks);
68
		if (entity.getItemBySlot(EquipmentSlot.HEAD).is(Items.DRAGON_HEAD)) {
69
			float extraSize = 0.5F;
70
			return aabb.inflate(0.5, 0.5, 0.5);
71
		} else {
72
			return aabb;
73
		}
74
	}
75
76
	public void submit(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
77
		poseStack.pushPose();
78
		if (state.hasPose(Pose.SLEEPING)) {
79
			Direction bedOrientation = state.bedOrientation;
80
			if (bedOrientation != null) {
81
				float headOffset = state.eyeHeight - 0.1F;
82
				poseStack.translate(-bedOrientation.getStepX() * headOffset, 0.0F, -bedOrientation.getStepZ() * headOffset);
83
			}
84
		}
85
86
		float scale = state.scale;
87
		poseStack.scale(scale, scale, scale);
88
		this.setupRotations(state, poseStack, state.bodyRot, scale);
89
		poseStack.scale(-1.0F, -1.0F, 1.0F);
90
		this.scale(state, poseStack);
91
		poseStack.translate(0.0F, -1.501F, 0.0F);
92
		boolean isBodyVisible = this.isBodyVisible(state);
93
		boolean forceTransparent = !isBodyVisible && !state.isInvisibleToPlayer;
94
		RenderType renderType = this.getRenderType(state, isBodyVisible, forceTransparent, state.appearsGlowing());
95
		if (renderType != null) {
96
			int overlayCoords = getOverlayCoords(state, this.getWhiteOverlayProgress(state));
97
			int baseColor = forceTransparent ? 654311423 : -1;
98
			int tintedColor = ARGB.multiply(baseColor, this.getModelTint(state));
99
			submitNodeCollector.submitModel(this.model, state, poseStack, renderType, state.lightCoords, overlayCoords, tintedColor, null, state.outlineColor);
100
		}
101
102
		if (this.shouldRenderLayers(state) && !this.layers.isEmpty()) {
103
			this.model.setupAnim(state);
104
105
			for (RenderLayer<S, M> layer : this.layers) {
106
				layer.submit(poseStack, submitNodeCollector, state.lightCoords, state, state.yRot, state.xRot);
107
			}
108
		}
109
110
		poseStack.popPose();
111
		super.submit(state, poseStack, submitNodeCollector, camera);
112
	}
113
114
	protected boolean shouldRenderLayers(final S state) {
115
		return true;
116
	}
117
118
	protected int getModelTint(final S state) {
119
		return -1;
120
	}
121
122
	public abstract Identifier getTextureLocation(final S state);
123
124
	protected @Nullable RenderType getRenderType(final S state, final boolean isBodyVisible, final boolean forceTransparent, final boolean appearGlowing) {
125
		Identifier texture = this.getTextureLocation(state);
126
		if (forceTransparent) {
127
			return RenderTypes.entityTranslucentCull(texture);
128
		} else if (isBodyVisible) {
129
			return this.model.renderType(texture);
130
		} else {
131
			return appearGlowing ? RenderTypes.outline(texture) : null;
132
		}
133
	}
134
135
	public static int getOverlayCoords(final LivingEntityRenderState state, final float whiteOverlayProgress) {
136
		return OverlayTexture.pack(OverlayTexture.u(whiteOverlayProgress), OverlayTexture.v(state.hasRedOverlay));
137
	}
138
139
	protected boolean isBodyVisible(final S state) {
140
		return !state.isInvisible;
141
	}
142
143
	private static float sleepDirectionToRotation(final Direction direction) {
144
		return switch (direction) {
145
			case SOUTH -> 90.0F;
146
			case WEST -> 0.0F;
147
			case NORTH -> 270.0F;
148
			case EAST -> 180.0F;
149
			default -> 0.0F;
150
		};
151
	}
152
153
	protected boolean isShaking(final S state) {
154
		return state.isFullyFrozen;
155
	}
156
157
	protected void setupRotations(final S state, final PoseStack poseStack, float bodyRot, final float entityScale) {
158
		if (this.isShaking(state)) {
159
			bodyRot += (float)(Math.cos(Mth.floor(state.ageInTicks) * 3.25F) * Math.PI * 0.4F);
160
		}
161
162
		if (!state.hasPose(Pose.SLEEPING)) {
163
			poseStack.rotateDegrees(Axis.YP, 180.0F - bodyRot);
164
		}
165
166
		if (state.deathTime > 0.0F) {
167
			float fall = (state.deathTime - 1.0F) / 20.0F * 1.6F;
168
			fall = Mth.sqrt(fall);
169
			if (fall > 1.0F) {
170
				fall = 1.0F;
171
			}
172
173
			poseStack.rotateDegrees(Axis.ZP, fall * this.getFlipDegrees());
174
		} else if (state.isAutoSpinAttack) {
175
			poseStack.rotateDegrees(Axis.XP, -90.0F - state.xRot);
176
			poseStack.rotateDegrees(Axis.YP, state.ageInTicks * -75.0F);
177
		} else if (state.hasPose(Pose.SLEEPING)) {
178
			Direction bedOrientation = state.bedOrientation;
179
			float angle = bedOrientation != null ? sleepDirectionToRotation(bedOrientation) : bodyRot;
180
			poseStack.rotateDegrees(Axis.YP, angle);
181
			poseStack.rotateDegrees(Axis.ZP, this.getFlipDegrees());
182
			poseStack.rotateDegrees(Axis.YP, 270.0F);
183
		} else if (state.isUpsideDown) {
184
			poseStack.translate(0.0F, (state.boundingBoxHeight + 0.1F) / entityScale, 0.0F);
185
			poseStack.rotateDegrees(Axis.ZP, 180.0F);
186
		}
187
	}
188
189
	protected float getFlipDegrees() {
190
		return 90.0F;
191
	}
192
193
	protected float getWhiteOverlayProgress(final S state) {
194
		return 0.0F;
195
	}
196
197
	protected void scale(final S state, final PoseStack poseStack) {
198
	}
199
200
	protected boolean shouldShowName(final T entity, final double distanceToCameraSq) {
201
		if (entity.isDiscrete()) {
202
			float maxDist = 32.0F;
203
			if (distanceToCameraSq >= 1024.0) {
204
				return false;
205
			}
206
		}
207
208
		Minecraft minecraft = Minecraft.getInstance();
209
		LocalPlayer player = minecraft.player;
210
		boolean isVisibleToPlayer = !entity.isInvisibleTo(player);
211
		if (entity != player) {
212
			Team team = entity.getTeam();
213
			Team myTeam = player.getTeam();
214
			if (team != null) {
215
				Visibility visibility = team.getNameTagVisibility();
216
217
				return switch (visibility) {
218
					case ALWAYS -> isVisibleToPlayer;
219
					case NEVER -> false;
220
					case HIDE_FOR_OTHER_TEAMS -> myTeam == null ? isVisibleToPlayer : team.isAlliedTo(myTeam) && (team.canSeeFriendlyInvisibles() || isVisibleToPlayer);
221
					case HIDE_FOR_OWN_TEAM -> myTeam == null ? isVisibleToPlayer : !team.isAlliedTo(myTeam) && isVisibleToPlayer;
222
				};
223
			}
224
		}
225
226
		return !Minecraft.getInstance().gui.hud.isHidden() && entity != minecraft.getCameraEntity() && isVisibleToPlayer && !entity.isVehicle();
227
	}
228
229
	public boolean isEntityUpsideDown(final T mob) {
230
		Component customName = mob.getCustomName();
231
		return customName != null && isUpsideDownName(customName.getString());
232
	}
233
234
	protected static boolean isUpsideDownName(final String name) {
235
		return "Dinnerbone".equals(name) || "Grumm".equals(name);
236
	}
237
238
	protected float getShadowRadius(final S state) {
239
		return super.getShadowRadius(state) * state.scale;
240
	}
241
242
	public void extractRenderState(final T entity, final S state, final float partialTicks) {
243
		super.extractRenderState(entity, state, partialTicks);
244
		float headRot = Mth.rotLerp(partialTicks, entity.yHeadRotO, entity.yHeadRot);
245
		state.bodyRot = solveBodyRot(entity, headRot, partialTicks);
246
		state.yRot = Mth.wrapDegrees(headRot - state.bodyRot);
247
		state.xRot = entity.getXRot(partialTicks);
248
		state.isUpsideDown = this.isEntityUpsideDown(entity);
249
		if (state.isUpsideDown) {
250
			state.xRot *= -1.0F;
251
			state.yRot *= -1.0F;
252
		}
253
254
		if (!entity.isPassenger() && entity.isAlive()) {
255
			state.walkAnimationPos = entity.walkAnimation.position(partialTicks);
256
			state.walkAnimationSpeed = entity.walkAnimation.speed(partialTicks);
257
		} else {
258
			state.walkAnimationPos = 0.0F;
259
			state.walkAnimationSpeed = 0.0F;
260
		}
261
262
		if (entity.getVehicle() instanceof LivingEntity vehicle) {
263
			state.wornHeadAnimationPos = vehicle.walkAnimation.position(partialTicks);
264
		} else {
265
			state.wornHeadAnimationPos = state.walkAnimationPos;
266
		}
267
268
		state.scale = entity.getScale();
269
		state.ageScale = entity.getAgeScale();
270
		state.pose = entity.getPose();
271
		state.bedOrientation = entity.getBedOrientation();
272
		if (state.bedOrientation != null) {
273
			state.eyeHeight = entity.getEyeHeight(Pose.STANDING);
274
		}
275
276
		state.isFullyFrozen = entity.isFullyFrozen();
277
		state.isBaby = entity.isBaby();
278
		state.isInWater = entity.isInWater();
279
		state.isAutoSpinAttack = entity.isAutoSpinAttack();
280
		state.ticksSinceKineticHitFeedback = entity.getTicksSinceLastKineticHitFeedback(partialTicks);
281
		state.hasRedOverlay = entity.hurtTime > 0 || entity.deathTime > 0;
282
		ItemStack headItem = entity.getItemBySlot(EquipmentSlot.HEAD);
283
		if (headItem.getItem() instanceof BlockItem blockItem && blockItem.getBlock() instanceof AbstractSkullBlock skullBlock) {
284
			state.wornHeadType = skullBlock.getType();
285
			state.wornHeadProfile = headItem.get(DataComponents.PROFILE);
286
			state.headItem.clear();
287
		} else {
288
			state.wornHeadType = null;
289
			state.wornHeadProfile = null;
290
			if (!HumanoidArmorLayer.shouldRender(headItem, EquipmentSlot.HEAD)) {
291
				this.itemModelResolver.updateForLiving(state.headItem, headItem, ItemDisplayContext.HEAD, entity);
292
			} else {
293
				state.headItem.clear();
294
			}
295
		}
296
297
		state.deathTime = entity.deathTime > 0 ? entity.deathTime + partialTicks : 0.0F;
298
		Minecraft minecraft = Minecraft.getInstance();
299
		state.isInvisibleToPlayer = state.isInvisible && entity.isInvisibleTo(minecraft.player);
300
	}
301
302
	protected void extractNameTags(final T entity, final S state, final float partialTicks) {
303
		double nameTagDistance = entity.getAttribute(Attributes.NAME_TAG_DISTANCE).getValue();
304
		double belowNameDistance = entity.getAttribute(Attributes.BELOW_NAME_DISTANCE).getValue();
305
		super.extractNameTags(entity, state, partialTicks, nameTagDistance, belowNameDistance);
306
	}
307
308
	private static float solveBodyRot(final LivingEntity entity, final float headRot, final float partialTicks) {
309
		if (entity.getVehicle() instanceof LivingEntity riding) {
310
			float bodyRot = Mth.rotLerp(partialTicks, riding.yBodyRotO, riding.yBodyRot);
311
			float maxHeadDiff = 85.0F;
312
			float headDiff = Mth.clamp(Mth.wrapDegrees(headRot - bodyRot), -85.0F, 85.0F);
313
			bodyRot = headRot - headDiff;
314
			if (Math.abs(headDiff) > 50.0F) {
315
				bodyRot += headDiff * 0.2F;
316
			}
317
318
			return bodyRot;
319
		} else {
320
			return Mth.rotLerp(partialTicks, entity.yBodyRotO, entity.yBodyRot);
321
		}
322
	}
323
}
324

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