| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 9 | import net.minecraft.client.renderer.culling.Frustum;
|
| 10 | import net.minecraft.client.renderer.entity.state.FishingHookRenderState;
|
| 11 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 12 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 13 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 14 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.world.entity.HumanoidArm;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.entity.projectile.FishingHook;
|
| 20 | import net.minecraft.world.item.FishingRodItem;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class FishingHookRenderer extends EntityRenderer<FishingHook, FishingHookRenderState> {
|
| 25 | private static final Identifier TEXTURE_LOCATION = Identifier.withDefaultNamespace("textures/entity/fishing/fishing_hook.png");
|
| 26 | private static final RenderType RENDER_TYPE = RenderTypes.entityCutoutCull(TEXTURE_LOCATION);
|
| 27 | private static final double VIEW_BOBBING_SCALE = 960.0;
|
| 28 |
|
| 29 | public FishingHookRenderer(final EntityRendererProvider.Context context) {
|
| 30 | super(context);
|
| 31 | }
|
| 32 |
|
| 33 | public boolean shouldRender(final FishingHook entity, final Frustum culler, final double camX, final double camY, final double camZ, final float partialTicks) {
|
| 34 | return super.shouldRender(entity, culler, camX, camY, camZ, partialTicks) && entity.getPlayerOwner() != null;
|
| 35 | }
|
| 36 |
|
| 37 | public void submit(
|
| 38 | final FishingHookRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera
|
| 39 | ) {
|
| 40 | poseStack.pushPose();
|
| 41 | poseStack.pushPose();
|
| 42 | poseStack.scale(0.5F, 0.5F, 0.5F);
|
| 43 | poseStack.rotate(camera.orientation);
|
| 44 | submitNodeCollector.submitCustomGeometry(poseStack, RENDER_TYPE, (pose, buffer) -> {
|
| 45 | vertex(buffer, pose, state.lightCoords, 0.0F, 0, 0, 1);
|
| 46 | vertex(buffer, pose, state.lightCoords, 1.0F, 0, 1, 1);
|
| 47 | vertex(buffer, pose, state.lightCoords, 1.0F, 1, 1, 0);
|
| 48 | vertex(buffer, pose, state.lightCoords, 0.0F, 1, 0, 0);
|
| 49 | });
|
| 50 | poseStack.popPose();
|
| 51 | float xa = (float)state.lineOriginOffset.x;
|
| 52 | float ya = (float)state.lineOriginOffset.y;
|
| 53 | float za = (float)state.lineOriginOffset.z;
|
| 54 | float width = Minecraft.getInstance().gameRenderer.gameRenderState().windowRenderState.appropriateLineWidth;
|
| 55 | submitNodeCollector.submitCustomGeometry(poseStack, RenderTypes.lines(), (pose, buffer) -> {
|
| 56 | int steps = 16;
|
| 57 |
|
| 58 | for (int i = 0; i < 16; i++) {
|
| 59 | float a0 = fraction(i, 16);
|
| 60 | float a1 = fraction(i + 1, 16);
|
| 61 | stringVertex(xa, ya, za, buffer, pose, a0, a1, width);
|
| 62 | stringVertex(xa, ya, za, buffer, pose, a1, a0, width);
|
| 63 | }
|
| 64 | });
|
| 65 | poseStack.popPose();
|
| 66 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 67 | }
|
| 68 |
|
| 69 | public static HumanoidArm getHoldingArm(final Player owner) {
|
| 70 | return owner.getMainHandItem().getItem() instanceof FishingRodItem ? owner.getMainArm() : owner.getMainArm().getOpposite();
|
| 71 | }
|
| 72 |
|
| 73 | private Vec3 getPlayerHandPos(final Player owner, final float swing, final float partialTicks) {
|
| 74 | int invert = getHoldingArm(owner) == HumanoidArm.RIGHT ? 1 : -1;
|
| 75 | if (this.entityRenderDispatcher.options.getCameraType().isFirstPerson() && owner == Minecraft.getInstance().player) {
|
| 76 | float fov = this.entityRenderDispatcher.options.fov().get().intValue();
|
| 77 | double viewBobbingScale = 960.0 / fov;
|
| 78 | Vec3 viewVec = this.entityRenderDispatcher
|
| 79 | .camera
|
| 80 | .getNearPlane(fov)
|
| 81 | .getPointOnPlane(invert * 0.525F, -0.1F)
|
| 82 | .scale(viewBobbingScale)
|
| 83 | .yRot(swing * 0.5F)
|
| 84 | .xRot(-swing * 0.7F);
|
| 85 | return owner.getEyePosition(partialTicks).add(viewVec);
|
| 86 | } else {
|
| 87 | float ownerYRot = Mth.lerp(partialTicks, owner.yBodyRotO, owner.yBodyRot) * (float) (Math.PI / 180.0);
|
| 88 | double sin = Mth.sin(ownerYRot);
|
| 89 | double cos = Mth.cos(ownerYRot);
|
| 90 | float playerScale = owner.getScale();
|
| 91 | double rightOffset = invert * 0.35 * playerScale;
|
| 92 | double forwardOffset = 0.8 * playerScale;
|
| 93 | float yOffset = owner.isCrouching() ? -0.1875F : 0.0F;
|
| 94 | return owner.getEyePosition(partialTicks)
|
| 95 | .add(-cos * rightOffset - sin * forwardOffset, yOffset - 0.45 * playerScale, -sin * rightOffset + cos * forwardOffset);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | private static float fraction(final int i, final int steps) {
|
| 100 | return (float)i / steps;
|
| 101 | }
|
| 102 |
|
| 103 | private static void vertex(
|
| 104 | final VertexConsumer builder, final PoseStack.Pose pose, final int lightCoords, final float x, final int y, final int u, final int v
|
| 105 | ) {
|
| 106 | builder.addVertex(pose, x - 0.5F, y - 0.5F, 0.0F)
|
| 107 | .setColor(-1)
|
| 108 | .setUv(u, v)
|
| 109 | .setOverlay(OverlayTexture.NO_OVERLAY)
|
| 110 | .setLight(lightCoords)
|
| 111 | .setNormal(pose, 0.0F, 1.0F, 0.0F);
|
| 112 | }
|
| 113 |
|
| 114 | private static void stringVertex(
|
| 115 | final float xa,
|
| 116 | final float ya,
|
| 117 | final float za,
|
| 118 | final VertexConsumer stringBuffer,
|
| 119 | final PoseStack.Pose stringPose,
|
| 120 | final float aa,
|
| 121 | final float nexta,
|
| 122 | final float width
|
| 123 | ) {
|
| 124 | float x = xa * aa;
|
| 125 | float y = ya * (aa * aa + aa) * 0.5F + 0.25F;
|
| 126 | float z = za * aa;
|
| 127 | float nx = xa * nexta - x;
|
| 128 | float ny = ya * (nexta * nexta + nexta) * 0.5F + 0.25F - y;
|
| 129 | float nz = za * nexta - z;
|
| 130 | float length = Mth.sqrt(nx * nx + ny * ny + nz * nz);
|
| 131 | nx /= length;
|
| 132 | ny /= length;
|
| 133 | nz /= length;
|
| 134 | stringBuffer.addVertex(stringPose, x, y, z).setColor(-16777216).setNormal(stringPose, nx, ny, nz).setLineWidth(width);
|
| 135 | }
|
| 136 |
|
| 137 | public FishingHookRenderState createRenderState() {
|
| 138 | return new FishingHookRenderState();
|
| 139 | }
|
| 140 |
|
| 141 | public void extractRenderState(final FishingHook entity, final FishingHookRenderState state, final float partialTicks) {
|
| 142 | super.extractRenderState(entity, state, partialTicks);
|
| 143 | Player owner = entity.getPlayerOwner();
|
| 144 | if (owner == null) {
|
| 145 | state.lineOriginOffset = Vec3.ZERO;
|
| 146 | } else {
|
| 147 | float swing = owner.getSwingAnimation(partialTicks);
|
| 148 | float swing2 = Mth.sin(Mth.sqrt(swing) * (float) Math.PI);
|
| 149 | Vec3 playerPos = this.getPlayerHandPos(owner, swing2, partialTicks);
|
| 150 | Vec3 hookPos = entity.getPosition(partialTicks).add(0.0, 0.25, 0.0);
|
| 151 | state.lineOriginOffset = playerPos.subtract(hookPos);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | protected boolean affectedByCulling(final FishingHook entity) {
|
| 156 | return false;
|
| 157 | }
|
| 158 | }
|
| 159 |
|