| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.client.renderer.StitcherException;
|
| 9 | import net.minecraft.util.MathHelper;
|
| 10 |
|
| 11 | public class Stitcher
|
| 12 | {
|
| 13 | private final int mipmapLevelStitcher;
|
| 14 | private final Set<Stitcher.Holder> setStitchHolders = Sets.<Stitcher.Holder>newHashSetWithExpectedSize(256);
|
| 15 | private final List<Stitcher.Slot> stitchSlots = Lists.<Stitcher.Slot>newArrayListWithCapacity(256);
|
| 16 | private int currentWidth;
|
| 17 | private int currentHeight;
|
| 18 | private final int maxWidth;
|
| 19 | private final int maxHeight;
|
| 20 | private final boolean forcePowerOf2;
|
| 21 |
|
| 22 | /** Max size (width or height) of a single tile */
|
| 23 | private final int maxTileDimension;
|
| 24 |
|
| 25 | public Stitcher(int maxTextureWidth, int maxTextureHeight, boolean p_i45095_3_, int p_i45095_4_, int mipmapLevel)
|
| 26 | {
|
| 27 | this.mipmapLevelStitcher = mipmapLevel;
|
| 28 | this.maxWidth = maxTextureWidth;
|
| 29 | this.maxHeight = maxTextureHeight;
|
| 30 | this.forcePowerOf2 = p_i45095_3_;
|
| 31 | this.maxTileDimension = p_i45095_4_;
|
| 32 | }
|
| 33 |
|
| 34 | public int getCurrentWidth()
|
| 35 | {
|
| 36 | return this.currentWidth;
|
| 37 | }
|
| 38 |
|
| 39 | public int getCurrentHeight()
|
| 40 | {
|
| 41 | return this.currentHeight;
|
| 42 | }
|
| 43 |
|
| 44 | public void addSprite(TextureAtlasSprite p_110934_1_)
|
| 45 | {
|
| 46 | Stitcher.Holder stitcher$holder = new Stitcher.Holder(p_110934_1_, this.mipmapLevelStitcher);
|
| 47 |
|
| 48 | if (this.maxTileDimension > 0)
|
| 49 | {
|
| 50 | stitcher$holder.setNewDimension(this.maxTileDimension);
|
| 51 | }
|
| 52 |
|
| 53 | this.setStitchHolders.add(stitcher$holder);
|
| 54 | }
|
| 55 |
|
| 56 | public void doStitch()
|
| 57 | {
|
| 58 | Stitcher.Holder[] astitcher$holder = (Stitcher.Holder[])this.setStitchHolders.toArray(new Stitcher.Holder[this.setStitchHolders.size()]);
|
| 59 | Arrays.sort((Object[])astitcher$holder);
|
| 60 |
|
| 61 | for (Stitcher.Holder stitcher$holder : astitcher$holder)
|
| 62 | {
|
| 63 | if (!this.allocateSlot(stitcher$holder))
|
| 64 | {
|
| 65 | String s = String.format("Unable to fit: %s - size: %dx%d - Maybe try a lowerresolution resourcepack?", new Object[] {stitcher$holder.getAtlasSprite().getIconName(), Integer.valueOf(stitcher$holder.getAtlasSprite().getIconWidth()), Integer.valueOf(stitcher$holder.getAtlasSprite().getIconHeight())});
|
| 66 | throw new StitcherException(stitcher$holder, s);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | if (this.forcePowerOf2)
|
| 71 | {
|
| 72 | this.currentWidth = MathHelper.roundUpToPowerOfTwo(this.currentWidth);
|
| 73 | this.currentHeight = MathHelper.roundUpToPowerOfTwo(this.currentHeight);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | public List<TextureAtlasSprite> getStichSlots()
|
| 78 | {
|
| 79 | List<Stitcher.Slot> list = Lists.<Stitcher.Slot>newArrayList();
|
| 80 |
|
| 81 | for (Stitcher.Slot stitcher$slot : this.stitchSlots)
|
| 82 | {
|
| 83 | stitcher$slot.getAllStitchSlots(list);
|
| 84 | }
|
| 85 |
|
| 86 | List<TextureAtlasSprite> list1 = Lists.<TextureAtlasSprite>newArrayList();
|
| 87 |
|
| 88 | for (Stitcher.Slot stitcher$slot1 : list)
|
| 89 | {
|
| 90 | Stitcher.Holder stitcher$holder = stitcher$slot1.getStitchHolder();
|
| 91 | TextureAtlasSprite textureatlassprite = stitcher$holder.getAtlasSprite();
|
| 92 | textureatlassprite.initSprite(this.currentWidth, this.currentHeight, stitcher$slot1.getOriginX(), stitcher$slot1.getOriginY(), stitcher$holder.isRotated());
|
| 93 | list1.add(textureatlassprite);
|
| 94 | }
|
| 95 |
|
| 96 | return list1;
|
| 97 | }
|
| 98 |
|
| 99 | private static int getMipmapDimension(int p_147969_0_, int p_147969_1_)
|
| 100 | {
|
| 101 | return (p_147969_0_ >> p_147969_1_) + ((p_147969_0_ & (1 << p_147969_1_) - 1) == 0 ? 0 : 1) << p_147969_1_;
|
| 102 | }
|
| 103 |
|
| 104 | /**
|
| 105 | * Attempts to find space for specified tile
|
| 106 | */
|
| 107 | private boolean allocateSlot(Stitcher.Holder p_94310_1_)
|
| 108 | {
|
| 109 | for (int i = 0; i < this.stitchSlots.size(); ++i)
|
| 110 | {
|
| 111 | if (((Stitcher.Slot)this.stitchSlots.get(i)).addSlot(p_94310_1_))
|
| 112 | {
|
| 113 | return true;
|
| 114 | }
|
| 115 |
|
| 116 | p_94310_1_.rotate();
|
| 117 |
|
| 118 | if (((Stitcher.Slot)this.stitchSlots.get(i)).addSlot(p_94310_1_))
|
| 119 | {
|
| 120 | return true;
|
| 121 | }
|
| 122 |
|
| 123 | p_94310_1_.rotate();
|
| 124 | }
|
| 125 |
|
| 126 | return this.expandAndAllocateSlot(p_94310_1_);
|
| 127 | }
|
| 128 |
|
| 129 | /**
|
| 130 | * Expand stitched texture in order to make space for specified tile
|
| 131 | */
|
| 132 | private boolean expandAndAllocateSlot(Stitcher.Holder p_94311_1_)
|
| 133 | {
|
| 134 | int i = Math.min(p_94311_1_.getWidth(), p_94311_1_.getHeight());
|
| 135 | boolean flag = this.currentWidth == 0 && this.currentHeight == 0;
|
| 136 | boolean flag1;
|
| 137 |
|
| 138 | if (this.forcePowerOf2)
|
| 139 | {
|
| 140 | int j = MathHelper.roundUpToPowerOfTwo(this.currentWidth);
|
| 141 | int k = MathHelper.roundUpToPowerOfTwo(this.currentHeight);
|
| 142 | int l = MathHelper.roundUpToPowerOfTwo(this.currentWidth + i);
|
| 143 | int i1 = MathHelper.roundUpToPowerOfTwo(this.currentHeight + i);
|
| 144 | boolean flag2 = l <= this.maxWidth;
|
| 145 | boolean flag3 = i1 <= this.maxHeight;
|
| 146 |
|
| 147 | if (!flag2 && !flag3)
|
| 148 | {
|
| 149 | return false;
|
| 150 | }
|
| 151 |
|
| 152 | boolean flag4 = j != l;
|
| 153 | boolean flag5 = k != i1;
|
| 154 |
|
| 155 | if (flag4 ^ flag5)
|
| 156 | {
|
| 157 | flag1 = !flag4;
|
| 158 | }
|
| 159 | else
|
| 160 | {
|
| 161 | flag1 = flag2 && j <= k;
|
| 162 | }
|
| 163 | }
|
| 164 | else
|
| 165 | {
|
| 166 | boolean flag6 = this.currentWidth + i <= this.maxWidth;
|
| 167 | boolean flag7 = this.currentHeight + i <= this.maxHeight;
|
| 168 |
|
| 169 | if (!flag6 && !flag7)
|
| 170 | {
|
| 171 | return false;
|
| 172 | }
|
| 173 |
|
| 174 | flag1 = flag6 && (flag || this.currentWidth <= this.currentHeight);
|
| 175 | }
|
| 176 |
|
| 177 | int j1 = Math.max(p_94311_1_.getWidth(), p_94311_1_.getHeight());
|
| 178 |
|
| 179 | if (MathHelper.roundUpToPowerOfTwo((flag1 ? this.currentHeight : this.currentWidth) + j1) > (flag1 ? this.maxHeight : this.maxWidth))
|
| 180 | {
|
| 181 | return false;
|
| 182 | }
|
| 183 | else
|
| 184 | {
|
| 185 | Stitcher.Slot stitcher$slot;
|
| 186 |
|
| 187 | if (flag1)
|
| 188 | {
|
| 189 | if (p_94311_1_.getWidth() > p_94311_1_.getHeight())
|
| 190 | {
|
| 191 | p_94311_1_.rotate();
|
| 192 | }
|
| 193 |
|
| 194 | if (this.currentHeight == 0)
|
| 195 | {
|
| 196 | this.currentHeight = p_94311_1_.getHeight();
|
| 197 | }
|
| 198 |
|
| 199 | stitcher$slot = new Stitcher.Slot(this.currentWidth, 0, p_94311_1_.getWidth(), this.currentHeight);
|
| 200 | this.currentWidth += p_94311_1_.getWidth();
|
| 201 | }
|
| 202 | else
|
| 203 | {
|
| 204 | stitcher$slot = new Stitcher.Slot(0, this.currentHeight, this.currentWidth, p_94311_1_.getHeight());
|
| 205 | this.currentHeight += p_94311_1_.getHeight();
|
| 206 | }
|
| 207 |
|
| 208 | stitcher$slot.addSlot(p_94311_1_);
|
| 209 | this.stitchSlots.add(stitcher$slot);
|
| 210 | return true;
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public static class Holder implements Comparable<Stitcher.Holder>
|
| 215 | {
|
| 216 | private final TextureAtlasSprite theTexture;
|
| 217 | private final int width;
|
| 218 | private final int height;
|
| 219 | private final int mipmapLevelHolder;
|
| 220 | private boolean rotated;
|
| 221 | private float scaleFactor = 1.0F;
|
| 222 |
|
| 223 | public Holder(TextureAtlasSprite p_i45094_1_, int p_i45094_2_)
|
| 224 | {
|
| 225 | this.theTexture = p_i45094_1_;
|
| 226 | this.width = p_i45094_1_.getIconWidth();
|
| 227 | this.height = p_i45094_1_.getIconHeight();
|
| 228 | this.mipmapLevelHolder = p_i45094_2_;
|
| 229 | this.rotated = Stitcher.getMipmapDimension(this.height, p_i45094_2_) > Stitcher.getMipmapDimension(this.width, p_i45094_2_);
|
| 230 | }
|
| 231 |
|
| 232 | public TextureAtlasSprite getAtlasSprite()
|
| 233 | {
|
| 234 | return this.theTexture;
|
| 235 | }
|
| 236 |
|
| 237 | public int getWidth()
|
| 238 | {
|
| 239 | return this.rotated ? Stitcher.getMipmapDimension((int)((float)this.height * this.scaleFactor), this.mipmapLevelHolder) : Stitcher.getMipmapDimension((int)((float)this.width * this.scaleFactor), this.mipmapLevelHolder);
|
| 240 | }
|
| 241 |
|
| 242 | public int getHeight()
|
| 243 | {
|
| 244 | return this.rotated ? Stitcher.getMipmapDimension((int)((float)this.width * this.scaleFactor), this.mipmapLevelHolder) : Stitcher.getMipmapDimension((int)((float)this.height * this.scaleFactor), this.mipmapLevelHolder);
|
| 245 | }
|
| 246 |
|
| 247 | public void rotate()
|
| 248 | {
|
| 249 | this.rotated = !this.rotated;
|
| 250 | }
|
| 251 |
|
| 252 | public boolean isRotated()
|
| 253 | {
|
| 254 | return this.rotated;
|
| 255 | }
|
| 256 |
|
| 257 | public void setNewDimension(int p_94196_1_)
|
| 258 | {
|
| 259 | if (this.width > p_94196_1_ && this.height > p_94196_1_)
|
| 260 | {
|
| 261 | this.scaleFactor = (float)p_94196_1_ / (float)Math.min(this.width, this.height);
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | public String toString()
|
| 266 | {
|
| 267 | return "Holder{width=" + this.width + ", height=" + this.height + '}';
|
| 268 | }
|
| 269 |
|
| 270 | public int compareTo(Stitcher.Holder p_compareTo_1_)
|
| 271 | {
|
| 272 | int i;
|
| 273 |
|
| 274 | if (this.getHeight() == p_compareTo_1_.getHeight())
|
| 275 | {
|
| 276 | if (this.getWidth() == p_compareTo_1_.getWidth())
|
| 277 | {
|
| 278 | if (this.theTexture.getIconName() == null)
|
| 279 | {
|
| 280 | return p_compareTo_1_.theTexture.getIconName() == null ? 0 : -1;
|
| 281 | }
|
| 282 |
|
| 283 | return this.theTexture.getIconName().compareTo(p_compareTo_1_.theTexture.getIconName());
|
| 284 | }
|
| 285 |
|
| 286 | i = this.getWidth() < p_compareTo_1_.getWidth() ? 1 : -1;
|
| 287 | }
|
| 288 | else
|
| 289 | {
|
| 290 | i = this.getHeight() < p_compareTo_1_.getHeight() ? 1 : -1;
|
| 291 | }
|
| 292 |
|
| 293 | return i;
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | public static class Slot
|
| 298 | {
|
| 299 | private final int originX;
|
| 300 | private final int originY;
|
| 301 | private final int width;
|
| 302 | private final int height;
|
| 303 | private List<Stitcher.Slot> subSlots;
|
| 304 | private Stitcher.Holder holder;
|
| 305 |
|
| 306 | public Slot(int p_i1277_1_, int p_i1277_2_, int widthIn, int heightIn)
|
| 307 | {
|
| 308 | this.originX = p_i1277_1_;
|
| 309 | this.originY = p_i1277_2_;
|
| 310 | this.width = widthIn;
|
| 311 | this.height = heightIn;
|
| 312 | }
|
| 313 |
|
| 314 | public Stitcher.Holder getStitchHolder()
|
| 315 | {
|
| 316 | return this.holder;
|
| 317 | }
|
| 318 |
|
| 319 | public int getOriginX()
|
| 320 | {
|
| 321 | return this.originX;
|
| 322 | }
|
| 323 |
|
| 324 | public int getOriginY()
|
| 325 | {
|
| 326 | return this.originY;
|
| 327 | }
|
| 328 |
|
| 329 | public boolean addSlot(Stitcher.Holder holderIn)
|
| 330 | {
|
| 331 | if (this.holder != null)
|
| 332 | {
|
| 333 | return false;
|
| 334 | }
|
| 335 | else
|
| 336 | {
|
| 337 | int i = holderIn.getWidth();
|
| 338 | int j = holderIn.getHeight();
|
| 339 |
|
| 340 | if (i <= this.width && j <= this.height)
|
| 341 | {
|
| 342 | if (i == this.width && j == this.height)
|
| 343 | {
|
| 344 | this.holder = holderIn;
|
| 345 | return true;
|
| 346 | }
|
| 347 | else
|
| 348 | {
|
| 349 | if (this.subSlots == null)
|
| 350 | {
|
| 351 | this.subSlots = Lists.<Stitcher.Slot>newArrayListWithCapacity(1);
|
| 352 | this.subSlots.add(new Stitcher.Slot(this.originX, this.originY, i, j));
|
| 353 | int k = this.width - i;
|
| 354 | int l = this.height - j;
|
| 355 |
|
| 356 | if (l > 0 && k > 0)
|
| 357 | {
|
| 358 | int i1 = Math.max(this.height, k);
|
| 359 | int j1 = Math.max(this.width, l);
|
| 360 |
|
| 361 | if (i1 >= j1)
|
| 362 | {
|
| 363 | this.subSlots.add(new Stitcher.Slot(this.originX, this.originY + j, i, l));
|
| 364 | this.subSlots.add(new Stitcher.Slot(this.originX + i, this.originY, k, this.height));
|
| 365 | }
|
| 366 | else
|
| 367 | {
|
| 368 | this.subSlots.add(new Stitcher.Slot(this.originX + i, this.originY, k, j));
|
| 369 | this.subSlots.add(new Stitcher.Slot(this.originX, this.originY + j, this.width, l));
|
| 370 | }
|
| 371 | }
|
| 372 | else if (k == 0)
|
| 373 | {
|
| 374 | this.subSlots.add(new Stitcher.Slot(this.originX, this.originY + j, i, l));
|
| 375 | }
|
| 376 | else if (l == 0)
|
| 377 | {
|
| 378 | this.subSlots.add(new Stitcher.Slot(this.originX + i, this.originY, k, j));
|
| 379 | }
|
| 380 | }
|
| 381 |
|
| 382 | for (Stitcher.Slot stitcher$slot : this.subSlots)
|
| 383 | {
|
| 384 | if (stitcher$slot.addSlot(holderIn))
|
| 385 | {
|
| 386 | return true;
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | return false;
|
| 391 | }
|
| 392 | }
|
| 393 | else
|
| 394 | {
|
| 395 | return false;
|
| 396 | }
|
| 397 | }
|
| 398 | }
|
| 399 |
|
| 400 | public void getAllStitchSlots(List<Stitcher.Slot> p_94184_1_)
|
| 401 | {
|
| 402 | if (this.holder != null)
|
| 403 | {
|
| 404 | p_94184_1_.add(this);
|
| 405 | }
|
| 406 | else if (this.subSlots != null)
|
| 407 | {
|
| 408 | for (Stitcher.Slot stitcher$slot : this.subSlots)
|
| 409 | {
|
| 410 | stitcher$slot.getAllStitchSlots(p_94184_1_);
|
| 411 | }
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | public String toString()
|
| 416 | {
|
| 417 | return "Slot{originX=" + this.originX + ", originY=" + this.originY + ", width=" + this.width + ", height=" + this.height + ", texture=" + this.holder + ", subSlots=" + this.subSlots + '}';
|
| 418 | }
|
| 419 | }
|
| 420 | }
|
| 421 |
|