From 67ff4268949c9f0254a8b6372c7351372c316cc6 Mon Sep 17 00:00:00 2001 From: dfsek Date: Sun, 29 Nov 2020 00:01:21 -0700 Subject: [PATCH] Calculate derivative for slant palettes --- .../dfsek/terra/config/base/ConfigPack.java | 1 + .../terra/config/templates/BiomeTemplate.java | 26 +++++++----------- .../com/dfsek/terra/generation/Sampler.java | 18 +++++++++++++ .../terra/generation/TerraChunkGenerator.java | 27 ++++++------------- .../java/com/dfsek/terra/math/MathUtil.java | 20 ++++++++++++++ 5 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/dfsek/terra/generation/Sampler.java create mode 100644 src/main/java/com/dfsek/terra/math/MathUtil.java diff --git a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java index 2b574245f..1a42067d8 100644 --- a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java +++ b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java @@ -165,6 +165,7 @@ public class ConfigPack { biomeTemplates.forEach(biome -> { biomeRegistry.add(biome.getID(), biomeFactory.build(biome)); Debug.info("Loaded biome " + biome.getID()); + Debug.info("Threshold: " + biome.getSlantThreshold()); }); List biomeGridTemplates = new ArrayList<>(); diff --git a/src/main/java/com/dfsek/terra/config/templates/BiomeTemplate.java b/src/main/java/com/dfsek/terra/config/templates/BiomeTemplate.java index 53e6474b4..5cd0a43ef 100644 --- a/src/main/java/com/dfsek/terra/config/templates/BiomeTemplate.java +++ b/src/main/java/com/dfsek/terra/config/templates/BiomeTemplate.java @@ -65,14 +65,7 @@ public class BiomeTemplate implements ConfigTemplate { @Abstractable @Default private Palette oceanPalette = new SinglePalette<>(Material.WATER.createBlockData()); - @Value("slant.y-offset.top") - @Abstractable - @Default - private double slantOffsetTop = 0.5D; - @Value("slant.y-offset-bottom") - @Abstractable - @Default - private double slantOffsetBottom = 0.25; + @Value("elevation.equation") @Default @Abstractable @@ -103,6 +96,15 @@ public class BiomeTemplate implements ConfigTemplate { @Default private Map> stairPalettes; + @Value("slant.threshold") + @Abstractable + @Default + private double slantThreshold = 0.1; + + public double getSlantThreshold() { + return slantThreshold; + } + public double getSlabThreshold() { return slabThreshold; } @@ -178,12 +180,4 @@ public class BiomeTemplate implements ConfigTemplate { public Map getOres() { return ores; } - - public double getSlantOffsetTop() { - return slantOffsetTop; - } - - public double getSlantOffsetBottom() { - return slantOffsetBottom; - } } diff --git a/src/main/java/com/dfsek/terra/generation/Sampler.java b/src/main/java/com/dfsek/terra/generation/Sampler.java new file mode 100644 index 000000000..4f5254e02 --- /dev/null +++ b/src/main/java/com/dfsek/terra/generation/Sampler.java @@ -0,0 +1,18 @@ +package com.dfsek.terra.generation; + +import net.jafama.FastMath; +import org.polydev.gaea.math.ChunkInterpolator; + +public class Sampler { + private final ChunkInterpolator interpolator; + private final ElevationInterpolator elevationInterpolator; + + public Sampler(ChunkInterpolator interpolator, ElevationInterpolator elevationInterpolator) { + this.interpolator = interpolator; + this.elevationInterpolator = elevationInterpolator; + } + + public double sample(double x, double y, double z) { + return interpolator.getNoise(x, y - elevationInterpolator.getElevation(FastMath.roundToInt(x), FastMath.roundToInt(z)), z); + } +} diff --git a/src/main/java/com/dfsek/terra/generation/TerraChunkGenerator.java b/src/main/java/com/dfsek/terra/generation/TerraChunkGenerator.java index 602821526..15d0b5673 100644 --- a/src/main/java/com/dfsek/terra/generation/TerraChunkGenerator.java +++ b/src/main/java/com/dfsek/terra/generation/TerraChunkGenerator.java @@ -9,6 +9,7 @@ import com.dfsek.terra.biome.palette.PaletteHolder; import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.templates.BiomeTemplate; +import com.dfsek.terra.math.MathUtil; import com.dfsek.terra.population.CavePopulator; import com.dfsek.terra.population.FloraPopulator; import com.dfsek.terra.population.OrePopulator; @@ -78,22 +79,10 @@ public class TerraChunkGenerator extends GaeaChunkGenerator { popMap.get(c.getWorld()).checkNeighbors(c.getX(), c.getZ(), c.getWorld()); } - private static Palette getPalette(int x, int y, int z, BiomeTemplate c, ChunkInterpolator interpolator, ElevationInterpolator elevationInterpolator) { + private static Palette getPalette(int x, int y, int z, BiomeTemplate c, Sampler sampler) { PaletteHolder slant = c.getSlantPalette(); - if(slant != null) { - double ySlantOffsetTop = c.getSlantOffsetTop(); - double ySlantOffsetBottom = c.getSlantOffsetBottom(); - boolean top = interpolator.getNoise(x, y + ySlantOffsetTop - elevationInterpolator.getElevation(x, z), z) > 0; - boolean bottom = interpolator.getNoise(x, y - ySlantOffsetBottom - elevationInterpolator.getElevation(x, z), z) > 0; - - if(top && bottom) { - boolean north = interpolator.getNoise(x, y - elevationInterpolator.getElevation(x, z + 1), z + 1) > 0; - boolean south = interpolator.getNoise(x, y - elevationInterpolator.getElevation(x, z - 1), z - 1) > 0; - boolean east = interpolator.getNoise(x + 1, y - elevationInterpolator.getElevation(x + 1, z), z) > 0; - boolean west = interpolator.getNoise(x - 1, y - elevationInterpolator.getElevation(x - 1, z), z) > 0; - - if((north || south || east || west) && (!(north && south && east && west))) return slant.getPalette(y); - } + if(slant != null && MathUtil.derivative(sampler, x, y, z) > c.getSlantThreshold()) { + return slant.getPalette(y); } return c.getPalette().getPalette(y); } @@ -157,6 +146,8 @@ public class TerraChunkGenerator extends GaeaChunkGenerator { elevationInterpolator = new ElevationInterpolator(chunkX, chunkZ, tw.getGrid()); } + Sampler sampler = new Sampler(interpolator, elevationInterpolator); + for(byte x = 0; x < 16; x++) { for(byte z = 0; z < 16; z++) { int paletteLevel = 0; @@ -167,13 +158,11 @@ public class TerraChunkGenerator extends GaeaChunkGenerator { Biome b = grid.getBiome(xOrig + x, zOrig + z, GenerationPhase.PALETTE_APPLY); BiomeTemplate c = ((UserDefinedBiome) b).getConfig(); - double elevate = elevationInterpolator.getElevation(x, z); - int sea = c.getSeaLevel(); Palette seaPalette = c.getOceanPalette(); for(int y = world.getMaxHeight() - 1; y >= 0; y--) { - if(interpolator.getNoise(x, y - elevate, z) > 0) { - BlockData data = getPalette(x, y, z, c, interpolator, elevationInterpolator).get(paletteLevel, cx, cz); + if(sampler.sample(x, y, z) > 0) { + BlockData data = getPalette(x, y, z, c, sampler).get(paletteLevel, cx, cz); chunk.setBlock(x, y, z, data); if(paletteLevel == 0 && c.doSlabs() && y < 255) { prepareBlockPart(data, chunk.getBlockData(x, y + 1, z), chunk, new Vector(x, y + 1, z), c.getSlabPalettes(), diff --git a/src/main/java/com/dfsek/terra/math/MathUtil.java b/src/main/java/com/dfsek/terra/math/MathUtil.java new file mode 100644 index 000000000..176fcc9fd --- /dev/null +++ b/src/main/java/com/dfsek/terra/math/MathUtil.java @@ -0,0 +1,20 @@ +package com.dfsek.terra.math; + +import com.dfsek.terra.generation.Sampler; + +public final class MathUtil { + private static final double CONST = 1; + + public static double derivative(Sampler sampler, double x, double y, double z) { + double baseSample = sampler.sample(x, y, z); + + double xVal1 = (sampler.sample(x + CONST, y, z) - baseSample) / CONST; + double xVal2 = (sampler.sample(x - CONST, y, z) - baseSample) / CONST; + double zVal1 = (sampler.sample(x, y, z + CONST) - baseSample) / CONST; + double zVal2 = (sampler.sample(x, y, z - CONST) - baseSample) / CONST; + double yVal1 = (sampler.sample(x, y + CONST, z) - baseSample) / CONST; + double yVal2 = (sampler.sample(x, y - CONST, z) - baseSample) / CONST; + + return Math.sqrt(((xVal2 - xVal1) * (xVal2 - xVal1)) + ((zVal2 - zVal1) * (zVal2 - zVal1)) + ((yVal2 - yVal1) * (yVal2 - yVal1))); + } +}