From 3c57778ea1fb81945f801dc36eb6d8f1a0a93968 Mon Sep 17 00:00:00 2001 From: quat1024 Date: Mon, 3 Jul 2023 04:58:26 -0400 Subject: [PATCH] Permute faces --- README.md | 6 +--- .../templates/model/TemplateBakedModel.java | 30 +++++++++++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 35eb14f..270a79e 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,8 @@ Template blocks can be placed in the world, then right-clicked with a full-size ## Todo -* Re-generalize the model system (I removed a layer of indirection while rewriting it, so it's just slopes now) * Upside-down slopes would be nice... -* "Counterrotate" blockstates - * In the old system, the north/south/east/west faces were constructed individually, so it'd look at the north side of the theme model when computing the north face, the east side of the theme model when computing the east face, etc - * In the current system, there is only one (south-facing) slope model, and I move its vertices around with a quad transformer to obtain other rotations. (built off the vanilla `AffineTransformation`) - * But this means... when i'm building the "left" side of the slope, what side of the block should i look at? I have to undo this affine transformation +* More templates !! ## Notes for addon developers diff --git a/src/main/java/io/github/cottonmc/templates/model/TemplateBakedModel.java b/src/main/java/io/github/cottonmc/templates/model/TemplateBakedModel.java index bbb7dc3..9ff3a4c 100644 --- a/src/main/java/io/github/cottonmc/templates/model/TemplateBakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/TemplateBakedModel.java @@ -22,6 +22,8 @@ import net.minecraft.util.math.random.Random; import net.minecraft.world.BlockRenderView; import org.jetbrains.annotations.NotNull; +import java.util.EnumMap; +import java.util.Map; import java.util.function.Supplier; public final class TemplateBakedModel extends ForwardingBakedModel { @@ -31,12 +33,28 @@ public final class TemplateBakedModel extends ForwardingBakedModel { this.tam = tam; this.affineTransformer = new AffineQuadTransformer(aff); this.baseMesh = baseMesh; + + //Hard to explain what this is for... + //Basically, the previous incarnation of this mod assembled the north/south/east/west faces all individually. + //This means it was easy to get the orientation of the block correct - to popular the north face of the slope, look at + //the north texture of the theme block. In this version, there is only *one* slope model that is dynamically rotated + //to form the other possible orientations. If I populate the north face of the model using the north face of the theme, + //that model will then be rotated so it's no longer facing the right way. + // + //This seems to work, but I'm kinda surprised I don't need to invert the transformation here, which is a clue that + //I don't really understand all the math, loool + for(Direction input : Direction.values()) { + Direction output = Direction.transform(aff.getMatrix(), input); + facePermutation.put(input, output); + } } private final TemplateAppearanceManager tam; private final AffineQuadTransformer affineTransformer; private final Mesh baseMesh; + private final Map facePermutation = new EnumMap<>(Direction.class); + @Override public boolean isVanillaAdapter() { return false; @@ -68,11 +86,11 @@ public final class TemplateBakedModel extends ForwardingBakedModel { public @NotNull RenderContext.QuadTransform retexturingBlockTransformer(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier randomSupplier) { BlockState template = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; - if(template == null || template.isAir()) return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF); + if(template == null || template.isAir()) return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF, facePermutation); BlockColorProvider prov = ColorProviderRegistry.BLOCK.get(template.getBlock()); int globalTint = prov != null ? prov.getColor(state, blockView, pos, 1) : 0xFFFFFFFF; - return new RetexturingTransformer(tam.getAppearance(template), globalTint); + return new RetexturingTransformer(tam.getAppearance(template), globalTint, facePermutation); } public @NotNull RenderContext.QuadTransform retexturingItemTransformer(ItemStack stack, Supplier randomSupplier) { @@ -80,13 +98,13 @@ public final class TemplateBakedModel extends ForwardingBakedModel { NbtCompound tag = BlockItem.getBlockEntityNbt(stack); if(tag != null && tag.contains("BlockState")) { BlockState state = NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), tag.getCompound("BlockState")); - if(!state.isAir()) return new RetexturingTransformer(tam.getAppearance(state), 0xFFFFFFFF); + if(!state.isAir()) return new RetexturingTransformer(tam.getAppearance(state), 0xFFFFFFFF, facePermutation); } - return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF); + return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF, facePermutation); } - public static record RetexturingTransformer(TemplateAppearance appearance, int color) implements RenderContext.QuadTransform { + public static record RetexturingTransformer(TemplateAppearance appearance, int color, Map facePermutation) implements RenderContext.QuadTransform { private static final Direction[] DIRECTIONS = Direction.values(); @Override @@ -94,7 +112,7 @@ public final class TemplateBakedModel extends ForwardingBakedModel { quad.material(appearance.getRenderMaterial()); //The quad tag numbers were selected so this magic trick works: - Direction dir = DIRECTIONS[quad.tag()]; + Direction dir = facePermutation.get(DIRECTIONS[quad.tag()]); //TODO: this newly-simplified direction passing to hasColor is almost certainly incorrect // I think hasColor was kinda incorrect in the first place tho