| 1 | package net.minecraft.client.renderer.blockentity;
|
| 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.player.LocalPlayer;
|
| 10 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 11 | import net.minecraft.client.renderer.blockentity.state.BeaconRenderState;
|
| 12 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 13 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 14 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 15 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.util.ARGB;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.level.block.entity.BeaconBeamOwner;
|
| 20 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class BeaconRenderer<T extends BlockEntity & BeaconBeamOwner> implements BlockEntityRenderer<T, BeaconRenderState> {
|
| 26 | public static final Identifier BEAM_LOCATION = Identifier.withDefaultNamespace("textures/entity/beacon/beacon_beam.png");
|
| 27 | public static final int MAX_RENDER_Y = 2048;
|
| 28 | private static final float BEAM_SCALE_THRESHOLD = 96.0F;
|
| 29 | public static final float SOLID_BEAM_RADIUS = 0.2F;
|
| 30 | public static final float BEAM_GLOW_RADIUS = 0.25F;
|
| 31 |
|
| 32 | public BeaconRenderState createRenderState() {
|
| 33 | return new BeaconRenderState();
|
| 34 | }
|
| 35 |
|
| 36 | public void extractRenderState(
|
| 37 | final T blockEntity,
|
| 38 | final BeaconRenderState state,
|
| 39 | final float partialTicks,
|
| 40 | final Vec3 cameraPosition,
|
| 41 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 42 | ) {
|
| 43 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 44 | extract(blockEntity, state, partialTicks, cameraPosition);
|
| 45 | }
|
| 46 |
|
| 47 | public static <T extends BlockEntity & BeaconBeamOwner> void extract(
|
| 48 | final T blockEntity, final BeaconRenderState state, final float partialTicks, final Vec3 cameraPosition
|
| 49 | ) {
|
| 50 | state.animationTime = blockEntity.getLevel() != null ? Math.floorMod(blockEntity.getLevel().getGameTime(), 40) + partialTicks : 0.0F;
|
| 51 | state.sections = blockEntity.getBeamSections().stream().map(section -> new BeaconRenderState.Section(section.getColor(), section.getHeight())).toList();
|
| 52 | float distanceToBeacon = (float)cameraPosition.subtract(Vec3.atCenterOf(state.blockPos)).horizontalDistance();
|
| 53 | LocalPlayer player = Minecraft.getInstance().player;
|
| 54 | state.beamRadiusScale = player != null && player.isScoping() ? 1.0F : Math.max(1.0F, distanceToBeacon / 96.0F);
|
| 55 | }
|
| 56 |
|
| 57 | public void submit(final BeaconRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 58 | int beamStart = 0;
|
| 59 |
|
| 60 | for (int i = 0; i < state.sections.size(); i++) {
|
| 61 | BeaconRenderState.Section beamSection = state.sections.get(i);
|
| 62 | submitBeaconBeam(
|
| 63 | poseStack,
|
| 64 | submitNodeCollector,
|
| 65 | state.beamRadiusScale,
|
| 66 | state.animationTime,
|
| 67 | beamStart,
|
| 68 | i == state.sections.size() - 1 ? 2048 : beamSection.height(),
|
| 69 | beamSection.color()
|
| 70 | );
|
| 71 | beamStart += beamSection.height();
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private static void submitBeaconBeam(
|
| 76 | final PoseStack poseStack,
|
| 77 | final SubmitNodeCollector submitNodeCollector,
|
| 78 | final float beamRadiusScale,
|
| 79 | final float animationTime,
|
| 80 | final int beamStart,
|
| 81 | final int height,
|
| 82 | final int color
|
| 83 | ) {
|
| 84 | submitBeaconBeam(
|
| 85 | poseStack, submitNodeCollector, BEAM_LOCATION, 1.0F, animationTime, beamStart, height, color, 0.2F * beamRadiusScale, 0.25F * beamRadiusScale
|
| 86 | );
|
| 87 | }
|
| 88 |
|
| 89 | public static void submitBeaconBeam(
|
| 90 | final PoseStack poseStack,
|
| 91 | final SubmitNodeCollector submitNodeCollector,
|
| 92 | final Identifier beamLocation,
|
| 93 | final float scale,
|
| 94 | final float animationTime,
|
| 95 | final int beamStart,
|
| 96 | final int height,
|
| 97 | final int color,
|
| 98 | final float solidBeamRadius,
|
| 99 | final float beamGlowRadius
|
| 100 | ) {
|
| 101 | int beamEnd = beamStart + height;
|
| 102 | poseStack.pushPose();
|
| 103 | poseStack.translate(0.5, 0.0, 0.5);
|
| 104 | float scroll = height < 0 ? animationTime : -animationTime;
|
| 105 | float texVOff = Mth.frac(scroll * 0.2F - Mth.floor(scroll * 0.1F));
|
| 106 | poseStack.pushPose();
|
| 107 | poseStack.rotateDegrees(Axis.YP, animationTime * 2.25F - 45.0F);
|
| 108 | float wnx = 0.0F;
|
| 109 | float wnz = solidBeamRadius;
|
| 110 | float enx = solidBeamRadius;
|
| 111 | float enz = 0.0F;
|
| 112 | float wsx = -solidBeamRadius;
|
| 113 | float wsz = 0.0F;
|
| 114 | float esx = 0.0F;
|
| 115 | float esz = -solidBeamRadius;
|
| 116 | float uu1 = 0.0F;
|
| 117 | float uu2 = 1.0F;
|
| 118 | float vv2 = -1.0F + texVOff;
|
| 119 | float vv1 = height * scale * (0.5F / solidBeamRadius) + vv2;
|
| 120 | submitNodeCollector.submitCustomGeometry(
|
| 121 | poseStack,
|
| 122 | RenderTypes.beaconBeam(beamLocation, false),
|
| 123 | (pose, buffer) -> renderPart(pose, buffer, color, beamStart, beamEnd, 0.0F, wnz, enx, 0.0F, wsx, 0.0F, 0.0F, esz, 0.0F, 1.0F, vv1, vv2)
|
| 124 | );
|
| 125 | poseStack.popPose();
|
| 126 | wnx = -beamGlowRadius;
|
| 127 | wnz = -beamGlowRadius;
|
| 128 | enx = beamGlowRadius;
|
| 129 | enz = -beamGlowRadius;
|
| 130 | wsx = -beamGlowRadius;
|
| 131 | wsz = beamGlowRadius;
|
| 132 | esx = beamGlowRadius;
|
| 133 | esz = beamGlowRadius;
|
| 134 | uu1 = 0.0F;
|
| 135 | uu2 = 1.0F;
|
| 136 | vv2 = -1.0F + texVOff;
|
| 137 | vv1 = height * scale + vv2;
|
| 138 | submitNodeCollector.submitCustomGeometry(
|
| 139 | poseStack,
|
| 140 | RenderTypes.beaconBeam(beamLocation, true),
|
| 141 | (pose, buffer) -> renderPart(pose, buffer, ARGB.color(32, color), beamStart, beamEnd, wnx, wnz, enx, enz, wsx, wsz, esx, esz, 0.0F, 1.0F, vv1, vv2)
|
| 142 | );
|
| 143 | poseStack.popPose();
|
| 144 | }
|
| 145 |
|
| 146 | private static void renderPart(
|
| 147 | final PoseStack.Pose pose,
|
| 148 | final VertexConsumer builder,
|
| 149 | final int color,
|
| 150 | final int beamStart,
|
| 151 | final int beamEnd,
|
| 152 | final float wnx,
|
| 153 | final float wnz,
|
| 154 | final float enx,
|
| 155 | final float enz,
|
| 156 | final float wsx,
|
| 157 | final float wsz,
|
| 158 | final float esx,
|
| 159 | final float esz,
|
| 160 | final float uu1,
|
| 161 | final float uu2,
|
| 162 | final float vv1,
|
| 163 | final float vv2
|
| 164 | ) {
|
| 165 | renderQuad(pose, builder, color, beamStart, beamEnd, wnx, wnz, enx, enz, uu1, uu2, vv1, vv2);
|
| 166 | renderQuad(pose, builder, color, beamStart, beamEnd, esx, esz, wsx, wsz, uu1, uu2, vv1, vv2);
|
| 167 | renderQuad(pose, builder, color, beamStart, beamEnd, enx, enz, esx, esz, uu1, uu2, vv1, vv2);
|
| 168 | renderQuad(pose, builder, color, beamStart, beamEnd, wsx, wsz, wnx, wnz, uu1, uu2, vv1, vv2);
|
| 169 | }
|
| 170 |
|
| 171 | private static void renderQuad(
|
| 172 | final PoseStack.Pose pose,
|
| 173 | final VertexConsumer builder,
|
| 174 | final int color,
|
| 175 | final int beamStart,
|
| 176 | final int beamEnd,
|
| 177 | final float wnx,
|
| 178 | final float wnz,
|
| 179 | final float enx,
|
| 180 | final float enz,
|
| 181 | final float uu1,
|
| 182 | final float uu2,
|
| 183 | final float vv1,
|
| 184 | final float vv2
|
| 185 | ) {
|
| 186 | addVertex(pose, builder, color, beamEnd, wnx, wnz, uu2, vv1);
|
| 187 | addVertex(pose, builder, color, beamStart, wnx, wnz, uu2, vv2);
|
| 188 | addVertex(pose, builder, color, beamStart, enx, enz, uu1, vv2);
|
| 189 | addVertex(pose, builder, color, beamEnd, enx, enz, uu1, vv1);
|
| 190 | }
|
| 191 |
|
| 192 | private static void addVertex(
|
| 193 | final PoseStack.Pose pose, final VertexConsumer builder, final int color, final int y, final float x, final float z, final float u, final float v
|
| 194 | ) {
|
| 195 | builder.addVertex(pose, x, y, z).setColor(color).setUv(u, v).setOverlay(OverlayTexture.NO_OVERLAY).setLight(15728880).setNormal(pose, 0.0F, 1.0F, 0.0F);
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public boolean shouldRenderOffScreen() {
|
| 200 | return true;
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public int getViewDistance() {
|
| 205 | return Minecraft.getInstance().options.getEffectiveRenderDistance() * 16;
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public boolean shouldRender(final T blockEntity, final Vec3 cameraPosition) {
|
| 210 | return Vec3.atCenterOf(blockEntity.getBlockPos()).multiply(1.0, 0.0, 1.0).closerThan(cameraPosition.multiply(1.0, 0.0, 1.0), this.getViewDistance());
|
| 211 | }
|
| 212 | }
|
| 213 |
|