From 871c3ac54e756b00c946a40cbe06e5df0ad60716 Mon Sep 17 00:00:00 2001 From: quat1024 Date: Thu, 15 Jun 2023 22:26:01 -0400 Subject: [PATCH] poke at SpriteSet a bit --- .../templates/model/SlopeBakedModel.java | 2 - .../templates/model/SlopeMeshTransformer.java | 2 +- .../templates/model/SlopeUnbakedModel.java | 2 +- .../cottonmc/templates/util/SpriteSet.java | 39 ++++++++++--------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java b/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java index a49a1fb..0bf0e1c 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java @@ -19,14 +19,12 @@ public final class SlopeBakedModel extends ForwardingBakedModel { public SlopeBakedModel(BakedModel baseModel, BlockState slopeState, Function spriteLookup) { this.wrapped = baseModel; this.slopeState = slopeState; - this.spriteLookup = spriteLookup; this.xform = new SlopeMeshTransformer(spriteLookup); this.baseMesh = SlopeBaseMesh.make(slopeState); } public final BlockState slopeState; - public final Function spriteLookup; private final MeshTransformer xform; private final Mesh baseMesh; diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java b/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java index 31ae5f7..6bb8bcd 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeMeshTransformer.java @@ -61,7 +61,7 @@ public class SlopeMeshTransformer implements MeshTransformer { .find(); BakedModel model = minecraft.getBlockRenderManager().getModel(template); - sprites.prepare(model, randomSupplier.get()); + sprites.inspect(model, randomSupplier.get()); BlockColorProvider blockColor = ColorProviderRegistry.BLOCK.get(block); if(blockColor != null) color = 0xff000000 | blockColor.getColor(template, blockView, pos, 1); } diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java b/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java index 3fc476a..edc7489 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java @@ -28,7 +28,7 @@ public record SlopeUnbakedModel(BlockState slopeState) implements UnbakedModel { @Override public BakedModel bake(Baker baker, Function function, ModelBakeSettings modelBakeSettings, Identifier identifier) { - //TODO: weird, should use my own model instead of STONE + //TODO: weird, should use my own model instead BakedModel baseModel = baker.bake(BlockModels.getModelId(Blocks.SANDSTONE_STAIRS.getDefaultState()), modelBakeSettings); return new SlopeBakedModel(baseModel, slopeState, function); diff --git a/src/main/java/io/github/cottonmc/templates/util/SpriteSet.java b/src/main/java/io/github/cottonmc/templates/util/SpriteSet.java index a640fdf..1dfdbcf 100644 --- a/src/main/java/io/github/cottonmc/templates/util/SpriteSet.java +++ b/src/main/java/io/github/cottonmc/templates/util/SpriteSet.java @@ -11,6 +11,7 @@ import net.minecraft.util.math.Direction; import net.minecraft.util.math.random.Random; import java.util.EnumMap; +import java.util.EnumSet; import java.util.List; import java.util.function.Function; @@ -33,7 +34,9 @@ public class SpriteSet { private final Sprite defaultSprite; private final Sprite missingSprite; - private final EnumMap quads = new EnumMap<>(Direction.class); + private final EnumMap sprites = new EnumMap<>(Direction.class); + private final EnumSet hasColor = EnumSet.noneOf(Direction.class); + private boolean isDefault = true; /** Allow re-use of instances to avoid allocation in render loop */ @@ -43,34 +46,34 @@ public class SpriteSet { /** Allow re-use of instances to avoid allocation in render loop */ //TODO: pass in block state? - public void prepare(BakedModel model, Random rand) { - this.quads.clear(); + public void inspect(BakedModel model, Random rand) { + sprites.clear(); + hasColor.clear(); isDefault = false; for(Direction dir : DIRECTIONS) { - List quads = model.getQuads(null, dir, rand); - if(!quads.isEmpty()) this.quads.put(dir, quads.get(0)); + List sideQuads = model.getQuads(null, dir, rand); + if(sideQuads.isEmpty()) continue; + + BakedQuad arbitraryQuad = sideQuads.get(0); //maybe pick a largest quad instead? + if(arbitraryQuad == null) continue; + + if(arbitraryQuad.hasColor()) hasColor.add(dir); + + Sprite sprite = arbitraryQuad.getSprite(); + if(sprite == null) continue; + + sprites.put(dir, sprite); } } public Sprite getSprite(Direction dir) { if(isDefault) return defaultSprite; - - BakedQuad quad = quads.get(dir); - if(quad == null) return missingSprite; - - Sprite sprite = quad.getSprite(); - if(sprite == null) return missingSprite; - - return sprite; + else return sprites.getOrDefault(dir, missingSprite); } public boolean hasColor(Direction dir) { if(isDefault) return false; - - BakedQuad quad = quads.get(dir); - if(quad == null) return false; - - return quad.hasColor(); + else return hasColor.contains(dir); } }