Search in sources :

Example 11 with IResource

use of net.minecraft.client.resources.IResource in project BuildCraft by BuildCraft.

the class SpriteBuilder method getImage.

public static BufferedImage getImage(IResourceManager manager, ResourceLocation loc) {
    String domain = loc.getResourceDomain();
    String path = loc.getResourcePath();
    if (!path.startsWith("textures/"))
        path = "textures/" + path;
    if (!path.endsWith(".png"))
        path = path + ".png";
    ResourceLocation altered = new ResourceLocation(domain, path);
    try (IResource res = manager.getResource(altered)) {
        BufferedImage image = ImageIO.read(res.getInputStream());
        return image;
    } catch (IOException e) {
        if (loc == TextureMap.LOCATION_MISSING_TEXTURE)
            throw new RuntimeException("Unable to load the missing texture!", e);
        return getImage(manager, TextureMap.LOCATION_MISSING_TEXTURE);
    }
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) IOException(java.io.IOException) IResource(net.minecraft.client.resources.IResource) BufferedImage(java.awt.image.BufferedImage)

Example 12 with IResource

use of net.minecraft.client.resources.IResource in project ImmersiveEngineering by BluSunrize.

the class IEShaderLayerCompositeTexture method loadTexture.

@Override
public void loadTexture(IResourceManager resourceManager) {
    this.deleteGlTexture();
    IResource iresource = null;
    BufferedImage bufferedimage;
    BufferedImage scaledImage;
    label255: {
        try {
            iresource = resourceManager.getResource(this.canvasTexture);
            BufferedImage canvasImage = TextureUtil.readBufferedImage(iresource.getInputStream());
            int imageType = canvasImage.getType();
            if (imageType == 0)
                imageType = 6;
            int canvasWidth = canvasImage.getWidth();
            int canvasHeight = canvasImage.getHeight();
            bufferedimage = new BufferedImage(canvasWidth, canvasHeight, imageType);
            int layer = 0;
            while (true) {
                if (layer >= 17 || layer >= this.layers.length)
                    break label255;
                IResource iresource1 = null;
                try {
                    String texPath = this.layers[layer].getTexture().getPath();
                    if (!texPath.startsWith("textures/"))
                        texPath = "textures/" + texPath;
                    if (!texPath.endsWith(".png"))
                        texPath += ".png";
                    String texture = this.layers[layer].getTexture().getNamespace() + ":" + texPath;
                    int colour = this.layers[layer].getColour();
                    iresource1 = resourceManager.getResource(new ResourceLocation(texture));
                    BufferedImage texureImage = TextureUtil.readBufferedImage(iresource1.getInputStream());
                    scaledImage = new BufferedImage(canvasWidth, canvasHeight, imageType);
                    float[] mod = { (colour >> 16 & 255) / 255f, (colour >> 8 & 255) / 255f, (colour & 255) / 255f, (colour >> 24 & 255) / 255f };
                    IntFunction<Integer> uInterpolate = uIn -> uIn;
                    IntFunction<Integer> vInterpolate = vIn -> vIn;
                    int bufImg2Size = Math.min(texureImage.getWidth(), texureImage.getHeight());
                    int uMin = 0;
                    int vMin = 0;
                    int uMax = canvasWidth;
                    int vMax = canvasHeight;
                    final double[] texBounds = this.layers[layer].getTextureBounds();
                    if (texBounds != null) {
                        final double uOffset = texBounds[0] * canvasWidth;
                        final double vOffset = texBounds[1] * canvasHeight;
                        final double uScale = bufImg2Size / ((texBounds[2] - texBounds[0]) * canvasWidth);
                        final double vScale = bufImg2Size / ((texBounds[3] - texBounds[1]) * canvasHeight);
                        uInterpolate = uIn -> (int) Math.round((uIn - uOffset) * uScale);
                        vInterpolate = vIn -> (int) Math.round((vIn - vOffset) * vScale);
                        uMin = (int) uOffset;
                        vMin = (int) vOffset;
                        uMax = (int) (texBounds[2] * canvasWidth);
                        vMax = (int) (texBounds[3] * canvasHeight);
                    }
                    try {
                        for (int v = vMin; v < vMax; ++v) for (int u = uMin; u < uMax; ++u) {
                            int interU = uInterpolate.apply(u) % bufImg2Size;
                            int interV = vInterpolate.apply(v) % bufImg2Size;
                            int iRGB = texureImage.getRGB(interU, interV);
                            float[] rgb = { (iRGB >> 16 & 255) / 255f, (iRGB >> 8 & 255) / 255f, (iRGB & 255) / 255f, (iRGB >> 24 & 255) / 255f };
                            if ((iRGB & -16777216) != 0) {
                                int iNoise = canvasImage.getRGB(u, v);
                                float[] noise = { (iNoise >> 16 & 255) / 255f, (iNoise >> 8 & 255) / 255f, (iNoise & 255) / 255f, (iNoise >> 24 & 255) / 255f };
                                for (int m = 0; m < 4; m++) rgb[m] = rgb[m] * mod[m] * noise[m];
                                int[] irgb = { (int) (rgb[0] * 255), (int) (rgb[1] * 255), (int) (rgb[2] * 255), (int) (rgb[3] * 255) };
                                int i2 = (irgb[0] << 16) + (irgb[1] << 8) + (irgb[2]) + (irgb[3] << 24);
                                scaledImage.setRGB(u, v, i2);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    bufferedimage.getGraphics().drawImage(scaledImage, 0, 0, null);
                } finally {
                    IOUtils.closeQuietly(iresource1);
                }
                ++layer;
            }
        } catch (IOException ioexception) {
            IELogger.error("Couldn't load layered image", ioexception);
        } finally {
            IOUtils.closeQuietly(iresource);
        }
        return;
    }
    TextureUtil.uploadTextureImage(this.getGlTextureId(), bufferedimage);
}
Also used : BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) IOUtils(org.apache.commons.io.IOUtils) AbstractTexture(net.minecraft.client.renderer.texture.AbstractTexture) TextureUtil(net.minecraft.client.renderer.texture.TextureUtil) Side(net.minecraftforge.fml.relauncher.Side) IELogger(blusunrize.immersiveengineering.common.util.IELogger) IResourceManager(net.minecraft.client.resources.IResourceManager) ResourceLocation(net.minecraft.util.ResourceLocation) ShaderLayer(blusunrize.immersiveengineering.api.shader.ShaderCase.ShaderLayer) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) IResource(net.minecraft.client.resources.IResource) IntFunction(java.util.function.IntFunction) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) ResourceLocation(net.minecraft.util.ResourceLocation) IResource(net.minecraft.client.resources.IResource)

