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);
}
}
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);
}
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;
}
}
}
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]);
}
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;
}
Aggregations