EntityRenderer .java

net.minecraft.client.renderer.entity.EntityRenderer (class, 347 lines): members: SHADOW_POWER_FALLOFF_Y, finalizeRenderState, MAX_SHADOW_RADIUS, camera.

net.minecraft.client.renderer.entityclass347 linesclientextends Entity14.1 KB

API Surface (19)

int getPackedLightCoords(final T entity, final float partialTickTime)
int getSkyLightLevel(final T entity, final BlockPos blockPos)
int getBlockLightLevel(final T entity, final BlockPos blockPos)
boolean shouldRender(final T entity, final Frustum culler, final double camX, final double camY, final double camZ, final float partialTicks)
AABB getBoundingBoxForCulling(final T entity, final float partialTicks)
boolean affectedByCulling(final T entity)
Vec3 getRenderOffset(final S state)
void submit(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera)
boolean shouldShowName(final T entity, final double distanceToCameraSq)
Font getFont()
void submitNameDisplay(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera)
<S extends EntityRenderState> void submitNameDisplay(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera, final int offset)
float getShadowRadius(final S state)
float getShadowStrength(final S state)
S createRenderState(final T entity, final float partialTicks)
void extractRenderState(final T entity, final S state, final float partialTicks)
void extractNameTags(final T entity, final S state, final float partialTicks)
void extractNameTags(final T entity, final S state, final float partialTicks, final double nameTagDistance, final double belowNameDistance)
void finalizeRenderState(final T entity, final S state)

Fields & Constants (8)

SHADOW_POWER_FALLOFF_Y
MAX_SHADOW_RADIUS
NAMETAG_SCALE
entityRenderDispatcher
font
shadowRadius
shadowStrength
camera

Full Source

