make leaf texture generator event based and more modular

This commit is contained in:
octarine-noise
2014-08-10 17:02:15 +02:00
parent 6b57840b8f
commit 51589bbad4
5 changed files with 143 additions and 92 deletions

View File

@@ -63,11 +63,11 @@ public class BetterFoliageClient {
grass.load(new File(BetterFoliage.configDir, "classesGrass.cfg"), new ResourceLocation("betterfoliage:classesGrassDefault.cfg")); grass.load(new File(BetterFoliage.configDir, "classesGrass.cfg"), new ResourceLocation("betterfoliage:classesGrassDefault.cfg"));
MinecraftForge.EVENT_BUS.register(grass); MinecraftForge.EVENT_BUS.register(grass);
MinecraftForge.EVENT_BUS.register(new LeafTextureEnumerator("bf_leaves"));
BetterFoliage.log.info("Registering texture generators"); BetterFoliage.log.info("Registering texture generators");
leafGenerator = new LeafGenerator("bf_leaves", missingTexture); leafGenerator = new LeafGenerator();
MinecraftForge.EVENT_BUS.register(leafGenerator); MinecraftForge.EVENT_BUS.register(leafGenerator);
MinecraftForge.EVENT_BUS.register(new LeafTextureEnumerator());
MinecraftForge.EVENT_BUS.register(new ReedGenerator("bf_reed_bottom", missingTexture, true)); MinecraftForge.EVENT_BUS.register(new ReedGenerator("bf_reed_bottom", missingTexture, true));
MinecraftForge.EVENT_BUS.register(new ReedGenerator("bf_reed_top", missingTexture, false)); MinecraftForge.EVENT_BUS.register(new ReedGenerator("bf_reed_top", missingTexture, false));
MinecraftForge.EVENT_BUS.register(new ShortGrassGenerator("bf_shortgrass", missingTexture, false)); MinecraftForge.EVENT_BUS.register(new ShortGrassGenerator("bf_shortgrass", missingTexture, false));

View File

@@ -6,55 +6,28 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import mods.betterfoliage.BetterFoliage; import mods.betterfoliage.BetterFoliage;
import mods.betterfoliage.client.BetterFoliageClient;
import mods.betterfoliage.client.ShadersModIntegration; import mods.betterfoliage.client.ShadersModIntegration;
import mods.betterfoliage.common.util.Utils;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.TextureStitchEvent.Post; import net.minecraftforge.client.event.TextureStitchEvent.Post;
import net.minecraftforge.client.event.TextureStitchEvent.Pre; import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class LeafGenerator extends BlockTextureGenerator { public class LeafGenerator extends LeafGeneratorBase {
/** Resource domain name of pre-drawn textures */
public String nonGeneratedDomain = "betterfoliage";
/** Number of textures generated in the current run */
public int generatedCounter = 0;
/** Number of pre-drawn textures found in the current run */
public int drawnCounter = 0;
/** Name of the default alpha mask to use */ /** Name of the default alpha mask to use */
public static String defaultMask = "rough"; public static String defaultMask = "default";
public LeafGenerator(String domainName, ResourceLocation missingResource) { public LeafGenerator() {
super(domainName, missingResource); super("bf_leaves", "betterfoliage", "textures/blocks/%s/%s", "betterfoliage:textures/blocks/leafmask_%d_%s.png", BetterFoliageClient.missingTexture);
} }
@Override @Override
public IResource getResource(ResourceLocation resourceLocation) throws IOException { protected BufferedImage generateLeaf(ResourceLocation originalWithDirs) throws IOException, TextureGenerationException {
IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
ResourceLocation originalNoDirs = unwrapResource(resourceLocation);
ResourceLocation originalWithDirs = new ResourceLocation(originalNoDirs.getResourceDomain(), "textures/blocks/" + originalNoDirs.getResourcePath());
// check for provided texture
ResourceLocation handDrawnLocation = new ResourceLocation(nonGeneratedDomain, String.format("textures/blocks/%s/%s", originalNoDirs.getResourceDomain(), originalNoDirs.getResourcePath()));
if (Utils.resourceExists(handDrawnLocation)) {
drawnCounter++;
return resourceManager.getResource(handDrawnLocation);
}
// generate our own
if (!Utils.resourceExists(originalWithDirs)) return getMissingResource();
// load normal leaf texture // load normal leaf texture
BufferedImage origImage = ImageIO.read(resourceManager.getResource(originalWithDirs).getInputStream()); BufferedImage origImage = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(originalWithDirs).getInputStream());
if (origImage.getWidth() != origImage.getHeight()) return getMissingResource(); if (origImage.getWidth() != origImage.getHeight()) throw new TextureGenerationException();
int size = origImage.getWidth(); int size = origImage.getWidth();
// tile leaf texture 2x2 // tile leaf texture 2x2
@@ -80,41 +53,7 @@ public class LeafGenerator extends BlockTextureGenerator {
} }
} }
generatedCounter++; return overlayIcon;
return new BufferedImageResource(overlayIcon);
}
/** Loads the alpha mask of the given type and size. If a mask of the exact size can not be found,
* will try to load progressively smaller masks down to 16x16
* @param type mask type
* @param size texture size
* @return alpha mask
*/
protected BufferedImage loadLeafMaskImage(String type, int size) {
IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
IResource maskResource = null;
while (maskResource == null && size >= 16) {
try {
maskResource = resourceManager.getResource(new ResourceLocation(String.format("betterfoliage:textures/blocks/leafmask_%d_%s.png", size, type)));
} catch (Exception e) {}
size /= 2;
}
try {
return maskResource == null ? null : ImageIO.read(maskResource.getInputStream());
} catch (IOException e) {
return null;
}
}
@Override
@SubscribeEvent
public void handleTextureReload(Pre event) {
super.handleTextureReload(event);
if (event.map.getTextureType() != 0) return;
generatedCounter = 0;
drawnCounter = 0;
} }
@Override @Override
@@ -123,8 +62,6 @@ public class LeafGenerator extends BlockTextureGenerator {
super.endTextureReload(event); super.endTextureReload(event);
if (event.map.getTextureType() != 0) return; if (event.map.getTextureType() != 0) return;
BetterFoliage.log.info(String.format("Found %d pre-drawn leaf textures", drawnCounter)); BetterFoliage.log.info(String.format("Found %d pre-drawn leaf textures", drawnCounter));
BetterFoliage.log.info(String.format("Found %d leaf textures", generatedCounter)); BetterFoliage.log.info(String.format("Generated %d leaf textures", generatedCounter));
} }
} }

