clean up noise eqs

This commit is contained in:
dfsek 2021-01-24 02:56:50 -07:00
parent ce62f1d7ac
commit bd4812ef2b
9 changed files with 308 additions and 171 deletions

View File

@ -0,0 +1,29 @@
package com.dfsek.terra.api.math.noise.samplers;
public class ConstantSampler implements NoiseSampler {
private final double constant;
public ConstantSampler(double constant) {
this.constant = constant;
}
@Override
public double getNoise(double x, double y) {
return constant;
}
@Override
public double getNoise(double x, double y, double z) {
return constant;
}
@Override
public double getNoiseSeeded(int seed, double x, double y) {
return constant;
}
@Override
public double getNoiseSeeded(int seed, double x, double y, double z) {
return constant;
}
}

View File

@ -0,0 +1,70 @@
package com.dfsek.terra.api.math.noise.samplers;
import com.dfsek.terra.api.math.noise.NoiseFunction2;
import com.dfsek.terra.api.math.noise.NoiseFunction3;
import com.dfsek.terra.api.math.parsii.RandomFunction;
import com.dfsek.terra.generation.config.NoiseBuilder;
import parsii.eval.Expression;
import parsii.eval.Parser;
import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException;
import java.util.Map;
/**
* Sampler implementation using parsii expression
*/
public class ExpressionSampler implements NoiseSampler {
private final Expression expression;
private final Variable x;
private final Variable y;
private final Variable z;
public ExpressionSampler(String equation, Scope parent, long seed, Map<String, NoiseBuilder> functions) throws ParseException {
Parser parser = new Parser();
Scope scope = new Scope().withParent(parent);
this.x = scope.create("x");
this.y = scope.create("y");
this.z = scope.create("z");
functions.forEach((id, noise) -> {
switch(noise.getDimensions()) {
case 2:
parser.registerFunction(id, new NoiseFunction2(seed, noise));
break;
case 3:
parser.registerFunction(id, new NoiseFunction3(seed, noise));
break;
}
});
parser.registerFunction("rand", new RandomFunction());
this.expression = parser.parse(equation, scope);
}
@Override
public synchronized double getNoise(double x, double y) {
return getNoise(x, 0, y);
}
@Override
public synchronized double getNoise(double x, double y, double z) {
this.x.setValue(x);
this.y.setValue(y);
this.z.setValue(z);
return expression.evaluate();
}
@Override
public double getNoiseSeeded(int seed, double x, double y) {
return getNoise(x, y);
}
@Override
public double getNoiseSeeded(int seed, double x, double y, double z) {
return getNoise(x, y, z);
}
}

View File

