| 1 | package net.minecraft.client.model;
|
| 2 |
|
| 3 | import java.util.function.Function;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.model.geom.ModelPart;
|
| 7 | import net.minecraft.client.model.geom.PartPose;
|
| 8 | import net.minecraft.client.model.geom.builders.CubeDeformation;
|
| 9 | import net.minecraft.client.model.geom.builders.CubeListBuilder;
|
| 10 | import net.minecraft.client.model.geom.builders.MeshDefinition;
|
| 11 | import net.minecraft.client.model.geom.builders.PartDefinition;
|
| 12 | import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
|
| 13 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 14 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class QuadrupedModel<T extends LivingEntityRenderState> extends EntityModel<T> {
|
| 20 | protected final ModelPart head;
|
| 21 | protected final ModelPart body;
|
| 22 | protected final ModelPart rightHindLeg;
|
| 23 | protected final ModelPart leftHindLeg;
|
| 24 | protected final ModelPart rightFrontLeg;
|
| 25 | protected final ModelPart leftFrontLeg;
|
| 26 |
|
| 27 | protected QuadrupedModel(final ModelPart root) {
|
| 28 | this(root, RenderTypes::entityCutout);
|
| 29 | }
|
| 30 |
|
| 31 | protected QuadrupedModel(final ModelPart root, final Function<Identifier, RenderType> renderType) {
|
| 32 | super(root, renderType);
|
| 33 | this.head = root.getChild("head");
|
| 34 | this.body = root.getChild("body");
|
| 35 | this.rightHindLeg = root.getChild("right_hind_leg");
|
| 36 | this.leftHindLeg = root.getChild("left_hind_leg");
|
| 37 | this.rightFrontLeg = root.getChild("right_front_leg");
|
| 38 | this.leftFrontLeg = root.getChild("left_front_leg");
|
| 39 | }
|
| 40 |
|
| 41 | public static MeshDefinition createBodyMesh(final int legSize, final boolean mirrorLeftLeg, final boolean mirrorRightLeg, final CubeDeformation g) {
|
| 42 | MeshDefinition mesh = new MeshDefinition();
|
| 43 | PartDefinition root = mesh.getRoot();
|
| 44 | root.addOrReplaceChild(
|
| 45 | "head", CubeListBuilder.create().texOffs(0, 0).addBox(-4.0F, -4.0F, -8.0F, 8.0F, 8.0F, 8.0F, g), PartPose.offset(0.0F, 18 - legSize, -6.0F)
|
| 46 | );
|
| 47 | root.addOrReplaceChild(
|
| 48 | "body",
|
| 49 | CubeListBuilder.create().texOffs(28, 8).addBox(-5.0F, -10.0F, -7.0F, 10.0F, 16.0F, 8.0F, g),
|
| 50 | PartPose.offsetAndRotation(0.0F, 17 - legSize, 2.0F, (float) (Math.PI / 2), 0.0F, 0.0F)
|
| 51 | );
|
| 52 | createLegs(root, mirrorLeftLeg, mirrorRightLeg, legSize, g);
|
| 53 | return mesh;
|
| 54 | }
|
| 55 |
|
| 56 | public static void createLegs(final PartDefinition root, final boolean mirrorLeftLeg, final boolean mirrorRightLeg, final int legSize, final CubeDeformation g) {
|
| 57 | CubeListBuilder rightLeg = CubeListBuilder.create().mirror(mirrorRightLeg).texOffs(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, legSize, 4.0F, g);
|
| 58 | CubeListBuilder leftLeg = CubeListBuilder.create().mirror(mirrorLeftLeg).texOffs(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, legSize, 4.0F, g);
|
| 59 | root.addOrReplaceChild("right_hind_leg", rightLeg, PartPose.offset(-3.0F, 24 - legSize, 7.0F));
|
| 60 | root.addOrReplaceChild("left_hind_leg", leftLeg, PartPose.offset(3.0F, 24 - legSize, 7.0F));
|
| 61 | root.addOrReplaceChild("right_front_leg", rightLeg, PartPose.offset(-3.0F, 24 - legSize, -5.0F));
|
| 62 | root.addOrReplaceChild("left_front_leg", leftLeg, PartPose.offset(3.0F, 24 - legSize, -5.0F));
|
| 63 | }
|
| 64 |
|
| 65 | public void setupAnim(final T state) {
|
| 66 | super.setupAnim(state);
|
| 67 | this.head.xRot = state.xRot * (float) (Math.PI / 180.0);
|
| 68 | this.head.yRot = state.yRot * (float) (Math.PI / 180.0);
|
| 69 | float animationPos = state.walkAnimationPos;
|
| 70 | float animationSpeed = state.walkAnimationSpeed;
|
| 71 | this.rightHindLeg.xRot = Mth.cos(animationPos * 0.6662F) * 1.4F * animationSpeed;
|
| 72 | this.leftHindLeg.xRot = Mth.cos(animationPos * 0.6662F + (float) Math.PI) * 1.4F * animationSpeed;
|
| 73 | this.rightFrontLeg.xRot = Mth.cos(animationPos * 0.6662F + (float) Math.PI) * 1.4F * animationSpeed;
|
| 74 | this.leftFrontLeg.xRot = Mth.cos(animationPos * 0.6662F) * 1.4F * animationSpeed;
|
| 75 | }
|
| 76 | }
|
| 77 |
|