| 1 | package net.minecraft.client.renderer.chunk;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntPriorityQueue;
|
| 5 | import java.util.BitSet;
|
| 6 | import java.util.EnumSet;
|
| 7 | import java.util.Set;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.util.Util;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class VisGraph {
|
| 16 | private static final int SIZE_IN_BITS = 4;
|
| 17 | private static final int LEN = 16;
|
| 18 | private static final int MASK = 15;
|
| 19 | private static final int SIZE = 4096;
|
| 20 | private static final int X_SHIFT = 0;
|
| 21 | private static final int Z_SHIFT = 4;
|
| 22 | private static final int Y_SHIFT = 8;
|
| 23 | private static final int DX = (int)Math.pow(16.0, 0.0);
|
| 24 | private static final int DZ = (int)Math.pow(16.0, 1.0);
|
| 25 | private static final int DY = (int)Math.pow(16.0, 2.0);
|
| 26 | private static final int INVALID_INDEX = -1;
|
| 27 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 28 | private final BitSet bitSet = new BitSet(4096);
|
| 29 | private static final int[] INDEX_OF_EDGES = Util.make(new int[1352], map -> {
|
| 30 | int min = 0;
|
| 31 | int max = 15;
|
| 32 | int index = 0;
|
| 33 |
|
| 34 | for (int x = 0; x < 16; x++) {
|
| 35 | for (int y = 0; y < 16; y++) {
|
| 36 | for (int z = 0; z < 16; z++) {
|
| 37 | if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) {
|
| 38 | map[index++] = getIndex(x, y, z);
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 | }
|
| 43 | });
|
| 44 | private int empty = 4096;
|
| 45 |
|
| 46 | public void setOpaque(final BlockPos pos) {
|
| 47 | this.bitSet.set(getIndex(pos), true);
|
| 48 | this.empty--;
|
| 49 | }
|
| 50 |
|
| 51 | private static int getIndex(final BlockPos pos) {
|
| 52 | return getIndex(pos.getX() & 15, pos.getY() & 15, pos.getZ() & 15);
|
| 53 | }
|
| 54 |
|
| 55 | private static int getIndex(final int x, final int y, final int z) {
|
| 56 | return x << 0 | y << 8 | z << 4;
|
| 57 | }
|
| 58 |
|
| 59 | public VisibilitySet resolve() {
|
| 60 | VisibilitySet visibilitySet = new VisibilitySet();
|
| 61 | if (4096 - this.empty < 256) {
|
| 62 | visibilitySet.setAll(true);
|
| 63 | } else if (this.empty == 0) {
|
| 64 | visibilitySet.setAll(false);
|
| 65 | } else {
|
| 66 | for (int i : INDEX_OF_EDGES) {
|
| 67 | if (!this.bitSet.get(i)) {
|
| 68 | visibilitySet.add(this.floodFill(i));
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return visibilitySet;
|
| 74 | }
|
| 75 |
|
| 76 | private Set<Direction> floodFill(final int startIndex) {
|
| 77 | Set<Direction> edges = EnumSet.noneOf(Direction.class);
|
| 78 | IntPriorityQueue queue = new IntArrayFIFOQueue();
|
| 79 | queue.enqueue(startIndex);
|
| 80 | this.bitSet.set(startIndex, true);
|
| 81 |
|
| 82 | while (!queue.isEmpty()) {
|
| 83 | int index = queue.dequeueInt();
|
| 84 | this.addEdges(index, edges);
|
| 85 |
|
| 86 | for (Direction direction : DIRECTIONS) {
|
| 87 | int neighborIndex = this.getNeighborIndexAtFace(index, direction);
|
| 88 | if (neighborIndex >= 0 && !this.bitSet.get(neighborIndex)) {
|
| 89 | this.bitSet.set(neighborIndex, true);
|
| 90 | queue.enqueue(neighborIndex);
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return edges;
|
| 96 | }
|
| 97 |
|
| 98 | private void addEdges(final int index, final Set<Direction> edges) {
|
| 99 | int x = index >> 0 & 15;
|
| 100 | if (x == 0) {
|
| 101 | edges.add(Direction.WEST);
|
| 102 | } else if (x == 15) {
|
| 103 | edges.add(Direction.EAST);
|
| 104 | }
|
| 105 |
|
| 106 | int y = index >> 8 & 15;
|
| 107 | if (y == 0) {
|
| 108 | edges.add(Direction.DOWN);
|
| 109 | } else if (y == 15) {
|
| 110 | edges.add(Direction.UP);
|
| 111 | }
|
| 112 |
|
| 113 | int z = index >> 4 & 15;
|
| 114 | if (z == 0) {
|
| 115 | edges.add(Direction.NORTH);
|
| 116 | } else if (z == 15) {
|
| 117 | edges.add(Direction.SOUTH);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | private int getNeighborIndexAtFace(final int index, final Direction direction) {
|
| 122 | return switch (direction) {
|
| 123 | case DOWN -> (index >> 8 & 15) == 0 ? -1 : index - DY;
|
| 124 | case UP -> (index >> 8 & 15) == 15 ? -1 : index + DY;
|
| 125 | case NORTH -> (index >> 4 & 15) == 0 ? -1 : index - DZ;
|
| 126 | case SOUTH -> (index >> 4 & 15) == 15 ? -1 : index + DZ;
|
| 127 | case WEST -> (index >> 0 & 15) == 0 ? -1 : index - DX;
|
| 128 | case EAST -> (index >> 0 & 15) == 15 ? -1 : index + DX;
|
| 129 | };
|
| 130 | }
|
| 131 | }
|
| 132 |
|