diff --git a/src/main/java/com/volmit/iris/NoiseView.java b/src/main/java/com/volmit/iris/NoiseView.java index 12e7b4040..881ef6f19 100644 --- a/src/main/java/com/volmit/iris/NoiseView.java +++ b/src/main/java/com/volmit/iris/NoiseView.java @@ -11,7 +11,6 @@ import javax.swing.JPanel; import com.volmit.iris.noise.CNG; import com.volmit.iris.object.NoiseStyle; -import com.volmit.iris.util.Form; import com.volmit.iris.util.GroupedExecutor; import com.volmit.iris.util.M; import com.volmit.iris.util.PrecisionStopwatch; @@ -23,7 +22,7 @@ public class NoiseView extends JPanel { private static final long serialVersionUID = 2094606939770332040L; RollingSequence r = new RollingSequence(256); - CNG cng = NoiseStyle.CELLULAR_IRIS_DOUBLE.create(new RNG(RNG.r.nextLong())).scale(0.25); + CNG cng = NoiseStyle.PERLIN_IRIS.create(new RNG(RNG.r.nextLong())).scale(0.25); GroupedExecutor gx = new GroupedExecutor(Runtime.getRuntime().availableProcessors(), Thread.MAX_PRIORITY, "Iris Renderer"); ReentrantLock l = new ReentrantLock(); @@ -87,7 +86,6 @@ public class NoiseView extends JPanel { p.end(); r.put(p.getMilliseconds()); - System.out.println("Accuracy: " + accuracy + " MS: " + Form.duration(r.getAverage(), 2)); EventQueue.invokeLater(() -> { repaint(); diff --git a/src/main/java/com/volmit/iris/noise/FractalCubicNoise.java b/src/main/java/com/volmit/iris/noise/FractalCubicNoise.java new file mode 100644 index 000000000..0e5e9fefb --- /dev/null +++ b/src/main/java/com/volmit/iris/noise/FractalCubicNoise.java @@ -0,0 +1,31 @@ +package com.volmit.iris.noise; + +import com.volmit.iris.noise.FastNoise.FractalType; + +public class FractalCubicNoise implements NoiseGenerator { + private final FastNoise n; + + public FractalCubicNoise(long seed) { + this.n = new FastNoise((int) seed); + n.SetFractalType(FractalType.Billow); + } + + private double f(double n) { + return (n / 2D) + 0.5D; + } + + @Override + public double noise(double x) { + return f(n.GetCubicFractal((float) x, 0)); + } + + @Override + public double noise(double x, double z) { + return f(n.GetCubicFractal((float) x, (float) z)); + } + + @Override + public double noise(double x, double y, double z) { + return f(n.GetCubicFractal((float) x, (float) y, (float) z)); + } +} diff --git a/src/main/java/com/volmit/iris/noise/NoiseType.java b/src/main/java/com/volmit/iris/noise/NoiseType.java index d881603d6..fdd4f8a27 100644 --- a/src/main/java/com/volmit/iris/noise/NoiseType.java +++ b/src/main/java/com/volmit/iris/noise/NoiseType.java @@ -3,12 +3,14 @@ package com.volmit.iris.noise; public enum NoiseType { WHITE(seed -> new WhiteNoise(seed)), SIMPLEX(seed -> new SimplexNoise(seed)), + PERLIN(seed -> new PerlinNoise(seed)), FRACTAL_BILLOW_SIMPLEX(seed -> new FractalBillowSimplexNoise(seed)), FRACTAL_FBM_SIMPLEX(seed -> new FractalFBMSimplexNoise(seed)), FRACTAL_RIGID_MULTI_SIMPLEX(seed -> new FractalRigidMultiSimplexNoise(seed)), CELLULAR(seed -> new CellularNoise(seed)), GLOB(seed -> new GlobNoise(seed)), CUBIC(seed -> new CubicNoise(seed)), + FRACTAL_CUBIC(seed -> new FractalCubicNoise(seed)), CELLULAR_HEIGHT(seed -> new CellHeightNoise(seed)), VASCULAR(seed -> new VascularNoise(seed)); diff --git a/src/main/java/com/volmit/iris/noise/PerlinNoise.java b/src/main/java/com/volmit/iris/noise/PerlinNoise.java index f450fa235..2c41ae05c 100644 --- a/src/main/java/com/volmit/iris/noise/PerlinNoise.java +++ b/src/main/java/com/volmit/iris/noise/PerlinNoise.java @@ -1,175 +1,77 @@ package com.volmit.iris.noise; -import java.util.Random; +import com.volmit.iris.util.RNG; -public class PerlinNoise extends BasePerlinNoiseGenerator -{ +public class PerlinNoise implements NoiseGenerator, OctaveNoise { + private final FastNoise n; + private int octaves; - /** - * 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 PerlinNoise(long seed) { + this.n = new FastNoise(new RNG(seed).imax()); + n.SetNoiseType(FastNoise.NoiseType.Perlin); + octaves = 1; } - public static int floor(double x) - { - int floored = (int) x; - return x < floored ? floored - 1 : floored; + public double f(double v) { + return (v / 2D) + 0.5D; } - /** - * 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); + @Override + public double noise(double x) { + if (octaves <= 1) { + return f(n.GetPerlin((float) x, 0f)); } - else - { - return get3dNoise(noise, x, y, z, sizeX, sizeY, sizeZ, scaleX, scaleY, scaleZ, amplitude); + + double f = 1; + double m = 0; + double v = 0; + + for (int i = 0; i < octaves; i++) { + v += n.GetPerlin((float) (x * (f == 1 ? f++ : (f *= 2))), 0f) * f; + m += f; } + + return f(v / m); } - 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; - } + @Override + public double noise(double x, double z) { + if (octaves <= 1) { + return f(n.GetPerlin((float) x, (float) z)); } - return noise; + double f = 1; + double m = 0; + double v = 0; + + for (int i = 0; i < octaves; i++) { + f = f == 1 ? f + 1 : f * 2; + v += n.GetPerlin((float) (x * f), (float) (z * f)) * f; + m += f; + } + + return f(v / m); } - 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; - } - } + @Override + public double noise(double x, double y, double z) { + if (octaves <= 1) { + return f(n.GetPerlin((float) x, (float) y, (float) z)); } - return noise; + double f = 1; + double m = 0; + double v = 0; + + for (int i = 0; i < octaves; i++) { + f = f == 1 ? f + 1 : f * 2; + v += n.GetPerlin((float) (x * f), (float) (y * f), (float) (z * f)) * f; + m += f; + } + + return f(v / m); } -} \ No newline at end of file + + @Override + public void setOctaves(int o) { + octaves = o; + } +} diff --git a/src/main/java/com/volmit/iris/noise/SNG.java b/src/main/java/com/volmit/iris/noise/SNG.java deleted file mode 100644 index 1037613bb..000000000 --- a/src/main/java/com/volmit/iris/noise/SNG.java +++ /dev/null @@ -1,377 +0,0 @@ -package com.volmit.iris.noise; - -import java.util.Random; - -/** - * A speed-improved simplex noise algorithm. - * - *

- * 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. - * - *

- * 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 new CNG(rng, 1D, 1).scale(1)), + @Desc("Perlin. Like simplex but more natural") + @DontObfuscate + PERLIN(rng -> new CNG(rng, NoiseType.PERLIN, 1D, 1).scale(1.47)), + + @Desc("Perlin. Like simplex but more natural") + @DontObfuscate + PERLIN_IRIS(rng -> CNG.signature(rng, NoiseType.PERLIN).scale(1.47)), + + @Desc("Perlin. Like simplex but more natural") + @DontObfuscate + PERLIN_IRIS_HALF(rng -> CNG.signatureHalf(rng, NoiseType.PERLIN).scale(1.47)), + + @Desc("Perlin. Like simplex but more natural") + @DontObfuscate + PERLIN_IRIS_DOUBLE(rng -> CNG.signatureDouble(rng, NoiseType.PERLIN).scale(1.47)), + + @Desc("Perlin. Like simplex but more natural") + @DontObfuscate + PERLIN_IRIS_THICK(rng -> CNG.signatureThick(rng, NoiseType.PERLIN).scale(1.47)), + @Desc("Billow Fractal Simplex Noise. Single octave.") @DontObfuscate FRACTAL_BILLOW_SIMPLEX(rng -> new CNG(rng, NoiseType.FRACTAL_BILLOW_SIMPLEX, 1D, 1)), @@ -244,23 +264,55 @@ public enum NoiseStyle { @Desc("Cubic Noise") @DontObfuscate - LAVALAMP(rng -> new CNG(rng, NoiseType.CUBIC, 1D, 1).scale(256)), + CUBIC(rng -> new CNG(rng, NoiseType.CUBIC, 1D, 1).scale(256)), + + @Desc("Fractal Cubic Noise") + @DontObfuscate + FRACTAL_CUBIC(rng -> new CNG(rng, NoiseType.FRACTAL_CUBIC, 1D, 1).scale(2)), + + @Desc("Fractal Cubic Noise With Iris Swirls") + @DontObfuscate + FRACTAL_CUBIC_IRIS(rng -> CNG.signature(rng, NoiseType.FRACTAL_CUBIC).scale(2)), + + @Desc("Fractal Cubic Noise With Iris Swirls") + @DontObfuscate + FRACTAL_CUBIC_IRIS_THICK(rng -> CNG.signatureThick(rng, NoiseType.FRACTAL_CUBIC).scale(2)), + + @Desc("Fractal Cubic Noise With Iris Swirls") + @DontObfuscate + FRACTAL_CUBIC_IRIS_HALF(rng -> CNG.signatureHalf(rng, NoiseType.FRACTAL_CUBIC).scale(2)), + + @Desc("Fractal Cubic Noise With Iris Swirls") + @DontObfuscate + FRACTAL_CUBIC_IRIS_DOUBLE(rng -> CNG.signatureDouble(rng, NoiseType.FRACTAL_CUBIC).scale(2)), + + @Desc("Fractal Cubic Noise, 2 Octaves") + @DontObfuscate + BIOCTAVE_FRACTAL_CUBIC(rng -> new CNG(rng, NoiseType.FRACTAL_CUBIC, 1D, 2).scale(2)), + + @Desc("Fractal Cubic Noise, 3 Octaves") + @DontObfuscate + TRIOCTAVE_FRACTAL_CUBIC(rng -> new CNG(rng, NoiseType.FRACTAL_CUBIC, 1D, 3).scale(1.5)), + + @Desc("Fractal Cubic Noise, 4 Octaves") + @DontObfuscate + QUADOCTAVE_FRACTAL_CUBIC(rng -> new CNG(rng, NoiseType.FRACTAL_CUBIC, 1D, 4).scale(1)), @Desc("Cubic Noise") @DontObfuscate - LAVALAMP_IRIS(rng -> CNG.signature(rng, NoiseType.CUBIC).scale(256)), + CUBIC_IRIS(rng -> CNG.signature(rng, NoiseType.CUBIC).scale(256)), @Desc("Cubic Noise") @DontObfuscate - LAVALAMP_IRIS_HALF(rng -> CNG.signatureHalf(rng, NoiseType.CUBIC).scale(256)), + CUBIC_IRIS_HALF(rng -> CNG.signatureHalf(rng, NoiseType.CUBIC).scale(256)), @Desc("Cubic Noise") @DontObfuscate - LAVALAMP_IRIS_DOUBLE(rng -> CNG.signatureDouble(rng, NoiseType.CUBIC).scale(256)), + CUBIC_IRIS_DOUBLE(rng -> CNG.signatureDouble(rng, NoiseType.CUBIC).scale(256)), @Desc("Cubic Noise") @DontObfuscate - LAVALAMP_IRIS_THICK(rng -> CNG.signatureThick(rng, NoiseType.CUBIC).scale(256)), + CUBIC_IRIS_THICK(rng -> CNG.signatureThick(rng, NoiseType.CUBIC).scale(256)), @Desc("Cellular noise creates the same noise level for cells, changes noise level on cell borders.") @DontObfuscate