improved shape for logistical casing

some changes to voxel shaper

Signed-off-by: Zelophed <zefren1@googlemail.com>
This commit is contained in:
zelophed 2019-12-08 02:23:19 +01:00 committed by Zelophed
parent 74721cda95
commit 6fb24e0d86
3 changed files with 90 additions and 12 deletions

View file

@ -12,6 +12,7 @@ import net.minecraft.util.Direction.AxisDirection;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes; import net.minecraft.util.math.shapes.VoxelShapes;
import org.apache.commons.lang3.tuple.ImmutablePair;
public class VoxelShaper { public class VoxelShaper {
@ -68,6 +69,29 @@ public class VoxelShaper {
} }
public static VoxelShaper forDirectional(VoxelShape southShape) { public static VoxelShaper forDirectional(VoxelShape southShape) {
return forDirectional(southShape, Direction.SOUTH);
}
public static VoxelShaper forDirectional(VoxelShape shape, Direction facing){
if (facing != Direction.UP) {
ImmutablePair<Integer, Integer> rot = rotationValuesForDirection(facing);
shape = rotatedCopy(shape, 360 - rot.getLeft(), 360 - rot.getRight());
}
return forDirectionsFacingUp(shape, Direction.values());
}
private static VoxelShaper forDirectionsFacingUp(VoxelShape shape, Direction[] directions){
VoxelShaper voxelShaper = new VoxelShaper();
for (Direction dir : directions) {
ImmutablePair<Integer, Integer> rotation = rotationValuesForDirection(dir);
voxelShaper.shapes.put(dir, rotatedCopy(shape, rotation.getLeft(), rotation.getRight()));
}
return voxelShaper;
}
public static VoxelShaper forDirectionalOld(VoxelShape southShape) {
VoxelShaper voxelShaper = new VoxelShaper(); VoxelShaper voxelShaper = new VoxelShaper();
for (Direction facing : Direction.values()) { for (Direction facing : Direction.values()) {
int rotX = facing.getAxis().isVertical() ? (facing == Direction.UP ? 270 : 90) : 0; int rotX = facing.getAxis().isVertical() ? (facing == Direction.UP ? 270 : 90) : 0;
@ -83,11 +107,18 @@ public class VoxelShaper {
return this; return this;
} }
private static Direction axisAsFace(Axis axis) { private static ImmutablePair<Integer, Integer> rotationValuesForDirection(Direction facing) {
//assume facing up as the default rotation
return ImmutablePair.of(
facing == Direction.UP ? 0 : (Direction.Plane.VERTICAL.test(facing) ? 180 : 90),
Direction.Plane.VERTICAL.test(facing) ? 0 : (int) -facing.getHorizontalAngle());
}
public static Direction axisAsFace(Axis axis) {
return Direction.getFacingFromAxis(AxisDirection.POSITIVE, axis); return Direction.getFacingFromAxis(AxisDirection.POSITIVE, axis);
} }
private static VoxelShape rotatedCopy(VoxelShape shape, int rotX, int rotY) { private static VoxelShape rotatedCopy(VoxelShape shape, int rotationX, int rotationY) {
MutableObject<VoxelShape> result = new MutableObject<>(VoxelShapes.empty()); MutableObject<VoxelShape> result = new MutableObject<>(VoxelShapes.empty());
shape.forEachBox((x1, y1, z1, x2, y2, z2) -> { shape.forEachBox((x1, y1, z1, x2, y2, z2) -> {
@ -95,10 +126,10 @@ public class VoxelShaper {
Vec3d v1 = new Vec3d(x1, y1, z1).scale(16).subtract(center); Vec3d v1 = new Vec3d(x1, y1, z1).scale(16).subtract(center);
Vec3d v2 = new Vec3d(x2, y2, z2).scale(16).subtract(center); Vec3d v2 = new Vec3d(x2, y2, z2).scale(16).subtract(center);
v1 = VecHelper.rotate(v1, rotX, Axis.X); v1 = VecHelper.rotate(v1, rotationX, Axis.X);
v1 = VecHelper.rotate(v1, rotY, Axis.Y).add(center); v1 = VecHelper.rotate(v1, rotationY, Axis.Y).add(center);
v2 = VecHelper.rotate(v2, rotX, Axis.X); v2 = VecHelper.rotate(v2, rotationX, Axis.X);
v2 = VecHelper.rotate(v2, rotY, Axis.Y).add(center); v2 = VecHelper.rotate(v2, rotationY, Axis.Y).add(center);
VoxelShape rotated = Block.makeCuboidShape(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z); VoxelShape rotated = Block.makeCuboidShape(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z);
result.setValue(VoxelShapes.or(result.getValue(), rotated)); result.setValue(VoxelShapes.or(result.getValue(), rotated));

View file

@ -1,10 +1,45 @@
package com.simibubi.create.foundation.utility; package com.simibubi.create.foundation.utility;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.util.Direction;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import static net.minecraft.block.Block.makeCuboidShape;
public class VoxelShapers { public class VoxelShapers {
public static final VoxelShaper SHORT_CASING = VoxelShaper public static final VoxelShape
.forDirectional(Block.makeCuboidShape(0, 0, 0, 16, 16, 12)); LOGISTICAL_CASING_SINGLE_SHAPE = VoxelShapes.or(
makeCuboidShape(0, 0, 0, 16, 2, 16),
makeCuboidShape(1, 1, 1, 15, 15, 15),
makeCuboidShape(0, 14, 0, 16, 16, 16))
;
private static final VoxelShape
LOGISTICAL_CASING_MIDDLE_SHAPE = VoxelShapes.or(
makeCuboidShape(1,0,1,15,16,15),
makeCuboidShape(0,0,0,2,16,2),
makeCuboidShape(14,0,0,16,16,2),
makeCuboidShape(0,0,14,2,16,16),
makeCuboidShape(14,0,14,16,16,16)),
LOGISTICAL_CASING_CAP_SHAPE = VoxelShapes.or(
LOGISTICAL_CASING_MIDDLE_SHAPE,
makeCuboidShape(0,0,0,16,2,16))
;
public static final VoxelShaper
SHORT_CASING = VoxelShaper.forDirectional(makeCuboidShape(0, 0, 0, 16, 12, 16), Direction.UP),
LOGISTICAL_CASING_MIDDLE = VoxelShaper.forDirectional(LOGISTICAL_CASING_MIDDLE_SHAPE, Direction.UP),
LOGISTICAL_CASING_CAP = VoxelShaper.forDirectional(LOGISTICAL_CASING_CAP_SHAPE, Direction.UP)
;
} }

View file

@ -11,6 +11,8 @@ import java.util.Set;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.foundation.block.IWithTileEntity; import com.simibubi.create.foundation.block.IWithTileEntity;
import com.simibubi.create.foundation.utility.VoxelShaper;
import com.simibubi.create.foundation.utility.VoxelShapers;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
@ -48,9 +50,6 @@ public class LogisticalCasingBlock extends Block implements IWithTileEntity<Logi
public static final IProperty<Part> PART = EnumProperty.create("part", Part.class); public static final IProperty<Part> PART = EnumProperty.create("part", Part.class);
public static final BooleanProperty ACTIVE = BooleanProperty.create("active"); public static final BooleanProperty ACTIVE = BooleanProperty.create("active");
public static final VoxelShape SINGLE_SHAPE = VoxelShapes.or(makeCuboidShape(0, 0, 0, 16, 2, 16),
makeCuboidShape(1, 1, 1, 15, 15, 15), makeCuboidShape(0, 14, 0, 16, 16, 16));
public LogisticalCasingBlock() { public LogisticalCasingBlock() {
super(Properties.from(Blocks.DARK_OAK_PLANKS)); super(Properties.from(Blocks.DARK_OAK_PLANKS));
setDefaultState(getDefaultState().with(PART, Part.NONE).with(AXIS, Axis.Y).with(ACTIVE, false)); setDefaultState(getDefaultState().with(PART, Part.NONE).with(AXIS, Axis.Y).with(ACTIVE, false));
@ -115,7 +114,20 @@ public class LogisticalCasingBlock extends Block implements IWithTileEntity<Logi
@Override @Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) { public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return state.get(PART) == Part.NONE ? SINGLE_SHAPE : VoxelShapes.fullCube(); Part part = state.get(PART);
if (part == Part.NONE)
return VoxelShapers.LOGISTICAL_CASING_SINGLE_SHAPE;
if (part == Part.MIDDLE)
return VoxelShapers.LOGISTICAL_CASING_MIDDLE.get(state.get(AXIS));
Direction facing = VoxelShaper.axisAsFace(state.get(AXIS));
if (part == Part.END)
facing = facing.getOpposite();
return VoxelShapers.LOGISTICAL_CASING_CAP.get(facing);
//return state.get(PART) == Part.NONE ? VoxelShapers.LOGISTICAL_CASING_SINGLE_SHAPE : VoxelShapes.fullCube();
} }
@Override @Override