@ -1,9 +1,13 @@
package com.dfsek.terra.config.builder;
import com.dfsek.terra.api.math.noise.samplers.ConstantSampler;
import com.dfsek.terra.api.math.noise.samplers.ExpressionSampler;
import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
import com.dfsek.terra.biome.palette.PaletteHolder;
import com.dfsek.terra.generation.config.NoiseBuilder;
import com.dfsek.terra.generation.config.WorldGenerator;
import parsii.eval.Scope;
import parsii.tokenizer.ParseException;
import java.util.Collections;
import java.util.HashMap;
@ -44,7 +48,17 @@ public class GeneratorBuilder {
public WorldGenerator build(long seed) {
synchronized(gens) {
return gens.computeIfAbsent(seed, k -> new WorldGenerator(seed, noiseEquation, elevationEquation, varScope, noiseBuilderMap, palettes, slantPalettes, noise2d, base, biomeNoise.build((int) seed), elevationWeight, blendDistance, blendStep, blendWeight));
return gens.computeIfAbsent(seed, k -> {
NoiseSampler noise;
NoiseSampler elevation;
try {
noise = new ExpressionSampler(noiseEquation, varScope, seed, noiseBuilderMap);
elevation = elevationEquation == null ? new ConstantSampler(0) : new ExpressionSampler(elevationEquation, varScope, seed, noiseBuilderMap);
} catch(ParseException e) {
throw new RuntimeException(e);
}
return new WorldGenerator(palettes, slantPalettes, noise, elevation, noise2d, base, biomeNoise.build((int) seed), elevationWeight, blendDistance, blendStep, blendWeight);
});
}
}

View File

@ -1,20 +1,10 @@
package com.dfsek.terra.generation.config;
import com.dfsek.terra.api.math.noise.NoiseFunction2;
import com.dfsek.terra.api.math.noise.NoiseFunction3;
import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
import com.dfsek.terra.api.math.parsii.RandomFunction;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.api.world.palette.Palette;
import com.dfsek.terra.biome.palette.PaletteHolder;
import parsii.eval.Expression;
import parsii.eval.Parser;
import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException;
import java.util.Map;
public class WorldGenerator implements Generator {
@SuppressWarnings({"unchecked", "rawtypes", "RedundantSuppression"})
@ -22,13 +12,9 @@ public class WorldGenerator implements Generator {
@SuppressWarnings({"unchecked", "rawtypes", "RedundantSuppression"})
private final PaletteHolder slantPalettes;
private final Expression noiseExp;
private final Expression elevationExp;
private final Variable xVar;
private final Variable yVar;
private final Variable zVar;
private final Variable elevationXVar;
private final Variable elevationZVar;
private final NoiseSampler noise;
private final NoiseSampler elevation;
private final boolean noise2d;
private final double base;
private final NoiseSampler biomeNoise;
@ -37,9 +23,11 @@ public class WorldGenerator implements Generator {
private final int blendStep;
private final double blendWeight;
public WorldGenerator(long seed, String equation, String elevateEquation, Scope vScope, Map<String, NoiseBuilder> noiseBuilders, PaletteHolder palettes, PaletteHolder slantPalettes, boolean noise2d, double base, NoiseSampler biomeNoise, double elevationWeight, int blendDistance, int blendStep, double blendWeight) {
public WorldGenerator(PaletteHolder palettes, PaletteHolder slantPalettes, NoiseSampler noise, NoiseSampler elevation, boolean noise2d, double base, NoiseSampler biomeNoise, double elevationWeight, int blendDistance, int blendStep, double blendWeight) {
this.palettes = palettes;
this.slantPalettes = slantPalettes;
this.noise = noise;
this.elevation = elevation;
this.noise2d = noise2d;
this.base = base;
@ -48,55 +36,11 @@ public class WorldGenerator implements Generator {
this.blendDistance = blendDistance;
this.blendStep = blendStep;
this.blendWeight = blendWeight;
Parser p = new Parser();
p.registerFunction("rand", new RandomFunction());
Parser ep = new Parser();
ep.registerFunction("rand", new RandomFunction());
Scope s = new Scope().withParent(vScope);
xVar = s.create("x");
if(!noise2d) yVar = s.create("y");
else yVar = null;
zVar = s.create("z");
s.create("seed").setValue(seed);
for(Map.Entry<String, NoiseBuilder> e : noiseBuilders.entrySet()) {
switch(e.getValue().getDimensions()) {
case 2:
p.registerFunction(e.getKey(), new NoiseFunction2(seed, e.getValue()));
ep.registerFunction(e.getKey(), new NoiseFunction2(seed, e.getValue()));
break;
case 3:
p.registerFunction(e.getKey(), new NoiseFunction3(seed, e.getValue()));
break;
}
}
try {
this.noiseExp = p.parse(equation, s).simplify();
if(elevateEquation != null) {
Scope es = new Scope().withParent(vScope);
es.create("seed").setValue(seed);
this.elevationXVar = es.create("x");
this.elevationZVar = es.create("z");
this.elevationExp = ep.parse(elevateEquation, es).simplify();
} else {
this.elevationExp = null;
this.elevationXVar = null;
this.elevationZVar = null;
}
} catch(ParseException e) {
throw new IllegalArgumentException();
}
}
@Override
public synchronized double getElevation(int x, int z) {
if(elevationExp == null) return 0;
elevationXVar.setValue(x);
elevationZVar.setValue(z);
return elevationExp.evaluate();
return elevation.getNoise(x, z);
}
@Override
@ -111,10 +55,7 @@ public class WorldGenerator implements Generator {
@Override
public synchronized double getNoise(double x, double y, double z) {
xVar.setValue(x);
if(!noise2d) yVar.setValue(y);
zVar.setValue(z);
return noiseExp.evaluate();
return noise.getNoise(x, y, z);
}
/**

View File

@ -2,16 +2,16 @@ package com.dfsek.terra.generation.math;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.biome.BiomeProvider;
import com.dfsek.terra.generation.math.interpolation.ChunkInterpolator;
import com.dfsek.terra.generation.math.interpolation.BiomeChunkInterpolator;
import com.dfsek.terra.generation.math.interpolation.ElevationInterpolator;
import net.jafama.FastMath;
public class Sampler {
private final ChunkInterpolator interpolator;
private final BiomeChunkInterpolator interpolator;
private final ElevationInterpolator elevationInterpolator;
public Sampler(int x, int z, BiomeProvider provider, World world, int elevationSmooth) {
this.interpolator = new ChunkInterpolator(world, x, z, provider);
this.interpolator = new BiomeChunkInterpolator(world, x, z, provider);
this.elevationInterpolator = new ElevationInterpolator(world, x, z, provider, elevationSmooth);
}

View File

@ -0,0 +1,107 @@
package com.dfsek.terra.generation.math.interpolation;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.util.mutable.MutableInteger;
import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.biome.BiomeProvider;
import net.jafama.FastMath;
import java.util.HashMap;
import java.util.Map;
/**
* Class to abstract away the Interpolators needed to generate a chunk.<br>
* Contains method to get interpolated noise at a coordinate within the chunk.
*/
public class BiomeChunkInterpolator implements ChunkInterpolator {
private final Interpolator3[][][] interpGrid = new Interpolator3[4][64][4];
/**
* Instantiates a 3D BiomeChunkInterpolator at a pair of chunk coordinates.
*
* @param chunkX X coordinate of the chunk.
* @param chunkZ Z coordinate of the chunk.
* @param provider Biome Provider to use for biome fetching.
*/
public BiomeChunkInterpolator(World w, int chunkX, int chunkZ, BiomeProvider provider) {
int xOrigin = chunkX << 4;
int zOrigin = chunkZ << 4;
double[][][] noiseStorage = new double[5][5][65];
for(int x = 0; x < 5; x++) {
for(int z = 0; z < 5; z++) {
Generator generator = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2)).getGenerator(w);
Map<Generator, MutableInteger> genMap = new HashMap<>();
int step = generator.getBlendStep();
int blend = generator.getBlendDistance();
for(int xi = -blend; xi <= blend; xi++) {
for(int zi = -blend; zi <= blend; zi++) {
genMap.computeIfAbsent(provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step)).getGenerator(w), g -> new MutableInteger(0)).increment(); // Increment by 1
}
}
for(int y = 0; y < 65; y++) {
noiseStorage[x][z][y] = computeNoise(genMap, (x << 2) + xOrigin, y << 2, (z << 2) + zOrigin);
}
}
}
for(byte x = 0; x < 4; x++) {
for(byte z = 0; z < 4; z++) {
for(int y = 0; y < 64; y++) {
interpGrid[x][y][z] = new Interpolator3(
noiseStorage[x][z][y],
noiseStorage[x + 1][z][y],
noiseStorage[x][z][y + 1],
noiseStorage[x + 1][z][y + 1],
noiseStorage[x][z + 1][y],
noiseStorage[x + 1][z + 1][y],
noiseStorage[x][z + 1][y + 1],
noiseStorage[x + 1][z + 1][y + 1]);
}
}
}
}
private static double computeNoise(Map<Generator, MutableInteger> gens, double x, double y, double z) {
double n = 0;
double div = 0;
for(Map.Entry<Generator, MutableInteger> entry : gens.entrySet()) {
Generator gen = entry.getKey();
int weight = entry.getValue().get();
double noise = computeNoise(gen, x, y, z);
n += noise * weight;
div += gen.getWeight() * weight;
}
return n / div;
}
private static double computeNoise(Generator generator, double x, double y, double z) {
if(generator.is2d()) return generator.getNoise(x, 0, z) + noise2dExtrude(y, generator.get2dBase());
else return generator.getNoise(x, y, z);
}
private static int reRange(int value, int high) {
return FastMath.max(FastMath.min(value, high), 0);
}
private static double noise2dExtrude(double y, double base) {
return ((-FastMath.pow2((y / base))) + 1);
}
/**
* Gets the noise at a pair of internal chunk coordinates.
*
* @param x The internal X coordinate (0-15).
* @param z The internal Z coordinate (0-15).
* @return double - The interpolated noise at the coordinates.
*/
@Override
public double getNoise(double x, double y, double z) {
return interpGrid[reRange(((int) x) / 4, 3)][reRange(((int) y) / 4, 63)][reRange(((int) z) / 4, 3)].trilerp((x % 4) / 4, (y % 4) / 4, (z % 4) / 4);
}
}

View File

@ -1,102 +1,6 @@
package com.dfsek.terra.generation.math.interpolation;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.util.mutable.MutableInteger;
import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.biome.BiomeProvider;
import net.jafama.FastMath;
import java.util.HashMap;
import java.util.Map;
/**
* Class to abstract away the Interpolators needed to generate a chunk.<br>
* Contains method to get interpolated noise at a coordinate within the chunk.
*/
public class ChunkInterpolator {
private final Interpolator3[][][] interpGrid = new Interpolator3[4][64][4];
/**
* Instantiates a 3D ChunkInterpolator at a pair of chunk coordinates.
*
* @param chunkX X coordinate of the chunk.
* @param chunkZ Z coordinate of the chunk.
* @param provider Biome Provider to use for biome fetching.
*/
public ChunkInterpolator(World w, int chunkX, int chunkZ, BiomeProvider provider) {
int xOrigin = chunkX << 4;
int zOrigin = chunkZ << 4;
double[][][] noiseStorage = new double[5][5][65];
for(int x = 0; x < 5; x++) {
for(int z = 0; z < 5; z++) {
Generator generator = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2)).getGenerator(w);
Map<Generator, MutableInteger> genMap = new HashMap<>();
int step = generator.getBlendStep();
int blend = generator.getBlendDistance();
for(int xi = -blend; xi <= blend; xi++) {
for(int zi = -blend; zi <= blend; zi++) {
genMap.computeIfAbsent(provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step)).getGenerator(w), g -> new MutableInteger(0)).increment(); // Increment by 1
}
}
for(int y = 0; y < 65; y++) {
noiseStorage[x][z][y] = computeNoise(genMap, (x << 2) + xOrigin, y << 2, (z << 2) + zOrigin);
}
}
}
for(byte x = 0; x < 4; x++) {
for(byte z = 0; z < 4; z++) {
for(int y = 0; y < 64; y++) {
interpGrid[x][y][z] = new Interpolator3(
biomeAvg(x, y, z, noiseStorage),
biomeAvg(x + 1, y, z, noiseStorage),
biomeAvg(x, y + 1, z, noiseStorage),
biomeAvg(x + 1, y + 1, z, noiseStorage),
biomeAvg(x, y, z + 1, noiseStorage),
biomeAvg(x + 1, y, z + 1, noiseStorage),
biomeAvg(x, y + 1, z + 1, noiseStorage),
biomeAvg(x + 1, y + 1, z + 1, noiseStorage));
}
}
}
}
private static double computeNoise(Map<Generator, MutableInteger> gens, double x, double y, double z) {
double n = 0;
double div = 0;
for(Map.Entry<Generator, MutableInteger> entry : gens.entrySet()) {
Generator gen = entry.getKey();
int weight = entry.getValue().get();
double noise = computeNoise(gen, x, y, z);
n += noise * weight;
div += gen.getWeight() * weight;
}
return n / div;
}
private static double computeNoise(Generator generator, double x, double y, double z) {
if(generator.is2d()) return generator.getNoise(x, 0, z) + noise2dExtrude(y, generator.get2dBase());
else return generator.getNoise(x, y, z);
}
private static int reRange(int value, int high) {
return FastMath.max(FastMath.min(value, high), 0);
}
private static double noise2dExtrude(double y, double base) {
return ((-FastMath.pow2((y / base))) + 1);
}
private double biomeAvg(int x, int y, int z, double[][][] noise) {
return noise[x][z][y];
}
public interface ChunkInterpolator {
/**
* Gets the noise at a pair of internal chunk coordinates.
*
@ -104,7 +8,5 @@ public class ChunkInterpolator {
* @param z The internal Z coordinate (0-15).
* @return double - The interpolated noise at the coordinates.
*/
public double getNoise(double x, double y, double z) {
return interpGrid[reRange(((int) x) / 4, 3)][reRange(((int) y) / 4, 63)][reRange(((int) z) / 4, 3)].trilerp((x % 4) / 4, (y % 4) / 4, (z % 4) / 4);
}
double getNoise(double x, double y, double z);
}

