| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 5 | import com.mojang.math.Transformation;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.Camera;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.client.gui.Font;
|
| 13 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 14 | import net.minecraft.client.renderer.block.BlockModelResolver;
|
| 15 | import net.minecraft.client.renderer.block.model.BlockDisplayContext;
|
| 16 | import net.minecraft.client.renderer.entity.state.BlockDisplayEntityRenderState;
|
| 17 | import net.minecraft.client.renderer.entity.state.DisplayEntityRenderState;
|
| 18 | import net.minecraft.client.renderer.entity.state.ItemDisplayEntityRenderState;
|
| 19 | import net.minecraft.client.renderer.entity.state.TextDisplayEntityRenderState;
|
| 20 | import net.minecraft.client.renderer.item.ItemModelResolver;
|
| 21 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 22 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 23 | import net.minecraft.core.BlockPos;
|
| 24 | import net.minecraft.network.chat.Component;
|
| 25 | import net.minecraft.util.FormattedCharSequence;
|
| 26 | import net.minecraft.util.LightCoordsUtil;
|
| 27 | import net.minecraft.world.entity.Display;
|
| 28 | import net.minecraft.world.entity.Display.BlockDisplay;
|
| 29 | import net.minecraft.world.entity.Display.ItemDisplay;
|
| 30 | import net.minecraft.world.entity.Display.RenderState;
|
| 31 | import net.minecraft.world.entity.Display.TextDisplay;
|
| 32 | import net.minecraft.world.entity.Display.BlockDisplay.BlockRenderState;
|
| 33 | import net.minecraft.world.entity.Display.ItemDisplay.ItemRenderState;
|
| 34 | import net.minecraft.world.entity.Display.TextDisplay.Align;
|
| 35 | import net.minecraft.world.entity.Display.TextDisplay.CachedInfo;
|
| 36 | import net.minecraft.world.entity.Display.TextDisplay.CachedLine;
|
| 37 | import net.minecraft.world.entity.Display.TextDisplay.TextRenderState;
|
| 38 | import net.minecraft.world.phys.AABB;
|
| 39 | import org.joml.Matrix4f;
|
| 40 | import org.joml.Quaternionf;
|
| 41 |
|
| 42 | @Environment(EnvType.CLIENT)
|
| 43 | public abstract class DisplayRenderer<T extends Display, S, ST extends DisplayEntityRenderState> extends EntityRenderer<T, ST> {
|
| 44 | public static final BlockDisplayContext BLOCK_DISPLAY_CONTEXT = BlockDisplayContext.create();
|
| 45 | private final EntityRenderDispatcher entityRenderDispatcher;
|
| 46 | protected final BlockModelResolver blockModelResolver;
|
| 47 |
|
| 48 | protected DisplayRenderer(final EntityRendererProvider.Context context) {
|
| 49 | super(context);
|
| 50 | this.entityRenderDispatcher = context.getEntityRenderDispatcher();
|
| 51 | this.blockModelResolver = context.getBlockModelResolver();
|
| 52 | }
|
| 53 |
|
| 54 | protected AABB getBoundingBoxForCulling(final T entity, final float partialTicks) {
|
| 55 | return entity.getInterpolatedBoundingBox(partialTicks);
|
| 56 | }
|
| 57 |
|
| 58 | protected boolean affectedByCulling(final T entity) {
|
| 59 | return entity.affectedByCulling();
|
| 60 | }
|
| 61 |
|
| 62 | private static int getBrightnessOverride(final Display entity) {
|
| 63 | RenderState renderState = entity.renderState();
|
| 64 | return renderState != null ? renderState.brightnessOverride() : -1;
|
| 65 | }
|
| 66 |
|
| 67 | protected int getSkyLightLevel(final T entity, final BlockPos blockPos) {
|
| 68 | int packedBrightnessOverride = getBrightnessOverride(entity);
|
| 69 | return packedBrightnessOverride != -1 ? LightCoordsUtil.sky(packedBrightnessOverride) : super.getSkyLightLevel(entity, blockPos);
|
| 70 | }
|
| 71 |
|
| 72 | protected int getBlockLightLevel(final T entity, final BlockPos blockPos) {
|
| 73 | int packedBrightnessOverride = getBrightnessOverride(entity);
|
| 74 | return packedBrightnessOverride != -1 ? LightCoordsUtil.block(packedBrightnessOverride) : super.getBlockLightLevel(entity, blockPos);
|
| 75 | }
|
| 76 |
|
| 77 | protected float getShadowRadius(final ST state) {
|
| 78 | RenderState renderState = state.renderState;
|
| 79 | return renderState == null ? 0.0F : renderState.shadowRadius().get(state.interpolationProgress);
|
| 80 | }
|
| 81 |
|
| 82 | protected float getShadowStrength(final ST state) {
|
| 83 | RenderState renderState = state.renderState;
|
| 84 | return renderState == null ? 0.0F : renderState.shadowStrength().get(state.interpolationProgress);
|
| 85 | }
|
| 86 |
|
| 87 | public void submit(final ST state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 88 | RenderState renderState = state.renderState;
|
| 89 | if (renderState != null && state.hasSubState()) {
|
| 90 | float interpolationProgress = state.interpolationProgress;
|
| 91 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 92 | poseStack.pushPose();
|
| 93 | poseStack.rotate(this.calculateOrientation(renderState, state, new Quaternionf()));
|
| 94 | Transformation transformation = renderState.transformation().get(interpolationProgress);
|
| 95 | poseStack.mulPose(transformation);
|
| 96 | this.submitInner(state, poseStack, submitNodeCollector, state.lightCoords, interpolationProgress);
|
| 97 | poseStack.popPose();
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | private Quaternionf calculateOrientation(final RenderState renderState, final ST state, final Quaternionf output) {
|
| 102 | return switch (renderState.billboardConstraints()) {
|
| 103 | case FIXED -> output.rotationYXZ((float) (-Math.PI / 180.0) * state.entityYRot, (float) (Math.PI / 180.0) * state.entityXRot, 0.0F);
|
| 104 | case HORIZONTAL -> output.rotationYXZ((float) (-Math.PI / 180.0) * state.entityYRot, (float) (Math.PI / 180.0) * transformXRot(state.cameraXRot), 0.0F);
|
| 105 | case VERTICAL -> output.rotationYXZ((float) (-Math.PI / 180.0) * transformYRot(state.cameraYRot), (float) (Math.PI / 180.0) * state.entityXRot, 0.0F);
|
| 106 | case CENTER -> output.rotationYXZ(
|
| 107 | (float) (-Math.PI / 180.0) * transformYRot(state.cameraYRot), (float) (Math.PI / 180.0) * transformXRot(state.cameraXRot), 0.0F
|
| 108 | );
|
| 109 | };
|
| 110 | }
|
| 111 |
|
| 112 | private static float transformYRot(final float cameraYRot) {
|
| 113 | return cameraYRot - 180.0F;
|
| 114 | }
|
| 115 |
|
| 116 | private static float transformXRot(final float cameraXRot) {
|
| 117 | return -cameraXRot;
|
| 118 | }
|
| 119 |
|
| 120 | private static <T extends Display> float entityYRot(final T entity, final float partialTicks) {
|
| 121 | return entity.getYRot(partialTicks);
|
| 122 | }
|
| 123 |
|
| 124 | private static <T extends Display> float entityXRot(final T entity, final float partialTicks) {
|
| 125 | return entity.getXRot(partialTicks);
|
| 126 | }
|
| 127 |
|
| 128 | protected abstract void submitInner(
|
| 129 | final ST state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords, final float interpolationProgress
|
| 130 | );
|
| 131 |
|
| 132 | public void extractRenderState(final T entity, final ST state, final float partialTicks) {
|
| 133 | super.extractRenderState(entity, state, partialTicks);
|
| 134 | state.renderState = entity.renderState();
|
| 135 | state.interpolationProgress = entity.calculateInterpolationProgress(partialTicks);
|
| 136 | state.entityYRot = entityYRot(entity, partialTicks);
|
| 137 | state.entityXRot = entityXRot(entity, partialTicks);
|
| 138 | Camera camera = this.entityRenderDispatcher.camera;
|
| 139 | state.cameraXRot = camera.xRot();
|
| 140 | state.cameraYRot = camera.yRot();
|
| 141 | }
|
| 142 |
|
| 143 | @Environment(EnvType.CLIENT)
|
| 144 | public static class BlockDisplayRenderer extends DisplayRenderer<BlockDisplay, BlockRenderState, BlockDisplayEntityRenderState> {
|
| 145 | protected BlockDisplayRenderer(final EntityRendererProvider.Context context) {
|
| 146 | super(context);
|
| 147 | }
|
| 148 |
|
| 149 | public BlockDisplayEntityRenderState createRenderState() {
|
| 150 | return new BlockDisplayEntityRenderState();
|
| 151 | }
|
| 152 |
|
| 153 | public void extractRenderState(final BlockDisplay entity, final BlockDisplayEntityRenderState state, final float partialTicks) {
|
| 154 | super.extractRenderState(entity, state, partialTicks);
|
| 155 | BlockRenderState blockRenderState = entity.blockRenderState();
|
| 156 | if (blockRenderState != null) {
|
| 157 | this.blockModelResolver.update(state.blockModel, blockRenderState.blockState(), BLOCK_DISPLAY_CONTEXT);
|
| 158 | } else {
|
| 159 | state.blockModel.clear();
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | public void submitInner(
|
| 164 | final BlockDisplayEntityRenderState state,
|
| 165 | final PoseStack poseStack,
|
| 166 | final SubmitNodeCollector submitNodeCollector,
|
| 167 | final int lightCoords,
|
| 168 | final float interpolationProgress
|
| 169 | ) {
|
| 170 | state.blockModel.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | @Environment(EnvType.CLIENT)
|
| 175 | public static class ItemDisplayRenderer extends DisplayRenderer<ItemDisplay, ItemRenderState, ItemDisplayEntityRenderState> {
|
| 176 | private final ItemModelResolver itemModelResolver;
|
| 177 |
|
| 178 | protected ItemDisplayRenderer(final EntityRendererProvider.Context context) {
|
| 179 | super(context);
|
| 180 | this.itemModelResolver = context.getItemModelResolver();
|
| 181 | }
|
| 182 |
|
| 183 | public ItemDisplayEntityRenderState createRenderState() {
|
| 184 | return new ItemDisplayEntityRenderState();
|
| 185 | }
|
| 186 |
|
| 187 | public void extractRenderState(final ItemDisplay entity, final ItemDisplayEntityRenderState state, final float partialTicks) {
|
| 188 | super.extractRenderState(entity, state, partialTicks);
|
| 189 | ItemRenderState itemRenderState = entity.itemRenderState();
|
| 190 | if (itemRenderState != null) {
|
| 191 | this.itemModelResolver.updateForNonLiving(state.item, itemRenderState.itemStack(), itemRenderState.itemTransform(), entity);
|
| 192 | } else {
|
| 193 | state.item.clear();
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | public void submitInner(
|
| 198 | final ItemDisplayEntityRenderState state,
|
| 199 | final PoseStack poseStack,
|
| 200 | final SubmitNodeCollector submitNodeCollector,
|
| 201 | final int lightCoords,
|
| 202 | final float interpolationProgress
|
| 203 | ) {
|
| 204 | if (!state.item.isEmpty()) {
|
| 205 | poseStack.rotate(Axis.YP, (float) Math.PI);
|
| 206 | state.item.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 207 | }
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | @Environment(EnvType.CLIENT)
|
| 212 | public static class TextDisplayRenderer extends DisplayRenderer<TextDisplay, TextRenderState, TextDisplayEntityRenderState> {
|
| 213 | private final Font font;
|
| 214 |
|
| 215 | protected TextDisplayRenderer(final EntityRendererProvider.Context context) {
|
| 216 | super(context);
|
| 217 | this.font = context.getFont();
|
| 218 | }
|
| 219 |
|
| 220 | public TextDisplayEntityRenderState createRenderState() {
|
| 221 | return new TextDisplayEntityRenderState();
|
| 222 | }
|
| 223 |
|
| 224 | public void extractRenderState(final TextDisplay entity, final TextDisplayEntityRenderState state, final float partialTicks) {
|
| 225 | super.extractRenderState(entity, state, partialTicks);
|
| 226 | state.textRenderState = entity.textRenderState();
|
| 227 | state.cachedInfo = entity.cacheDisplay(this::splitLines);
|
| 228 | }
|
| 229 |
|
| 230 | private CachedInfo splitLines(final Component input, final int width) {
|
| 231 | List<FormattedCharSequence> lines = this.font.split(input, width);
|
| 232 | List<CachedLine> result = new ArrayList<>(lines.size());
|
| 233 | int maxLineWidth = 0;
|
| 234 |
|
| 235 | for (FormattedCharSequence line : lines) {
|
| 236 | int lineWidth = this.font.width(line);
|
| 237 | maxLineWidth = Math.max(maxLineWidth, lineWidth);
|
| 238 | result.add(new CachedLine(line, lineWidth));
|
| 239 | }
|
| 240 |
|
| 241 | return new CachedInfo(result, maxLineWidth);
|
| 242 | }
|
| 243 |
|
| 244 | public void submitInner(
|
| 245 | final TextDisplayEntityRenderState state,
|
| 246 | final PoseStack poseStack,
|
| 247 | final SubmitNodeCollector submitNodeCollector,
|
| 248 | final int lightCoords,
|
| 249 | final float interpolationProgress
|
| 250 | ) {
|
| 251 | TextRenderState renderState = state.textRenderState;
|
| 252 | byte flags = renderState.flags();
|
| 253 | boolean seeThrough = (flags & 2) != 0;
|
| 254 | Font.DisplayMode textDisplayMode = seeThrough ? Font.DisplayMode.SEE_THROUGH : Font.DisplayMode.POLYGON_OFFSET;
|
| 255 | boolean useDefaultBackground = (flags & 4) != 0;
|
| 256 | boolean shadow = (flags & 1) != 0;
|
| 257 | Align alignment = TextDisplay.getAlign(flags);
|
| 258 | byte textOpacity = (byte)renderState.textOpacity().get(interpolationProgress);
|
| 259 | int backgroundColor;
|
| 260 | if (useDefaultBackground) {
|
| 261 | float backgroundAlpha = Minecraft.getInstance().gameRenderer.gameRenderState().optionsRenderState.getBackgroundOpacity(0.25F);
|
| 262 | backgroundColor = (int)(backgroundAlpha * 255.0F) << 24;
|
| 263 | } else {
|
| 264 | backgroundColor = renderState.backgroundColor().get(interpolationProgress);
|
| 265 | }
|
| 266 |
|
| 267 | float y = 0.0F;
|
| 268 | Matrix4f pose = poseStack.last().pose();
|
| 269 | pose.rotate((float) Math.PI, 0.0F, 1.0F, 0.0F);
|
| 270 | pose.scale(-0.025F, -0.025F, -0.025F);
|
| 271 | CachedInfo cachedInfo = state.cachedInfo;
|
| 272 | int lineSpacing = 1;
|
| 273 | int lineHeight = 9 + 1;
|
| 274 | int width = cachedInfo.width();
|
| 275 | int height = cachedInfo.lines().size() * lineHeight - 1;
|
| 276 | pose.translate(1.0F - width / 2.0F, -height, 0.0F);
|
| 277 | if (backgroundColor != 0) {
|
| 278 | submitNodeCollector.submitTextBackground(poseStack, -1.0F, -1.0F, width, height, backgroundColor, textDisplayMode, lightCoords);
|
| 279 | }
|
| 280 |
|
| 281 | for (CachedLine line : cachedInfo.lines()) {
|
| 282 | float offset = switch (alignment) {
|
| 283 | case LEFT -> 0.0F;
|
| 284 | case RIGHT -> width - line.width();
|
| 285 | case CENTER -> width / 2.0F - line.width() / 2.0F;
|
| 286 | default -> throw new MatchException(null, null);
|
| 287 | };
|
| 288 | submitNodeCollector.submitText(poseStack, offset, y, line.contents(), shadow, textDisplayMode, lightCoords, textOpacity << 24 | 16777215, 0, 0);
|
| 289 | y += lineHeight;
|
| 290 | }
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 |
|