Skip to content

Commit

Permalink
support for animated textures
Browse files Browse the repository at this point in the history
  • Loading branch information
octarine-noise committed Jan 21, 2015
1 parent 57a6cdc commit 632d276
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
Expand Up @@ -8,22 +8,28 @@

import javax.imageio.ImageIO;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.data.IMetadataSection;
import net.minecraft.util.ResourceLocation;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

/** {@link IResource} for a {@link BufferedImage}
* @author octarine-noise
*/
@SideOnly(Side.CLIENT)
public class BufferedImageResource implements IResource {

/** Raw PNG data*/
/** Raw PNG data */
protected byte[] data = null;

public BufferedImageResource(BufferedImage image) {
/** Underlying resource for generated resources, get metadata from this */
ResourceLocation metaSource;

public BufferedImageResource(BufferedImage image, ResourceLocation metaSource) {
this.metaSource = metaSource;

// create PNG image
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand All @@ -40,12 +46,20 @@ public InputStream getInputStream() {

@Override
public boolean hasMetadata() {
return false;
try {
return metaSource == null ? false : Minecraft.getMinecraft().getResourceManager().getResource(metaSource).hasMetadata();
} catch (IOException e) {
return false;
}
}

@Override
public IMetadataSection getMetadata(String var1) {
return null;
public IMetadataSection getMetadata(String type) {
try {
return metaSource == null ? null : Minecraft.getMinecraft().getResourceManager().getResource(metaSource).getMetadata(type);
} catch (IOException e) {
return null;
}
}

}
60 changes: 36 additions & 24 deletions src/main/java/mods/betterfoliage/client/resource/LeafGenerator.java
Expand Up @@ -29,32 +29,44 @@ public LeafGenerator() {
@Override
protected BufferedImage generateLeaf(ResourceLocation originalWithDirs) throws IOException, TextureGenerationException {
// load normal leaf texture
BufferedImage origImage = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(originalWithDirs).getInputStream());
if (origImage.getWidth() != origImage.getHeight()) throw new TextureGenerationException();
int size = origImage.getWidth();

// tile leaf texture 2x2
BufferedImage overlayIcon = new BufferedImage(size * 2, size * 2, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D graphics = overlayIcon.createGraphics();
graphics.drawImage(origImage, 0, 0, null);
graphics.drawImage(origImage, 0, size, null);
graphics.drawImage(origImage, size, 0, null);
graphics.drawImage(origImage, size, size, null);
BufferedImage origFullIcon = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(originalWithDirs).getInputStream());
int size = origFullIcon.getWidth();
int frames = origFullIcon.getHeight() / origFullIcon.getWidth();
if (origFullIcon.getHeight() % origFullIcon.getWidth() != 0) throw new TextureGenerationException();

// overlay mask alpha on texture
if (!ShadersModIntegration.isSpecialTexture(originalWithDirs)) {
// load alpha mask of appropriate size
BufferedImage maskImage = loadLeafMaskImage(defaultMask, size * 2);
int scale = size * 2 / maskImage.getWidth();

for (int x = 0; x < overlayIcon.getWidth(); x++) for (int y = 0; y < overlayIcon.getHeight(); y++) {
long origPixel = overlayIcon.getRGB(x, y) & 0xFFFFFFFFl;
long maskPixel = maskImage.getRGB(x / scale, y / scale) & 0xFF000000l | 0x00FFFFFF;
overlayIcon.setRGB(x, y, (int) (origPixel & maskPixel));
}
}
BufferedImage genFullIcon = new BufferedImage(size * 2, size * frames * 2, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D genFullGraphics = genFullIcon.createGraphics();

// iterate all frames
for (int frame = 0; frame < frames; frame++) {
BufferedImage origIcon = origFullIcon.getSubimage(0, size * frame, size, size);

// tile leaf texture 2x2
BufferedImage genIcon = new BufferedImage(size * 2, size * 2, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D genGraphics = genIcon.createGraphics();
genGraphics.drawImage(origIcon, 0, 0, null);
genGraphics.drawImage(origIcon, 0, size, null);
genGraphics.drawImage(origIcon, size, 0, null);
genGraphics.drawImage(origIcon, size, size, null);

// overlay mask alpha on texture
if (!ShadersModIntegration.isSpecialTexture(originalWithDirs)) {
// load alpha mask of appropriate size
BufferedImage maskImage = loadLeafMaskImage(defaultMask, size * 2);
int scale = size * 2 / maskImage.getWidth();

for (int x = 0; x < genIcon.getWidth(); x++) for (int y = 0; y < genIcon.getHeight(); y++) {
long origPixel = genIcon.getRGB(x, y) & 0xFFFFFFFFl;
long maskPixel = maskImage.getRGB(x / scale, y / scale) & 0xFF000000l | 0x00FFFFFF;
genIcon.setRGB(x, y, (int) (origPixel & maskPixel));
}
}

return overlayIcon;
// add to animated png
genFullGraphics.drawImage(genIcon, 0, size * frame * 2, null);
}

return genFullIcon;
}

@Override
Expand Down
Expand Up @@ -56,6 +56,9 @@ public IResource getResource(ResourceLocation resourceLocation) throws IOExcepti
ResourceLocation originalNoDirs = unwrapResource(resourceLocation);
ResourceLocation originalWithDirs = new ResourceLocation(originalNoDirs.getResourceDomain(), "textures/blocks/" + originalNoDirs.getResourcePath());

// use animation metadata as-is
if (resourceLocation.getResourcePath().toLowerCase().endsWith(".mcmeta")) return resourceManager.getResource(originalWithDirs);

// check for provided texture
ResourceLocation handDrawnLocation = new ResourceLocation(nonGeneratedDomain, String.format(handDrawnLocationFormat, originalNoDirs.getResourceDomain(), originalNoDirs.getResourcePath()));
if (ResourceUtils.resourceExists(handDrawnLocation)) {
Expand All @@ -73,7 +76,7 @@ public IResource getResource(ResourceLocation resourceLocation) throws IOExcepti
return getMissingResource();
}
generatedCounter++;
return new BufferedImageResource(result);
return new BufferedImageResource(result, originalWithDirs);
}

protected abstract BufferedImage generateLeaf(ResourceLocation originalWithDirs) throws IOException, TextureGenerationException;
Expand Down
Expand Up @@ -38,7 +38,7 @@ public IResource getResource(ResourceLocation resourceLocation) throws IOExcepti
Graphics2D graphics = result.createGraphics();
graphics.drawImage(origImage, 0, isBottom ? -origImage.getHeight() / 2 : 0, null);

return new BufferedImageResource(result);
return new BufferedImageResource(result, originalWithDirs);
}

}
Expand Up @@ -50,7 +50,7 @@ public IResource getResource(ResourceLocation resourceLocation) throws IOExcepti
}
}

return new BufferedImageResource(result);
return new BufferedImageResource(result, originalWithDirs);
}

}

0 comments on commit 632d276

Please sign in to comment.