View File

@ -0,0 +1,48 @@
package com.dfsek.terra.generation.math.interpolation;
import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
import net.jafama.FastMath;
public class NoiseChunkInterpolator implements ChunkInterpolator {
private final Interpolator3[][][] interpGrid = new Interpolator3[4][64][4];
public NoiseChunkInterpolator(int chunkX, int chunkZ, NoiseSampler noise) {
int xOrigin = chunkX << 4;
int zOrigin = chunkZ << 4;
double[][][] noiseStorage = new double[5][5][65];
for(int x = 0; x < 5; x++) {
for(int z = 0; z < 5; z++) {
for(int y = 0; y < 65; y++) {
noiseStorage[x][z][y] = noise.getNoise((x << 2) + xOrigin, y << 2, (z << 2) + zOrigin);
}
}
}
for(byte x = 0; x < 4; x++) {
for(byte z = 0; z < 4; z++) {
for(int y = 0; y < 64; y++) {
interpGrid[x][y][z] = new Interpolator3(
noiseStorage[x][z][y],
noiseStorage[x + 1][z][y],
noiseStorage[x][z][y + 1],
noiseStorage[x + 1][z][y + 1],
noiseStorage[x][z + 1][y],
noiseStorage[x + 1][z + 1][y],
noiseStorage[x][z + 1][y + 1],
noiseStorage[x + 1][z + 1][y + 1]);
}
}
}
}
private static int reRange(int value, int high) {
return FastMath.max(FastMath.min(value, high), 0);
}
@Override
public double getNoise(double x, double y, double z) {
return interpGrid[reRange(((int) x) / 4, 3)][reRange(((int) y) / 4, 63)][reRange(((int) z) / 4, 3)].trilerp((x % 4) / 4, (y % 4) / 4, (z % 4) / 4);
}
}

View File

@ -0,0 +1,26 @@
package com.dfsek.terra.population.items.carving;
import com.dfsek.terra.api.math.Range;
import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
import com.dfsek.terra.api.platform.world.ChunkAccess;
import com.dfsek.terra.carving.Carver;
import com.dfsek.terra.generation.math.interpolation.NoiseChunkInterpolator;
public class NoiseCarver implements Carver {
private final NoiseSampler noise;
private final Range range;
public NoiseCarver(NoiseSampler noise, Range range) {
this.noise = noise;
this.range = range;
}
@Override
public void carve(int chunkX, int chunkZ, ChunkAccess chunk) {
NoiseChunkInterpolator chunkInterpolator = new NoiseChunkInterpolator(chunkX, chunkZ, noise);
for(int y : range) {
}
}
}