9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-29 20:19:06 +00:00
This commit is contained in:
Dan Macbook
2020-08-10 23:09:52 -04:00
parent d3a4b0451d
commit a6b65c8d76
9 changed files with 0 additions and 0 deletions

View File

@@ -1,219 +0,0 @@
package com.volmit.iris.util;
import java.util.Random;
/**
* Generates noise using the "classic" perlin generator
*
* @see SimplexNoiseC "Improved" and faster version with slighly
* different results
*/
public class BasePerlinNoiseGenerator extends NoiseGenerator
{
protected static final int grad3[][] = {{1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {1, 0, 1}, {-1, 0, 1}, {1, 0, -1}, {-1, 0, -1}, {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1}};
private static final BasePerlinNoiseGenerator instance = new BasePerlinNoiseGenerator();
protected BasePerlinNoiseGenerator()
{
int p[] = {151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180};
for(int i = 0; i < 512; i++)
{
perm[i] = p[i & 255];
}
}
/**
* Creates a seeded perlin noise generator for the given seed
*
* @param seed
* Seed to construct this generator for
*/
public BasePerlinNoiseGenerator(long seed)
{
this(new Random(seed));
}
/**
* Creates a seeded perlin noise generator with the given Random
*
* @param rand
* Random to construct with
*/
public BasePerlinNoiseGenerator(Random rand)
{
offsetX = rand.nextDouble() * 256;
offsetY = rand.nextDouble() * 256;
offsetZ = rand.nextDouble() * 256;
for(int i = 0; i < 256; i++)
{
perm[i] = rand.nextInt(256);
}
for(int i = 0; i < 256; i++)
{
int pos = rand.nextInt(256 - i) + i;
int old = perm[i];
perm[i] = perm[pos];
perm[pos] = old;
perm[i + 256] = perm[i];
}
}
/**
* Computes and returns the 1D unseeded perlin noise for the given coordinates
* in 1D space
*
* @param x
* X coordinate
* @return Noise at given location, from range -1 to 1
*/
public static double getNoise(double x)
{
return instance.noise(x);
}
/**
* Computes and returns the 2D unseeded perlin noise for the given coordinates
* in 2D space
*
* @param x
* X coordinate
* @param y
* Y coordinate
* @return Noise at given location, from range -1 to 1
*/
public static double getNoise(double x, double y)
{
return instance.noise(x, y);
}
/**
* Computes and returns the 3D unseeded perlin noise for the given coordinates
* in 3D space
*
* @param x
* X coordinate
* @param y
* Y coordinate
* @param z
* Z coordinate
* @return Noise at given location, from range -1 to 1
*/
public static double getNoise(double x, double y, double z)
{
return instance.noise(x, y, z);
}
/**
* Gets the singleton unseeded instance of this generator
*
* @return Singleton
*/
public static BasePerlinNoiseGenerator getInstance()
{
return instance;
}
@Override
public double noise(double x, double y, double z)
{
x += offsetX;
y += offsetY;
z += offsetZ;
int floorX = floor(x);
int floorY = floor(y);
int floorZ = floor(z);
// Find unit cube containing the point
int X = floorX & 255;
int Y = floorY & 255;
int Z = floorZ & 255;
// Get relative xyz coordinates of the point within the cube
x -= floorX;
y -= floorY;
z -= floorZ;
// Compute fade curves for xyz
double fX = fade(x);
double fY = fade(y);
double fZ = fade(z);
// Hash coordinates of the cube corners
int A = perm[X] + Y;
int AA = perm[A] + Z;
int AB = perm[A + 1] + Z;
int B = perm[X + 1] + Y;
int BA = perm[B] + Z;
int BB = perm[B + 1] + Z;
return lerp(fZ, lerp(fY, lerp(fX, grad(perm[AA], x, y, z), grad(perm[BA], x - 1, y, z)), lerp(fX, grad(perm[AB], x, y - 1, z), grad(perm[BB], x - 1, y - 1, z))), lerp(fY, lerp(fX, grad(perm[AA + 1], x, y, z - 1), grad(perm[BA + 1], x - 1, y, z - 1)), lerp(fX, grad(perm[AB + 1], x, y - 1, z - 1), grad(perm[BB + 1], x - 1, y - 1, z - 1))));
}
/**
* Generates noise for the 1D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @return Resulting noise
*/
public static double getNoise(double x, int octaves, double frequency, double amplitude)
{
return instance.noise(x, octaves, frequency, amplitude);
}
/**
* Generates noise for the 2D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param y
* Y-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @return Resulting noise
*/
public static double getNoise(double x, double y, int octaves, double frequency, double amplitude)
{
return instance.noise(x, y, octaves, frequency, amplitude);
}
/**
* Generates noise for the 3D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param y
* Y-coordinate
* @param z
* Z-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @return Resulting noise
*/
public static double getNoise(double x, double y, double z, int octaves, double frequency, double amplitude)
{
return instance.noise(x, y, z, octaves, frequency, amplitude);
}
}

View File

@@ -1,223 +0,0 @@
package com.volmit.iris.util;
public class CNG
{
public static long hits = 0;
public static long creates = 0;
public static final NoiseInjector ADD = (s, v) -> new double[] {s + v, 1};
public static final NoiseInjector SRC_SUBTRACT = (s, v) -> new double[] {s - v < 0 ? 0 : s - v, -1};
public static final NoiseInjector DST_SUBTRACT = (s, v) -> new double[] {v - s < 0 ? 0 : s - v, -1};
public static final NoiseInjector MULTIPLY = (s, v) -> new double[] {s * v, 0};
public static final NoiseInjector MAX = (s, v) -> new double[] {Math.max(s, v), 0};
public static final NoiseInjector MIN = (s, v) -> new double[] {Math.min(s, v), 0};
public static final NoiseInjector SRC_MOD = (s, v) -> new double[] {s % v, 0};
public static final NoiseInjector SRC_POW = (s, v) -> new double[] {Math.pow(s, v), 0};
public static final NoiseInjector DST_MOD = (s, v) -> new double[] {v % s, 0};
public static final NoiseInjector DST_POW = (s, v) -> new double[] {Math.pow(v, s), 0};
private double freq;
private double amp;
private double scale;
private double fscale;
private KList<CNG> children;
private CNG fracture;
private SNG generator;
private final double opacity;
private NoiseInjector injector;
private RNG rng;
private int oct;
private double patch;
private double up;
private double down;
private double power;
public static CNG signature(RNG rng)
{
//@builder
return new CNG(rng.nextParallelRNG(17), 1D, 3)
.scale(0.012)
.fractureWith(new CNG(rng.nextParallelRNG(18), 1, 2)
.scale(0.018)
.child(new CNG(rng.nextParallelRNG(19), 1, 2)
.scale(0.1))
.fractureWith(new CNG(rng.nextParallelRNG(20), 1, 2)
.scale(0.15), 24), 44).down(0.3).patch(2.5);
//@done
}
public CNG(RNG random)
{
this(random, 1);
}
public CNG(RNG random, int octaves)
{
this(random, 1D, octaves);
}
public CNG(RNG random, double opacity, int octaves)
{
creates += octaves;
this.oct = octaves;
this.rng = random;
power = 1;
freq = 1;
amp = 1;
scale = 1;
patch = 1;
fscale = 1;
fracture = null;
generator = new SNG(random);
this.opacity = opacity;
this.injector = ADD;
}
public CNG child(CNG c)
{
if(children == null)
{
children = new KList<>();
}
children.add(c);
return this;
}
@Deprecated
public RNG nextRNG()
{
return getRNG().nextRNG();
}
public RNG getRNG()
{
return rng;
}
public CNG fractureWith(CNG c, double scale)
{
fracture = c;
fscale = scale;
return this;
}
public CNG scale(double c)
{
scale = c;
return this;
}
public CNG freq(double c)
{
freq = c;
return this;
}
public CNG amp(double c)
{
amp = c;
return this;
}
public CNG patch(double c)
{
patch = c;
return this;
}
public CNG up(double c)
{
up = c;
return this;
}
public CNG down(double c)
{
down = c;
return this;
}
public CNG injectWith(NoiseInjector i)
{
injector = i;
return this;
}
public int fit(int min, int max, double... dim)
{
if(min == max)
{
return min;
}
double noise = noise(dim);
return (int) Math.round(IrisInterpolation.lerp(min, max, noise));
}
public int fitDouble(double min, double max, double... dim)
{
if(min == max)
{
return (int) Math.round(min);
}
double noise = noise(dim);
return (int) Math.round(IrisInterpolation.lerp(min, max, noise));
}
public double fitDoubleD(double min, double max, double... dim)
{
if(min == max)
{
return min;
}
double noise = noise(dim);
return IrisInterpolation.lerp(min, max, noise);
}
public int fitDoubleExponent(double min, double max, double exponent, double... dim)
{
if(min == max)
{
return (int) Math.round(min);
}
double noise = noise(dim);
return (int) Math.round(IrisInterpolation.lerp(min, max, exponent == 1 ? noise : Math.pow(noise, exponent)));
}
public double noise(double... dim)
{
double f = fracture != null ? (fracture.noise(dim) - 0.5) * fscale : 0D;
double x = dim.length > 0 ? dim[0] + f : 0D;
double y = dim.length > 1 ? dim[1] - f : 0D;
double z = dim.length > 2 ? dim[2] + f : 0D;
double n = ((generator.noise(x * scale, y * scale, z * scale, oct, freq, amp, true) / 2D) + 0.5D) * opacity;
n = power != 1D ? Math.pow(n, power) : n;
double m = 1;
hits += oct;
if(children == null)
{
return (n - down + up) * patch;
}
for(CNG i : children)
{
double[] r = injector.combine(n, i.noise(dim));
n = r[0];
m += r[1];
}
return ((n / m) - down + up) * patch;
}
public CNG pow(double power)
{
this.power = power;
return this;
}
}

View File

@@ -1,94 +0,0 @@
package com.volmit.iris.util;
import com.volmit.iris.util.FastNoise.CellularDistanceFunction;
import com.volmit.iris.util.FastNoise.CellularReturnType;
import com.volmit.iris.util.FastNoise.NoiseType;
import lombok.Getter;
import lombok.Setter;
public class CellGenerator
{
private FastNoise fn;
private FastNoise fd;
private CNG cng;
@Getter
@Setter
private double cellScale;
@Getter
@Setter
private double shuffle;
public CellGenerator(RNG rng)
{
shuffle = 128;
cellScale = 0.73;
cng = CNG.signature(rng.nextParallelRNG(3204));
RNG rx = rng.nextParallelRNG(8735652);
int s = rx.nextInt();
fn = new FastNoise(s);
fn.SetNoiseType(NoiseType.Cellular);
fn.SetCellularReturnType(CellularReturnType.CellValue);
fn.SetCellularDistanceFunction(CellularDistanceFunction.Natural);
fd = new FastNoise(s);
fd.SetNoiseType(NoiseType.Cellular);
fd.SetCellularReturnType(CellularReturnType.Distance2Sub);
fd.SetCellularDistanceFunction(CellularDistanceFunction.Natural);
}
public float getDistance(double x, double z)
{
return ((fd.GetCellular((float) ((x * cellScale) + (cng.noise(x, z) * shuffle)), (float) ((z * cellScale) + (cng.noise(z, x) * shuffle)))) + 1f) / 2f;
}
public float getDistance(double x, double y, double z)
{
return ((fd.GetCellular((float) ((x * cellScale) + (cng.noise(x, y, z) * shuffle)), (float) ((y * cellScale) + (cng.noise(x, y, z) * shuffle)), (float) ((z * cellScale) + (cng.noise(z, y, x) * shuffle)))) + 1f) / 2f;
}
public float getValue(double x, double z, int possibilities)
{
if(possibilities == 1)
{
return 0;
}
return ((fn.GetCellular((float) ((x * cellScale) + (cng.noise(x, z) * shuffle)), (float) ((z * cellScale) + (cng.noise(z, x) * shuffle))) + 1f) / 2f) * (possibilities - 1);
}
public float getValue(double x, double y, double z, int possibilities)
{
if(possibilities == 1)
{
return 0;
}
return ((fn.GetCellular((float) ((x * cellScale) + (cng.noise(x, z) * shuffle)),
(float) ((y * 8 * cellScale) + (cng.noise(x, y * 8) * shuffle))
, (float) ((z * cellScale) + (cng.noise(z, x) * shuffle))) + 1f) / 2f) * (possibilities - 1);
}
public int getIndex(double x, double z, int possibilities)
{
if(possibilities == 1)
{
return 0;
}
return (int) Math.round(getValue(x, z, possibilities));
}
public int getIndex(double x, double y, double z, int possibilities)
{
if(possibilities == 1)
{
return 0;
}
return (int) Math.round(getValue(x, y, z, possibilities));
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,231 +0,0 @@
package com.volmit.iris.util;
/**
* Base class for all noise generators
*/
public abstract class NoiseGenerator
{
protected final int perm[] = new int[512];
protected double offsetX;
protected double offsetY;
protected double offsetZ;
/**
* Speedy floor, faster than (int)Math.floor(x)
*
* @param x
* Value to floor
* @return Floored value
*/
public static int floor(double x)
{
return x >= 0 ? (int) x : (int) x - 1;
}
protected static double fade(double x)
{
return x * x * x * (x * (x * 6 - 15) + 10);
}
protected static double lerp(double x, double y, double z)
{
return y + x * (z - y);
}
protected static double grad(int hash, double x, double y, double z)
{
hash &= 15;
double u = hash < 8 ? x : y;
double v = hash < 4 ? y : hash == 12 || hash == 14 ? x : z;
return ((hash & 1) == 0 ? u : -u) + ((hash & 2) == 0 ? v : -v);
}
/**
* Computes and returns the 1D noise for the given coordinate in 1D space
*
* @param x
* X coordinate
* @return Noise at given location, from range -1 to 1
*/
public double noise(double x)
{
return noise(x, 0, 0);
}
/**
* Computes and returns the 2D noise for the given coordinates in 2D space
*
* @param x
* X coordinate
* @param y
* Y coordinate
* @return Noise at given location, from range -1 to 1
*/
public double noise(double x, double y)
{
return noise(x, y, 0);
}
/**
* Computes and returns the 3D noise for the given coordinates in 3D space
*
* @param x
* X coordinate
* @param y
* Y coordinate
* @param z
* Z coordinate
* @return Noise at given location, from range -1 to 1
*/
public abstract double noise(double x, double y, double z);
/**
* Generates noise for the 1D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @return Resulting noise
*/
public double noise(double x, int octaves, double frequency, double amplitude)
{
return noise(x, 0, 0, octaves, frequency, amplitude);
}
/**
* Generates noise for the 1D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @param normalized
* If true, normalize the value to [-1, 1]
* @return Resulting noise
*/
public double noise(double x, int octaves, double frequency, double amplitude, boolean normalized)
{
return noise(x, 0, 0, octaves, frequency, amplitude, normalized);
}
/**
* Generates noise for the 2D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param y
* Y-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @return Resulting noise
*/
public double noise(double x, double y, int octaves, double frequency, double amplitude)
{
return noise(x, y, 0, octaves, frequency, amplitude);
}
/**
* Generates noise for the 2D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param y
* Y-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @param normalized
* If true, normalize the value to [-1, 1]
* @return Resulting noise
*/
public double noise(double x, double y, int octaves, double frequency, double amplitude, boolean normalized)
{
return noise(x, y, 0, octaves, frequency, amplitude, normalized);
}
/**
* Generates noise for the 3D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param y
* Y-coordinate
* @param z
* Z-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @return Resulting noise
*/
public double noise(double x, double y, double z, int octaves, double frequency, double amplitude)
{
return noise(x, y, z, octaves, frequency, amplitude, false);
}
/**
* Generates noise for the 3D coordinates using the specified number of octaves
* and parameters
*
* @param x
* X-coordinate
* @param y
* Y-coordinate
* @param z
* Z-coordinate
* @param octaves
* Number of octaves to use
* @param frequency
* How much to alter the frequency by each octave
* @param amplitude
* How much to alter the amplitude by each octave
* @param normalized
* If true, normalize the value to [-1, 1]
* @return Resulting noise
*/
public double noise(double x, double y, double z, int octaves, double frequency, double amplitude, boolean normalized)
{
double result = 0;
double amp = 1;
double freq = 1;
double max = 0;
for(int i = 0; i < octaves; i++)
{
result += noise(x * freq, y * freq, z * freq) * amp;
max += amp;
freq *= frequency;
amp *= amplitude;
}
if(normalized)
{
result /= max;
}
return result;
}
}

View File

@@ -1,175 +0,0 @@
package com.volmit.iris.util;
import java.util.Random;
public class PerlinNoise extends BasePerlinNoiseGenerator
{
/**
* Creates an instance using the given PRNG.
*
* @param rand
* the PRNG used to generate the seed permutation
*/
public PerlinNoise(Random rand)
{
offsetX = rand.nextDouble() * 256;
offsetY = rand.nextDouble() * 256;
offsetZ = rand.nextDouble() * 256;
// The only reason why I'm re-implementing the constructor code is that I've
// read
// on at least 3 different sources that the permutation table should initially
// be
// populated with indices.
// "The permutation table is his answer to the issue of random numbers.
// First take an array of decent length, usually 256 values. Fill it
// sequentially with each
// number in that range: so index 1 gets 1, index 8 gets 8, index 251 gets 251,
// etc...
// Then randomly shuffle the values so you have a table of 256 random values,
// but only
// contains the values between 0 and 255."
// source: https://code.google.com/p/fractalterraingeneration/wiki/Perlin_Noise
for(int i = 0; i < 256; i++)
{
perm[i] = i;
}
for(int i = 0; i < 256; i++)
{
int pos = rand.nextInt(256 - i) + i;
int old = perm[i];
perm[i] = perm[pos];
perm[pos] = old;
perm[i + 256] = perm[i];
}
}
public static int floor(double x)
{
int floored = (int) x;
return x < floored ? floored - 1 : floored;
}
/**
* Generates a rectangular section of this generator's noise.
*
* @param noise
* the output of the previous noise layer
* @param x
* the X offset
* @param y
* the Y offset
* @param z
* the Z offset
* @param sizeX
* the size on the X axis
* @param sizeY
* the size on the Y axis
* @param sizeZ
* the size on the Z axis
* @param scaleX
* the X scale parameter
* @param scaleY
* the Y scale parameter
* @param scaleZ
* the Z scale parameter
* @param amplitude
* the amplitude parameter
* @return {@code noise} with this layer of noise added
*/
public double[] getNoise(double[] noise, double x, double y, double z, int sizeX, int sizeY, int sizeZ, double scaleX, double scaleY, double scaleZ, double amplitude)
{
if(sizeY == 1)
{
return get2dNoise(noise, x, z, sizeX, sizeZ, scaleX, scaleZ, amplitude);
}
else
{
return get3dNoise(noise, x, y, z, sizeX, sizeY, sizeZ, scaleX, scaleY, scaleZ, amplitude);
}
}
protected double[] get2dNoise(double[] noise, double x, double z, int sizeX, int sizeZ, double scaleX, double scaleZ, double amplitude)
{
int index = 0;
for(int i = 0; i < sizeX; i++)
{
double dx = x + offsetX + i * scaleX;
int floorX = floor(dx);
int ix = floorX & 255;
dx -= floorX;
double fx = fade(dx);
for(int j = 0; j < sizeZ; j++)
{
double dz = z + offsetZ + j * scaleZ;
int floorZ = floor(dz);
int iz = floorZ & 255;
dz -= floorZ;
double fz = fade(dz);
// Hash coordinates of the square corners
int a = perm[ix];
int aa = perm[a] + iz;
int b = perm[ix + 1];
int ba = perm[b] + iz;
double x1 = lerp(fx, grad(perm[aa], dx, 0, dz), grad(perm[ba], dx - 1, 0, dz));
double x2 = lerp(fx, grad(perm[aa + 1], dx, 0, dz - 1), grad(perm[ba + 1], dx - 1, 0, dz - 1));
noise[index++] += lerp(fz, x1, x2) * amplitude;
}
}
return noise;
}
protected double[] get3dNoise(double[] noise, double x, double y, double z, int sizeX, int sizeY, int sizeZ, double scaleX, double scaleY, double scaleZ, double amplitude)
{
int n = -1;
double x1 = 0;
double x2 = 0;
double x3 = 0;
double x4 = 0;
int index = 0;
for(int i = 0; i < sizeX; i++)
{
double dx = x + offsetX + i * scaleX;
int floorX = floor(dx);
int ix = floorX & 255;
dx -= floorX;
double fx = fade(dx);
for(int j = 0; j < sizeZ; j++)
{
double dz = z + offsetZ + j * scaleZ;
int floorZ = floor(dz);
int iz = floorZ & 255;
dz -= floorZ;
double fz = fade(dz);
for(int k = 0; k < sizeY; k++)
{
double dy = y + offsetY + k * scaleY;
int floorY = floor(dy);
int iy = floorY & 255;
dy -= floorY;
double fy = fade(dy);
if(k == 0 || iy != n)
{
n = iy;
// Hash coordinates of the cube corners
int a = perm[ix] + iy;
int aa = perm[a] + iz;
int ab = perm[a + 1] + iz;
int b = perm[ix + 1] + iy;
int ba = perm[b] + iz;
int bb = perm[b + 1] + iz;
x1 = lerp(fx, grad(perm[aa], dx, dy, dz), grad(perm[ba], dx - 1, dy, dz));
x2 = lerp(fx, grad(perm[ab], dx, dy - 1, dz), grad(perm[bb], dx - 1, dy - 1, dz));
x3 = lerp(fx, grad(perm[aa + 1], dx, dy, dz - 1), grad(perm[ba + 1], dx - 1, dy, dz - 1));
x4 = lerp(fx, grad(perm[ab + 1], dx, dy - 1, dz - 1), grad(perm[bb + 1], dx - 1, dy - 1, dz - 1));
}
double y1 = lerp(fy, x1, x2);
double y2 = lerp(fy, x3, x4);
noise[index++] += lerp(fz, y1, y2) * amplitude;
}
}
}
return noise;
}
}

View File

@@ -1,214 +0,0 @@
package com.volmit.iris.util;
import java.util.function.Function;
public class PolygonGenerator
{
private double[] rarity;
private CNG[] gen;
private int bits;
private int possibilities;
private boolean useRarity;
public PolygonGenerator(RNG rng, int possibilities, double scale, int octaves, Function<CNG, CNG> factory)
{
useRarity = false;
bits = 1;
this.possibilities = possibilities;
while(Math.pow(2, bits) <= possibilities)
{
bits++;
}
bits++;
bits = bits > 32 ? 32 : bits;
rarity = new double[possibilities];
gen = new CNG[bits];
for(int i = 0; i < bits; i++)
{
gen[i] = new CNG(rng.nextParallelRNG(2118 + (i * 3305)), 1D, 1).scale(scale / possibilities);
gen[i] = factory.apply(gen[i]);
}
}
public PolygonGenerator useRarity()
{
useRarity = true;
return this;
}
public void setRarity(int index, double r)
{
rarity[index] = 1D - Math.pow(0.5, r);
}
public boolean hasBorder(int checks, double distance, double... dims)
{
int current = getIndex(dims);
double ajump = 360D / (double) checks;
if(dims.length == 2)
{
for(int i = 0; i < checks; i++)
{
double dx = M.sin((float) Math.toRadians(ajump * i));
double dz = M.cos((float) Math.toRadians(ajump * i));
if(current != getIndex((dx * distance) + dims[0], (dz * distance) + dims[1]))
{
return true;
}
}
}
if(dims.length == 3)
{
for(int i = 0; i < checks; i++)
{
double dx = M.sin((float) Math.toRadians(ajump * i));
double dz = M.cos((float) Math.toRadians(ajump * i));
double dy = Math.tan(Math.toRadians(ajump * i));
if(current != getIndex((dx * distance) + dims[0], (dz * distance) + dims[1], (dy * distance) + dims[2]))
{
return true;
}
}
}
return false;
}
public boolean hasBorder3D(int checks, double distance, double... dims)
{
int current = getIndex(dims);
double ajump = 360D / (double) checks;
int hit = -1;
if(dims.length == 3)
{
for(int i = 0; i < checks; i++)
{
double dx = M.sin((float) Math.toRadians(ajump * i));
double dz = M.cos((float) Math.toRadians(ajump * i));
double dy = Math.tan(Math.toRadians(ajump * i));
int d = getIndex((dx * distance) + dims[0], (dz * distance) + dims[1], (dy * distance) + dims[2]);
if(current != d)
{
if(hit >= 0 && hit != current && hit != d)
{
return true;
}
if(hit < 0)
{
hit = d;
}
}
}
}
return false;
}
/**
* Returns 0.0 to 1.0 where 0.0 is directly on the border of another region and
* 1.0 is perfectly in the center of a region
*
* @param x
* the x
* @param z
* the z
* @return the closest neighbor threshold.
*/
public double getClosestNeighbor(double... dim)
{
double closest = 0.5;
for(int i = 0; i < gen.length; i++)
{
double distance = Math.abs(gen[i].noise(dim) - 0.5);
if(distance < closest)
{
closest = distance;
}
}
return (closest * 2);
}
public int getIndex(double... dim)
{
int data = 0;
int adjusted = 0;
double[] noise = new double[gen.length];
for(int i = 0; i < gen.length; i++)
{
data |= (noise[i] = gen[i].noise(dim)) > 0.5 ? i == 0 ? 1 : 1 << i : 0;
}
if(!useRarity)
{
return data % possibilities;
}
double r = rarity[data % possibilities];
for(int i = 0; i < gen.length; i++)
{
adjusted |= noise[i] > r ? i == 0 ? 1 : 1 << i : 0;
}
return adjusted % possibilities;
}
public static class EnumPolygonGenerator<T> extends PolygonGenerator
{
private T[] choices;
public EnumPolygonGenerator(RNG rng, double scale, int octaves, T[] choices, Function<CNG, CNG> factory)
{
super(rng, choices.length, scale / (double) choices.length, octaves, factory);
this.choices = choices;
}
public EnumPolygonGenerator<T> useRarity()
{
super.useRarity();
return this;
}
@SuppressWarnings("unchecked")
public EnumPolygonGenerator(RNG rng, double scale, int octaves, KList<T> c, KMap<T, Double> choiceRarities, Function<CNG, CNG> factory)
{
super(rng, choiceRarities.size(), scale / (double) choiceRarities.size(), octaves, factory);
this.choices = (T[]) c.toArray();
int m = 0;
for(T i : c)
{
setRarity(m++, choiceRarities.get(i));
}
}
public void setRarity(T t, double rarity)
{
for(int i = 0; i < choices.length; i++)
{
if(choices[i].equals(t))
{
setRarity(i, rarity);
return;
}
}
}
public T getChoice(double... dim)
{
return choices[getIndex(dim)];
}
}
}

View File

@@ -1,63 +0,0 @@
package com.volmit.iris.util;
public class RarityCellGenerator<T extends IRare> extends CellGenerator
{
public RarityCellGenerator(RNG rng)
{
super(rng);
}
public T get(double x, double z, KList<T> b)
{
if(b.size() == 0)
{
return null;
}
if(b.size() == 1)
{
return b.get(0);
}
KList<T> rarityMapped = new KList<>();
boolean o = false;
int max = 1;
for(T i : b)
{
if(i.getRarity() > max)
{
max = i.getRarity();
}
}
max++;
for(T i : b)
{
for(int j = 0; j < max - i.getRarity(); j++)
{
if(o = !o)
{
rarityMapped.add(i);
}
else
{
rarityMapped.add(0, i);
}
}
}
if(rarityMapped.size() == 1)
{
return rarityMapped.get(0);
}
if(rarityMapped.isEmpty())
{
throw new RuntimeException("BAD RARITY MAP! RELATED TO: " + b.toString(", or possibly "));
}
return rarityMapped.get(getIndex(x, z, rarityMapped.size()));
}
}

View File

@@ -1,377 +0,0 @@
package com.volmit.iris.util;
import java.util.Random;
/**
* A speed-improved simplex noise algorithm.
*
* <p>
* Based on example code by Stefan Gustavson (stegu@itn.liu.se). Optimisations
* by Peter Eastman (peastman@drizzle.stanford.edu). Better rank ordering method
* by Stefan Gustavson in 2012.
*
* <p>
* This could be sped up even further, but it's useful as is.
*/
public class SNG extends PerlinNoise
{
protected static final double SQRT_3 = 1.7320508075688772; // Math.sqrt(3)
protected static final double F2 = 0.5 * (SQRT_3 - 1);
protected static final double G2 = (3 - SQRT_3) / 6;
protected static final double G22 = G2 * 2.0 - 1;
protected static final double F3 = 1.0 / 3.0;
protected static final double G3 = 1.0 / 6.0;
protected static final double G32 = G3 * 2.0;
protected static final double G33 = G3 * 3.0 - 1.0;
private static Grad[] grad3 = {new Grad(1, 1, 0), new Grad(-1, 1, 0), new Grad(1, -1, 0), new Grad(-1, -1, 0), new Grad(1, 0, 1), new Grad(-1, 0, 1), new Grad(1, 0, -1), new Grad(-1, 0, -1), new Grad(0, 1, 1), new Grad(0, -1, 1), new Grad(0, 1, -1), new Grad(0, -1, -1)};
protected final int[] permMod12 = new int[512];
/**
* Creates a simplex noise generator.
*
* @param rand
* the PRNG to use
*/
public SNG(Random rand)
{
super(rand);
for(int i = 0; i < 512; i++)
{
permMod12[i] = perm[i] % 12;
}
}
public static int floor(double x)
{
return x > 0 ? (int) x : (int) x - 1;
}
protected static double dot(Grad g, double x, double y)
{
return g.x * x + g.y * y;
}
protected static double dot(Grad g, double x, double y, double z)
{
return g.x * x + g.y * y + g.z * z;
}
@Override
protected double[] get2dNoise(double[] noise, double x, double z, int sizeX, int sizeY, double scaleX, double scaleY, double amplitude)
{
int index = 0;
for(int i = 0; i < sizeY; i++)
{
double zin = offsetY + (z + i) * scaleY;
for(int j = 0; j < sizeX; j++)
{
double xin = offsetX + (x + j) * scaleX;
noise[index++] += simplex2D(xin, zin) * amplitude;
}
}
return noise;
}
@Override
protected double[] get3dNoise(double[] noise, double x, double y, double z, int sizeX, int sizeY, int sizeZ, double scaleX, double scaleY, double scaleZ, double amplitude)
{
int index = 0;
for(int i = 0; i < sizeZ; i++)
{
double zin = offsetZ + (z + i) * scaleZ;
for(int j = 0; j < sizeX; j++)
{
double xin = offsetX + (x + j) * scaleX;
for(int k = 0; k < sizeY; k++)
{
double yin = offsetY + (y + k) * scaleY;
noise[index++] += simplex3D(xin, yin, zin) * amplitude;
}
}
}
return noise;
}
@Override
public double noise(double xin, double yin)
{
xin += offsetX;
yin += offsetY;
return simplex2D(xin, yin);
}
@Override
public double noise(double xin, double yin, double zin)
{
xin += offsetX;
yin += offsetY;
zin += offsetZ;
return simplex3D(xin, yin, zin);
}
private double simplex2D(double xin, double yin)
{
// Skew the input space to determine which simplex cell we're in
double s = (xin + yin) * F2; // Hairy factor for 2D
int i = floor(xin + s);
int j = floor(yin + s);
double t = (i + j) * G2;
double dx0 = i - t; // Unskew the cell origin back to (x,y) space
double dy0 = j - t;
double x0 = xin - dx0; // The x,y distances from the cell origin
double y0 = yin - dy0;
// For the 2D case, the simplex shape is an equilateral triangle.
// Determine which simplex we are in.
int i1; // Offsets for second (middle) corner of simplex in (i,j) coords
int j1;
if(x0 > y0)
{
i1 = 1; // lower triangle, XY order: (0,0)->(1,0)->(1,1)
j1 = 0;
}
else
{
i1 = 0; // upper triangle, YX order: (0,0)->(0,1)->(1,1)
j1 = 1;
}
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
// c = (3-sqrt(3))/6
double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
double y1 = y0 - j1 + G2;
double x2 = x0 + G22; // Offsets for last corner in (x,y) unskewed coords
double y2 = y0 + G22;
// Work out the hashed gradient indices of the three simplex corners
int ii = i & 255;
int jj = j & 255;
int gi0 = permMod12[ii + perm[jj]];
int gi1 = permMod12[ii + i1 + perm[jj + j1]];
int gi2 = permMod12[ii + 1 + perm[jj + 1]];
// Calculate the contribution from the three corners
double t0 = 0.5 - x0 * x0 - y0 * y0;
double n0;
if(t0 < 0)
{
n0 = 0.0;
}
else
{
t0 *= t0;
n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
}
double t1 = 0.5 - x1 * x1 - y1 * y1;
double n1;
if(t1 < 0)
{
n1 = 0.0;
}
else
{
t1 *= t1;
n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
}
double t2 = 0.5 - x2 * x2 - y2 * y2;
double n2;
if(t2 < 0)
{
n2 = 0.0;
}
else
{
t2 *= t2;
n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to return values in the interval [-1,1].
return 70.0 * (n0 + n1 + n2);
}
private double simplex3D(double xin, double yin, double zin)
{
// Skew the input space to determine which simplex cell we're in
double s = (xin + yin + zin) * F3; // Very nice and simple skew factor for 3D
int i = floor(xin + s);
int j = floor(yin + s);
int k = floor(zin + s);
double t = (i + j + k) * G3;
double dx0 = i - t; // Unskew the cell origin back to (x,y,z) space
double dy0 = j - t;
double dz0 = k - t;
// For the 3D case, the simplex shape is a slightly irregular tetrahedron.
int i1; // Offsets for second corner of simplex in (i,j,k) coords
int j1;
int k1;
int i2; // Offsets for third corner of simplex in (i,j,k) coords
int j2;
int k2;
double x0 = xin - dx0; // The x,y,z distances from the cell origin
double y0 = yin - dy0;
double z0 = zin - dz0;
// Determine which simplex we are in
if(x0 >= y0)
{
if(y0 >= z0)
{
i1 = 1; // X Y Z order
j1 = 0;
k1 = 0;
i2 = 1;
j2 = 1;
k2 = 0;
}
else if(x0 >= z0)
{
i1 = 1; // X Z Y order
j1 = 0;
k1 = 0;
i2 = 1;
j2 = 0;
k2 = 1;
}
else
{
i1 = 0; // Z X Y order
j1 = 0;
k1 = 1;
i2 = 1;
j2 = 0;
k2 = 1;
}
}
else
{ // x0<y0
if(y0 < z0)
{
i1 = 0; // Z Y X order
j1 = 0;
k1 = 1;
i2 = 0;
j2 = 1;
k2 = 1;
}
else if(x0 < z0)
{
i1 = 0; // Y Z X order
j1 = 1;
k1 = 0;
i2 = 0;
j2 = 1;
k2 = 1;
}
else
{
i1 = 0; // Y X Z order
j1 = 1;
k1 = 0;
i2 = 1;
j2 = 1;
k2 = 0;
}
}
// A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
// a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
// a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
// c = 1/6.
double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
double y1 = y0 - j1 + G3;
double z1 = z0 - k1 + G3;
double x2 = x0 - i2 + G32; // Offsets for third corner in (x,y,z) coords
double y2 = y0 - j2 + G32;
double z2 = z0 - k2 + G32;
// Work out the hashed gradient indices of the four simplex corners
int ii = i & 255;
int jj = j & 255;
int kk = k & 255;
int gi0 = permMod12[ii + perm[jj + perm[kk]]];
int gi1 = permMod12[ii + i1 + perm[jj + j1 + perm[kk + k1]]];
int gi2 = permMod12[ii + i2 + perm[jj + j2 + perm[kk + k2]]];
int gi3 = permMod12[ii + 1 + perm[jj + 1 + perm[kk + 1]]];
// Calculate the contribution from the four corners
double t0 = 0.5 - x0 * x0 - y0 * y0 - z0 * z0;
double n0; // Noise contributions from the four corners
if(t0 < 0)
{
n0 = 0.0;
}
else
{
t0 *= t0;
n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
}
double t1 = 0.5 - x1 * x1 - y1 * y1 - z1 * z1;
double n1;
if(t1 < 0)
{
n1 = 0.0;
}
else
{
t1 *= t1;
n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
}
double t2 = 0.5 - x2 * x2 - y2 * y2 - z2 * z2;
double n2;
if(t2 < 0)
{
n2 = 0.0;
}
else
{
t2 *= t2;
n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
}
double x3 = x0 + G33; // Offsets for last corner in (x,y,z) coords
double y3 = y0 + G33;
double z3 = z0 + G33;
double t3 = 0.5 - x3 * x3 - y3 * y3 - z3 * z3;
double n3;
if(t3 < 0)
{
n3 = 0.0;
}
else
{
t3 *= t3;
n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to stay just inside [-1,1]
return 32.0 * (n0 + n1 + n2 + n3);
}
// Inner class to speed up gradient computations
// (array access is a lot slower than member access)
private static class Grad
{
public double x;
public double y;
public double z;
Grad(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
}