Spaces:
Build error
Build error
File size: 6,652 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
package net.minecraft.client.renderer.texture;
import com.mojang.blaze3d.platform.TextureUtil;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.logging.LogUtils;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import net.minecraft.CrashReport;
import net.minecraft.CrashReportCategory;
import net.minecraft.ReportedException;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;
@OnlyIn(Dist.CLIENT)
public class TextureAtlas extends AbstractTexture implements Dumpable, Tickable {
private static final Logger LOGGER = LogUtils.getLogger();
@Deprecated
public static final ResourceLocation LOCATION_BLOCKS = ResourceLocation.withDefaultNamespace("textures/atlas/blocks.png");
@Deprecated
public static final ResourceLocation LOCATION_PARTICLES = ResourceLocation.withDefaultNamespace("textures/atlas/particles.png");
private List<SpriteContents> sprites = List.of();
private List<TextureAtlasSprite.Ticker> animatedTextures = List.of();
private Map<ResourceLocation, TextureAtlasSprite> texturesByName = Map.of();
@Nullable
private TextureAtlasSprite missingSprite;
private final ResourceLocation location;
private final int maxSupportedTextureSize;
private int width;
private int height;
private int mipLevel;
public TextureAtlas(ResourceLocation p_118269_) {
this.location = p_118269_;
this.maxSupportedTextureSize = RenderSystem.maxSupportedTextureSize();
}
public void upload(SpriteLoader.Preparations p_250662_) {
LOGGER.info("Created: {}x{}x{} {}-atlas", p_250662_.width(), p_250662_.height(), p_250662_.mipLevel(), this.location);
TextureUtil.prepareImage(this.getId(), p_250662_.mipLevel(), p_250662_.width(), p_250662_.height());
this.width = p_250662_.width();
this.height = p_250662_.height();
this.mipLevel = p_250662_.mipLevel();
this.clearTextureData();
this.setFilter(false, this.mipLevel > 1);
this.texturesByName = Map.copyOf(p_250662_.regions());
this.missingSprite = this.texturesByName.get(MissingTextureAtlasSprite.getLocation());
if (this.missingSprite == null) {
throw new IllegalStateException("Atlas '" + this.location + "' (" + this.texturesByName.size() + " sprites) has no missing texture sprite");
} else {
List<SpriteContents> list = new ArrayList<>();
List<TextureAtlasSprite.Ticker> list1 = new ArrayList<>();
for (TextureAtlasSprite textureatlassprite : p_250662_.regions().values()) {
list.add(textureatlassprite.contents());
try {
textureatlassprite.uploadFirstFrame();
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Stitching texture atlas");
CrashReportCategory crashreportcategory = crashreport.addCategory("Texture being stitched together");
crashreportcategory.setDetail("Atlas path", this.location);
crashreportcategory.setDetail("Sprite", textureatlassprite);
throw new ReportedException(crashreport);
}
TextureAtlasSprite.Ticker textureatlassprite$ticker = textureatlassprite.createTicker();
if (textureatlassprite$ticker != null) {
list1.add(textureatlassprite$ticker);
}
}
this.sprites = List.copyOf(list);
this.animatedTextures = List.copyOf(list1);
}
}
@Override
public void dumpContents(ResourceLocation p_276106_, Path p_276127_) throws IOException {
String s = p_276106_.toDebugFileName();
TextureUtil.writeAsPNG(p_276127_, s, this.getId(), this.mipLevel, this.width, this.height);
dumpSpriteNames(p_276127_, s, this.texturesByName);
}
private static void dumpSpriteNames(Path p_261769_, String p_262102_, Map<ResourceLocation, TextureAtlasSprite> p_261722_) {
Path path = p_261769_.resolve(p_262102_ + ".txt");
try (Writer writer = Files.newBufferedWriter(path)) {
for (Entry<ResourceLocation, TextureAtlasSprite> entry : p_261722_.entrySet().stream().sorted(Entry.comparingByKey()).toList()) {
TextureAtlasSprite textureatlassprite = entry.getValue();
writer.write(
String.format(
Locale.ROOT,
"%s\tx=%d\ty=%d\tw=%d\th=%d%n",
entry.getKey(),
textureatlassprite.getX(),
textureatlassprite.getY(),
textureatlassprite.contents().width(),
textureatlassprite.contents().height()
)
);
}
} catch (IOException ioexception) {
LOGGER.warn("Failed to write file {}", path, ioexception);
}
}
public void cycleAnimationFrames() {
this.bind();
for (TextureAtlasSprite.Ticker textureatlassprite$ticker : this.animatedTextures) {
textureatlassprite$ticker.tickAndUpload();
}
}
@Override
public void tick() {
this.cycleAnimationFrames();
}
public TextureAtlasSprite getSprite(ResourceLocation p_118317_) {
TextureAtlasSprite textureatlassprite = this.texturesByName.getOrDefault(p_118317_, this.missingSprite);
if (textureatlassprite == null) {
throw new IllegalStateException("Tried to lookup sprite, but atlas is not initialized");
} else {
return textureatlassprite;
}
}
public void clearTextureData() {
this.sprites.forEach(SpriteContents::close);
this.animatedTextures.forEach(TextureAtlasSprite.Ticker::close);
this.sprites = List.of();
this.animatedTextures = List.of();
this.texturesByName = Map.of();
this.missingSprite = null;
}
public ResourceLocation location() {
return this.location;
}
public int maxSupportedTextureSize() {
return this.maxSupportedTextureSize;
}
int getWidth() {
return this.width;
}
int getHeight() {
return this.height;
}
} |