From 8acb584fd24cf23d690f3aae47182ffb2415fd59 Mon Sep 17 00:00:00 2001 From: quat1024 Date: Sun, 9 Jul 2023 22:53:11 -0400 Subject: [PATCH] they shouldnt make dragons do vector math :( --- .../templates/block/TemplateSlopeBlock.java | 36 ++++------ .../templates/model/SlopeBaseMesh.java | 2 + .../util/{EdgeDirection.java => Edge.java} | 41 ++++++++++-- .../templates/util/StairShapeMaker.java | 67 +++++-------------- .../assets/templates/blockstates/slope.json | 24 +++---- 5 files changed, 78 insertions(+), 92 deletions(-) rename src/main/java/io/github/cottonmc/templates/util/{EdgeDirection.java => Edge.java} (65%) diff --git a/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java index 27aea82..1022ab9 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java @@ -1,56 +1,46 @@ package io.github.cottonmc.templates.block; import com.google.common.base.MoreObjects; -import io.github.cottonmc.templates.util.EdgeDirection; +import io.github.cottonmc.templates.api.TemplateInteractionUtil; +import io.github.cottonmc.templates.util.Edge; import io.github.cottonmc.templates.util.StairShapeMaker; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.ShapeContext; -import net.minecraft.block.enums.BlockHalf; import net.minecraft.item.ItemPlacementContext; import net.minecraft.state.StateManager; import net.minecraft.state.property.EnumProperty; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; import net.minecraft.util.shape.VoxelShape; import net.minecraft.world.BlockView; import javax.annotation.Nullable; public class TemplateSlopeBlock extends WaterloggableTemplateBlock { - public static final EnumProperty FACING = EnumProperty.of("facing", EdgeDirection.class); + public static final EnumProperty EDGE = EnumProperty.of("edge", Edge.class); - private static final VoxelShape[] shapes = new VoxelShape[8]; - private static int shapeIndex(Direction dir, BlockHalf half) { - return dir.getHorizontal() + (half == BlockHalf.TOP ? 4 : 0); - } + private static final VoxelShape[] shapes = new VoxelShape[Edge.values().length]; static { - //TODO - for(BlockHalf half : BlockHalf.values()) { - for(Direction d : Direction.values()) { - if(d.getHorizontal() == -1) continue; - shapes[shapeIndex(d, half)] = StairShapeMaker.createHorizontal(d, half, 8, 0.125d, 0.125d); - } + for(Edge edge : Edge.values()) { + shapes[edge.ordinal()] = StairShapeMaker.makeStair(edge, 1, 0.125d, 0.125d, 0.125d, 8); } } public TemplateSlopeBlock(Settings settings) { super(settings); - setDefaultState(getDefaultState().with(FACING, EdgeDirection.DOWN_NORTH)); + setDefaultState(getDefaultState().with(EDGE, Edge.DOWN_NORTH)); } @Override protected void appendProperties(StateManager.Builder builder) { - super.appendProperties(builder.add(FACING)); + super.appendProperties(builder.add(EDGE)); } @Nullable @Override public BlockState getPlacementState(ItemPlacementContext ctx) { - EdgeDirection.guessFromHitResult(ctx.getHitPos(), ctx.getBlockPos()); - BlockState sup = super.getPlacementState(ctx); - if(sup != null) sup = sup.with(FACING, EdgeDirection.guessFromHitResult(ctx.getHitPos(), ctx.getBlockPos())); + if(sup != null) sup = sup.with(EDGE, Edge.closestTo(ctx.getHitPos(), ctx.getBlockPos())); return sup; } @@ -58,15 +48,13 @@ public class TemplateSlopeBlock extends WaterloggableTemplateBlock { @Override public VoxelShape getCollisionShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ctx) { return MoreObjects.firstNonNull( - super.getCollisionShape(state, view, pos, ctx), - //shapes[shapeIndex(state.get(FACING_OLD), state.get(HALF_OLD))] - shapes[0] //TODO + TemplateInteractionUtil.getCollisionShape(state, view, pos, ctx), + shapes[state.get(EDGE).ordinal()] ); } @Override public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ctx) { - //return shapes[shapeIndex(state.get(FACING_OLD), state.get(HALF_OLD))]; - return shapes[0]; //TODO + return shapes[state.get(EDGE).ordinal()]; } } diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeBaseMesh.java b/src/main/java/io/github/cottonmc/templates/model/SlopeBaseMesh.java index 8e00e26..9212802 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeBaseMesh.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeBaseMesh.java @@ -55,6 +55,8 @@ public class SlopeBaseMesh { return builder.build(); } + //My mfw (my face when) mfw face when you can't rotate blockmodels on the z axis from a blockstate file + //Fine i will do it myself !!! public static Mesh makeSide() { Matrix4f mat = new Matrix4f(); RotationAxis.POSITIVE_Z.rotationDegrees(90).get(mat); diff --git a/src/main/java/io/github/cottonmc/templates/util/EdgeDirection.java b/src/main/java/io/github/cottonmc/templates/util/Edge.java similarity index 65% rename from src/main/java/io/github/cottonmc/templates/util/EdgeDirection.java rename to src/main/java/io/github/cottonmc/templates/util/Edge.java index 9810ea2..c8ba996 100644 --- a/src/main/java/io/github/cottonmc/templates/util/EdgeDirection.java +++ b/src/main/java/io/github/cottonmc/templates/util/Edge.java @@ -2,13 +2,15 @@ package io.github.cottonmc.templates.util; import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap; import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap; +import net.minecraft.util.Pair; import net.minecraft.util.StringIdentifiable; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; +import org.joml.Vector3d; import java.util.Locale; -public enum EdgeDirection implements StringIdentifiable { +public enum Edge implements StringIdentifiable { DOWN_NORTH(AxisRelation.PARALLEL, AxisRelation.LOW_SIDE, AxisRelation.LOW_SIDE), DOWN_SOUTH(AxisRelation.PARALLEL, AxisRelation.LOW_SIDE, AxisRelation.HIGH_SIDE), UP_SOUTH(AxisRelation.PARALLEL, AxisRelation.HIGH_SIDE, AxisRelation.HIGH_SIDE), @@ -23,20 +25,20 @@ public enum EdgeDirection implements StringIdentifiable { DOWN_EAST(AxisRelation.HIGH_SIDE, AxisRelation.LOW_SIDE, AxisRelation.PARALLEL), ; - EdgeDirection(AxisRelation x, AxisRelation y, AxisRelation z) { + Edge(AxisRelation x, AxisRelation y, AxisRelation z) { this.key = key(x, y, z); } private final byte key; - private static final Byte2ObjectMap LOOKUP = new Byte2ObjectOpenHashMap<>(); - static { for(EdgeDirection e : values()) LOOKUP.put(e.key, e); } + private static final Byte2ObjectMap LOOKUP = new Byte2ObjectOpenHashMap<>(); + static { for(Edge e : values()) LOOKUP.put(e.key, e); } private static byte key(AxisRelation x, AxisRelation y, AxisRelation z) { return (byte) (x.ordinal() + 1 << 4 | y.ordinal() + 1 << 2 | z.ordinal() + 1); } - public static EdgeDirection guessFromHitResult(Vec3d precise, BlockPos coarse) { + public static Edge closestTo(Vec3d precise, BlockPos coarse) { double dx = precise.x - coarse.getX(); double dy = precise.y - coarse.getY(); double dz = precise.z - coarse.getZ(); @@ -49,7 +51,7 @@ public enum EdgeDirection implements StringIdentifiable { float distToLowZ = (float) (dz); float distToHighZ = (float) (1 - dz); - //distances your click was from either pair of edges (each 0..0.5) + //distances your click was from either pair of edges (each 0..5) float distToAnyX = Math.min(distToLowX, distToHighX); float distToAnyY = Math.min(distToLowY, distToHighY); float distToAnyZ = Math.min(distToLowZ, distToHighZ); @@ -73,6 +75,33 @@ public enum EdgeDirection implements StringIdentifiable { return LOOKUP.getOrDefault(key(clickX, clickY, clickZ), DOWN_SOUTH); } + //I may have skill issue + //Beep boop i am very maintainable yes + public record CoordinateFrame(Vector3d origin, Vector3d along, Vector3d a, Vector3d b) {} + private static final Vector3d ZR = new Vector3d(); + private static final Vector3d PX = new Vector3d(1, 0, 0); + private static final Vector3d NX = new Vector3d(-1, 0, 0); + private static final Vector3d PY = new Vector3d(0, 1, 0); + private static final Vector3d NY = new Vector3d(0, -1, 0); + private static final Vector3d PZ = new Vector3d(0, 0, 1); + private static final Vector3d NZ = new Vector3d(0, 0, -1); + public CoordinateFrame makeCoordinateFrame() { + return switch(this) { + case DOWN_NORTH -> new CoordinateFrame(ZR, PX, PZ, PY); + case DOWN_EAST -> new CoordinateFrame(PX, PZ, NX, PY); + case DOWN_SOUTH -> new CoordinateFrame(new Vector3d(1, 0, 1), NX, NZ, PY); + case DOWN_WEST -> new CoordinateFrame(PZ, NZ, PX, PY); + case UP_NORTH -> new CoordinateFrame(PY, PX, PZ, NY); + case UP_EAST -> new CoordinateFrame(new Vector3d(1, 1, 0), PZ, NX, NY); + case UP_SOUTH -> new CoordinateFrame(new Vector3d(1, 1, 1), NX, NZ, NY); + case UP_WEST -> new CoordinateFrame(new Vector3d(0, 1, 1), NZ, PX, NY); + case NORTH_WEST -> new CoordinateFrame(ZR, PY, PZ, PX); + case NORTH_EAST -> new CoordinateFrame(PX, PY, NX, PZ); + case SOUTH_EAST -> new CoordinateFrame(new Vector3d(1, 0, 1), PY, NZ, NX); + case SOUTH_WEST -> new CoordinateFrame(PZ, PY, PX, NZ); + }; + } + @Override public String asString() { return name().toLowerCase(Locale.ROOT); diff --git a/src/main/java/io/github/cottonmc/templates/util/StairShapeMaker.java b/src/main/java/io/github/cottonmc/templates/util/StairShapeMaker.java index 02abc2d..910762a 100644 --- a/src/main/java/io/github/cottonmc/templates/util/StairShapeMaker.java +++ b/src/main/java/io/github/cottonmc/templates/util/StairShapeMaker.java @@ -1,64 +1,31 @@ package io.github.cottonmc.templates.util; -import net.minecraft.block.enums.BlockHalf; -import net.minecraft.util.math.Direction; -import net.minecraft.util.math.Vec3d; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; +import org.joml.Vector3d; public class StairShapeMaker { - public static VoxelShape createHorizontal(Direction shortSide, BlockHalf half, int steps, double rise, double run) { + public static VoxelShape makeStair(Edge innerEdge, double stepIn, double initialStepRise, double stepRise, double stepRun, int stepCount) { + Edge.CoordinateFrame frame = innerEdge.makeCoordinateFrame(); + Vector3d origin = frame.origin(); + + Vector3d in = new Vector3d(frame.a()).mul(stepIn); + Vector3d fstRise = new Vector3d(frame.b()).mul(initialStepRise); + + Vector3d cursor = new Vector3d(origin).add(frame.along()).add(in).add(fstRise); + Vector3d step = new Vector3d(frame.b()).mul(stepRise).add(new Vector3d(frame.a()).mul(-stepRun)); + VoxelShape shape = VoxelShapes.empty(); - - Vec3d a, b, march; - switch(shortSide) { - case SOUTH -> { - a = new Vec3d(0, 0, 1); - b = new Vec3d(1, rise, 0); - march = new Vec3d(0, rise, run); - } - case NORTH -> { - a = new Vec3d(0, 0, 0); - b = new Vec3d(1, rise, 1); - march = new Vec3d(0, rise, -run); - } - case EAST -> { - a = new Vec3d(1, 0, 0); - b = new Vec3d(0, rise, 1); - march = new Vec3d(run, rise, 0); - } - case WEST -> { - a = new Vec3d(0, 0, 0); - b = new Vec3d(1, rise, 1); - march = new Vec3d(-run, rise, 0); - } - default -> { - return VoxelShapes.fullCube(); //TODO - } - } - - if(half == BlockHalf.TOP) { - a = new Vec3d(a.x, 1 - a.y, a.z); - b = new Vec3d(b.x, 1 - b.y, b.z); - march = march.multiply(1, -1, 1); - } - - for(int i = 0; i < steps; i++) { - VoxelShape newShape = agh(a.x, a.y, a.z, b.x, b.y, b.z); - shape = VoxelShapes.union(shape, newShape); - b = b.add(march); + for(int i = 0; i < stepCount; i++) { + shape = VoxelShapes.union(shape, box(origin.x, origin.y, origin.z, cursor.x, cursor.y, cursor.z)); + cursor.add(step); } return shape.simplify(); } - private static VoxelShape agh(double x1, double y1, double z1, double x2, double y2, double z2) { - double minX = Math.min(x1, x2); - double maxX = Math.max(x1, x2); - double minY = Math.min(y1, y2); - double maxY = Math.max(y1, y2); - double minZ = Math.min(z1, z2); - double maxZ = Math.max(z1, z2); - return VoxelShapes.cuboid(minX, minY, minZ, maxX, maxY, maxZ); + //VoxelShape's constructor is picky about specifying your mins before your maxs + private static VoxelShape box(double x1, double y1, double z1, double x2, double y2, double z2) { + return VoxelShapes.cuboid(Math.min(x1, x2), Math.min(y1, y2), Math.min(z1, z2), Math.max(x1, x2), Math.max(y1, y2), Math.max(z1, z2)); } } diff --git a/src/main/resources/assets/templates/blockstates/slope.json b/src/main/resources/assets/templates/blockstates/slope.json index 55cd966..9967652 100644 --- a/src/main/resources/assets/templates/blockstates/slope.json +++ b/src/main/resources/assets/templates/blockstates/slope.json @@ -1,61 +1,61 @@ { "variants": { - "facing=down_east": { + "edge=down_east": { "model": "templates:slope_special", "uvlock": true, "y": 270 }, - "facing=down_north": { + "edge=down_north": { "model": "templates:slope_special", "uvlock": true, "y": 180 }, - "facing=down_south": { + "edge=down_south": { "model": "templates:slope_special" }, - "facing=down_west": { + "edge=down_west": { "model": "templates:slope_special", "uvlock": true, "y": 90 }, - "facing=up_east": { + "edge=up_east": { "model": "templates:slope_special", "uvlock": true, "x": 180, "y": 90 }, - "facing=up_north": { + "edge=up_north": { "model": "templates:slope_special", "uvlock": true, "x": 180 }, - "facing=up_south": { + "edge=up_south": { "model": "templates:slope_special", "uvlock": true, "x": 180, "y": 180 }, - "facing=up_west": { + "edge=up_west": { "model": "templates:slope_special", "uvlock": true, "x": 180, "y": 270 }, - "facing=north_east": { + "edge=north_east": { "model": "templates:slope_side_special", "uvlock": true, "y": 270 }, - "facing=north_west": { + "edge=north_west": { "model": "templates:slope_side_special", "uvlock": true, "y": 180 }, - "facing=south_east": { + "edge=south_east": { "model": "templates:slope_side_special", "uvlock": true }, - "facing=south_west": { + "edge=south_west": { "model": "templates:slope_side_special", "uvlock": true, "y": 90