add offset to all noise types to prevent common value between seeds

This commit is contained in:
Julian Krings 2025-06-05 15:25:57 +02:00
parent 5705caa1ba
commit 4387aa814b
No known key found for this signature in database
GPG Key ID: 208C6E08C3B718D2
3 changed files with 71 additions and 1 deletions

View File

@ -39,4 +39,8 @@ public interface NoiseGenerator {
default ProceduralStream<Double> stream() {
return ProceduralStream.of(this::noise, this::noise, Interpolated.DOUBLE);
}
default OffsetNoiseGenerator offset(long seed) {
return new OffsetNoiseGenerator(this, seed);
}
}

View File

@ -18,7 +18,9 @@
package com.volmit.iris.util.noise;
import com.volmit.iris.util.collection.KSet;
import com.volmit.iris.util.interpolation.InterpolationMethod;
import com.volmit.iris.util.math.RNG;
public enum NoiseType {
WHITE(WhiteNoise::new),
@ -77,6 +79,24 @@ public enum NoiseType {
}
public NoiseGenerator create(long seed) {
return f.create(seed);
return f.create(seed).offset(seed);
}
public static void main(String[] args) {
long[] seeds = new long[10000];
for (int i = 0; i < seeds.length; i++) {
seeds[i] = RNG.r.lmax();
}
for (NoiseType t : NoiseType.values()) {
KSet<Double> set = new KSet<>();
for (long seed : seeds) {
set.add(t.create(seed).noise(0, 0));
}
if (set.size() == 1) {
System.out.println(t + " " + set);
}
}
}
}

View File

@ -0,0 +1,46 @@
package com.volmit.iris.util.noise;
import com.volmit.iris.util.math.RNG;
import org.jetbrains.annotations.NotNull;
public class OffsetNoiseGenerator implements NoiseGenerator {
private final NoiseGenerator base;
private final double ox, oz;
public OffsetNoiseGenerator(NoiseGenerator base, long seed) {
this.base = base;
RNG rng = new RNG(seed);
ox = rng.nextDouble(Integer.MIN_VALUE, Integer.MAX_VALUE);
oz = rng.nextDouble(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
@Override
public double noise(double x) {
return base.noise(x + ox);
}
@Override
public double noise(double x, double z) {
return base.noise(x + ox, z + oz);
}
@Override
public double noise(double x, double y, double z) {
return base.noise(x + ox, y, z + oz);
}
@Override
public boolean isNoScale() {
return base.isNoScale();
}
@Override
public boolean isStatic() {
return base.isStatic();
}
@NotNull
public NoiseGenerator getBase() {
return base;
}
}