View File

@@ -0,0 +1,111 @@
package mods.betterfoliage.client.resource;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import mods.betterfoliage.client.resource.LeafTextureEnumerator.LeafTextureFoundEvent;
import mods.betterfoliage.common.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.TextureStitchEvent.Pre;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public abstract class LeafGeneratorBase extends BlockTextureGenerator {
public static class TextureGenerationException extends Exception {
private static final long serialVersionUID = 7339757761980002651L;
};
/** Format string of pre-drawn texture location */
public String handDrawnLocationFormat;
/** Format string of alpha mask location */
public String maskImageLocationFormat;
/** Resource domain name of pre-drawn textures */
public String nonGeneratedDomain;
/** Number of textures generated in the current run */
public int generatedCounter = 0;
/** Number of pre-drawn textures found in the current run */
public int drawnCounter = 0;
public LeafGeneratorBase(String domain, String nonGeneratedDomain, String handDrawnLocationFormat, String maskImageLocationFormat, ResourceLocation missingResource) {
super(domain, missingResource);
this.nonGeneratedDomain = nonGeneratedDomain;
this.handDrawnLocationFormat = handDrawnLocationFormat;
this.maskImageLocationFormat = maskImageLocationFormat;
}
@Override
public IResource getResource(ResourceLocation resourceLocation) throws IOException {
IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
ResourceLocation originalNoDirs = unwrapResource(resourceLocation);
ResourceLocation originalWithDirs = new ResourceLocation(originalNoDirs.getResourceDomain(), "textures/blocks/" + originalNoDirs.getResourcePath());
// check for provided texture
ResourceLocation handDrawnLocation = new ResourceLocation(nonGeneratedDomain, String.format(handDrawnLocationFormat, originalNoDirs.getResourceDomain(), originalNoDirs.getResourcePath()));
if (Utils.resourceExists(handDrawnLocation)) {
drawnCounter++;
return resourceManager.getResource(handDrawnLocation);
}
// generate our own
if (!Utils.resourceExists(originalWithDirs)) return getMissingResource();
BufferedImage result;
try {
result = generateLeaf(originalWithDirs);
} catch (TextureGenerationException e) {
return getMissingResource();
}
generatedCounter++;
return new BufferedImageResource(result);
}
protected abstract BufferedImage generateLeaf(ResourceLocation originalWithDirs) throws IOException, TextureGenerationException;
/** Loads the alpha mask of the given type and size. If a mask of the exact size can not be found,
* will try to load progressively smaller masks down to 16x16
* @param type mask type
* @param size texture size
* @return alpha mask
*/
protected BufferedImage loadLeafMaskImage(String type, int size) {
IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
IResource maskResource = null;
while (maskResource == null && size >= 1) {
try {
maskResource = resourceManager.getResource(new ResourceLocation(String.format(maskImageLocationFormat, size, type)));
} catch (Exception e) {}
size /= 2;
}
try {
return maskResource == null ? null : ImageIO.read(maskResource.getInputStream());
} catch (IOException e) {
return null;
}
}
@Override
@SubscribeEvent
public void handleTextureReload(Pre event) {
super.handleTextureReload(event);
if (event.map.getTextureType() != 0) return;
generatedCounter = 0;
drawnCounter = 0;
}
@SubscribeEvent
public void handleRegisterTexture(LeafTextureFoundEvent event) {
event.blockTextures.registerIcon(new ResourceLocation(domainName, event.icon.getIconName()).toString());
}
}

