Skip to content

Commit acd2c0f

Browse files
Use the proper atlas for a material when loading its texture
1 parent cdef4b2 commit acd2c0f

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

rainbow/src/main/java/org/geysermc/rainbow/Rainbow.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.geysermc.rainbow;
22

33
import com.mojang.logging.LogUtils;
4+
import net.minecraft.client.renderer.texture.TextureAtlas;
5+
import net.minecraft.client.resources.model.Material;
6+
import net.minecraft.data.AtlasIds;
47
import net.minecraft.resources.Identifier;
58
import org.slf4j.Logger;
69

@@ -25,4 +28,14 @@ public static Identifier decorateIdentifier(Identifier identifier, String type,
2528
public static Identifier decorateTextureIdentifier(Identifier identifier) {
2629
return decorateIdentifier(identifier, "textures", "png");
2730
}
31+
32+
public static Identifier getAtlasIdFromMaterial(Material material) {
33+
Identifier atlasLocation = material.atlasLocation();
34+
if (atlasLocation.equals(TextureAtlas.LOCATION_BLOCKS)) {
35+
return AtlasIds.BLOCKS;
36+
} else if (atlasLocation.equals(TextureAtlas.LOCATION_ITEMS)) {
37+
return AtlasIds.ITEMS;
38+
}
39+
return atlasLocation;
40+
}
2841
}

rainbow/src/main/java/org/geysermc/rainbow/mapping/AssetResolver.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import net.minecraft.client.renderer.item.ClientItem;
44
import net.minecraft.client.resources.model.EquipmentClientInfo;
5+
import net.minecraft.client.resources.model.ModelManager;
56
import net.minecraft.client.resources.model.ResolvedModel;
7+
import net.minecraft.data.AtlasIds;
68
import net.minecraft.resources.Identifier;
79
import net.minecraft.resources.ResourceKey;
810
import net.minecraft.world.item.equipment.EquipmentAsset;
@@ -19,4 +21,12 @@ public interface AssetResolver {
1921
Optional<EquipmentClientInfo> getEquipmentInfo(ResourceKey<EquipmentAsset> key);
2022

2123
Optional<TextureResource> getTexture(Identifier atlas, Identifier identifier);
24+
25+
default Optional<TextureResource> getTextureSafely(Identifier atlas, Identifier identifier) {
26+
if (atlas.equals(ModelManager.BLOCK_OR_ITEM)) {
27+
return getTexture(AtlasIds.BLOCKS, identifier)
28+
.or(() -> getTexture(AtlasIds.ITEMS, identifier));
29+
}
30+
return getTexture(atlas, identifier);
31+
}
2232
}

