add multiple leaf particle types

This commit is contained in:
octarine-noise
2014-08-26 21:22:33 +02:00
parent 5a5daf6750
commit 5ad3bbe342
3 changed files with 109 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
package mods.betterfoliage.client;
import java.util.List;
import java.util.Map;
import mods.betterfoliage.common.util.ResourceUtils;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class TextureMatcher {
public static class TextureMapping {
public String matchDomain;
public String matchName;
public String textureType;
public TextureMapping(String matchDomain, String matchName, String textureType) {
this.matchDomain = matchDomain;
this.matchName = matchName;
this.textureType = textureType;
}
public boolean matches(TextureAtlasSprite icon) {
ResourceLocation iconLocation = new ResourceLocation(icon.getIconName());
if (matchDomain != null && !matchDomain.equals(iconLocation.getResourceDomain())) return false;
return iconLocation.getResourcePath().contains(matchName);
}
}
public List<TextureMapping> mappings = Lists.newLinkedList();
public Map<IIcon, String> types = Maps.newHashMap();
public String put(TextureAtlasSprite icon) {
if (types.keySet().contains(icon)) return types.get(icon);
for (TextureMapping mapping : mappings) if (mapping.matches(icon)) {
types.put(icon, mapping.textureType);
return mapping.textureType;
}
return null;
}
public String get(IIcon icon) {
return types.get(icon);
}
public void loadMappings(ResourceLocation resource) {
mappings = Lists.newLinkedList();
for (String line : ResourceUtils.getLines(resource)) {
String[] lineSplit = line.split("=");
if (lineSplit.length != 2) continue;
String[] match = lineSplit[0].split(":");
if (match.length == 2) {
mappings.add(new TextureMapping(match[0], match[1], lineSplit[1]));
} else if (match.length == 1) {
mappings.add(new TextureMapping(null, match[0], lineSplit[1]));
}
}
}
}

View File

@@ -40,12 +40,12 @@ public class EntityFXFallingLeaves extends EntityFX {
isMirrored = (rand.nextInt() & 1) == 1;
motionY = -Config.leafFXSpeed;
particleRotation = rand.nextInt(64);
particleScale = (float) Config.leafFXSize;
particleIcon = BetterFoliageClient.leafParticles.icons.get(rand.nextInt(1024));
Block block = world.getBlock(x, y, z);
IIcon blockIcon = block.getIcon(world, x, y, z, ForgeDirection.DOWN.ordinal());
particleIcon = BetterFoliageClient.leafParticles.getIconSet(blockIcon).get(rand.nextInt(1024));
calculateParticleColor(BetterFoliageClient.leafParticles.getColor(blockIcon), block.colorMultiplier(world, x, y, z));
}

View File

@@ -7,6 +7,7 @@ import java.util.Map;
import javax.imageio.ImageIO;
import mods.betterfoliage.client.TextureMatcher;
import mods.betterfoliage.client.render.IconSet;
import mods.betterfoliage.client.resource.LeafTextureEnumerator.LeafTextureFoundEvent;
import net.minecraft.client.Minecraft;
@@ -21,17 +22,20 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/** Holds the texture for the falling leaf particles, and stores average texture color values for leaf textures
/** Holds the textures for the falling leaf particles, and stores average texture color values for leaf textures
* @author octarine-noise
*/
@SideOnly(Side.CLIENT)
public class LeafParticleTextures {
/** Icons for leaf particles */
public IconSet icons = new IconSet("betterfoliage", "falling_leaf_default_%d");
/** Icons for leaf particles */
public Map<String, IconSet> iconSets = Maps.newHashMap();
/** Leaf type mappings */
public TextureMatcher leafTypes = new TextureMatcher();
/** Map of average color values */
public Map<IIcon, Integer> colors = Maps.newHashMap();
public Map<IIcon, Integer> iconColors = Maps.newHashMap();
/** Default color value */
public int defaultColor = 0x208040;
@@ -40,15 +44,32 @@ public class LeafParticleTextures {
this.defaultColor = defaultColor;
}
public IconSet getIconSet(IIcon icon) {
String leafType = leafTypes.get(icon);
if (leafType == null) leafType = "default";
IconSet result = iconSets.get(leafType);
return result.hasIcons() ? result : iconSets.get("default");
}
public int getColor(IIcon icon) {
Integer result = colors.get(icon);
Integer result = iconColors.get(icon);
return result == null ? defaultColor : result;
}
/** Calculate average color value (in HSB color space) for a texture and store it in the map.
* @param icon texture
*/
protected void addAtlasTexture(TextureAtlasSprite icon) {
Integer textureColor = calculateTextureColor(icon);
if (textureColor != null) iconColors.put(icon, textureColor);
String leafType = leafTypes.put(icon);
if (leafType != null && !iconSets.keySet().contains(leafType)) {
iconSets.put(leafType, new IconSet("betterfoliage", String.format("falling_leaf_%s_%%d", leafType)));
}
}
/** Calculate average color value (in HSB color space) for a texture.
* @param icon texture
*/
protected Integer calculateTextureColor(TextureAtlasSprite icon) {
ResourceLocation locationNoDirs = new ResourceLocation(icon.getIconName());
ResourceLocation locationWithDirs = new ResourceLocation(locationNoDirs.getResourceDomain(), String.format("textures/blocks/%s.png", locationNoDirs.getResourcePath()));
try {
@@ -74,20 +95,27 @@ public class LeafParticleTextures {
// average hue as usual for circular values - transform average unit vector back to polar angle
float avgHue = (float) (Math.atan2(sumHueY, sumHueX) / (2.0 * Math.PI) + 0.5);
colors.put(icon, Color.HSBtoRGB(avgHue, sumSaturation / numOpaque, sumBrightness / numOpaque));
return Color.HSBtoRGB(avgHue, sumSaturation / numOpaque, sumBrightness / numOpaque);
} catch (IOException e) {
return null;
}
}
@SubscribeEvent
public void handleTextureReload(TextureStitchEvent.Pre event) {
if (event.map.getTextureType() != 0) return;
colors.clear();
icons.registerIcons(event.map);
iconSets.clear();
iconColors.clear();
leafTypes.loadMappings(new ResourceLocation("betterfoliage", "leafTextureMappings.cfg"));
IconSet defaultIcons = new IconSet("betterfoliage", "falling_leaf_default_%d");
iconSets.put("default", defaultIcons);
defaultIcons.registerIcons(event.map);
}
@SubscribeEvent
public void handleRegisterTexture(LeafTextureFoundEvent event) {
addAtlasTexture(event.icon);
}
}