| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 5 | import java.util.Objects;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.model.geom.ModelLayerLocation;
|
| 9 | import net.minecraft.client.model.object.cart.MinecartModel;
|
| 10 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 11 | import net.minecraft.client.renderer.block.BlockModelRenderState;
|
| 12 | import net.minecraft.client.renderer.block.BlockModelResolver;
|
| 13 | import net.minecraft.client.renderer.block.model.BlockDisplayContext;
|
| 14 | import net.minecraft.client.renderer.entity.state.MinecartRenderState;
|
| 15 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 16 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 20 | import net.minecraft.world.entity.vehicle.minecart.NewMinecartBehavior;
|
| 21 | import net.minecraft.world.entity.vehicle.minecart.OldMinecartBehavior;
|
| 22 | import net.minecraft.world.phys.AABB;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public abstract class AbstractMinecartRenderer<T extends AbstractMinecart, S extends MinecartRenderState> extends EntityRenderer<T, S> {
|
| 27 | private static final Identifier MINECART_LOCATION = Identifier.withDefaultNamespace("textures/entity/minecart/minecart.png");
|
| 28 | private static final float DISPLAY_BLOCK_SCALE = 0.75F;
|
| 29 | public static final BlockDisplayContext BLOCK_DISPLAY_CONTEXT = BlockDisplayContext.create();
|
| 30 | protected final MinecartModel model;
|
| 31 | private final BlockModelResolver blockModelResolver;
|
| 32 |
|
| 33 | public AbstractMinecartRenderer(final EntityRendererProvider.Context context, final ModelLayerLocation model) {
|
| 34 | super(context);
|
| 35 | this.shadowRadius = 0.7F;
|
| 36 | this.model = new MinecartModel(context.bakeLayer(model));
|
| 37 | this.blockModelResolver = context.getBlockModelResolver();
|
| 38 | }
|
| 39 |
|
| 40 | public void submit(final S state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 41 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 42 | poseStack.pushPose();
|
| 43 | long seed = state.offsetSeed;
|
| 44 | float offsetX = (((float)(seed >> 16 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
|
| 45 | float offsetY = (((float)(seed >> 20 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
|
| 46 | float offsetZ = (((float)(seed >> 24 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F;
|
| 47 | poseStack.translate(offsetX, offsetY, offsetZ);
|
| 48 | if (state.isNewRender) {
|
| 49 | newRender(state, poseStack);
|
| 50 | } else {
|
| 51 | oldRender(state, poseStack);
|
| 52 | }
|
| 53 |
|
| 54 | float hurt = state.hurtTime;
|
| 55 | if (hurt > 0.0F) {
|
| 56 | poseStack.rotateDegrees(Axis.XP, Mth.sin(hurt) * hurt * state.damageTime / 10.0F * state.hurtDir);
|
| 57 | }
|
| 58 |
|
| 59 | BlockModelRenderState displayBlockModel = state.displayBlockModel;
|
| 60 | if (!displayBlockModel.isEmpty()) {
|
| 61 | poseStack.pushPose();
|
| 62 | poseStack.scale(0.75F, 0.75F, 0.75F);
|
| 63 | poseStack.translate(-0.5F, (state.displayOffset - 8) / 16.0F, 0.5F);
|
| 64 | poseStack.rotateDegrees(Axis.YP, 90.0F);
|
| 65 | this.submitMinecartContents(state, displayBlockModel, poseStack, submitNodeCollector, state.lightCoords);
|
| 66 | poseStack.popPose();
|
| 67 | }
|
| 68 |
|
| 69 | poseStack.scale(-1.0F, -1.0F, 1.0F);
|
| 70 | submitNodeCollector.submitModel(this.model, state, poseStack, MINECART_LOCATION, state.lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 71 | poseStack.popPose();
|
| 72 | }
|
| 73 |
|
| 74 | private static <S extends MinecartRenderState> void newRender(final S state, final PoseStack poseStack) {
|
| 75 | poseStack.rotateDegrees(Axis.YP, state.yRot);
|
| 76 | poseStack.rotateDegrees(Axis.ZP, -state.xRot);
|
| 77 | poseStack.translate(0.0F, 0.375F, 0.0F);
|
| 78 | }
|
| 79 |
|
| 80 | private static <S extends MinecartRenderState> void oldRender(final S state, final PoseStack poseStack) {
|
| 81 | double entityX = state.x;
|
| 82 | double entityY = state.y;
|
| 83 | double entityZ = state.z;
|
| 84 | float xRot = state.xRot;
|
| 85 | float rotation = state.yRot;
|
| 86 | if (state.posOnRail != null && state.frontPos != null && state.backPos != null) {
|
| 87 | Vec3 frontPos = state.frontPos;
|
| 88 | Vec3 backPos = state.backPos;
|
| 89 | poseStack.translate(state.posOnRail.x - entityX, (frontPos.y + backPos.y) / 2.0 - entityY, state.posOnRail.z - entityZ);
|
| 90 | Vec3 direction = backPos.add(-frontPos.x, -frontPos.y, -frontPos.z);
|
| 91 | if (direction.length() != 0.0) {
|
| 92 | direction = direction.normalize();
|
| 93 | rotation = (float)(Math.atan2(direction.z, direction.x) * 180.0 / Math.PI);
|
| 94 | xRot = (float)(Math.atan(direction.y) * 73.0);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | poseStack.translate(0.0F, 0.375F, 0.0F);
|
| 99 | poseStack.rotateDegrees(Axis.YP, 180.0F - rotation);
|
| 100 | poseStack.rotateDegrees(Axis.ZP, -xRot);
|
| 101 | }
|
| 102 |
|
| 103 | public void extractRenderState(final T entity, final S state, final float partialTicks) {
|
| 104 | super.extractRenderState(entity, state, partialTicks);
|
| 105 | if (entity.getBehavior() instanceof NewMinecartBehavior behavior) {
|
| 106 | newExtractState(entity, behavior, state, partialTicks);
|
| 107 | state.isNewRender = true;
|
| 108 | } else if (entity.getBehavior() instanceof OldMinecartBehavior behavior) {
|
| 109 | oldExtractState(entity, behavior, state, partialTicks);
|
| 110 | state.isNewRender = false;
|
| 111 | }
|
| 112 |
|
| 113 | long seed = entity.getId() * 493286711L;
|
| 114 | state.offsetSeed = seed * seed * 4392167121L + seed * 98761L;
|
| 115 | state.hurtTime = entity.getHurtTime() - partialTicks;
|
| 116 | state.hurtDir = entity.getHurtDir();
|
| 117 | state.damageTime = Math.max(entity.getDamage() - partialTicks, 0.0F);
|
| 118 | state.displayOffset = entity.getDisplayOffset();
|
| 119 | this.blockModelResolver.update(state.displayBlockModel, entity.getDisplayBlockState(), BLOCK_DISPLAY_CONTEXT);
|
| 120 | }
|
| 121 |
|
| 122 | private static <T extends AbstractMinecart, S extends MinecartRenderState> void newExtractState(
|
| 123 | final T entity, final NewMinecartBehavior behavior, final S state, final float partialTicks
|
| 124 | ) {
|
| 125 | if (behavior.cartHasPosRotLerp()) {
|
| 126 | state.renderPos = behavior.getCartLerpPosition(partialTicks);
|
| 127 | state.xRot = behavior.getCartLerpXRot(partialTicks);
|
| 128 | state.yRot = behavior.getCartLerpYRot(partialTicks);
|
| 129 | } else {
|
| 130 | state.renderPos = null;
|
| 131 | state.xRot = entity.getXRot();
|
| 132 | state.yRot = entity.getYRot();
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private static <T extends AbstractMinecart, S extends MinecartRenderState> void oldExtractState(
|
| 137 | final T entity, final OldMinecartBehavior behavior, final S state, final float partialTicks
|
| 138 | ) {
|
| 139 | float HALF_LENGTH = 0.3F;
|
| 140 | state.xRot = entity.getXRot(partialTicks);
|
| 141 | state.yRot = entity.getYRot(partialTicks);
|
| 142 | double entityX = state.x;
|
| 143 | double entityY = state.y;
|
| 144 | double entityZ = state.z;
|
| 145 | Vec3 pos = behavior.getPos(entityX, entityY, entityZ);
|
| 146 | if (pos != null) {
|
| 147 | state.posOnRail = pos;
|
| 148 | Vec3 p0 = behavior.getPosOffs(entityX, entityY, entityZ, 0.3F);
|
| 149 | Vec3 p1 = behavior.getPosOffs(entityX, entityY, entityZ, -0.3F);
|
| 150 | state.frontPos = Objects.requireNonNullElse(p0, pos);
|
| 151 | state.backPos = Objects.requireNonNullElse(p1, pos);
|
| 152 | } else {
|
| 153 | state.posOnRail = null;
|
| 154 | state.frontPos = null;
|
| 155 | state.backPos = null;
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | protected void submitMinecartContents(
|
| 160 | final S state, final BlockModelRenderState blockModel, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final int lightCoords
|
| 161 | ) {
|
| 162 | blockModel.submit(poseStack, submitNodeCollector, lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 163 | }
|
| 164 |
|
| 165 | protected AABB getBoundingBoxForCulling(final T entity, final float partialTicks) {
|
| 166 | AABB aabb = super.getBoundingBoxForCulling(entity, partialTicks);
|
| 167 | return !entity.getDisplayBlockState().isAir() ? aabb.expandTowards(0.0, entity.getDisplayOffset() * 0.75F / 16.0F, 0.0) : aabb;
|
| 168 | }
|
| 169 |
|
| 170 | public Vec3 getRenderOffset(final S state) {
|
| 171 | Vec3 offset = super.getRenderOffset(state);
|
| 172 | return state.isNewRender && state.renderPos != null
|
| 173 | ? offset.add(state.renderPos.x - state.x, state.renderPos.y - state.y, state.renderPos.z - state.z)
|
| 174 | : offset;
|
| 175 | }
|
| 176 | }
|
| 177 |
|