rainbow/src/main/java/org/geysermc/rainbow/mapping/geometry/BedrockGeometryContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import net.minecraft.client.renderer.block.model.TextureSlots;
44
import net.minecraft.client.resources.model.Material;
55
import net.minecraft.client.resources.model.ResolvedModel;
6-
import net.minecraft.data.AtlasIds;
76
import net.minecraft.resources.Identifier;
87
import net.minecraft.world.item.ItemStack;
98
import org.geysermc.rainbow.Rainbow;
@@ -37,7 +36,7 @@ public static BedrockGeometryContext create(Identifier bedrockIdentifier, Resolv
3736
if (layer0Texture != null) {
3837
geometry = Optional.empty();
3938
animation = Optional.empty();
40-
icon = TextureHolder.createBuiltIn(AtlasIds.BLOCKS, layer0Texture.texture());
39+
icon = TextureHolder.createBuiltIn(Rainbow.getAtlasIdFromMaterial(layer0Texture), layer0Texture.texture());
4140
} else {
4241
// Unknown model (doesn't use layer0), so we immediately assume the geometry is custom
4342
// This check should probably be done differently (actually check if the model is 2D or 3D)

rainbow/src/main/java/org/geysermc/rainbow/mapping/texture/BuiltInTextureHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public BuiltInTextureHolder(Identifier identifier, Identifier atlas, Identifier
2323
@Override
2424
public Optional<byte[]> load(AssetResolver assetResolver, ProblemReporter reporter) {
2525
return RainbowIO.safeIO(() -> {
26-
try (TextureResource texture = assetResolver.getTexture(atlas, source).orElse(null)) {
26+
try (TextureResource texture = assetResolver.getTextureSafely(atlas, source).orElse(null)) {
2727
Objects.requireNonNull(texture);
2828
try (NativeImage firstFrame = texture.getFirstFrame(false)) {
2929
return NativeImageUtil.writeToByteArray(firstFrame);

rainbow/src/main/java/org/geysermc/rainbow/mapping/texture/StitchedTextures.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
88
import net.minecraft.client.resources.model.Material;
99
import net.minecraft.data.AtlasIds;
10-
import net.minecraft.resources.Identifier;
1110
import net.minecraft.util.Util;
11+
import org.geysermc.rainbow.Rainbow;
1212
import org.geysermc.rainbow.RainbowIO;
1313
import org.geysermc.rainbow.mapping.PackContext;
1414
import org.geysermc.rainbow.mixin.SpriteContentsAccessor;
@@ -37,7 +37,7 @@ public Optional<TextureAtlasSprite> getSprite(String key) {
3737

3838
public static StitchedTextures stitchModelTextures(TextureSlots textures, PackContext context) {
3939
Map<String, Material> materials = ((TextureSlotsAccessor) textures).getResolvedValues();
40-
SpriteLoader.Preparations preparations = prepareStitching(materials.values().stream().map(Material::texture), context);
40+
SpriteLoader.Preparations preparations = prepareStitching(materials.values().stream(), context);
4141

4242
Map<String, TextureAtlasSprite> sprites = new HashMap<>();
4343
for (Map.Entry<String, Material> material : materials.entrySet()) {
@@ -50,21 +50,21 @@ public static StitchedTextures stitchModelTextures(TextureSlots textures, PackCo
5050
return new StitchedTextures(Map.copyOf(sprites), () -> stitchTextureAtlas(preparations), preparations.width(), preparations.height());
5151
}
5252

53-
private static SpriteLoader.Preparations prepareStitching(Stream<Identifier> textures, PackContext context) {
54-
// Atlas ID doesn't matter much here, but BLOCKS is the most appropriate
55-
SpriteLoader spriteLoader = new SpriteLoader(AtlasIds.BLOCKS, MAX_TEXTURE_SIZE);
56-
List<SpriteContents> sprites = textures.distinct()
57-
.map(texture -> readSpriteContents(texture, context))
53+
private static SpriteLoader.Preparations prepareStitching(Stream<Material> materials, PackContext context) {
54+
// Atlas ID doesn't matter much here, but ITEMS is the most appropriate (though not always right)
55+
SpriteLoader spriteLoader = new SpriteLoader(AtlasIds.ITEMS, MAX_TEXTURE_SIZE);
56+
List<SpriteContents> sprites = materials.distinct()
57+
.map(material -> readSpriteContents(material, context))
5858
.<SpriteContents>mapMulti(Optional::ifPresent)
5959
.toList();
6060
return ((SpriteLoaderAccessor) spriteLoader).invokeStitch(sprites, 0, Util.backgroundExecutor());
6161
}
6262

63-
private static Optional<SpriteContents> readSpriteContents(Identifier identifier, PackContext context) {
63+
private static Optional<SpriteContents> readSpriteContents(Material material, PackContext context) {
6464
return RainbowIO.safeIO(() -> {
65-
try (TextureResource texture = context.assetResolver().getTexture(AtlasIds.BLOCKS, identifier).orElse(null)) {
65+
try (TextureResource texture = context.assetResolver().getTextureSafely(Rainbow.getAtlasIdFromMaterial(material), material.texture()).orElse(null)) {
6666
if (texture != null) {
67-
return new SpriteContents(identifier, texture.sizeOfFrame(), texture.getFirstFrame(true));
67+
return new SpriteContents(material.texture(), texture.sizeOfFrame(), texture.getFirstFrame(true));
6868
}
6969
}
7070
return null;

0 commit comments

Comments
 (0)