Stitcher .java

net.minecraft.client.renderer.texture.Stitcher (class, 235 lines): members: texturesToBeStitched, HOLDER_COMPARATOR, registerSprite, gatherSprites, Holder.

net.minecraft.client.renderer.textureclass235 linesclientextends Stitcher.Entry>7.0 KB

API Surface (10)

int getWidth()
int getHeight()
void registerSprite(final T entry)
void stitch()
void gatherSprites(final Stitcher.SpriteLoader<T> loader)
int getX()
int getY()
boolean add(final Stitcher.Holder<T> holder)
void walk(final Stitcher.SpriteLoader<T> output, final int padding)
String toString()

Fields & Constants (9)

HOLDER_COMPARATOR
mipLevel
texturesToBeStitched
storage
storageX
storageY
maxWidth
maxHeight
padding

String literals (6)

"Slot{originX="
", originY="
", width="
", height="
", texture="
", subSlots="

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Stitcher.java235 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.texture;
2
3
import com.google.common.collect.ImmutableList;
4
import java.util.ArrayList;
5
import java.util.Comparator;
6
import java.util.List;
7
import net.fabricmc.api.EnvType;
8
import net.fabricmc.api.Environment;
9
import net.minecraft.resources.Identifier;
10
import net.minecraft.util.Mth;
11
import org.jspecify.annotations.Nullable;
12
13
@Environment(EnvType.CLIENT)
14
public class Stitcher<T extends Stitcher.Entry> {
15
	private static final Comparator<Stitcher.Holder<?>> HOLDER_COMPARATOR = Comparator.<Stitcher.Holder<?>, Integer>comparing(h -> -h.height)
16
		.thenComparing(h -> -h.width)
17
		.thenComparing(h -> h.entry.name());
18
	private final int mipLevel;
19
	private final List<Stitcher.Holder<T>> texturesToBeStitched = new ArrayList<>();
20
	private final List<Stitcher.Region<T>> storage = new ArrayList<>();
21
	private int storageX;
22
	private int storageY;
23
	private final int maxWidth;
24
	private final int maxHeight;
25
	private final int padding;
26
27
	public Stitcher(final int maxWidth, final int maxHeight, final int mipLevel, final int anisotropyBit) {
28
		this.mipLevel = mipLevel;
29
		this.maxWidth = maxWidth;
30
		this.maxHeight = maxHeight;
31
		this.padding = 1 << mipLevel << Mth.clamp(anisotropyBit - 1, 0, 4);
32
	}
33
34
	public int getWidth() {
35
		return this.storageX;
36
	}
37
38
	public int getHeight() {
39
		return this.storageY;
40
	}
41
42
	public void registerSprite(final T entry) {
43
		Stitcher.Holder<T> holder = new Stitcher.Holder<>(
44
			entry, smallestFittingMinTexel(entry.width() + this.padding * 2, this.mipLevel), smallestFittingMinTexel(entry.height() + this.padding * 2, this.mipLevel)
45
		);
46
		this.texturesToBeStitched.add(holder);
47
	}
48
49
	public void stitch() {
50
		List<Stitcher.Holder<T>> holders = new ArrayList<>(this.texturesToBeStitched);
51
		holders.sort(HOLDER_COMPARATOR);
52
53
		for (Stitcher.Holder<T> holder : holders) {
54
			if (!this.addToStorage(holder)) {
55
				throw new StitcherException(holder.entry, holders.stream().map(h -> h.entry).collect(ImmutableList.toImmutableList()));
56
			}
57
		}
58
	}
59
60
	public void gatherSprites(final Stitcher.SpriteLoader<T> loader) {
61
		for (Stitcher.Region<T> topRegion : this.storage) {
62
			topRegion.walk(loader, this.padding);
63
		}
64
	}
65
66
	private static int smallestFittingMinTexel(final int input, final int maxMipLevel) {
67
		return (input >> maxMipLevel) + ((input & (1 << maxMipLevel) - 1) == 0 ? 0 : 1) << maxMipLevel;
68
	}
69
70
	private boolean addToStorage(final Stitcher.Holder<T> holder) {
71
		for (Stitcher.Region<T> region : this.storage) {
72
			if (region.add(holder)) {
73
				return true;
74
			}
75
		}
76
77
		return this.expand(holder);
78
	}
79
80
	private boolean expand(final Stitcher.Holder<T> holder) {
81
		int xCurrentSize = Mth.smallestEncompassingPowerOfTwo(this.storageX);
82
		int yCurrentSize = Mth.smallestEncompassingPowerOfTwo(this.storageY);
83
		int xNewSize = Mth.smallestEncompassingPowerOfTwo(this.storageX + holder.width);
84
		int yNewSize = Mth.smallestEncompassingPowerOfTwo(this.storageY + holder.height);
85
		boolean xCanGrow = xNewSize <= this.maxWidth;
86
		boolean yCanGrow = yNewSize <= this.maxHeight;
87
		if (!xCanGrow && !yCanGrow) {
88
			return false;
89
		}
90
91
		boolean xWillGrow = xCanGrow && xCurrentSize != xNewSize;
92
		boolean yWillGrow = yCanGrow && yCurrentSize != yNewSize;
93
		boolean growOnX;
94
		if (xWillGrow ^ yWillGrow) {
95
			growOnX = xWillGrow;
96
		} else {
97
			growOnX = xCanGrow && xCurrentSize <= yCurrentSize;
98
		}
99
100
		Stitcher.Region<T> slot;
101
		if (growOnX) {
102
			if (this.storageY == 0) {
103
				this.storageY = yNewSize;
104
			}
105
106
			slot = new Stitcher.Region<>(this.storageX, 0, xNewSize - this.storageX, this.storageY);
107
			this.storageX = xNewSize;
108
		} else {
109
			slot = new Stitcher.Region<>(0, this.storageY, this.storageX, yNewSize - this.storageY);
110
			this.storageY = yNewSize;
111
		}
112
113
		slot.add(holder);
114
		this.storage.add(slot);
115
		return true;
116
	}
117
118
	@Environment(EnvType.CLIENT)
119
	public interface Entry {
120
		int width();
121
122
		int height();
123
124
		Identifier name();
125
	}
126
127
	@Environment(EnvType.CLIENT)
128
	private record Holder<T extends Stitcher.Entry>(T entry, int width, int height) {
129
	}
130
131
	@Environment(EnvType.CLIENT)
132
	public static class Region<T extends Stitcher.Entry> {
133
		private final int originX;
134
		private final int originY;
135
		private final int width;
136
		private final int height;
137
		private @Nullable List<Stitcher.Region<T>> subSlots;
138
		private Stitcher.@Nullable Holder<T> holder;
139
140
		public Region(final int originX, final int originY, final int width, final int height) {
141
			this.originX = originX;
142
			this.originY = originY;
143
			this.width = width;
144
			this.height = height;
145
		}
146
147
		public int getX() {
148
			return this.originX;
149
		}
150
151
		public int getY() {
152
			return this.originY;
153
		}
154
155
		public boolean add(final Stitcher.Holder<T> holder) {
156
			if (this.holder != null) {
157
				return false;
158
			}
159
160
			int textureWidth = holder.width;
161
			int textureHeight = holder.height;
162
			if (textureWidth <= this.width && textureHeight <= this.height) {
163
				if (textureWidth == this.width && textureHeight == this.height) {
164
					this.holder = holder;
165
					return true;
166
				}
167
168
				if (this.subSlots == null) {
169
					this.subSlots = new ArrayList<>(1);
170
					this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY, textureWidth, textureHeight));
171
					int spareWidth = this.width - textureWidth;
172
					int spareHeight = this.height - textureHeight;
173
					if (spareHeight > 0 && spareWidth > 0) {
174
						int right = Math.max(this.height, spareWidth);
175
						int bottom = Math.max(this.width, spareHeight);
176
						if (right >= bottom) {
177
							this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY + textureHeight, textureWidth, spareHeight));
178
							this.subSlots.add(new Stitcher.Region<>(this.originX + textureWidth, this.originY, spareWidth, this.height));
179
						} else {
180
							this.subSlots.add(new Stitcher.Region<>(this.originX + textureWidth, this.originY, spareWidth, textureHeight));
181
							this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY + textureHeight, this.width, spareHeight));
182
						}