EntityRenderer.java347 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.entity;
2
3
import com.mojang.blaze3d.vertex.PoseStack;
4
import java.util.ArrayList;
5
import net.fabricmc.api.EnvType;
6
import net.fabricmc.api.Environment;
7
import net.minecraft.client.Minecraft;
8
import net.minecraft.client.gui.Font;
9
import net.minecraft.client.renderer.Lightmap;
10
import net.minecraft.client.renderer.SubmitNodeCollector;
11
import net.minecraft.client.renderer.culling.Frustum;
12
import net.minecraft.client.renderer.entity.state.EntityRenderState;
13
import net.minecraft.client.renderer.state.level.CameraRenderState;
14
import net.minecraft.client.server.IntegratedServer;
15
import net.minecraft.core.BlockPos;
16
import net.minecraft.core.BlockPos.MutableBlockPos;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.server.level.ServerLevel;
19
import net.minecraft.util.ARGB;
20
import net.minecraft.util.LightCoordsUtil;
21
import net.minecraft.util.Mth;
22
import net.minecraft.world.entity.Entity;
23
import net.minecraft.world.entity.EntityAttachment;
24
import net.minecraft.world.entity.Leashable;
25
import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
26
import net.minecraft.world.entity.vehicle.minecart.NewMinecartBehavior;
27
import net.minecraft.world.level.Level;
28
import net.minecraft.world.level.LightLayer;
29
import net.minecraft.world.level.block.RenderShape;
30
import net.minecraft.world.level.block.state.BlockState;
31
import net.minecraft.world.level.chunk.ChunkAccess;
32
import net.minecraft.world.phys.AABB;
33
import net.minecraft.world.phys.Vec3;
34
import net.minecraft.world.phys.shapes.VoxelShape;
35
import org.jspecify.annotations.Nullable;
36
37
@Environment(EnvType.CLIENT)
38
public abstract class EntityRenderer<T extends Entity, S extends EntityRenderState> {
39
	private static final float SHADOW_POWER_FALLOFF_Y = 0.5F;
40
	private static final float MAX_SHADOW_RADIUS = 32.0F;
41
	public static final float NAMETAG_SCALE = 0.025F;
42
	protected final EntityRenderDispatcher entityRenderDispatcher;
43
	private final Font font;
44
	protected float shadowRadius;
45
	protected float shadowStrength = 1.0F;
46
47
	protected EntityRenderer(final EntityRendererProvider.Context context) {
48
		this.entityRenderDispatcher = context.getEntityRenderDispatcher();
49
		this.font = context.getFont();
50
	}
51
52
	public final int getPackedLightCoords(final T entity, final float partialTickTime) {
53
		BlockPos blockPos = BlockPos.containing(entity.getLightProbePosition(partialTickTime));
54
		return LightCoordsUtil.pack(this.getBlockLightLevel(entity, blockPos), this.getSkyLightLevel(entity, blockPos));
55
	}
56
57
	protected int getSkyLightLevel(final T entity, final BlockPos blockPos) {
58
		return entity.level().getBrightness(LightLayer.SKY, blockPos);
59
	}
60
61
	protected int getBlockLightLevel(final T entity, final BlockPos blockPos) {
62
		return entity.isOnFire() ? 15 : entity.level().getBrightness(LightLayer.BLOCK, blockPos);
63
	}
64
65
	public boolean shouldRender(final T entity, final Frustum culler, final double camX, final double camY, final double camZ, final float partialTicks) {
66
		if (!entity.shouldRender(camX, camY, camZ)) {
67
			return false;
68
		}
69
70
		if (!this.affectedByCulling(entity)) {
71
			return true;
72
		}
73
74
		AABB boundingBox = this.getBoundingBoxForCulling(entity, partialTicks).inflate(0.5);
75
		if (boundingBox.hasNaN() || boundingBox.getSize() == 0.0) {
76
			boundingBox = new AABB(entity.getX() - 2.0, entity.getY() - 2.0, entity.getZ() - 2.0, entity.getX() + 2.0, entity.getY() + 2.0, entity.getZ() + 2.0);
77
		}
78
79
		if (culler.isVisible(boundingBox)) {
80
			return true;
81
		}
82
83
		if (entity instanceof Leashable leashable) {
84
			Entity leashHolder = leashable.getLeashHolder();
85
			if (leashHolder != null) {
86
				AABB leasherBox = this.entityRenderDispatcher.getRenderer(leashHolder).getBoundingBoxForCulling(leashHolder, partialTicks);
87
				return culler.isVisible(leasherBox) || culler.isVisible(boundingBox.minmax(leasherBox));
88
			}
89
		}
90
91
		return false;
92
	}
93
94
	protected AABB getBoundingBoxForCulling(final T entity, final float partialTicks) {
95
		return entity.getInterpolatedBoundingBox(partialTicks);
96
	}
97
98
	protected boolean affectedByCulling(final T entity) {
99
		return true;
100
	}
101
102
	public Vec3 getRenderOffset(final S state) {
103
		return state.passengerOffset != null ? state.passengerOffset : Vec3.ZERO;
104
	}
105
106
	public void submit(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
107
		if (state.leashStates != null) {
108
			for (EntityRenderState.LeashState leashState : state.leashStates) {
109
				submitNodeCollector.submitLeash(poseStack, leashState);
110
			}
111
		}
112
113
		this.submitNameDisplay(state, poseStack, submitNodeCollector, camera);
114
	}
115
116
	protected boolean shouldShowName(final T entity, final double distanceToCameraSq) {
117
		return entity.shouldShowName() || entity.hasCustomName() && entity == this.entityRenderDispatcher.crosshairPickEntity;
118
	}
119
120
	public Font getFont() {
121
		return this.font;
122
	}
123
124
	protected void submitNameDisplay(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
125
		this.submitNameDisplay(state, poseStack, submitNodeCollector, camera, 0);
126
	}
127
128
	protected final <S extends EntityRenderState> void submitNameDisplay(
129
		final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera, final int offset
130
	) {
131
		poseStack.pushPose();
132
		if (state.scoreText != null) {
133
			submitNodeCollector.submitNameTag(poseStack, state.nameTagAttachment, offset, state.scoreText, !state.isDiscrete, state.lightCoords, camera);
134
			poseStack.translate(0.0F, 9.0F * 1.15F * 0.025F, 0.0F);
135
		}
136
137
		if (state.nameTag != null) {
138
			submitNodeCollector.submitNameTag(poseStack, state.nameTagAttachment, offset, state.nameTag, !state.isDiscrete, state.lightCoords, camera);
139
		}
140
141
		poseStack.popPose();
142
	}
143
144
	protected @Nullable Component getNameTag(final T entity) {
145
		return entity.getDisplayName();
146
	}
147
148
	protected float getShadowRadius(final S state) {
149
		return this.shadowRadius;
150
	}
151
152
	protected float getShadowStrength(final S state) {
153
		return this.shadowStrength;
154
	}
155
156
	public abstract S createRenderState();
157
158
	public final S createRenderState(final T entity, final float partialTicks) {
159
		S state = this.createRenderState();
160
		this.extractRenderState(entity, state, partialTicks);
161
		this.finalizeRenderState(entity, state);
162
		return state;
163
	}
164
165
	public void extractRenderState(final T entity, final S state, final float partialTicks) {
166
		state.entityType = entity.getType();
167
		state.x = Mth.lerp(partialTicks, entity.xOld, entity.getX());
168
		state.y = Mth.lerp(partialTicks, entity.yOld, entity.getY());
169
		state.z = Mth.lerp(partialTicks, entity.zOld, entity.getZ());
170
		state.isInvisible = entity.isInvisible();
171
		state.ageInTicks = entity.tickCount + partialTicks;
172
		state.boundingBoxWidth = entity.getBbWidth();
173
		state.boundingBoxHeight = entity.getBbHeight();
174
		state.eyeHeight = entity.getEyeHeight();
175
		if (entity.isPassenger()
176
			&& entity.getVehicle() instanceof AbstractMinecart minecart
177
			&& minecart.getBehavior() instanceof NewMinecartBehavior behavior
178
			&& behavior.cartHasPosRotLerp()) {
179
			double cartLerpX = Mth.lerp(partialTicks, minecart.xOld, minecart.getX());
180
			double cartLerpY = Mth.lerp(partialTicks, minecart.yOld, minecart.getY());
181
			double cartLerpZ = Mth.lerp(partialTicks, minecart.zOld, minecart.getZ());
182
			state.passengerOffset = behavior.getCartLerpPosition(partialTicks).subtract(new Vec3(cartLerpX, cartLerpY, cartLerpZ));
183
		} else {
184
			state.passengerOffset = null;
185
		}
186
187
		this.extractNameTags(entity, state, partialTicks);
188
		state.isDiscrete = entity.isDiscrete();
189
		Level level = entity.level();
190
		if (entity instanceof Leashable leashable && leashable.getLeashHolder() != null) {
191
			Entity roper = leashable.getLeashHolder();
192
			float entityYRot = entity.getPreciseBodyRotation(partialTicks) * (float) (Math.PI / 180.0);
193
			Vec3 attachOffset = leashable.getLeashOffset(partialTicks);
194
			BlockPos entityEyePos = BlockPos.containing(entity.getEyePosition(partialTicks));
195
			BlockPos roperEyePos = BlockPos.containing(roper.getEyePosition(partialTicks));
196
			int startBlockLight = this.getBlockLightLevel(entity, entityEyePos);
197
			int endBlockLight = this.entityRenderDispatcher.getRenderer(roper).getBlockLightLevel(roper, roperEyePos);
198
			int startSkyLight = level.getBrightness(LightLayer.SKY, entityEyePos);
199
			int endSkyLight = level.getBrightness(LightLayer.SKY, roperEyePos);
200
			boolean quadConnection = roper.supportQuadLeashAsHolder() && leashable.supportQuadLeash();
201
			int leashCount = quadConnection ? 4 : 1;
202
			if (state.leashStates == null || state.leashStates.size() != leashCount) {
203
				state.leashStates = new ArrayList<>(leashCount);
204
205
				for (int i = 0; i < leashCount; i++) {
206
					state.leashStates.add(new EntityRenderState.LeashState());
207
				}
208
			}
209
210
			if (quadConnection) {
211
				float roperYRot = roper.getPreciseBodyRotation(partialTicks) * (float) (Math.PI / 180.0);
212
				Vec3 holderPos = roper.getPosition(partialTicks);
213
				Vec3[] leashableAttachmentPoints = leashable.getQuadLeashOffsets();
214
				Vec3[] roperAttachmentPoints = roper.getQuadLeashHolderOffsets();
215
216
				for (int i = 0; i < leashCount; i++) {
217
					EntityRenderState.LeashState leashState = state.leashStates.get(i);
218
					leashState.offset = leashableAttachmentPoints[i].yRot(-entityYRot);
219
					leashState.start = entity.getPosition(partialTicks).add(leashState.offset);
220
					leashState.end = holderPos.add(roperAttachmentPoints[i].yRot(-roperYRot));
221
					leashState.startBlockLight = startBlockLight;
222
					leashState.endBlockLight = endBlockLight;
223
					leashState.startSkyLight = startSkyLight;
224
					leashState.endSkyLight = endSkyLight;
225
					leashState.slack = false;
226
				}
227
			} else {
228
				Vec3 rotatedAttachOffset = attachOffset.yRot(-entityYRot);
229
				EntityRenderState.LeashState leashState = state.leashStates.getFirst();
230
				leashState.offset = rotatedAttachOffset;
231
				leashState.start = entity.getPosition(partialTicks).add(rotatedAttachOffset);
232
				leashState.end = roper.getRopeHoldPosition(partialTicks);
233
				leashState.startBlockLight = startBlockLight;
234
				leashState.endBlockLight = endBlockLight;
235
				leashState.startSkyLight = startSkyLight;
236
				leashState.endSkyLight = endSkyLight;
237
			}
238
		} else {
239
			state.leashStates = null;
240
		}
241
242
		state.displayFireAnimation = entity.displayFireAnimation();
243
		Minecraft minecraft = Minecraft.getInstance();
244
		boolean appearsGlowing = minecraft.shouldEntityAppearGlowing(entity);
245
		state.outlineColor = appearsGlowing ? ARGB.opaque(entity.getTeamColor()) : 0;
246
		state.lightCoords = this.getPackedLightCoords(entity, partialTicks);
247
	}
248
249
	protected void extractNameTags(final T entity, final S state, final float partialTicks) {
250
		this.extractNameTags(entity, state, partialTicks, 64.0, 10.0);
251
	}
252
253
	protected final void extractNameTags(final T entity, final S state, final float partialTicks, final double nameTagDistance, final double belowNameDistance) {
254
		if (this.entityRenderDispatcher.camera != null) {
255
			state.distanceToCameraSq = this.entityRenderDispatcher.distanceToSqr(entity);
256
			boolean shouldShowName = state.distanceToCameraSq < Mth.square(nameTagDistance) && this.shouldShowName(entity, state.distanceToCameraSq);
257
			if (shouldShowName) {
258
				state.nameTag = this.getNameTag(entity);
259
				state.nameTagAttachment = entity.getAttachments().getNullable(EntityAttachment.NAME_TAG, 0, entity.getYRot(partialTicks));
260
			} else {
261
				state.nameTag = null;
262
			}
263
264
			if (state.distanceToCameraSq < Mth.square(belowNameDistance)) {
265
				state.scoreText = entity.belowNameDisplay();
266
			} else {
267
				state.scoreText = null;
268
			}
269
		}
270
	}
271
272
	protected void finalizeRenderState(final T entity, final S state) {
273
		Minecraft minecraft = Minecraft.getInstance();
274
		Level level = entity.level();
275
		this.extractShadow(state, minecraft, level);
276
	}
277
278
	private void extractShadow(final S state, final Minecraft minecraft, final Level level) {
279
		state.shadowPieces.clear();
280
		if (minecraft.options.entityShadows().get() && !state.isInvisible) {
281
			float shadowRadius = Math.min(this.getShadowRadius(state), 32.0F);
282
			state.shadowRadius = shadowRadius;
283
			if (shadowRadius > 0.0F) {
284
				double distSq = state.distanceToCameraSq;
285
				float pow = (float)((1.0 - distSq / 256.0) * this.getShadowStrength(state));
286
				if (pow > 0.0F) {
287
					int x0 = Mth.floor(state.x - shadowRadius);
288
					int x1 = Mth.floor(state.x + shadowRadius);
289
					int z0 = Mth.floor(state.z - shadowRadius);
290
					int z1 = Mth.floor(state.z + shadowRadius);
291
					float depth = Math.min(pow / 0.5F - 1.0F, shadowRadius);
292
					int y0 = Mth.floor(state.y - depth);
293
					int y1 = Mth.floor(state.y);
294
					MutableBlockPos pos = new MutableBlockPos();
295
296
					for (int z = z0; z <= z1; z++) {
297
						for (int x = x0; x <= x1; x++) {
298
							pos.set(x, 0, z);
299
							ChunkAccess chunk = level.getChunk(pos);
300
301
							for (int y = y0; y <= y1; y++) {
302
								pos.setY(y);
303
								this.extractShadowPiece(state, level, pow, pos, chunk);
304
							}
305
						}
306
					}
307
				}
308
			}
309
		} else {
310
			state.shadowRadius = 0.0F;
311
		}
312
	}
313
314
	private void extractShadowPiece(final S state, final Level level, final float pow, final MutableBlockPos pos, final ChunkAccess chunk) {
315
		float powerAtDepth = pow - (float)(state.y - pos.getY()) * 0.5F;
316
		BlockPos belowPos = pos.below();
317
		BlockState belowState = chunk.getBlockState(belowPos);
318
		if (belowState.getRenderShape() != RenderShape.INVISIBLE) {
319
			int brightness = level.getMaxLocalRawBrightness(pos);
320
			if (brightness > 3) {
321
				if (belowState.isCollisionShapeFullBlock(chunk, belowPos)) {
322
					VoxelShape belowShape = belowState.getShape(chunk, belowPos);
323
					if (!belowShape.isEmpty()) {
324
						float alpha = Mth.clamp(powerAtDepth * 0.5F * Lightmap.getBrightness(level.dimensionType(), brightness), 0.0F, 1.0F);
325
						float relativeX = (float)(pos.getX() - state.x);
326
						float relativeY = (float)(pos.getY() - state.y);
327
						float relativeZ = (float)(pos.getZ() - state.z);
328
						state.shadowPieces.add(new EntityRenderState.ShadowPiece(relativeX, relativeY, relativeZ, belowShape, alpha));
329
					}
330
				}
331
			}
332
		}
333
	}
334
335
	private static @Nullable Entity getServerSideEntity(final Entity entity) {
336
		IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
337
		if (server != null) {
338
			ServerLevel level = server.getLevel(entity.level().dimension());
339
			if (level != null) {
340
				return level.getEntity(entity.getId());
341
			}
342
		}
343
344
		return null;
345
	}
346
}
347

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