mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-03 16:35:50 +00:00
Rewrite the image biome provider to use the image library
This commit is contained in:
parent
3580267532
commit
03ab463723
@ -2,6 +2,7 @@ version = version("1.0.0")
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnlyApi(project(":common:addons:manifest-addon-loader"))
|
compileOnlyApi(project(":common:addons:manifest-addon-loader"))
|
||||||
|
compileOnlyApi(project(":common:addons:library-image"))
|
||||||
implementation("net.jafama", "jafama", Versions.Libraries.Internal.jafama)
|
implementation("net.jafama", "jafama", Versions.Libraries.Internal.jafama)
|
||||||
testImplementation("net.jafama", "jafama", Versions.Libraries.Internal.jafama)
|
testImplementation("net.jafama", "jafama", Versions.Libraries.Internal.jafama)
|
||||||
}
|
}
|
||||||
|
@ -7,34 +7,28 @@
|
|||||||
|
|
||||||
package com.dfsek.terra.addons.biome.image;
|
package com.dfsek.terra.addons.biome.image;
|
||||||
|
|
||||||
import net.jafama.FastMath;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.image.converter.ColorConverter;
|
||||||
|
import com.dfsek.terra.addons.image.picker.ColorPicker;
|
||||||
import com.dfsek.terra.api.world.biome.Biome;
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||||
|
|
||||||
|
|
||||||
public class ImageBiomeProvider implements BiomeProvider {
|
public class ImageBiomeProvider implements BiomeProvider {
|
||||||
private final Map<Color, Biome> colorBiomeMap = new HashMap<>();
|
|
||||||
private final BufferedImage image;
|
private final BufferedImage image;
|
||||||
private final int resolution;
|
private final int resolution;
|
||||||
private final Align align;
|
|
||||||
|
|
||||||
public ImageBiomeProvider(Set<Biome> registry, BufferedImage image, int resolution, Align align) {
|
private final ColorConverter<Biome> colorConverter;
|
||||||
|
|
||||||
|
private final ColorPicker colorPicker;
|
||||||
|
|
||||||
|
public ImageBiomeProvider(BufferedImage image, ColorConverter<Biome> colorConverter, ColorPicker colorPicker, int resolution) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
this.resolution = resolution;
|
this.resolution = resolution;
|
||||||
this.align = align;
|
this.colorConverter = colorConverter;
|
||||||
registry.forEach(biome -> colorBiomeMap.put(new Color(biome.getColor()), biome));
|
this.colorPicker = colorPicker;
|
||||||
}
|
|
||||||
|
|
||||||
private static int distance(Color a, Color b) {
|
|
||||||
return FastMath.abs(a.getRed() - b.getRed()) + FastMath.abs(a.getGreen() - b.getGreen()) + FastMath.abs(a.getBlue() - b.getBlue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -45,15 +39,7 @@ public class ImageBiomeProvider implements BiomeProvider {
|
|||||||
public Biome getBiome(int x, int z) {
|
public Biome getBiome(int x, int z) {
|
||||||
x /= resolution;
|
x /= resolution;
|
||||||
z /= resolution;
|
z /= resolution;
|
||||||
Color color = align.getColor(image, x, z);
|
return colorConverter.apply(colorPicker.apply(image, x, z));
|
||||||
return colorBiomeMap.get(colorBiomeMap.keySet()
|
|
||||||
.stream()
|
|
||||||
.reduce(colorBiomeMap.keySet().stream().findAny().orElseThrow(IllegalStateException::new),
|
|
||||||
(running, element) -> {
|
|
||||||
int d1 = distance(color, running);
|
|
||||||
int d2 = distance(color, element);
|
|
||||||
return d1 < d2 ? running : element;
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -63,24 +49,6 @@ public class ImageBiomeProvider implements BiomeProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Biome> getBiomes() {
|
public Iterable<Biome> getBiomes() {
|
||||||
return colorBiomeMap.values();
|
return colorConverter.getEntries();
|
||||||
}
|
|
||||||
|
|
||||||
public enum Align {
|
|
||||||
CENTER {
|
|
||||||
@Override
|
|
||||||
public Color getColor(BufferedImage image, int x, int z) {
|
|
||||||
return new Color(image.getRGB(FastMath.floorMod(x - image.getWidth() / 2, image.getWidth()),
|
|
||||||
FastMath.floorMod(z - image.getHeight() / 2, image.getHeight())));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
NONE {
|
|
||||||
@Override
|
|
||||||
public Color getColor(BufferedImage image, int x, int z) {
|
|
||||||
return new Color(image.getRGB(FastMath.floorMod(x, image.getWidth()), FastMath.floorMod(z, image.getHeight())));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public abstract Color getColor(BufferedImage image, int x, int z);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,13 @@ import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
|||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.biome.image.config.ImageProviderTemplate;
|
||||||
|
import com.dfsek.terra.addons.biome.image.config.converter.ClosestBiomeColorConverterTemplate;
|
||||||
|
import com.dfsek.terra.addons.biome.image.config.converter.ExactBiomeColorConverterTemplate;
|
||||||
|
import com.dfsek.terra.addons.biome.image.config.converter.mapping.DefinedBiomeColorMappingTemplate;
|
||||||
|
import com.dfsek.terra.addons.image.converter.ColorConverter;
|
||||||
|
import com.dfsek.terra.addons.image.converter.mapping.BiomeDefinedColorMapping;
|
||||||
|
import com.dfsek.terra.addons.image.converter.mapping.ColorMapping;
|
||||||
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
|
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
|
||||||
import com.dfsek.terra.api.Platform;
|
import com.dfsek.terra.api.Platform;
|
||||||
import com.dfsek.terra.api.addon.BaseAddon;
|
import com.dfsek.terra.api.addon.BaseAddon;
|
||||||
@ -27,6 +34,12 @@ public class ImageBiomeProviderAddon implements AddonInitializer {
|
|||||||
public static final TypeKey<Supplier<ObjectTemplate<BiomeProvider>>> PROVIDER_REGISTRY_KEY = new TypeKey<>() {
|
public static final TypeKey<Supplier<ObjectTemplate<BiomeProvider>>> PROVIDER_REGISTRY_KEY = new TypeKey<>() {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static final TypeKey<Supplier<ObjectTemplate<ColorConverter<Biome>>>> BIOME_COLOR_CONVERTER_REGISTRY_KEY = new TypeKey<>() {
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final TypeKey<Supplier<ObjectTemplate<ColorMapping<Biome>>>> BIOME_COLOR_MAPPING_REGISTRY_KEY = new TypeKey<>() {
|
||||||
|
};
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private Platform platform;
|
private Platform platform;
|
||||||
|
|
||||||
@ -38,11 +51,23 @@ public class ImageBiomeProviderAddon implements AddonInitializer {
|
|||||||
platform.getEventManager()
|
platform.getEventManager()
|
||||||
.getHandler(FunctionalEventHandler.class)
|
.getHandler(FunctionalEventHandler.class)
|
||||||
.register(addon, ConfigPackPreLoadEvent.class)
|
.register(addon, ConfigPackPreLoadEvent.class)
|
||||||
|
.priority(501)
|
||||||
.then(event -> {
|
.then(event -> {
|
||||||
CheckedRegistry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry = event.getPack().getOrCreateRegistry(
|
CheckedRegistry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry = event.getPack().getOrCreateRegistry(
|
||||||
PROVIDER_REGISTRY_KEY);
|
PROVIDER_REGISTRY_KEY);
|
||||||
providerRegistry.register(addon.key("IMAGE"),
|
providerRegistry.register(addon.key("IMAGE"), ImageProviderTemplate::new);
|
||||||
() -> new ImageProviderTemplate(event.getPack().getRegistry(Biome.class)));
|
})
|
||||||
|
.then(event -> {
|
||||||
|
CheckedRegistry<Supplier<ObjectTemplate<ColorConverter<Biome>>>> biomeColorConverterRegistry = event.getPack().getOrCreateRegistry(
|
||||||
|
BIOME_COLOR_CONVERTER_REGISTRY_KEY);
|
||||||
|
biomeColorConverterRegistry.register(addon.key("EXACT"), ExactBiomeColorConverterTemplate::new);
|
||||||
|
biomeColorConverterRegistry.register(addon.key("CLOSEST"), ClosestBiomeColorConverterTemplate::new);
|
||||||
|
})
|
||||||
|
.then(event -> {
|
||||||
|
CheckedRegistry<Supplier<ObjectTemplate<ColorMapping<Biome>>>> biomeColorMappingRegistry = event.getPack().getOrCreateRegistry(
|
||||||
|
BIOME_COLOR_MAPPING_REGISTRY_KEY);
|
||||||
|
biomeColorMappingRegistry.register(addon.key("USE_BIOME_COLORS"), () -> () -> new BiomeDefinedColorMapping<>(event.getPack().getRegistry(Biome.class), b -> b));
|
||||||
|
biomeColorMappingRegistry.register(addon.key("MAP"), DefinedBiomeColorMappingTemplate::new);
|
||||||
})
|
})
|
||||||
.failThrough();
|
.failThrough();
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
* reference the LICENSE file in this module's root directory.
|
* reference the LICENSE file in this module's root directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.dfsek.terra.addons.biome.image;
|
package com.dfsek.terra.addons.biome.image.config;
|
||||||
|
|
||||||
import com.dfsek.tectonic.api.config.template.annotations.Default;
|
import com.dfsek.tectonic.api.config.template.annotations.Default;
|
||||||
import com.dfsek.tectonic.api.config.template.annotations.Description;
|
import com.dfsek.tectonic.api.config.template.annotations.Description;
|
||||||
@ -13,33 +13,33 @@ import com.dfsek.tectonic.api.config.template.annotations.Value;
|
|||||||
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
import com.dfsek.terra.api.registry.Registry;
|
import com.dfsek.terra.addons.biome.image.ImageBiomeProvider;
|
||||||
|
import com.dfsek.terra.addons.image.converter.ColorConverter;
|
||||||
|
import com.dfsek.terra.addons.image.picker.ColorPicker;
|
||||||
import com.dfsek.terra.api.world.biome.Biome;
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("FieldMayBeFinal")
|
@SuppressWarnings("FieldMayBeFinal")
|
||||||
public class ImageProviderTemplate implements ObjectTemplate<BiomeProvider> {
|
public class ImageProviderTemplate implements ObjectTemplate<BiomeProvider> {
|
||||||
private final Registry<Biome> biomes;
|
|
||||||
@Value("resolution")
|
@Value("resolution")
|
||||||
@Default
|
@Default
|
||||||
@Description("Sets the resolution at which to sample the image.")
|
@Description("Sets the resolution at which to sample the image.")
|
||||||
private int resolution = 1;
|
private int resolution = 1;
|
||||||
@Value("image.name")
|
|
||||||
@Description("Sets the location of the image on the filesystem, relative to the pack root.")
|
|
||||||
private BufferedImage image;
|
|
||||||
@Value("image.align")
|
|
||||||
@Description("Sets the alignment style to use for the image.")
|
|
||||||
private ImageBiomeProvider.Align align;
|
|
||||||
|
|
||||||
public ImageProviderTemplate(Registry<Biome> set) {
|
@Value("image")
|
||||||
this.biomes = set;
|
private BufferedImage image;
|
||||||
}
|
|
||||||
|
@Value("mode")
|
||||||
|
private ColorPicker colorPicker;
|
||||||
|
|
||||||
|
@Value("biomes")
|
||||||
|
private ColorConverter<Biome> colorConverter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BiomeProvider get() {
|
public BiomeProvider get() {
|
||||||
return new ImageBiomeProvider(new HashSet<>(biomes.entries()), image, resolution, align);
|
return new ImageBiomeProvider(image, colorConverter, colorPicker, resolution);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.image.config.converter;
|
||||||
|
|
||||||
|
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.image.config.converter.ClosestColorConverterTemplate;
|
||||||
|
import com.dfsek.terra.addons.image.converter.mapping.ColorMapping;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
|
||||||
|
public class ClosestBiomeColorConverterTemplate extends ClosestColorConverterTemplate<Biome> {
|
||||||
|
|
||||||
|
@Value("match")
|
||||||
|
private ColorMapping<Biome> match;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ColorMapping<Biome> getMapping() {
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.image.config.converter;
|
||||||
|
|
||||||
|
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.image.config.converter.ExactColorConverterTemplate;
|
||||||
|
import com.dfsek.terra.addons.image.converter.mapping.ColorMapping;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
|
||||||
|
public class ExactBiomeColorConverterTemplate extends ExactColorConverterTemplate<Biome> {
|
||||||
|
|
||||||
|
@Value("match")
|
||||||
|
private ColorMapping<Biome> match;
|
||||||
|
|
||||||
|
@Value("fallback")
|
||||||
|
private Biome fallback;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ColorMapping<Biome> getMapping() {
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Biome getFallback() {
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.image.config.converter.mapping;
|
||||||
|
|
||||||
|
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||||
|
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.image.converter.mapping.ColorMapping;
|
||||||
|
import com.dfsek.terra.addons.image.util.MapUtil;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
|
||||||
|
public class DefinedBiomeColorMappingTemplate implements ObjectTemplate<ColorMapping<Biome>> {
|
||||||
|
|
||||||
|
@Value("map")
|
||||||
|
Map<String, Biome> map;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ColorMapping<Biome> get() {
|
||||||
|
return () -> MapUtil.convertKeysToInt(map);
|
||||||
|
}
|
||||||
|
}
|
@ -9,4 +9,6 @@ website:
|
|||||||
issues: https://github.com/PolyhedralDev/Terra/issues
|
issues: https://github.com/PolyhedralDev/Terra/issues
|
||||||
source: https://github.com/PolyhedralDev/Terra
|
source: https://github.com/PolyhedralDev/Terra
|
||||||
docs: https://terra.polydev.org
|
docs: https://terra.polydev.org
|
||||||
license: MIT License
|
license: MIT License
|
||||||
|
depends:
|
||||||
|
library-image: "1.+"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user