183
					} else if (spareWidth == 0) {
184
						this.subSlots.add(new Stitcher.Region<>(this.originX, this.originY + textureHeight, textureWidth, spareHeight));
185
					} else if (spareHeight == 0) {
186
						this.subSlots.add(new Stitcher.Region<>(this.originX + textureWidth, this.originY, spareWidth, textureHeight));
187
					}
188
				}
189
190
				for (Stitcher.Region<T> subSlot : this.subSlots) {
191
					if (subSlot.add(holder)) {
192
						return true;
193
					}
194
				}
195
196
				return false;
197
			} else {
198
				return false;
199
			}
200
		}
201
202
		public void walk(final Stitcher.SpriteLoader<T> output, final int padding) {
203
			if (this.holder != null) {
204
				output.load(this.holder.entry, this.getX(), this.getY(), padding);
205
			} else if (this.subSlots != null) {
206
				for (Stitcher.Region<T> subSlot : this.subSlots) {
207
					subSlot.walk(output, padding);
208
				}
209
			}
210
		}
211
212
		@Override
213
		public String toString() {
214
			return "Slot{originX="
215
				+ this.originX
216
				+ ", originY="
217
				+ this.originY
218
				+ ", width="
219
				+ this.width
220
				+ ", height="
221
				+ this.height
222
				+ ", texture="
223
				+ this.holder
224
				+ ", subSlots="
225
				+ this.subSlots
226
				+ "}";
227
		}
228
	}
229
230
	@Environment(EnvType.CLIENT)
231
	public interface SpriteLoader<T extends Stitcher.Entry> {
232
		void load(T entry, int x, int z, int padding);
233
	}
234
}
235

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