cleanup generation code

This commit is contained in:
dfsek
2020-12-30 02:41:57 -07:00
parent ce45bacc6f
commit d16c28aebd
19 changed files with 67 additions and 80 deletions
@@ -1,116 +0,0 @@
package com.dfsek.terra.api.math.interpolation;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.biome.BiomeGrid;
import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.api.world.generation.GenerationPhase;
import net.jafama.FastMath;
/**
* Class to abstract away the 16 Interpolators needed to generate a chunk.<br>
* Contains method to get interpolated noise at a coordinate within the chunk.
*/
public class ChunkInterpolator3 {
private final Interpolator3[][][] interpGrid = new Interpolator3[4][64][4];
private final Generator[][] gens = new Generator[7][7];
private final boolean[][] needsBiomeInterp = new boolean[5][5];
private final double[][][] noiseStorage = new double[7][7][65];
private final int smooth;
/**
* Instantiates a 3D ChunkInterpolator at a pair of chunk coordinates, with a BiomeGrid and FastNoiseLite instance.
*
* @param chunkX X coordinate of the chunk.
* @param chunkZ Z coordinate of the chunk.
* @param grid BiomeGrid to use for noise fetching.
*/
public ChunkInterpolator3(World w, int chunkX, int chunkZ, BiomeGrid grid, int smooth) {
int xOrigin = chunkX << 4;
int zOrigin = chunkZ << 4;
this.smooth = smooth;
for(int x = -1; x < 6; x++) {
for(int z = -1; z < 6; z++) {
gens[x + 1][z + 1] = grid.getBiome(xOrigin + (x * smooth), zOrigin + (z * smooth), GenerationPhase.BASE).getGenerator();
}
}
for(int x = 0; x < 5; x++) {
for(int z = 0; z < 5; z++) {
needsBiomeInterp[x][z] = compareGens(x + 1, z + 1);
}
}
for(byte x = -1; x < 6; x++) {
for(byte z = -1; z < 6; z++) {
for(int y = 0; y < 65; y++) {
noiseStorage[x + 1][z + 1][y] = gens[x + 1][z + 1].getNoise(w, (x * smooth) + xOrigin, y << 2, (z * smooth) + 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),
biomeAvg(x + 1, y, z),
biomeAvg(x, y + 1, z),
biomeAvg(x + 1, y + 1, z),
biomeAvg(x, y, z + 1),
biomeAvg(x + 1, y, z + 1),
biomeAvg(x, y + 1, z + 1),
biomeAvg(x + 1, y + 1, z + 1));
}
}
}
}
private static int reRange(int value, int high) {
return FastMath.max(FastMath.min(value, high), 0);
}
private boolean compareGens(int x, int z) {
Generator comp = gens[x][z];
if(!comp.equals(gens[x + 1][z])) return true;
if(!comp.equals(gens[x][z + 1])) return true;
if(!comp.equals(gens[x - 1][z])) return true;
if(!comp.equals(gens[x][z - 1])) return true;
if(!comp.equals(gens[x + 1][z + 1])) return true;
if(!comp.equals(gens[x - 1][z - 1])) return true;
if(!comp.equals(gens[x + 1][z - 1])) return true;
return !comp.equals(gens[x - 1][z + 1]);
}
private double biomeAvg(int x, int y, int z) {
if(needsBiomeInterp[x][z]) {
double t = 0d;
for(int xi = 0; xi <= 2; xi++) {
for(int zi = 0; zi <= 2; zi++) {
t += noiseStorage[x + xi][z + zi][y];
}
}
return t / 9d;
} else {
return noiseStorage[x + 1][z + 1][y];
}
}
/**
* 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.
*/
public double getNoise(double x, double y, double z) {
return interpGrid[reRange(((int) x) / smooth, 3)][reRange(((int) y) / 4, 63)][reRange(((int) z) / smooth, 3)].trilerp((x % smooth) / smooth, (y % 4) / 4, (z % smooth) / smooth);
}
}
@@ -1,49 +0,0 @@
package com.dfsek.terra.api.math.interpolation;
/**
* Class for bilinear interpolation of values arranged on a unit square.
*/
public class Interpolator {
private final double v0, v1, v2, v3;
/**
* Constructs an interpolator with given values as vertices of a unit square.
*
* @param v0 - (0,0)
* @param v1 - (1,0)
* @param v2 - (0,1)
* @param v3 - (1,1)
*/
public Interpolator(double v0, double v1, double v2, double v3) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
/**
* 1D Linear interpolation between 2 points 1 unit apart.
*
* @param t - Distance from v0. Total distance between v0 and v1 is 1 unit.
* @param v0 - Value at v0.
* @param v1 - Value at v1.
* @return double - The interpolated value.
*/
public static double lerp(double t, double v0, double v1) {
return v0 + t * (v1 - v0);
}
/**
* 2D Bilinear interpolation between 4 points on a unit square.
*
* @param s - X value
* @param t - Z value
* @return double - The interpolated value.
*/
public double bilerp(double s, double t) {
double v01 = lerp(s, v0, v1);
double v23 = lerp(s, v2, v3);
return lerp(t, v01, v23);
}
}
@@ -1,38 +0,0 @@
package com.dfsek.terra.api.math.interpolation;
/**
* Class for bilinear interpolation of values arranged on a unit square.
*/
public class Interpolator3 {
private final double _000, _100, _010, _110, _001, _101, _011, _111;
/**
* Constructs an interpolator with given values as vertices of a unit cube.
* * @param _000 The value at <code>(t, u, v) = (0, 0, 0)</code>.
* * @param _100 The value at <code>(t, u, v) = (1, 0, 0)</code>.
* * @param _010 The value at <code>(t, u, v) = (0, 1, 0)</code>.
* * @param _110 The value at <code>(t, u, v) = (1, 1, 0)</code>.
* * @param _001 The value at <code>(t, u, v) = (0, 0, 1)</code>.
* * @param _101 The value at <code>(t, u, v) = (1, 0, 1)</code>.
* * @param _011 The value at <code>(t, u, v) = (0, 1, 1)</code>.
* * @param _111 The value at <code>(t, u, v) = (1, 1, 1)</code>.
*/
public Interpolator3(double _000, double _100,
double _010, double _110, double _001, double _101,
double _011, double _111) {
this._000 = _000;
this._001 = _001;
this._010 = _010;
this._011 = _011;
this._100 = _100;
this._101 = _101;
this._110 = _110;
this._111 = _111;
}
public double trilerp(double x, double y, double z) {
Interpolator top = new Interpolator(_000, _010, _001, _011);
Interpolator bottom = new Interpolator(_100, _110, _101, _111);
return Interpolator.lerp(x, top.bilerp(y, z), bottom.bilerp(y, z));
}
}
@@ -19,7 +19,7 @@ public class AirCheck extends SpawnCheck {
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeTemplate c = b.getConfig();
if(y <= c.getSeaLevel()) return false;
double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z);
return b.getGenerator().getNoise(world, x, y, z) + elevation <= 0;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation <= 0;
}
}
@@ -16,7 +16,7 @@ public class LandCheck extends SpawnCheck {
public boolean check(int x, int y, int z) {
TerraWorld tw = main.getWorld(world);
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z);
return b.getGenerator().getNoise(world, x, y, z) + elevation > 0;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation > 0;
}
}
@@ -19,7 +19,7 @@ public class OceanCheck extends SpawnCheck {
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeTemplate c = b.getConfig();
if(y > c.getSeaLevel()) return false;
double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z);
return b.getGenerator().getNoise(world, x, y, z) + elevation <= 0;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation <= 0;
}
}
@@ -21,14 +21,5 @@ public interface Biome {
*
* @return BiomeTerrain - The terrain generation instance.
*/
Generator getGenerator();
/**
* Gets the BiomeTerrain instance used to generate the biome in this world.
*
* @return BiomeTerrain - The terrain generation instance.
*/
default Generator getGenerator(World w) {
return getGenerator();
}
Generator getGenerator(World w);
}
@@ -1,7 +1,6 @@
package com.dfsek.terra.api.world.biome;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.palette.Palette;
public interface Generator {
@@ -13,7 +12,7 @@ public interface Generator {
* @param z - The z coordinate.
* @return double - Noise value at the specified coordinates.
*/
double getNoise(World w, int x, int y, int z);
double getNoise(int x, int y, int z);
/**
* Gets the BlocPalette to generate the biome with.