Search in sources :

Example 1 with AnimationMetadataSection

use of net.minecraft.client.resources.data.AnimationMetadataSection in project Charset by CharsetMC.

the class TextureAtlasSpriteCustom method addFrameTextureData.

protected void addFrameTextureData(int width, int height, int[] pixels, @Nullable IResource resource) {
    AnimationMetadataSection section = resource != null ? resource.getMetadata("animation") : null;
    this.clearFramesTextureData();
    if (section == null) {
        this.setIconWidth(width);
        this.setIconHeight(height);
        int[][] pixelsCtr = new int[Minecraft.getMinecraft().getTextureMapBlocks().getMipmapLevels() + 1][];
        pixelsCtr[0] = pixels;
        framesTextureData.add(pixelsCtr);
    } else {
        this.setIconWidth(width);
        this.setIconHeight(width);
        int i = height / width;
        if (section.getFrameCount() > 0) {
            for (Integer index : section.getFrameIndexSet()) {
                if (index >= i) {
                    throw new RuntimeException("invalid frameindex " + index);
                }
                while (framesTextureData.size() < index) {
                    framesTextureData.add(null);
                }
                int[] data = new int[width * width];
                int[][] container = new int[Minecraft.getMinecraft().getTextureMapBlocks().getMipmapLevels() + 1][];
                System.arraycopy(pixels, width * width * index, data, 0, width * width);
                container[0] = data;
                framesTextureData.set(index, container);
            }
        } else {
            List<AnimationFrame> frames = new ArrayList<>(i);
            for (int index = 0; index < i; index++) {
                int[] data = new int[width * width];
                int[][] container = new int[Minecraft.getMinecraft().getTextureMapBlocks().getMipmapLevels() + 1][];
                System.arraycopy(pixels, width * width * index, data, 0, width * width);
                container[0] = data;
                framesTextureData.add(container);
                frames.add(new AnimationFrame(index, -1));
            }
            section = new AnimationMetadataSection(frames, width, height, section.getFrameTime(), section.isInterpolate());
        }
        try {
            ANIMATION_METADATA_SETTER.invokeExact((TextureAtlasSprite) this, section);
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }
}
Also used : AnimationMetadataSection(net.minecraft.client.resources.data.AnimationMetadataSection) ArrayList(java.util.ArrayList) AnimationFrame(net.minecraft.client.resources.data.AnimationFrame)

Example 2 with AnimationMetadataSection

use of net.minecraft.client.resources.data.AnimationMetadataSection in project UtilityClient2 by Utility-Client.

the class TextureAtlasSprite method loadSprite.

public void loadSprite(BufferedImage[] images, AnimationMetadataSection meta) throws IOException {
    this.resetSprite();
    int i = images[0].getWidth();
    int j = images[0].getHeight();
    this.width = i;
    this.height = j;
    int[][] aint = new int[images.length][];
    for (int k = 0; k < images.length; ++k) {
        BufferedImage bufferedimage = images[k];
        if (bufferedimage != null) {
            if (k > 0 && (bufferedimage.getWidth() != i >> k || bufferedimage.getHeight() != j >> k)) {
                throw new RuntimeException(String.format("Unable to load miplevel: %d, image is size: %dx%d, expected %dx%d", new Object[] { Integer.valueOf(k), Integer.valueOf(bufferedimage.getWidth()), Integer.valueOf(bufferedimage.getHeight()), Integer.valueOf(i >> k), Integer.valueOf(j >> k) }));
            }
            aint[k] = new int[bufferedimage.getWidth() * bufferedimage.getHeight()];
            bufferedimage.getRGB(0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), aint[k], 0, bufferedimage.getWidth());
        }
    }
    if (meta == null) {
        if (j != i) {
            throw new RuntimeException("broken aspect ratio and not an animation");
        }
        this.framesTextureData.add(aint);
    } else {
        int j1 = j / i;
        int k1 = i;
        int l = i;
        this.height = this.width;
        if (meta.getFrameCount() > 0) {
            Iterator iterator = meta.getFrameIndexSet().iterator();
            while (iterator.hasNext()) {
                int i1 = ((Integer) iterator.next()).intValue();
                if (i1 >= j1) {
                    throw new RuntimeException("invalid frameindex " + i1);
                }
                this.allocateFrameTextureData(i1);
                this.framesTextureData.set(i1, getFrameTextureData(aint, k1, l, i1));
            }
            this.animationMetadata = meta;
        } else {
            List<AnimationFrame> list = Lists.<AnimationFrame>newArrayList();
            for (int l1 = 0; l1 < j1; ++l1) {
                this.framesTextureData.add(getFrameTextureData(aint, k1, l, l1));
                list.add(new AnimationFrame(l1, -1));
            }
            this.animationMetadata = new AnimationMetadataSection(list, this.width, this.height, meta.getFrameTime(), meta.isInterpolate());
        }
    }
}
Also used : AnimationMetadataSection(net.minecraft.client.resources.data.AnimationMetadataSection) Iterator(java.util.Iterator) AnimationFrame(net.minecraft.client.resources.data.AnimationFrame) BufferedImage(java.awt.image.BufferedImage)

