| 1 | package net.minecraft.client.renderer.entity.layers;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.model.Model;
|
| 8 | import net.minecraft.client.model.geom.ModelPart;
|
| 9 | import net.minecraft.client.model.player.PlayerModel;
|
| 10 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 11 | import net.minecraft.client.renderer.entity.LivingEntityRenderer;
|
| 12 | import net.minecraft.client.renderer.entity.state.AvatarRenderState;
|
| 13 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public abstract class StuckInBodyLayer<M extends PlayerModel, S> extends RenderLayer<AvatarRenderState, M> {
|
| 20 | private final Model<S> model;
|
| 21 | private final S modelState;
|
| 22 | private final Identifier texture;
|
| 23 | private final StuckInBodyLayer.PlacementStyle placementStyle;
|
| 24 |
|
| 25 | public StuckInBodyLayer(
|
| 26 | final LivingEntityRenderer<?, AvatarRenderState, M> renderer,
|
| 27 | final Model<S> model,
|
| 28 | final S modelState,
|
| 29 | final Identifier texture,
|
| 30 | final StuckInBodyLayer.PlacementStyle placementStyle
|
| 31 | ) {
|
| 32 | super(renderer);
|
| 33 | this.model = model;
|
| 34 | this.modelState = modelState;
|
| 35 | this.texture = texture;
|
| 36 | this.placementStyle = placementStyle;
|
| 37 | }
|
| 38 |
|
| 39 | protected abstract int numStuck(final AvatarRenderState state);
|
| 40 |
|
| 41 | private void submitStuckItem(
|
| 42 | final PoseStack poseStack,
|
| 43 | final SubmitNodeCollector submitNodeCollector,
|
| 44 | final int lightCoords,
|
| 45 | final float directionX,
|
| 46 | final float directionY,
|
| 47 | final float directionZ,
|
| 48 | final int outlineColor
|
| 49 | ) {
|
| 50 | float directionXZ = Mth.sqrt(directionX * directionX + directionZ * directionZ);
|
| 51 | float yRot = (float)(Math.atan2(directionX, directionZ) * 180.0F / (float)Math.PI);
|
| 52 | float xRot = (float)(Math.atan2(directionY, directionXZ) * 180.0F / (float)Math.PI);
|
| 53 | poseStack.rotateDegrees(Axis.YP, yRot - 90.0F);
|
| 54 | poseStack.rotateDegrees(Axis.ZP, xRot);
|
| 55 | submitNodeCollector.submitModel(this.model, this.modelState, poseStack, this.texture, lightCoords, OverlayTexture.NO_OVERLAY, outlineColor);
|
| 56 | }
|
| 57 |
|
| 58 | public void submit(
|
| 59 | final PoseStack poseStack,
|
| 60 | final SubmitNodeCollector submitNodeCollector,
|
| 61 | final int lightCoords,
|
| 62 | final AvatarRenderState state,
|
| 63 | final float yRot,
|
| 64 | final float xRot
|
| 65 | ) {
|
| 66 | int count = this.numStuck(state);
|
| 67 | if (count > 0) {
|
| 68 | RandomSource random = RandomSource.createThreadLocalInstance(state.id);
|
| 69 |
|
| 70 | for (int i = 0; i < count; i++) {
|
| 71 | poseStack.pushPose();
|
| 72 | ModelPart modelPart = this.getParentModel().getRandomBodyPart(random);
|
| 73 | ModelPart.Cube cube = modelPart.getRandomCube(random);
|
| 74 | modelPart.translateAndRotate(poseStack);
|
| 75 | float midX = random.nextFloat();
|
| 76 | float midY = random.nextFloat();
|
| 77 | float midZ = random.nextFloat();
|
| 78 | if (this.placementStyle == StuckInBodyLayer.PlacementStyle.ON_SURFACE) {
|
| 79 | int plane = random.nextInt(3);
|
| 80 | switch (plane) {
|
| 81 | case 0:
|
| 82 | midX = snapToFace(midX);
|
| 83 | break;
|
| 84 | case 1:
|
| 85 | midY = snapToFace(midY);
|
| 86 | break;
|
| 87 | default:
|
| 88 | midZ = snapToFace(midZ);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | poseStack.translate(
|
| 93 | Mth.lerp(midX, cube.minX, cube.maxX) / 16.0F, Mth.lerp(midY, cube.minY, cube.maxY) / 16.0F, Mth.lerp(midZ, cube.minZ, cube.maxZ) / 16.0F
|
| 94 | );
|
| 95 | this.submitStuckItem(poseStack, submitNodeCollector, lightCoords, -(midX * 2.0F - 1.0F), -(midY * 2.0F - 1.0F), -(midZ * 2.0F - 1.0F), state.outlineColor);
|
| 96 | poseStack.popPose();
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | private static float snapToFace(final float value) {
|
| 102 | return value > 0.5F ? 1.0F : 0.5F;
|
| 103 | }
|
| 104 |
|
| 105 | @Environment(EnvType.CLIENT)
|
| 106 | public enum PlacementStyle {
|
| 107 | IN_CUBE,
|
| 108 | ON_SURFACE;
|
| 109 | }
|
| 110 | }
|
| 111 |
|