Unstitcher .java

net.minecraft.client.renderer.texture.atlas.sources.Unstitcher (record): members: resource, regions, xDivisor, yDivisor, RegionInstance, Region, run.

net.minecraft.client.renderer.texture.atlas.sourcesrecord113 linesclient4.4 KB

Implements

API Surface (5)

void run(final ResourceManager resourceManager, final SpriteSource.Output output)
MapCodec<Unstitcher> codec()
record Region(Identifier sprite, double x, double y, double width, double height)
SpriteContents get(final SpriteResourceLoader loader)
void discard()

Fields & Constants (2)

LOGGER
MAP_CODEC

String literals (7)

"resource"
"regions"
"divisor_x"
"divisor_y"
"sprite"
"width"
"height"

Full Source

Unstitcher.java113 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.texture.atlas.sources;
2
3
import com.mojang.blaze3d.platform.NativeImage;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.MapCodec;
7
import com.mojang.serialization.codecs.RecordCodecBuilder;
8
import java.util.List;
9
import java.util.Optional;
10
import net.fabricmc.api.EnvType;
11
import net.fabricmc.api.Environment;
12
import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
13
import net.minecraft.client.renderer.texture.SpriteContents;
14
import net.minecraft.client.renderer.texture.atlas.SpriteResourceLoader;
15
import net.minecraft.client.renderer.texture.atlas.SpriteSource;
16
import net.minecraft.client.resources.metadata.animation.FrameSize;
17
import net.minecraft.resources.Identifier;
18
import net.minecraft.server.packs.resources.Resource;
19
import net.minecraft.server.packs.resources.ResourceManager;
20
import net.minecraft.util.ExtraCodecs;
21
import net.minecraft.util.Mth;
22
import org.slf4j.Logger;
23
24
@Environment(EnvType.CLIENT)
25
public record Unstitcher(Identifier resource, List<Unstitcher.Region> regions, double xDivisor, double yDivisor) implements SpriteSource {
26
	private static final Logger LOGGER = LogUtils.getLogger();
27
	public static final MapCodec<Unstitcher> MAP_CODEC = RecordCodecBuilder.mapCodec(
28
		i -> i.group(
29
				Identifier.CODEC.fieldOf("resource").forGetter(Unstitcher::resource),
30
				ExtraCodecs.nonEmptyList(Unstitcher.Region.CODEC.listOf()).fieldOf("regions").forGetter(Unstitcher::regions),
31
				Codec.DOUBLE.optionalFieldOf("divisor_x", 1.0).forGetter(Unstitcher::xDivisor),
32
				Codec.DOUBLE.optionalFieldOf("divisor_y", 1.0).forGetter(Unstitcher::yDivisor)
33
			)
34
			.apply(i, Unstitcher::new)
35
	);
36
37
	@Override
38
	public void run(final ResourceManager resourceManager, final SpriteSource.Output output) {
39
		Identifier resourceId = TEXTURE_ID_CONVERTER.idToFile(this.resource);
40
		Optional<Resource> resource = resourceManager.getResource(resourceId);
41
		if (resource.isPresent()) {
42
			LazyLoadedImage image = new LazyLoadedImage(resourceId, resource.get(), this.regions.size());
43
44
			for (Unstitcher.Region region : this.regions) {
45
				output.add(region.sprite, new Unstitcher.RegionInstance(image, region, this.xDivisor, this.yDivisor));
46
			}
47
		} else {
48
			LOGGER.warn("Missing sprite: {}", resourceId);
49
		}
50
	}
51
52
	@Override
53
	public MapCodec<Unstitcher> codec() {
54
		return MAP_CODEC;
55
	}
56
57
	@Environment(EnvType.CLIENT)
58
	public record Region(Identifier sprite, double x, double y, double width, double height) {
59
		public static final Codec<Unstitcher.Region> CODEC = RecordCodecBuilder.create(
60
			i -> i.group(
61
					Identifier.CODEC.fieldOf("sprite").forGetter(Unstitcher.Region::sprite),
62
					Codec.DOUBLE.fieldOf("x").forGetter(Unstitcher.Region::x),
63
					Codec.DOUBLE.fieldOf("y").forGetter(Unstitcher.Region::y),
64
					Codec.DOUBLE.fieldOf("width").forGetter(Unstitcher.Region::width),
65
					Codec.DOUBLE.fieldOf("height").forGetter(Unstitcher.Region::height)
66
				)
67
				.apply(i, Unstitcher.Region::new)
68
		);
69
	}
70
71
	@Environment(EnvType.CLIENT)
72
	private static class RegionInstance implements SpriteSource.DiscardableLoader {
73
		private final LazyLoadedImage image;
74
		private final Unstitcher.Region region;
75
		private final double xDivisor;
76
		private final double yDivisor;
77
78
		private RegionInstance(final LazyLoadedImage image, final Unstitcher.Region region, final double xDivisor, final double yDivisor) {
79
			this.image = image;
80
			this.region = region;
81
			this.xDivisor = xDivisor;
82
			this.yDivisor = yDivisor;
83
		}
84
85
		@Override
86
		public SpriteContents get(final SpriteResourceLoader loader) {
87
			try {
88
				NativeImage fullImage = this.image.get();
89
				double xScale = fullImage.getWidth() / this.xDivisor;
90
				double yScale = fullImage.getHeight() / this.yDivisor;
91
				int x = Mth.floor(this.region.x * xScale);
92
				int y = Mth.floor(this.region.y * yScale);
93
				int width = Mth.floor(this.region.width * xScale);
94
				int height = Mth.floor(this.region.height * yScale);
95
				NativeImage target = new NativeImage(NativeImage.Format.RGBA, width, height, false);
96
				fullImage.copyRect(target, x, y, 0, 0, width, height, false, false);
97
				return new SpriteContents(this.region.sprite, new FrameSize(width, height), target);
98
			} catch (Exception e) {
99
				Unstitcher.LOGGER.error("Failed to unstitch region {}", this.region.sprite, e);
100
			} finally {
101
				this.image.release();
102
			}
103
104
			return MissingTextureAtlasSprite.create();
105
		}
106
107
		@Override
108
		public void discard() {
109
			this.image.release();
110
		}
111
	}
112
}
113

Decompiled Minecraft 26.3 source of Unstitcher (net/minecraft/client/renderer/texture/atlas/sources/Unstitcher.java) with Mojang official mappings, stored verbatim on the site.