Example 3 with AnimationMetadataSection

use of net.minecraft.client.resources.data.AnimationMetadataSection in project UtilityClient2 by Utility-Client.

the class TextureMap method loadTextureAtlas.

public void loadTextureAtlas(IResourceManager resourceManager) {
    int i = Minecraft.getGLMaximumTextureSize();
    Stitcher stitcher = new Stitcher(i, i, true, 0, this.mipmapLevels);
    this.mapUploadedSprites.clear();
    this.listAnimatedSprites.clear();
    int j = Integer.MAX_VALUE;
    int k = 1 << this.mipmapLevels;
    for (Entry<String, TextureAtlasSprite> entry : this.mapRegisteredSprites.entrySet()) {
        TextureAtlasSprite textureatlassprite = (TextureAtlasSprite) entry.getValue();
        ResourceLocation resourcelocation = new ResourceLocation(textureatlassprite.getIconName());
        ResourceLocation resourcelocation1 = this.completeResourceLocation(resourcelocation, 0);
        try {
            IResource iresource = resourceManager.getResource(resourcelocation1);
            BufferedImage[] abufferedimage = new BufferedImage[1 + this.mipmapLevels];
            abufferedimage[0] = TextureUtil.readBufferedImage(iresource.getInputStream());
            TextureMetadataSection texturemetadatasection = (TextureMetadataSection) iresource.getMetadata("texture");
            if (texturemetadatasection != null) {
                List<Integer> list = texturemetadatasection.getListMipmaps();
                if (!list.isEmpty()) {
                    int l = abufferedimage[0].getWidth();
                    int i1 = abufferedimage[0].getHeight();
                    if (MathHelper.roundUpToPowerOfTwo(l) != l || MathHelper.roundUpToPowerOfTwo(i1) != i1) {
                        throw new RuntimeException("Unable to load extra miplevels, source-texture is not power of two");
                    }
                }
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    int i2 = ((Integer) iterator.next()).intValue();
                    if (i2 > 0 && i2 < abufferedimage.length - 1 && abufferedimage[i2] == null) {
                        ResourceLocation resourcelocation2 = this.completeResourceLocation(resourcelocation, i2);
                        try {
                            abufferedimage[i2] = TextureUtil.readBufferedImage(resourceManager.getResource(resourcelocation2).getInputStream());
                        } catch (IOException ioexception) {
                            logger.error("Unable to load miplevel {} from: {}", new Object[] { Integer.valueOf(i2), resourcelocation2, ioexception });
                        }
                    }
                }
            }
            AnimationMetadataSection animationmetadatasection = (AnimationMetadataSection) iresource.getMetadata("animation");
            textureatlassprite.loadSprite(abufferedimage, animationmetadatasection);
        } catch (RuntimeException runtimeexception) {
            logger.error((String) ("Unable to parse metadata from " + resourcelocation1), (Throwable) runtimeexception);
            continue;
        } catch (IOException ioexception1) {
            logger.error((String) ("Using missing texture, unable to load " + resourcelocation1), (Throwable) ioexception1);
            continue;
        }
        j = Math.min(j, Math.min(textureatlassprite.getIconWidth(), textureatlassprite.getIconHeight()));
        int l1 = Math.min(Integer.lowestOneBit(textureatlassprite.getIconWidth()), Integer.lowestOneBit(textureatlassprite.getIconHeight()));
        if (l1 < k) {
            logger.warn("Texture {} with size {}x{} limits mip level from {} to {}", new Object[] { resourcelocation1, Integer.valueOf(textureatlassprite.getIconWidth()), Integer.valueOf(textureatlassprite.getIconHeight()), Integer.valueOf(MathHelper.calculateLogBaseTwo(k)), Integer.valueOf(MathHelper.calculateLogBaseTwo(l1)) });
            k = l1;
        }
        stitcher.addSprite(textureatlassprite);
    }
    int j1 = Math.min(j, k);
    int k1 = MathHelper.calculateLogBaseTwo(j1);
    if (k1 < this.mipmapLevels) {
        logger.warn("{}: dropping miplevel from {} to {}, because of minimum power of two: {}", new Object[] { this.basePath, Integer.valueOf(this.mipmapLevels), Integer.valueOf(k1), Integer.valueOf(j1) });
        this.mipmapLevels = k1;
    }
    for (final TextureAtlasSprite textureatlassprite1 : this.mapRegisteredSprites.values()) {
        try {
            textureatlassprite1.generateMipmaps(this.mipmapLevels);
        } catch (Throwable throwable1) {
            CrashReport crashreport = CrashReport.makeCrashReport(throwable1, "Applying mipmap");
            CrashReportCategory crashreportcategory = crashreport.makeCategory("Sprite being mipmapped");
            crashreportcategory.addCrashSectionCallable("Sprite name", new Callable<String>() {

                public String call() throws Exception {
                    return textureatlassprite1.getIconName();
                }
            });
            crashreportcategory.addCrashSectionCallable("Sprite size", new Callable<String>() {

                public String call() throws Exception {
                    return textureatlassprite1.getIconWidth() + " x " + textureatlassprite1.getIconHeight();
                }
            });
            crashreportcategory.addCrashSectionCallable("Sprite frames", new Callable<String>() {

                public String call() throws Exception {
                    return textureatlassprite1.getFrameCount() + " frames";
                }
            });
            crashreportcategory.addCrashSection("Mipmap levels", Integer.valueOf(this.mipmapLevels));
            throw new ReportedException(crashreport);
        }
    }
    this.missingImage.generateMipmaps(this.mipmapLevels);
    stitcher.addSprite(this.missingImage);
    try {
        stitcher.doStitch();
    } catch (StitcherException stitcherexception) {
        throw stitcherexception;
    }
    logger.info("Created: {}x{} {}-atlas", new Object[] { Integer.valueOf(stitcher.getCurrentWidth()), Integer.valueOf(stitcher.getCurrentHeight()), this.basePath });
    TextureUtil.allocateTextureImpl(this.getGlTextureId(), this.mipmapLevels, stitcher.getCurrentWidth(), stitcher.getCurrentHeight());
    Map<String, TextureAtlasSprite> map = Maps.<String, TextureAtlasSprite>newHashMap(this.mapRegisteredSprites);
    for (TextureAtlasSprite textureatlassprite2 : stitcher.getStichSlots()) {
        String s = textureatlassprite2.getIconName();
        map.remove(s);
        this.mapUploadedSprites.put(s, textureatlassprite2);
        try {
            TextureUtil.uploadTextureMipmap(textureatlassprite2.getFrameTextureData(0), textureatlassprite2.getIconWidth(), textureatlassprite2.getIconHeight(), textureatlassprite2.getOriginX(), textureatlassprite2.getOriginY(), false, false);
        } catch (Throwable throwable) {
            CrashReport crashreport1 = CrashReport.makeCrashReport(throwable, "Stitching texture atlas");
            CrashReportCategory crashreportcategory1 = crashreport1.makeCategory("Texture being stitched together");
            crashreportcategory1.addCrashSection("Atlas path", this.basePath);
            crashreportcategory1.addCrashSection("Sprite", textureatlassprite2);
            throw new ReportedException(crashreport1);
        }
        if (textureatlassprite2.hasAnimationMetadata()) {
            this.listAnimatedSprites.add(textureatlassprite2);
        }
    }
    for (TextureAtlasSprite textureatlassprite3 : map.values()) {
        textureatlassprite3.copyFrom(this.missingImage);
    }
}
Also used : StitcherException(net.minecraft.client.renderer.StitcherException) AnimationMetadataSection(net.minecraft.client.resources.data.AnimationMetadataSection) CrashReport(net.minecraft.crash.CrashReport) TextureMetadataSection(net.minecraft.client.resources.data.TextureMetadataSection) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Callable(java.util.concurrent.Callable) ResourceLocation(net.minecraft.util.ResourceLocation) Iterator(java.util.Iterator) IResource(net.minecraft.client.resources.IResource) CrashReportCategory(net.minecraft.crash.CrashReportCategory) ReportedException(net.minecraft.util.ReportedException)

Aggregations

AnimationMetadataSection (net.minecraft.client.resources.data.AnimationMetadataSection)3 BufferedImage (java.awt.image.BufferedImage)2 Iterator (java.util.Iterator)2 AnimationFrame (net.minecraft.client.resources.data.AnimationFrame)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 StitcherException (net.minecraft.client.renderer.StitcherException)1 IResource (net.minecraft.client.resources.IResource)1 TextureMetadataSection (net.minecraft.client.resources.data.TextureMetadataSection)1 CrashReport (net.minecraft.crash.CrashReport)1 CrashReportCategory (net.minecraft.crash.CrashReportCategory)1 ReportedException (net.minecraft.util.ReportedException)1 ResourceLocation (net.minecraft.util.ResourceLocation)1