| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import com.mojang.math.Axis;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.model.geom.ModelLayerLocation;
|
| 10 | import net.minecraft.client.model.geom.ModelLayers;
|
| 11 | import net.minecraft.client.model.monster.guardian.GuardianModel;
|
| 12 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 13 | import net.minecraft.client.renderer.culling.Frustum;
|
| 14 | import net.minecraft.client.renderer.entity.state.GuardianRenderState;
|
| 15 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 16 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 17 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 18 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.Mth;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.entity.LivingEntity;
|
| 23 | import net.minecraft.world.entity.monster.Guardian;
|
| 24 | import net.minecraft.world.phys.AABB;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class GuardianRenderer extends MobRenderer<Guardian, GuardianRenderState, GuardianModel> {
|
| 30 | private static final Identifier GUARDIAN_LOCATION = Identifier.withDefaultNamespace("textures/entity/guardian/guardian.png");
|
| 31 | private static final Identifier GUARDIAN_BEAM_LOCATION = Identifier.withDefaultNamespace("textures/entity/guardian/guardian_beam.png");
|
| 32 | private static final RenderType BEAM_RENDER_TYPE = RenderTypes.entityCutout(GUARDIAN_BEAM_LOCATION);
|
| 33 |
|
| 34 | public GuardianRenderer(final EntityRendererProvider.Context context) {
|
| 35 | this(context, 0.5F, ModelLayers.GUARDIAN);
|
| 36 | }
|
| 37 |
|
| 38 | protected GuardianRenderer(final EntityRendererProvider.Context context, final float shadow, final ModelLayerLocation modelId) {
|
| 39 | super(context, new GuardianModel(context.bakeLayer(modelId)), shadow);
|
| 40 | }
|
| 41 |
|
| 42 | public boolean shouldRender(final Guardian entity, final Frustum culler, final double camX, final double camY, final double camZ, final float partialTicks) {
|
| 43 | if (super.shouldRender(entity, culler, camX, camY, camZ, partialTicks)) {
|
| 44 | return true;
|
| 45 | }
|
| 46 |
|
| 47 | if (entity.hasActiveAttackTarget()) {
|
| 48 | LivingEntity lookAtEntity = entity.getActiveAttackTarget();
|
| 49 | if (lookAtEntity != null) {
|
| 50 | Vec3 targetPos = this.getPosition(lookAtEntity, lookAtEntity.getBbHeight() * 0.5, 1.0F);
|
| 51 | Vec3 startPos = this.getPosition(entity, entity.getEyeHeight(), 1.0F);
|
| 52 | return culler.isVisible(new AABB(startPos.x, startPos.y, startPos.z, targetPos.x, targetPos.y, targetPos.z));
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | return false;
|
| 57 | }
|
| 58 |
|
| 59 | private Vec3 getPosition(final LivingEntity entity, final double yOffset, final float partialTicks) {
|
| 60 | double sx = Mth.lerp(partialTicks, entity.xOld, entity.getX());
|
| 61 | double sy = Mth.lerp(partialTicks, entity.yOld, entity.getY()) + yOffset;
|
| 62 | double sz = Mth.lerp(partialTicks, entity.zOld, entity.getZ());
|
| 63 | return new Vec3(sx, sy, sz);
|
| 64 | }
|
| 65 |
|
| 66 | public void submit(final GuardianRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 67 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 68 | Vec3 targetPosition = state.attackTargetPosition;
|
| 69 | if (targetPosition != null) {
|
| 70 | float texVOff = state.attackTime * 0.5F % 1.0F;
|
| 71 | poseStack.pushPose();
|
| 72 | poseStack.translate(0.0F, state.eyeHeight, 0.0F);
|
| 73 | renderBeam(poseStack, submitNodeCollector, targetPosition.subtract(state.eyePosition), state.attackTime, state.attackScale, texVOff);
|
| 74 | poseStack.popPose();
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | private static void renderBeam(
|
| 79 | final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, Vec3 beamVector, final float timeInTicks, final float scale, final float texVOff
|
| 80 | ) {
|
| 81 | float length = (float)(beamVector.length() + 1.0);
|
| 82 | beamVector = beamVector.normalize();
|
| 83 | float xRot = (float)Math.acos(beamVector.y);
|
| 84 | float yRot = (float) (Math.PI / 2) - (float)Math.atan2(beamVector.z, beamVector.x);
|
| 85 | poseStack.rotateDegrees(Axis.YP, yRot * (180.0F / (float)Math.PI));
|
| 86 | poseStack.rotateDegrees(Axis.XP, xRot * (180.0F / (float)Math.PI));
|
| 87 | float rot = timeInTicks * 0.05F * -1.5F;
|
| 88 | float colorScale = scale * scale;
|
| 89 | int red = 64 + (int)(colorScale * 191.0F);
|
| 90 | int green = 32 + (int)(colorScale * 191.0F);
|
| 91 | int blue = 128 - (int)(colorScale * 64.0F);
|
| 92 | float rr1 = 0.2F;
|
| 93 | float rr2 = 0.282F;
|
| 94 | float wnx = Mth.cos(rot + (float) (Math.PI * 3.0 / 4.0)) * 0.282F;
|
| 95 | float wnz = Mth.sin(rot + (float) (Math.PI * 3.0 / 4.0)) * 0.282F;
|
| 96 | float enx = Mth.cos(rot + (float) (Math.PI / 4)) * 0.282F;
|
| 97 | float enz = Mth.sin(rot + (float) (Math.PI / 4)) * 0.282F;
|
| 98 | float wsx = Mth.cos(rot + ((float) Math.PI * 5.0F / 4.0F)) * 0.282F;
|
| 99 | float wsz = Mth.sin(rot + ((float) Math.PI * 5.0F / 4.0F)) * 0.282F;
|
| 100 | float esx = Mth.cos(rot + ((float) Math.PI * 7.0F / 4.0F)) * 0.282F;
|
| 101 | float esz = Mth.sin(rot + ((float) Math.PI * 7.0F / 4.0F)) * 0.282F;
|
| 102 | float wx = Mth.cos(rot + (float) Math.PI) * 0.2F;
|
| 103 | float wz = Mth.sin(rot + (float) Math.PI) * 0.2F;
|
| 104 | float ex = Mth.cos(rot + 0.0F) * 0.2F;
|
| 105 | float ez = Mth.sin(rot + 0.0F) * 0.2F;
|
| 106 | float nx = Mth.cos(rot + (float) (Math.PI / 2)) * 0.2F;
|
| 107 | float nz = Mth.sin(rot + (float) (Math.PI / 2)) * 0.2F;
|
| 108 | float sx = Mth.cos(rot + (float) (Math.PI * 3.0 / 2.0)) * 0.2F;
|
| 109 | float sz = Mth.sin(rot + (float) (Math.PI * 3.0 / 2.0)) * 0.2F;
|
| 110 | float top = length;
|
| 111 | float minU = 0.0F;
|
| 112 | float maxU = 0.4999F;
|
| 113 | float minV = -1.0F + texVOff;
|
| 114 | float maxV = minV + length * 2.5F;
|
| 115 | submitNodeCollector.submitCustomGeometry(poseStack, BEAM_RENDER_TYPE, (pose, buffer) -> {
|
| 116 | vertex(buffer, pose, wx, top, wz, red, green, blue, 0.4999F, maxV);
|
| 117 | vertex(buffer, pose, wx, 0.0F, wz, red, green, blue, 0.4999F, minV);
|
| 118 | vertex(buffer, pose, ex, 0.0F, ez, red, green, blue, 0.0F, minV);
|
| 119 | vertex(buffer, pose, ex, top, ez, red, green, blue, 0.0F, maxV);
|
| 120 | vertex(buffer, pose, nx, top, nz, red, green, blue, 0.4999F, maxV);
|
| 121 | vertex(buffer, pose, nx, 0.0F, nz, red, green, blue, 0.4999F, minV);
|
| 122 | vertex(buffer, pose, sx, 0.0F, sz, red, green, blue, 0.0F, minV);
|
| 123 | vertex(buffer, pose, sx, top, sz, red, green, blue, 0.0F, maxV);
|
| 124 | float vBase = Mth.floor(timeInTicks) % 2 == 0 ? 0.5F : 0.0F;
|
| 125 | vertex(buffer, pose, wnx, top, wnz, red, green, blue, 0.5F, vBase + 0.5F);
|
| 126 | vertex(buffer, pose, enx, top, enz, red, green, blue, 1.0F, vBase + 0.5F);
|
| 127 | vertex(buffer, pose, esx, top, esz, red, green, blue, 1.0F, vBase);
|
| 128 | vertex(buffer, pose, wsx, top, wsz, red, green, blue, 0.5F, vBase);
|
| 129 | });
|
| 130 | }
|
| 131 |
|
| 132 | private static void vertex(
|
| 133 | final VertexConsumer builder,
|
| 134 | final PoseStack.Pose pose,
|
| 135 | final float x,
|
| 136 | final float y,
|
| 137 | final float z,
|
| 138 | final int red,
|
| 139 | final int green,
|
| 140 | final int blue,
|
| 141 | final float u,
|
| 142 | final float v
|
| 143 | ) {
|
| 144 | builder.addVertex(pose, x, y, z)
|
| 145 | .setColor(red, green, blue, 255)
|
| 146 | .setUv(u, v)
|
| 147 | .setOverlay(OverlayTexture.NO_OVERLAY)
|
| 148 | .setLight(15728880)
|
| 149 | .setNormal(pose, 0.0F, 1.0F, 0.0F);
|
| 150 | }
|
| 151 |
|
| 152 | public Identifier getTextureLocation(final GuardianRenderState state) {
|
| 153 | return GUARDIAN_LOCATION;
|
| 154 | }
|
| 155 |
|
| 156 | public GuardianRenderState createRenderState() {
|
| 157 | return new GuardianRenderState();
|
| 158 | }
|
| 159 |
|
| 160 | public void extractRenderState(final Guardian entity, final GuardianRenderState state, final float partialTicks) {
|
| 161 | super.extractRenderState(entity, state, partialTicks);
|
| 162 | state.spikesAnimation = entity.getSpikesAnimation(partialTicks);
|
| 163 | state.tailAnimation = entity.getTailAnimation(partialTicks);
|
| 164 | state.eyePosition = entity.getEyePosition(partialTicks);
|
| 165 | Entity lookAtEntity = getEntityToLookAt(entity);
|
| 166 | if (lookAtEntity != null) {
|
| 167 | state.lookDirection = entity.getViewVector(partialTicks);
|
| 168 | state.lookAtPosition = lookAtEntity.getEyePosition(partialTicks);
|
| 169 | } else {
|
| 170 | state.lookDirection = null;
|
| 171 | state.lookAtPosition = null;
|
| 172 | }
|
| 173 |
|
| 174 | LivingEntity targetEntity = entity.getActiveAttackTarget();
|
| 175 | if (targetEntity != null) {
|
| 176 | state.attackScale = entity.getAttackAnimationScale(partialTicks);
|
| 177 | state.attackTime = entity.getClientSideAttackTime() + partialTicks;
|
| 178 | state.attackTargetPosition = this.getPosition(targetEntity, targetEntity.getBbHeight() * 0.5, partialTicks);
|
| 179 | } else {
|
| 180 | state.attackTargetPosition = null;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | private static @Nullable Entity getEntityToLookAt(final Guardian entity) {
|
| 185 | Entity lookAtEntity = Minecraft.getInstance().getCameraEntity();
|
| 186 | return entity.hasActiveAttackTarget() ? entity.getActiveAttackTarget() : lookAtEntity;
|
| 187 | }
|
| 188 | }
|
| 189 |
|