| 1 | package net.minecraft.client.renderer.blockentity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.player.LocalPlayer;
|
| 8 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 9 | import net.minecraft.client.renderer.blockentity.state.BlockEntityWithBoundingBoxRenderState;
|
| 10 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 11 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.Vec3i;
|
| 14 | import net.minecraft.gizmos.GizmoStyle;
|
| 15 | import net.minecraft.gizmos.Gizmos;
|
| 16 | import net.minecraft.util.ARGB;
|
| 17 | import net.minecraft.world.level.block.Blocks;
|
| 18 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 19 | import net.minecraft.world.level.block.entity.BoundingBoxRenderable;
|
| 20 | import net.minecraft.world.level.block.entity.BoundingBoxRenderable.Mode;
|
| 21 | import net.minecraft.world.level.block.entity.BoundingBoxRenderable.RenderableBox;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.phys.AABB;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import net.minecraft.world.phys.shapes.BitSetDiscreteVoxelShape;
|
| 26 | import net.minecraft.world.phys.shapes.DiscreteVoxelShape;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class BlockEntityWithBoundingBoxRenderer<T extends BlockEntity & BoundingBoxRenderable>
|
| 31 | implements BlockEntityRenderer<T, BlockEntityWithBoundingBoxRenderState> {
|
| 32 | public static final int STRUCTURE_VOIDS_COLOR = ARGB.colorFromFloat(0.2F, 0.75F, 0.75F, 1.0F);
|
| 33 |
|
| 34 | public BlockEntityWithBoundingBoxRenderState createRenderState() {
|
| 35 | return new BlockEntityWithBoundingBoxRenderState();
|
| 36 | }
|
| 37 |
|
| 38 | public void extractRenderState(
|
| 39 | final T blockEntity,
|
| 40 | final BlockEntityWithBoundingBoxRenderState state,
|
| 41 | final float partialTicks,
|
| 42 | final Vec3 cameraPosition,
|
| 43 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 44 | ) {
|
| 45 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 46 | extract(blockEntity, state);
|
| 47 | }
|
| 48 |
|
| 49 | public static <T extends BlockEntity & BoundingBoxRenderable> void extract(final T blockEntity, final BlockEntityWithBoundingBoxRenderState state) {
|
| 50 | LocalPlayer player = Minecraft.getInstance().player;
|
| 51 | state.isVisible = player.canUseGameMasterBlocks() || player.isSpectator();
|
| 52 | state.box = blockEntity.getRenderableBox();
|
| 53 | state.mode = blockEntity.renderMode();
|
| 54 | BlockPos pos = state.box.localPos();
|
| 55 | Vec3i size = state.box.size();
|
| 56 | BlockPos entityPos = state.blockPos;
|
| 57 | BlockPos startingPos = entityPos.offset(pos);
|
| 58 | if (state.isVisible && blockEntity.getLevel() != null && state.mode == Mode.BOX_AND_INVISIBLE_BLOCKS) {
|
| 59 | state.invisibleBlocks = new BlockEntityWithBoundingBoxRenderState.InvisibleBlockType[size.getX() * size.getY() * size.getZ()];
|
| 60 |
|
| 61 | for (int x = 0; x < size.getX(); x++) {
|
| 62 | for (int y = 0; y < size.getY(); y++) {
|
| 63 | for (int z = 0; z < size.getZ(); z++) {
|
| 64 | int index = z * size.getX() * size.getY() + y * size.getX() + x;
|
| 65 | BlockState blockState = blockEntity.getLevel().getBlockState(startingPos.offset(x, y, z));
|
| 66 | if (blockState.isAir()) {
|
| 67 | state.invisibleBlocks[index] = BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.AIR;
|
| 68 | } else if (blockState.is(Blocks.STRUCTURE_VOID)) {
|
| 69 | state.invisibleBlocks[index] = BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.STRUCTURE_VOID;
|
| 70 | } else if (blockState.is(Blocks.BARRIER)) {
|
| 71 | state.invisibleBlocks[index] = BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.BARRIER;
|
| 72 | } else if (blockState.is(Blocks.LIGHT)) {
|
| 73 | state.invisibleBlocks[index] = BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.LIGHT;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | } else {
|
| 79 | state.invisibleBlocks = null;
|
| 80 | }
|
| 81 |
|
| 82 | if (state.isVisible) {
|
| 83 | }
|
| 84 |
|
| 85 | state.structureVoids = null;
|
| 86 | }
|
| 87 |
|
| 88 | public void submit(
|
| 89 | final BlockEntityWithBoundingBoxRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera
|
| 90 | ) {
|
| 91 | if (state.isVisible) {
|
| 92 | Mode mode = state.mode;
|
| 93 | if (mode != Mode.NONE) {
|
| 94 | RenderableBox box = state.box;
|
| 95 | BlockPos pos = box.localPos();
|
| 96 | Vec3i size = box.size();
|
| 97 | if (size.getX() >= 1 && size.getY() >= 1 && size.getZ() >= 1) {
|
| 98 | float lineAlpha = 1.0F;
|
| 99 | float lineRGB = 0.9F;
|
| 100 | BlockPos far = pos.offset(size);
|
| 101 | Gizmos.cuboid(
|
| 102 | new AABB(pos.getX(), pos.getY(), pos.getZ(), far.getX(), far.getY(), far.getZ()).move(state.blockPos),
|
| 103 | GizmoStyle.stroke(ARGB.colorFromFloat(1.0F, 0.9F, 0.9F, 0.9F)),
|
| 104 | true
|
| 105 | );
|
| 106 | this.renderInvisibleBlocks(state, pos, size);
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private void renderInvisibleBlocks(final BlockEntityWithBoundingBoxRenderState state, final BlockPos localPos, final Vec3i size) {
|
| 113 | if (state.invisibleBlocks != null) {
|
| 114 | BlockPos entityPos = state.blockPos;
|
| 115 | BlockPos startingPos = entityPos.offset(localPos);
|
| 116 |
|
| 117 | for (int x = 0; x < size.getX(); x++) {
|
| 118 | for (int y = 0; y < size.getY(); y++) {
|
| 119 | for (int z = 0; z < size.getZ(); z++) {
|
| 120 | int index = z * size.getX() * size.getY() + y * size.getX() + x;
|
| 121 | BlockEntityWithBoundingBoxRenderState.InvisibleBlockType invisibleBlockType = state.invisibleBlocks[index];
|
| 122 | if (invisibleBlockType != null) {
|
| 123 | float scale = invisibleBlockType == BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.AIR ? 0.05F : 0.0F;
|
| 124 | double renderX0 = startingPos.getX() + x + 0.45F - scale;
|
| 125 | double renderY0 = startingPos.getY() + y + 0.45F - scale;
|
| 126 | double renderZ0 = startingPos.getZ() + z + 0.45F - scale;
|
| 127 | double renderX1 = startingPos.getX() + x + 0.55F + scale;
|
| 128 | double renderY1 = startingPos.getY() + y + 0.55F + scale;
|
| 129 | double renderZ1 = startingPos.getZ() + z + 0.55F + scale;
|
| 130 | AABB aabb = new AABB(renderX0, renderY0, renderZ0, renderX1, renderY1, renderZ1);
|
| 131 | if (invisibleBlockType == BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.AIR) {
|
| 132 | Gizmos.cuboid(aabb, GizmoStyle.stroke(ARGB.colorFromFloat(1.0F, 0.5F, 0.5F, 1.0F)));
|
| 133 | } else if (invisibleBlockType == BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.STRUCTURE_VOID) {
|
| 134 | Gizmos.cuboid(aabb, GizmoStyle.stroke(ARGB.colorFromFloat(1.0F, 1.0F, 0.75F, 0.75F)));
|
| 135 | } else if (invisibleBlockType == BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.BARRIER) {
|
| 136 | Gizmos.cuboid(aabb, GizmoStyle.stroke(-65536));
|
| 137 | } else if (invisibleBlockType == BlockEntityWithBoundingBoxRenderState.InvisibleBlockType.LIGHT) {
|
| 138 | Gizmos.cuboid(aabb, GizmoStyle.stroke(-256));
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | private void renderStructureVoids(final BlockEntityWithBoundingBoxRenderState state, final BlockPos startingPosition, final Vec3i size) {
|
| 148 | if (state.structureVoids != null) {
|
| 149 | DiscreteVoxelShape shape = new BitSetDiscreteVoxelShape(size.getX(), size.getY(), size.getZ());
|
| 150 |
|
| 151 | for (int x = 0; x < size.getX(); x++) {
|
| 152 | for (int y = 0; y < size.getY(); y++) {
|
| 153 | for (int z = 0; z < size.getZ(); z++) {
|
| 154 | int index = z * size.getX() * size.getY() + y * size.getX() + x;
|
| 155 | if (state.structureVoids[index]) {
|
| 156 | shape.fill(x, y, z);
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | shape.forAllFaces((direction, xx, yx, zx) -> {
|
| 163 | float scale = 0.48F;
|
| 164 | float x0 = xx + startingPosition.getX() + 0.5F - 0.48F;
|
| 165 | float y0 = yx + startingPosition.getY() + 0.5F - 0.48F;
|
| 166 | float z0 = zx + startingPosition.getZ() + 0.5F - 0.48F;
|
| 167 | float x1 = xx + startingPosition.getX() + 0.5F + 0.48F;
|
| 168 | float y1 = yx + startingPosition.getY() + 0.5F + 0.48F;
|
| 169 | float z1 = zx + startingPosition.getZ() + 0.5F + 0.48F;
|
| 170 | Gizmos.rect(new Vec3(x0, y0, z0), new Vec3(x1, y1, z1), direction, GizmoStyle.fill(STRUCTURE_VOIDS_COLOR));
|
| 171 | });
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public boolean shouldRenderOffScreen() {
|
| 177 | return true;
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public int getViewDistance() {
|
| 182 | return 96;
|
| 183 | }
|
| 184 | }
|
| 185 |
|