mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-31 04:46:40 +00:00
New Noise System
This commit is contained in:
@@ -1,31 +1,30 @@
|
||||
package com.volmit.iris.noise;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.volmit.iris.util.IrisInterpolation;
|
||||
import com.volmit.iris.util.KList;
|
||||
import com.volmit.iris.util.NoiseInjector;
|
||||
import com.volmit.iris.util.RNG;
|
||||
|
||||
public class CNG
|
||||
{
|
||||
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;
|
||||
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 scale;
|
||||
private double fscale;
|
||||
private KList<CNG> children;
|
||||
private CNG fracture;
|
||||
private SNG generator;
|
||||
private NoiseGenerator generator;
|
||||
private final double opacity;
|
||||
private NoiseInjector injector;
|
||||
private RNG rng;
|
||||
@@ -35,51 +34,52 @@ public class CNG
|
||||
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 static CNG signature(RNG rng) {
|
||||
return signature(rng, NoiseType.SIMPLEX);
|
||||
}
|
||||
|
||||
public CNG(RNG random)
|
||||
{
|
||||
public static CNG signature(RNG rng, NoiseType t) {
|
||||
// @builder
|
||||
return new CNG(rng.nextParallelRNG(17), t, 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)
|
||||
{
|
||||
public CNG(RNG random, int octaves) {
|
||||
this(random, 1D, octaves);
|
||||
}
|
||||
|
||||
public CNG(RNG random, double opacity, int octaves)
|
||||
{
|
||||
creates += octaves;
|
||||
public CNG(RNG random, double opacity, int octaves) {
|
||||
this(random, NoiseType.SIMPLEX, opacity, octaves);
|
||||
}
|
||||
|
||||
public CNG(RNG random, NoiseType t, double opacity, int octaves) {
|
||||
creates++;
|
||||
this.oct = octaves;
|
||||
this.rng = random;
|
||||
power = 1;
|
||||
freq = 1;
|
||||
amp = 1;
|
||||
scale = 1;
|
||||
patch = 1;
|
||||
fscale = 1;
|
||||
fracture = null;
|
||||
generator = new SNG(random);
|
||||
generator = t.create(random.nextParallelRNG(33).lmax());
|
||||
this.opacity = opacity;
|
||||
this.injector = ADD;
|
||||
|
||||
if (generator instanceof OctaveNoise) {
|
||||
((OctaveNoise) generator).setOctaves(octaves);
|
||||
}
|
||||
}
|
||||
|
||||
public CNG child(CNG c)
|
||||
{
|
||||
if(children == null)
|
||||
{
|
||||
public CNG child(CNG c) {
|
||||
if (children == null) {
|
||||
children = new KList<>();
|
||||
}
|
||||
|
||||
@@ -88,69 +88,71 @@ public class CNG
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public RNG nextRNG()
|
||||
{
|
||||
public RNG nextRNG() {
|
||||
return getRNG().nextRNG();
|
||||
}
|
||||
|
||||
public RNG getRNG()
|
||||
{
|
||||
public RNG getRNG() {
|
||||
return rng;
|
||||
}
|
||||
|
||||
public CNG fractureWith(CNG c, double scale)
|
||||
{
|
||||
public CNG fractureWith(CNG c, double scale) {
|
||||
fracture = c;
|
||||
fscale = scale;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CNG scale(double c)
|
||||
{
|
||||
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)
|
||||
{
|
||||
public CNG patch(double c) {
|
||||
patch = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CNG up(double c)
|
||||
{
|
||||
public CNG up(double c) {
|
||||
up = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CNG down(double c)
|
||||
{
|
||||
public CNG down(double c) {
|
||||
down = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CNG injectWith(NoiseInjector i)
|
||||
{
|
||||
public CNG injectWith(NoiseInjector i) {
|
||||
injector = i;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int fit(int min, int max, double... dim)
|
||||
{
|
||||
if(min == max)
|
||||
{
|
||||
public <T> T fit(T[] v, double... dim) {
|
||||
if (v.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (v.length == 1) {
|
||||
return v[0];
|
||||
}
|
||||
|
||||
return v[fit(0, v.length - 1, dim)];
|
||||
}
|
||||
|
||||
public <T> T fit(List<T> v, double... dim) {
|
||||
if (v.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (v.size() == 1) {
|
||||
return v.get(0);
|
||||
}
|
||||
|
||||
return v.get(fit(0, v.size() - 1, dim));
|
||||
}
|
||||
|
||||
public int fit(int min, int max, double... dim) {
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
|
||||
@@ -159,10 +161,8 @@ public class CNG
|
||||
return (int) Math.round(IrisInterpolation.lerp(min, max, noise));
|
||||
}
|
||||
|
||||
public int fitDouble(double min, double max, double... dim)
|
||||
{
|
||||
if(min == max)
|
||||
{
|
||||
public int fitDouble(double min, double max, double... dim) {
|
||||
if (min == max) {
|
||||
return (int) Math.round(min);
|
||||
}
|
||||
|
||||
@@ -171,10 +171,8 @@ public class CNG
|
||||
return (int) Math.round(IrisInterpolation.lerp(min, max, noise));
|
||||
}
|
||||
|
||||
public double fitDoubleD(double min, double max, double... dim)
|
||||
{
|
||||
if(min == max)
|
||||
{
|
||||
public double fitDoubleD(double min, double max, double... dim) {
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
|
||||
@@ -183,10 +181,8 @@ public class CNG
|
||||
return IrisInterpolation.lerp(min, max, noise);
|
||||
}
|
||||
|
||||
public int fitDoubleExponent(double min, double max, double exponent, double... dim)
|
||||
{
|
||||
if(min == max)
|
||||
{
|
||||
public int fitDoubleExponent(double min, double max, double exponent, double... dim) {
|
||||
if (min == max) {
|
||||
return (int) Math.round(min);
|
||||
}
|
||||
|
||||
@@ -195,23 +191,20 @@ public class CNG
|
||||
return (int) Math.round(IrisInterpolation.lerp(min, max, exponent == 1 ? noise : Math.pow(noise, exponent)));
|
||||
}
|
||||
|
||||
public double noise(double... dim)
|
||||
{
|
||||
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;
|
||||
double n = generator.noise(x * scale, y * scale, z * scale) * opacity;
|
||||
n = power != 1D ? Math.pow(n, power) : n;
|
||||
double m = 1;
|
||||
hits += oct;
|
||||
if(children == null)
|
||||
{
|
||||
if (children == null) {
|
||||
return (n - down + up) * patch;
|
||||
}
|
||||
|
||||
for(CNG i : children)
|
||||
{
|
||||
for (CNG i : children) {
|
||||
double[] r = injector.combine(n, i.noise(dim));
|
||||
n = r[0];
|
||||
m += r[1];
|
||||
@@ -220,9 +213,13 @@ public class CNG
|
||||
return ((n / m) - down + up) * patch;
|
||||
}
|
||||
|
||||
public CNG pow(double power)
|
||||
{
|
||||
public CNG pow(double power) {
|
||||
this.power = power;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CNG oct(int octaves) {
|
||||
oct = octaves;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
9
src/main/java/com/volmit/iris/noise/CNGFactory.java
Normal file
9
src/main/java/com/volmit/iris/noise/CNGFactory.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.volmit.iris.noise;
|
||||
|
||||
import com.volmit.iris.util.RNG;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CNGFactory
|
||||
{
|
||||
CNG create(RNG seed);
|
||||
}
|
||||
6
src/main/java/com/volmit/iris/noise/OctaveNoise.java
Normal file
6
src/main/java/com/volmit/iris/noise/OctaveNoise.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.volmit.iris.noise;
|
||||
|
||||
public interface OctaveNoise
|
||||
{
|
||||
public void setOctaves(int o);
|
||||
}
|
||||
@@ -1,24 +1,79 @@
|
||||
package com.volmit.iris.noise;
|
||||
|
||||
public class SimplexNoise implements NoiseGenerator {
|
||||
public class SimplexNoise implements NoiseGenerator, OctaveNoise {
|
||||
private final OpenSimplex n;
|
||||
private int octaves;
|
||||
|
||||
public SimplexNoise(long seed) {
|
||||
this.n = new OpenSimplex(seed);
|
||||
octaves = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double noise(double x) {
|
||||
return (n.noise2(x, 0) / 2D) + 0.5D;
|
||||
if (octaves <= 1) {
|
||||
return (n.noise2_XBeforeY(x, 0) / 2D) + 0.5D;
|
||||
}
|
||||
|
||||
double result = 0;
|
||||
double amp = 1;
|
||||
double freq = 1;
|
||||
double max = 0;
|
||||
|
||||
for (int i = 0; i < octaves; i++) {
|
||||
result += ((n.noise2_XBeforeY(x * freq, 0) * amp) / 2D) + 0.5D;
|
||||
max += amp;
|
||||
freq *= 2;
|
||||
amp *= 2;
|
||||
}
|
||||
|
||||
return result / max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double noise(double x, double z) {
|
||||
return (n.noise2(x, z) / 2D) + 0.5D;
|
||||
if (octaves <= 1) {
|
||||
return (n.noise2(x, z) / 2D) + 0.5D;
|
||||
}
|
||||
|
||||
double result = 0;
|
||||
double amp = 1;
|
||||
double freq = 1;
|
||||
double max = 0;
|
||||
|
||||
for (int i = 0; i < octaves; i++) {
|
||||
result += ((n.noise2(x * freq, z * freq) * amp) / 2D) + 0.5D;
|
||||
max += amp;
|
||||
freq *= 2;
|
||||
amp *= 2;
|
||||
}
|
||||
|
||||
return result / max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double noise(double x, double y, double z) {
|
||||
return (n.noise3_XZBeforeY(x, y, z) / 2D) + 0.5D;
|
||||
if (octaves <= 1) {
|
||||
return (n.noise3_XZBeforeY(x, y, z) / 2D) + 0.5D;
|
||||
}
|
||||
|
||||
double result = 0;
|
||||
double amp = 1;
|
||||
double freq = 1;
|
||||
double max = 0;
|
||||
|
||||
for (int i = 0; i < octaves; i++) {
|
||||
result += ((n.noise3_XZBeforeY(x * freq, y * freq, z * freq) * amp) / 2D) + 0.5D;
|
||||
max += amp;
|
||||
freq *= 2;
|
||||
amp *= 2;
|
||||
}
|
||||
|
||||
return result / max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOctaves(int o) {
|
||||
octaves = o;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user