View File

@@ -13,32 +13,36 @@ import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.util.IIcon; import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.common.MinecraftForge;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class LeafTextureEnumerator implements IIconRegister { public class LeafTextureEnumerator implements IIconRegister {
/** Resource domain name of generated textures */ public static class LeafTextureFoundEvent extends Event {
public String domainName; public TextureMap blockTextures;
public TextureAtlasSprite icon;
private LeafTextureFoundEvent(TextureMap blockTextures, TextureAtlasSprite icon) {
super();
this.blockTextures = blockTextures;
this.icon = icon;
}
}
/** Texture atlas for block textures used in the current run */ /** Texture atlas for block textures used in the current run */
public TextureMap blockTextures; public TextureMap blockTextures;
public LeafTextureEnumerator(String domainName) {
this.domainName = domainName;
}
/** Leaf blocks register their textures here. An extra texture will be registered in the atlas /** Leaf blocks register their textures here. An extra texture will be registered in the atlas
* for each, with the resource domain of this generator. * for each, with the resource domain of this generator.
* @return the originally registered {@link IIcon} already in the atlas * @return the originally registered {@link IIcon} already in the atlas
*/ */
public IIcon registerIcon(String resourceLocation) { public IIcon registerIcon(String resourceLocation) {
IIcon original = blockTextures.getTextureExtry(resourceLocation); TextureAtlasSprite original = blockTextures.getTextureExtry(resourceLocation);
blockTextures.registerIcon(new ResourceLocation(domainName, resourceLocation).toString()); MinecraftForge.EVENT_BUS.post(new LeafTextureFoundEvent(blockTextures, original));
BetterFoliage.log.debug(String.format("Found leaf texture: %s", resourceLocation)); BetterFoliage.log.debug(String.format("Found leaf texture: %s", resourceLocation));
return original; return original;
} }
@@ -72,13 +76,12 @@ public class LeafTextureEnumerator implements IIconRegister {
if (mapAtlas == null) { if (mapAtlas == null) {
BetterFoliage.log.warn("Failed to reflect texture atlas, textures may be missing"); BetterFoliage.log.warn("Failed to reflect texture atlas, textures may be missing");
} else { } else {
Set<String> foundLeafTextures = Sets.newHashSet(); Set<TextureAtlasSprite> foundLeafTextures = Sets.newHashSet();
for (TextureAtlasSprite icon : mapAtlas.values()) for (TextureAtlasSprite icon : mapAtlas.values())
if (BetterFoliageClient.isLeafTexture(icon)) if (BetterFoliageClient.isLeafTexture(icon)) foundLeafTextures.add(icon);
foundLeafTextures.add(icon.getIconName()); for (TextureAtlasSprite icon : foundLeafTextures) {
for (String resourceLocation : foundLeafTextures) { BetterFoliage.log.debug(String.format("Found non-block-registered leaf texture: %s", icon.getIconName()));
BetterFoliage.log.debug(String.format("Found non-block-registered leaf texture: %s", resourceLocation)); MinecraftForge.EVENT_BUS.post(new LeafTextureFoundEvent(blockTextures, icon));
blockTextures.registerIcon(new ResourceLocation(domainName, resourceLocation).toString());
} }
} }
} }