Example 13 with IResource

use of net.minecraft.client.resources.IResource in project RecurrentComplex by Ivorforce.

the class GuiTexturedButton method setTexture.

public void setTexture(ResourceLocation texture) {
    this.texture = texture;
    if (texture != null) {
        try {
            IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(texture);
            BufferedImage bufferedimage = TextureUtil.readBufferedImage(iresource.getInputStream());
            textureWidth = bufferedimage.getWidth();
            textureHeight = bufferedimage.getHeight();
        } catch (IOException e) {
            RecurrentComplex.logger.error(e);
            this.texture = null;
        }
    }
}
Also used : IOException(java.io.IOException) IResource(net.minecraft.client.resources.IResource) BufferedImage(java.awt.image.BufferedImage)

Example 14 with IResource

use of net.minecraft.client.resources.IResource in project Almura by AlmuraDev.

the class OBJModelParser method parseMaterialLibrary.

private MaterialLibrary parseMaterialLibrary(final ResourceLocation source) throws Exception {
    final MaterialLibrary.Builder mtllibBuilder = MaterialLibrary.builder();
    final IResource resource = this.resourceManager.getResource(source);
    try (final InputStream stream = resource.getInputStream()) {
        MaterialDefinition.Builder mtlBuilder = null;
        String currentMaterial = null;
        final List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
        for (int i = 0; i < lines.size(); i++) {
            final String line = lines.get(i);
            // Skip comments and newlines
            if (line.startsWith(OBJModelConfig.COMMENT) || line.isEmpty()) {
                continue;
            }
            final String[] combinedLineContents = line.split(" ");
            final String lineHeader = combinedLineContents[0];
            final String[] lineContents = Arrays.copyOfRange(combinedLineContents, 1, combinedLineContents.length);
            switch(lineHeader) {
                case OBJModelConfig.Material.NEW_MATERIAL:
                    if (mtlBuilder != null) {
                        mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
                    }
                    mtlBuilder = MaterialDefinition.builder();
                    currentMaterial = lineContents[0];
                    break;
                case OBJModelConfig.Material.DIFFUSE:
                    if (mtlBuilder == null) {
                        throw new MalformedMaterialLibraryException("Material attribute cannot occur before defining new material " + "definition! Source -> Line: " + (i + 1) + ", Content: " + Arrays.toString(combinedLineContents));
                    }
                    @Nullable final ResourceLocation parentLocation = getParent(this.source);
                    String parentPath = null;
                    if (parentLocation != null) {
                        // Making some assumptions here...carry on now
                        final int lastSlashIndex = parentLocation.getPath().lastIndexOf("/");
                        if (lastSlashIndex != -1) {
                            parentPath = parentLocation.getPath().substring(lastSlashIndex + 1, parentLocation.getPath().length());
                        }
                    }
                    mtlBuilder.diffuseTexture(ResourceLocations.buildResourceLocationPath(lineContents[0], parentPath));
                    break;
                default:
            }
        }
        if (mtlBuilder != null) {
            mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
        }
    }
    return mtllibBuilder.build(source, getFileName(source).split("\\.")[0]);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MaterialDefinition(com.almuradev.content.model.obj.material.MaterialDefinition) MaterialLibrary(com.almuradev.content.model.obj.material.MaterialLibrary) ResourceLocation(net.minecraft.util.ResourceLocation) BufferedReader(java.io.BufferedReader) MalformedMaterialLibraryException(com.almuradev.content.model.obj.material.MalformedMaterialLibraryException) IResource(net.minecraft.client.resources.IResource) Nullable(javax.annotation.Nullable)

Example 15 with IResource

use of net.minecraft.client.resources.IResource in project Almura by AlmuraDev.

the class OBJModelLoader method loadModel.

@Nullable
@Override
public IModel loadModel(final ResourceLocation modelLocation) throws Exception {
    // Suppress the first model loading exception (like Forge does in their loader)
    final Exception exception = this.errors.get(modelLocation);
    if (exception != null) {
        throw new ModelLoaderRegistry.LoaderException("Failed to load model [" + modelLocation + "]!", exception);
    }
    IModel cached = this.cache.get(modelLocation);
    if (cached == null) {
        final IResource model = this.resourceManager.getResource(modelLocation);
        final OBJModelParser parser = this.parserFactory.create(this.resourceManager, modelLocation, model);
        try {
            cached = parser.parse();
        } catch (final Exception ex) {
            this.errors.put(modelLocation, ex);
        } finally {
            this.cache.put(modelLocation, cached);
        }
    }
    return cached;
}
Also used : IModel(net.minecraftforge.client.model.IModel) IResource(net.minecraft.client.resources.IResource) Nullable(javax.annotation.Nullable)

Aggregations

IResource (net.minecraft.client.resources.IResource)38 IOException (java.io.IOException)23 ResourceLocation (net.minecraft.util.ResourceLocation)21 InputStreamReader (java.io.InputStreamReader)12 BufferedImage (java.awt.image.BufferedImage)9 IResourceManager (net.minecraft.client.resources.IResourceManager)8 BufferedReader (java.io.BufferedReader)6 JsonObject (com.google.gson.JsonObject)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 Nullable (javax.annotation.Nullable)3 PngSizeInfo (net.minecraft.client.renderer.texture.PngSizeInfo)3 TextureAtlasSprite (net.minecraft.client.renderer.texture.TextureAtlasSprite)3 IModel (net.minecraftforge.client.model.IModel)3 JsonElement (com.google.gson.JsonElement)2 RasterFormatException (java.awt.image.RasterFormatException)2 Minecraft (net.minecraft.client.Minecraft)2 CrashReport (net.minecraft.crash.CrashReport)2 CrashReportCategory (net.minecraft.crash.CrashReportCategory)2 ReportedException (net.minecraft.util.ReportedException)2