|
| 1 | +package ca.fxco.moreculling.utils; |
| 2 | + |
| 3 | +import net.minecraft.Util; |
| 4 | +import net.minecraft.core.Direction; |
| 5 | +import net.minecraft.world.phys.AABB; |
| 6 | +import net.minecraft.world.phys.shapes.BooleanOp; |
| 7 | +import net.minecraft.world.phys.shapes.Shapes; |
| 8 | +import net.minecraft.world.phys.shapes.VoxelShape; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class ShapeUtils { |
| 13 | + private static final int[] DIR_ROT_X_2D_DATA = Util.make(new int[6], arr -> { |
| 14 | + arr[Direction.DOWN.ordinal()] = 2; |
| 15 | + arr[Direction.UP.ordinal()] = 0; |
| 16 | + arr[Direction.NORTH.ordinal()] = 3; |
| 17 | + arr[Direction.SOUTH.ordinal()] = 1; |
| 18 | + arr[Direction.WEST.ordinal()] = -1; |
| 19 | + arr[Direction.EAST.ordinal()] = -1; |
| 20 | + }); |
| 21 | + private static final int[] DIR_ROT_Z_2D_DATA = Util.make(new int[6], arr -> { |
| 22 | + arr[Direction.DOWN.ordinal()] = 2; |
| 23 | + arr[Direction.UP.ordinal()] = 0; |
| 24 | + arr[Direction.NORTH.ordinal()] = -1; |
| 25 | + arr[Direction.SOUTH.ordinal()] = -1; |
| 26 | + arr[Direction.WEST.ordinal()] = 3; |
| 27 | + arr[Direction.EAST.ordinal()] = 1; |
| 28 | + }); |
| 29 | + |
| 30 | + public static VoxelShape rotateShapeAroundY(Direction from, Direction to, VoxelShape shape) { |
| 31 | + return rotateShapeUnoptimizedAroundY(from, to, shape).optimize(); |
| 32 | + } |
| 33 | + |
| 34 | + public static VoxelShape rotateShapeUnoptimizedAroundY(Direction from, Direction to, VoxelShape shape) { |
| 35 | + if (isY(from) || isY(to)) { |
| 36 | + throw new IllegalArgumentException("Invalid Direction!"); |
| 37 | + } |
| 38 | + if (from == to) { |
| 39 | + return shape; |
| 40 | + } |
| 41 | + |
| 42 | + List<AABB> sourceBoxes = shape.toAabbs(); |
| 43 | + VoxelShape rotatedShape = Shapes.empty(); |
| 44 | + int times = (to.get2DDataValue() - from.get2DDataValue() + 4) % 4; |
| 45 | + for (AABB box : sourceBoxes) { |
| 46 | + for (int i = 0; i < times; i++) |
| 47 | + { |
| 48 | + box = new AABB(1 - box.maxZ, box.minY, box.minX, 1 - box.minZ, box.maxY, box.maxX); |
| 49 | + } |
| 50 | + rotatedShape = orUnoptimized(rotatedShape, Shapes.create(box)); |
| 51 | + } |
| 52 | + |
| 53 | + return rotatedShape; |
| 54 | + } |
| 55 | + |
| 56 | + public static VoxelShape rotateShapeAroundX(Direction from, Direction to, VoxelShape shape) { |
| 57 | + return rotateShapeUnoptimizedAroundX(from, to, shape).optimize(); |
| 58 | + } |
| 59 | + |
| 60 | + public static VoxelShape rotateShapeUnoptimizedAroundX(Direction from, Direction to, VoxelShape shape) { |
| 61 | + if (isX(from) || isX(to)) { |
| 62 | + throw new IllegalArgumentException("Invalid Direction!"); |
| 63 | + } |
| 64 | + if (from == to) |
| 65 | + { |
| 66 | + return shape; |
| 67 | + } |
| 68 | + |
| 69 | + List<AABB> sourceBoxes = shape.toAabbs(); |
| 70 | + VoxelShape rotatedShape = Shapes.empty(); |
| 71 | + int times = (DIR_ROT_X_2D_DATA[to.ordinal()] - DIR_ROT_X_2D_DATA[from.ordinal()] + 4) % 4; |
| 72 | + for (AABB box : sourceBoxes) |
| 73 | + { |
| 74 | + for (int i = 0; i < times; i++) |
| 75 | + { |
| 76 | + box = new AABB(box.minX, 1 - box.maxZ, box.minY, box.maxX, 1 - box.minZ, box.maxY); |
| 77 | + } |
| 78 | + rotatedShape = orUnoptimized(rotatedShape, Shapes.create(box)); |
| 79 | + } |
| 80 | + |
| 81 | + return rotatedShape; |
| 82 | + } |
| 83 | + |
| 84 | + public static VoxelShape rotateShapeAroundZ(Direction from, Direction to, VoxelShape shape) { |
| 85 | + return rotateShapeUnoptimizedAroundZ(from, to, shape).optimize(); |
| 86 | + } |
| 87 | + |
| 88 | + public static VoxelShape rotateShapeUnoptimizedAroundZ(Direction from, Direction to, VoxelShape shape) { |
| 89 | + if (isZ(from) || isZ(to)) |
| 90 | + { |
| 91 | + throw new IllegalArgumentException("Invalid Direction!"); |
| 92 | + } |
| 93 | + if (from == to) |
| 94 | + { |
| 95 | + return shape; |
| 96 | + } |
| 97 | + |
| 98 | + List<AABB> sourceBoxes = shape.toAabbs(); |
| 99 | + VoxelShape rotatedShape = Shapes.empty(); |
| 100 | + int times = (DIR_ROT_Z_2D_DATA[to.ordinal()] - DIR_ROT_Z_2D_DATA[from.ordinal()] + 4) % 4; |
| 101 | + for (AABB box : sourceBoxes) |
| 102 | + { |
| 103 | + for (int i = 0; i < times; i++) |
| 104 | + { |
| 105 | + //noinspection SuspiciousNameCombination |
| 106 | + box = new AABB(box.minY, 1 - box.maxX, box.minZ, box.maxY, 1 - box.minX, box.maxZ); |
| 107 | + } |
| 108 | + rotatedShape = orUnoptimized(rotatedShape, Shapes.create(box)); |
| 109 | + } |
| 110 | + |
| 111 | + return rotatedShape; |
| 112 | + } |
| 113 | + |
| 114 | + public static boolean isX(Direction dir) { |
| 115 | + return dir.getAxis() == Direction.Axis.X; |
| 116 | + } |
| 117 | + |
| 118 | + public static boolean isY(Direction dir) { |
| 119 | + return dir.getAxis() == Direction.Axis.Y; |
| 120 | + } |
| 121 | + |
| 122 | + public static boolean isZ(Direction dir) { |
| 123 | + return dir.getAxis() == Direction.Axis.Z; |
| 124 | + } |
| 125 | + |
| 126 | + public static VoxelShape orUnoptimized(VoxelShape first, VoxelShape second) { |
| 127 | + return Shapes.joinUnoptimized(first, second, BooleanOp.OR); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments