mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Add style seed and generator exponent
This commit is contained in:
1 parent
ed037ea40b
commit
53a74e546a
15 files changed
+514
-26
No files matched your search
@@ -520,15 +520,16 @@ public class IrisBiome extends IrisRegistrant implements Rarity {
|
||||
}
|
||||
|
||||
public CNG getChildrenGenerator(long seed, double scale) {
|
||||
double childScale = getChildStyle().isSeeded() ? 1 : scale;
|
||||
IrisData data = getLoader();
|
||||
Engine engine = data == null ? null : data.getEngine();
|
||||
CachedChildGenerator recent = recentChildGenerator;
|
||||
if (recent != null && recent.key.matches(seed, scale, data, engine)) {
|
||||
if (recent != null && recent.key.matches(seed, childScale, data, engine)) {
|
||||
return recent.generator;
|
||||
}
|
||||
ChildGeneratorKey key = new ChildGeneratorKey(seed, scale, data, engine);
|
||||
ChildGeneratorKey key = new ChildGeneratorKey(seed, childScale, data, engine);
|
||||
CNG generator = childrenGenerators.computeIfAbsent(key,
|
||||
ignored -> getChildStyle().createScaledGenerator(new RNG(seed), data, scale));
|
||||
ignored -> getChildStyle().createScaledGenerator(new RNG(seed), data, childScale));
|
||||
recentChildGenerator = new CachedChildGenerator(key, generator);
|
||||
return generator;
|
||||
}
|
||||
|
||||
@@ -227,10 +227,10 @@ final class IrisBiomeLayerGenerator {
|
||||
int layerIndex = findLayer(boundaries, position);
|
||||
IrisBiomePaletteLayer layer = layers.get(layerIndex);
|
||||
int offset = position - boundaries[layerIndex];
|
||||
double zoom = layer.getZoom();
|
||||
double blocksPerSample = layer.blocksPerSample();
|
||||
try {
|
||||
data.add(layer.get(random, layerIndex + offset,
|
||||
(wx + offset) / zoom, offset, (wz - offset) / zoom, rdata));
|
||||
(wx + offset) / blocksPerSample, offset, (wz - offset) / blocksPerSample, rdata));
|
||||
} catch (Throwable error) {
|
||||
GenerationFailures.skipPlacement(error, "surface layer", "block", (int) wx, height - depth, (int) wz, biome.getLoadKey());
|
||||
}
|
||||
@@ -327,11 +327,11 @@ final class IrisBiomeLayerGenerator {
|
||||
private static void appendLayer(KList<NativeBlockState> blocks, IrisBiomePaletteLayer layer,
|
||||
int layerIndex, int thickness, double x, double z,
|
||||
RNG random, int maxDepth, IrisData data) {
|
||||
double zoom = layer.getZoom();
|
||||
double blocksPerSample = layer.blocksPerSample();
|
||||
for (int offset = 0; offset < thickness && blocks.size() < maxDepth; offset++) {
|
||||
try {
|
||||
blocks.add(layer.get(random, layerIndex + offset,
|
||||
(x + offset) / zoom, offset, (z - offset) / zoom, data));
|
||||
(x + offset) / blocksPerSample, offset, (z - offset) / blocksPerSample, data));
|
||||
} catch (Throwable error) {
|
||||
GenerationFailures.skipPlacement(error, "surface layer", "block", (int) x, -1, (int) z, "none");
|
||||
}
|
||||
|
||||
@@ -133,11 +133,21 @@ public class IrisBiomePaletteLayer {
|
||||
return localBlockData.get(0);
|
||||
}
|
||||
|
||||
CNG generator = getLayerGenerator(parent, signature, data);
|
||||
if (style.isSeeded()) {
|
||||
return generator.fit(localBlockData, x, z);
|
||||
}
|
||||
|
||||
double localZoom = zoom;
|
||||
double scaledX = x / localZoom;
|
||||
double scaledY = y / localZoom;
|
||||
double scaledZ = z / localZoom;
|
||||
return getLayerGenerator(parent, signature, data).fit(localBlockData, scaledX, scaledY, scaledZ);
|
||||
return generator.fit(localBlockData, scaledX, scaledY, scaledZ);
|
||||
}
|
||||
|
||||
/** How many blocks one step of the layer's block noise spans: its zoom, or 1 for a seeded style, which is read flat. */
|
||||
public double blocksPerSample() {
|
||||
return style.isSeeded() ? 1 : zoom;
|
||||
}
|
||||
|
||||
public CNG getLayerGenerator(RNG rng, IrisData data) {
|
||||
|
||||
@@ -177,11 +177,17 @@ public class IrisDecorator {
|
||||
if (getBlockData(data).isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
double xx = x / style.getZoom();
|
||||
double zz = z / style.getZoom();
|
||||
double blocksPerSample = chanceBlocksPerSample();
|
||||
double xx = x / blocksPerSample;
|
||||
double zz = z / blocksPerSample;
|
||||
return getGenerator(rng, data).fitDouble(0D, 1D, xx, zz) <= chance;
|
||||
}
|
||||
|
||||
/** A style without a seed divides by its zoom here on top of its own scale; a seeded one is read in plain blocks. */
|
||||
private double chanceBlocksPerSample() {
|
||||
return style.isSeeded() ? 1 : style.getZoom();
|
||||
}
|
||||
|
||||
public NativeBlockState getBlockData(IrisBiome b, RNG rng, double x, double z, IrisData data) {
|
||||
if (!passesChanceGate(rng, x, z, data)) {
|
||||
return null;
|
||||
@@ -212,8 +218,9 @@ public class IrisDecorator {
|
||||
return getBlockData100(b, rng, x, y, z, data);
|
||||
}
|
||||
|
||||
double xx = x / style.getZoom();
|
||||
double zz = z / style.getZoom();
|
||||
double blocksPerSample = chanceBlocksPerSample();
|
||||
double xx = x / blocksPerSample;
|
||||
double zz = z / blocksPerSample;
|
||||
|
||||
if (getGenerator(rng, data).fitDouble(0D, 1D, xx, zz) <= chance) { //Exclude y from here
|
||||
if (topPalette.size() == 1) {
|
||||
|
||||
@@ -14,6 +14,7 @@ public final class GeneratorProgram {
|
||||
private final NoiseLayerProgram cliffs;
|
||||
private final boolean multiplicitive;
|
||||
private final double opacity;
|
||||
private final double exponent;
|
||||
private final double offsetX;
|
||||
private final double offsetZ;
|
||||
private final double zoom;
|
||||
@@ -30,6 +31,7 @@ public final class GeneratorProgram {
|
||||
this.cliffs = cliffs;
|
||||
this.multiplicitive = generator.isMultiplicitive();
|
||||
this.opacity = generator.getOpacity();
|
||||
this.exponent = generator.getExponent();
|
||||
this.offsetX = generator.getOffsetX();
|
||||
this.offsetZ = generator.getOffsetZ();
|
||||
this.zoom = generator.getZoom();
|
||||
@@ -116,7 +118,8 @@ public final class GeneratorProgram {
|
||||
double noise = layer.sample(scratch, sampleX, sampleZ);
|
||||
value = multiplicitive ? value * noise : value + noise;
|
||||
}
|
||||
value = multiplicitive ? value * opacity : (value / totalOpacity) * opacity;
|
||||
double combined = multiplicitive ? value : value / totalOpacity;
|
||||
value = IrisGenerator.applyExponent(combined, exponent) * opacity;
|
||||
if (Double.isNaN(value)) {
|
||||
value = 0D;
|
||||
}
|
||||
@@ -296,7 +299,8 @@ public final class GeneratorProgram {
|
||||
}
|
||||
}
|
||||
for (int index = 0; index < count; index++) {
|
||||
double value = multiplicitive ? out[index] * opacity : (out[index] / totalOpacity) * opacity;
|
||||
double combined = multiplicitive ? out[index] : out[index] / totalOpacity;
|
||||
double value = IrisGenerator.applyExponent(combined, exponent) * opacity;
|
||||
out[index] = Double.isNaN(value) ? 0D : value;
|
||||
}
|
||||
if (cliffs != null) {
|
||||
|
||||
@@ -76,6 +76,9 @@ public class IrisGenerator extends IrisRegistrant {
|
||||
@Description("Multiply the compsites instead of adding them")
|
||||
private boolean multiplicitive = false;
|
||||
@MinNumber(0.001)
|
||||
@Description("Curves the combined composite value (0-1) as value^exponent before opacity. Above 1 flattens lows and sharpens peaks, below 1 does the opposite. Unlike the exponent on a single noise generator, this acts on the combined result of all composites.")
|
||||
private double exponent = 1;
|
||||
@MinNumber(0.001)
|
||||
@Description("The size of the cell fractures")
|
||||
private double cellFractureZoom = 1D;
|
||||
@MinNumber(0)
|
||||
@@ -329,7 +332,8 @@ public class IrisGenerator extends IrisRegistrant {
|
||||
}
|
||||
}
|
||||
|
||||
double v = multiplicitive ? h * opacity : (h / tp) * opacity;
|
||||
double combined = multiplicitive ? h : h / tp;
|
||||
double v = applyExponent(combined, exponent) * opacity;
|
||||
|
||||
if (Double.isNaN(v)) {
|
||||
v = 0;
|
||||
@@ -341,6 +345,14 @@ public class IrisGenerator extends IrisRegistrant {
|
||||
return v;
|
||||
}
|
||||
|
||||
static double applyExponent(double value, double exponent) {
|
||||
if (exponent == 1) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return value < 0 ? -Math.pow(-value, exponent) : Math.pow(value, exponent);
|
||||
}
|
||||
|
||||
private double cachedSurfaceHeight(double x, double z, long superSeed, SurfaceCacheStripe[] stripes, IrisData data, Engine engine) {
|
||||
if (x != (int) x || z != (int) z) {
|
||||
return sampleHeight(x, z, superSeed, data, engine);
|
||||
|
||||
@@ -91,6 +91,8 @@ public class IrisGeneratorStyle {
|
||||
@MaxNumber(8192)
|
||||
@Description("If the cache size is set above 0, this generator will be cached")
|
||||
private int cacheSize = 0;
|
||||
@Description("Rolls this noise from the world seed and this number, so every style with the same seed shares one noise. Child biomes, palettes and decorator placement read a seeded style flat in blocks: zoom is its only scale, so a layer's zoom, childShrinkFactor and the decorator's extra zoom don't apply.")
|
||||
private Long seed = null;
|
||||
|
||||
public IrisGeneratorStyle(NoiseStyle s) {
|
||||
this.style = s;
|
||||
@@ -131,7 +133,34 @@ public class IrisGeneratorStyle {
|
||||
}
|
||||
|
||||
private int hash() {
|
||||
return Objects.hash(expression, imageMapHash(), multiplier, fracture != null ? fracture.hash() : 0, exponent, cacheSize, zoom, cellularZoom, cellularFrequency, style == null ? null : style.name());
|
||||
int unseededHash = Objects.hash(expression, imageMapHash(), multiplier, fracture != null ? fracture.hash() : 0, exponent, cacheSize, zoom, cellularZoom, cellularFrequency, style == null ? null : style.name());
|
||||
return isSeeded() ? Objects.hash(unseededHash, seed) : unseededHash;
|
||||
}
|
||||
|
||||
public boolean isSeeded() {
|
||||
return seed != null;
|
||||
}
|
||||
|
||||
/** The random a style is built from: the caller's, or for a seeded style one rolled from its seed and the world seed. */
|
||||
public RNG rollFor(RNG callerRandom, Engine engine) {
|
||||
if (!isSeeded()) {
|
||||
return callerRandom;
|
||||
}
|
||||
|
||||
return new RNG(rolledSeed(engine));
|
||||
}
|
||||
|
||||
private long rolledSeed(Engine engine) {
|
||||
long worldSeed = engine == null ? 0L : engine.getSeedManager().getSeed();
|
||||
return splitMix(worldSeed ^ splitMix(seed));
|
||||
}
|
||||
|
||||
// Spreads nearby seed numbers far apart; Iris's own seed forking only adds.
|
||||
private static long splitMix(long value) {
|
||||
long mixed = value + 0x9E3779B97F4A7C15L;
|
||||
mixed = (mixed ^ (mixed >>> 30)) * 0xBF58476D1CE4E5B9L;
|
||||
mixed = (mixed ^ (mixed >>> 27)) * 0x94D049BB133111EBL;
|
||||
return mixed ^ (mixed >>> 31);
|
||||
}
|
||||
|
||||
public int prebakeSignature() {
|
||||
@@ -175,8 +204,9 @@ public class IrisGeneratorStyle {
|
||||
}
|
||||
}
|
||||
|
||||
private CNG createNoCache(RNG rng, IrisData data, int octaveMultiplier, int fallbackCacheSize,
|
||||
private CNG createNoCache(RNG callerRandom, IrisData data, int octaveMultiplier, int fallbackCacheSize,
|
||||
boolean quietCacheLog, Engine engine) {
|
||||
RNG rng = rollFor(callerRandom, engine);
|
||||
requirePositiveFinite("zoom", zoom);
|
||||
requirePositiveFinite("exponent", exponent);
|
||||
if (!Double.isFinite(cellularFrequency) || cellularFrequency < 0D) {
|
||||
@@ -248,14 +278,15 @@ public class IrisGeneratorStyle {
|
||||
return cachedGenerator(rng.getSeed(), data, engine);
|
||||
}
|
||||
|
||||
private CNG cachedGenerator(long seed, IrisData data, Engine engine) {
|
||||
private CNG cachedGenerator(long callerSeed, IrisData data, Engine engine) {
|
||||
long generatorSeed = isSeeded() ? rolledSeed(engine) : callerSeed;
|
||||
CachedGenerator recent = recentGenerator;
|
||||
if (recent != null && recent.key.matches(data, engine, seed)) {
|
||||
if (recent != null && recent.key.matches(data, engine, generatorSeed)) {
|
||||
return recent.generator;
|
||||
}
|
||||
GeneratorCacheKey key = new GeneratorCacheKey(data, engine, seed);
|
||||
GeneratorCacheKey key = new GeneratorCacheKey(data, engine, generatorSeed);
|
||||
CNG generator = generatorCache.computeIfAbsent(key,
|
||||
ignored -> createNoCache(new RNG(seed), data, 1, 0, false, engine));
|
||||
ignored -> createNoCache(new RNG(generatorSeed), data, 1, 0, false, engine));
|
||||
recentGenerator = new CachedGenerator(key, generator);
|
||||
return generator;
|
||||
}
|
||||
|
||||
+1
-1
@@ -101,7 +101,7 @@ public class IrisFloatingChildBiomeModifier extends EngineAssignedModifier<Nativ
|
||||
}
|
||||
|
||||
try {
|
||||
generated.add(layer.get(random.nextParallelRNG(i + j), (wx + j) / layer.getZoom(), j, (wz - j) / layer.getZoom(), data));
|
||||
generated.add(layer.get(random.nextParallelRNG(i + j), (wx + j) / layer.blocksPerSample(), j, (wz - j) / layer.blocksPerSample(), data));
|
||||
} catch (Throwable e) {
|
||||
GenerationFailures.skipPlacement(e, "surface layer", "block", (int) wx, -1, (int) wz, "floating");
|
||||
}
|
||||
|
||||
+6
-2
@@ -87,7 +87,7 @@ public class IrisDimensionCarvingEntry {
|
||||
}
|
||||
|
||||
private CNG createChildrenGenerator(long childrenGeneratorSeed, IrisData data) {
|
||||
return getChildStyle().createScaledGenerator(deriveChildStyleSeed(childrenGeneratorSeed), data, childShrinkFactor());
|
||||
return getChildStyle().createScaledGenerator(deriveChildStyleSeed(childrenGeneratorSeed), data, childScale());
|
||||
}
|
||||
|
||||
long deriveChildrenGeneratorSeed(long worldSeed) {
|
||||
@@ -98,7 +98,11 @@ public class IrisDimensionCarvingEntry {
|
||||
return new RNG(childrenGeneratorSeed).nextParallelRNG(CHILDREN_SEED_SALT);
|
||||
}
|
||||
|
||||
private double childShrinkFactor() {
|
||||
private double childScale() {
|
||||
if (getChildStyle().isSeeded()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return Math.max(MINIMUM_CHILD_SHRINK_FACTOR, getChildShrinkFactor());
|
||||
}
|
||||
|
||||
|
||||
@@ -95,10 +95,15 @@ public class IrisMaterialPalette implements StructurePalette {
|
||||
return localBlockData.get(0);
|
||||
}
|
||||
|
||||
CNG generator = getLayerGenerator(rng, rdata);
|
||||
if (style.isSeeded()) {
|
||||
return generator.fit(localBlockData, x, z);
|
||||
}
|
||||
|
||||
double scaledX = x / zoom;
|
||||
double scaledY = y / zoom;
|
||||
double scaledZ = z / zoom;
|
||||
return getLayerGenerator(rng, rdata).fit(localBlockData, scaledX, scaledY, scaledZ);
|
||||
return generator.fit(localBlockData, scaledX, scaledY, scaledZ);
|
||||
}
|
||||
|
||||
public Optional<TileData> getTile(RNG rng, double x, double y, double z, IrisData rdata) {
|
||||
|
||||
@@ -63,6 +63,7 @@ public final class IrisBiomeLayerSlopeSourceTest {
|
||||
when(complex.terrainSurfaceSlope(12, 32, -8)).thenReturn(0D);
|
||||
when(complex.terrainSurfaceSlope(12, 96, -8)).thenReturn(5D);
|
||||
when(grass.getZoom()).thenReturn(1D);
|
||||
when(grass.blocksPerSample()).thenReturn(1D);
|
||||
when(grass.getMinHeight()).thenReturn(1);
|
||||
when(grass.getMaxHeight()).thenReturn(1);
|
||||
when(grass.getSlopeCondition()).thenReturn(new IrisSlopeClip(0D, 2.6D));
|
||||
@@ -103,6 +104,7 @@ public final class IrisBiomeLayerSlopeSourceTest {
|
||||
when(hostSlope.getDouble(12D, -8D)).thenReturn(0D);
|
||||
when(sourceSlope.getDouble(12D, -8D)).thenReturn(5D);
|
||||
when(paletteLayer.getZoom()).thenReturn(1D);
|
||||
when(paletteLayer.blocksPerSample()).thenReturn(1D);
|
||||
when(paletteLayer.getMinHeight()).thenReturn(1);
|
||||
when(paletteLayer.getMaxHeight()).thenReturn(1);
|
||||
when(paletteLayer.getSlopeCondition()).thenReturn(new IrisSlopeClip(5D, 5D));
|
||||
|
||||
@@ -32,6 +32,7 @@ public final class IrisSurfaceLayerFallbackTest {
|
||||
|
||||
private IrisBiome biome() {
|
||||
when(layer.getZoom()).thenReturn(1D);
|
||||
when(layer.blocksPerSample()).thenReturn(1D);
|
||||
when(layer.getMinHeight()).thenReturn(1);
|
||||
when(layer.getMaxHeight()).thenReturn(2);
|
||||
when(layer.getHeightGenerator(any(RNG.class), same(data))).thenReturn(heightGenerator);
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package art.arcane.iris.generation.noise;
|
||||
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class IrisGeneratorExponentTest {
|
||||
private static final long SUPER_SEED = 4242L;
|
||||
private static final double OPACITY = 0.8D;
|
||||
private static final double TOLERANCE = 1e-12D;
|
||||
private static final int POINT_COUNT = 400;
|
||||
|
||||
private final double[] xs = new double[POINT_COUNT];
|
||||
private final double[] zs = new double[POINT_COUNT];
|
||||
|
||||
public IrisGeneratorExponentTest() {
|
||||
SplittableRandom random = new SplittableRandom(0xE590L);
|
||||
for (int index = 0; index < POINT_COUNT; index++) {
|
||||
xs[index] = random.nextDouble(-3000D, 3000D);
|
||||
zs[index] = random.nextDouble(-3000D, 3000D);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exponentDefaultsToOne() {
|
||||
assertEquals(1D, new IrisGenerator().getExponent(), 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exponentOfOneKeepsOriginalValuesBitForBit() {
|
||||
for (boolean multiplicitive : new boolean[]{false, true}) {
|
||||
IrisGenerator unset = generator(multiplicitive, OPACITY);
|
||||
IrisGenerator explicit = generator(multiplicitive, OPACITY).setExponent(1D);
|
||||
IrisGenerator unitOpacity = generator(multiplicitive, 1D);
|
||||
double[] fromUnset = batch(unset);
|
||||
double[] fromExplicit = batch(explicit);
|
||||
for (int index = 0; index < POINT_COUNT; index++) {
|
||||
double original = unitOpacity.getHeight(xs[index], zs[index], SUPER_SEED) * OPACITY;
|
||||
assertBits(original, unset.getHeight(xs[index], zs[index], SUPER_SEED));
|
||||
assertBits(original, explicit.getHeight(xs[index], zs[index], SUPER_SEED));
|
||||
assertBits(original, fromUnset[index]);
|
||||
assertBits(original, fromExplicit[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scalarProgramAndBatchPathsApplySignedPowerBeforeOpacity() {
|
||||
for (double exponent : new double[]{3D, 0.5D}) {
|
||||
for (boolean multiplicitive : new boolean[]{false, true}) {
|
||||
IrisGenerator combined = generator(multiplicitive, 1D);
|
||||
IrisGenerator curved = generator(multiplicitive, OPACITY).setExponent(exponent);
|
||||
GeneratorProgram program = curved.compileProgram(SUPER_SEED);
|
||||
TerrainBatchScratch scratch = new TerrainBatchScratch();
|
||||
double[] batched = batch(curved);
|
||||
for (int index = 0; index < POINT_COUNT; index++) {
|
||||
double expected = signedPow(combined.getHeight(xs[index], zs[index], SUPER_SEED), exponent) * OPACITY;
|
||||
String point = "exponent " + exponent + " multiplicitive " + multiplicitive + " point " + index;
|
||||
assertEquals(point + " scalar", expected, curved.getHeight(xs[index], zs[index], SUPER_SEED), TOLERANCE);
|
||||
assertEquals(point + " program", expected, program.height(scratch, xs[index], zs[index]), TOLERANCE);
|
||||
assertEquals(point + " batch", expected, batched[index], TOLERANCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exponentChangesTheOutput() {
|
||||
IrisGenerator flat = generator(false, 1D);
|
||||
IrisGenerator curved = generator(false, 1D).setExponent(3D);
|
||||
boolean anyDifference = false;
|
||||
for (int index = 0; index < POINT_COUNT; index++) {
|
||||
anyDifference |= flat.getHeight(xs[index], zs[index], SUPER_SEED) != curved.getHeight(xs[index], zs[index], SUPER_SEED);
|
||||
}
|
||||
assertTrue(anyDifference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyExponentKeepsTheSignOfNegativeValues() {
|
||||
assertEquals(-8D, IrisGenerator.applyExponent(-2D, 3D), TOLERANCE);
|
||||
assertEquals(-Math.sqrt(2D), IrisGenerator.applyExponent(-2D, 0.5D), TOLERANCE);
|
||||
assertEquals(-0.3D, IrisGenerator.applyExponent(-0.3D, 1D), 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonExponentDeserializesIntoTheField() {
|
||||
IrisGenerator generator = new Gson().fromJson("{\"exponent\":3}", IrisGenerator.class);
|
||||
assertEquals(3D, generator.getExponent(), 0D);
|
||||
assertEquals(1D, new Gson().fromJson("{}", IrisGenerator.class).getExponent(), 0D);
|
||||
}
|
||||
|
||||
private double[] batch(IrisGenerator generator) {
|
||||
double[] out = new double[POINT_COUNT];
|
||||
generator.compileProgram(SUPER_SEED).heights(new TerrainBatchScratch(), xs, zs, POINT_COUNT, out);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static IrisGenerator generator(boolean multiplicitive, double opacity) {
|
||||
KList<IrisNoiseGenerator> composite = new KList<>();
|
||||
composite.add(new IrisNoiseGenerator().setStyle(NoiseStyle.SIMPLEX.style()).setSeed(11L).setZoom(1.3D).setOpacity(0.7D));
|
||||
composite.add(new IrisNoiseGenerator().setStyle(NoiseStyle.PERLIN.style()).setSeed(23L).setZoom(2.1D).setOpacity(1.4D));
|
||||
return new IrisGenerator().setComposite(composite)
|
||||
.setMultiplicitive(multiplicitive)
|
||||
.setSeed(5L)
|
||||
.setZoom(37D)
|
||||
.setOpacity(opacity);
|
||||
}
|
||||
|
||||
private static double signedPow(double value, double exponent) {
|
||||
return value < 0 ? -Math.pow(-value, exponent) : Math.pow(value, exponent);
|
||||
}
|
||||
|
||||
private static void assertBits(double expected, double actual) {
|
||||
assertEquals(Double.doubleToRawLongBits(expected), Double.doubleToRawLongBits(actual));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
package art.arcane.iris.generation.noise;
|
||||
|
||||
import art.arcane.iris.generation.biome.IrisBiome;
|
||||
import art.arcane.iris.generation.runtime.Engine;
|
||||
import art.arcane.iris.generation.runtime.SeedManager;
|
||||
import art.arcane.iris.generation.terrain.IrisDimensionCarvingEntry;
|
||||
import art.arcane.iris.pack.loading.IrisData;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import art.arcane.volmlib.util.noise.CNG;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class IrisGeneratorStyleSeedTest {
|
||||
private static final long WORLD_SEED = 7_777_123L;
|
||||
private static final long OTHER_WORLD_SEED = -90_210L;
|
||||
private static final long STYLE_SEED = 510L;
|
||||
private static final long FRACTURE_SEED = 77L;
|
||||
private static final long CALLER_SEED = 4242L;
|
||||
private static final int GRID_SIZE = 36;
|
||||
private static final double GRID_STEP_BLOCKS = 53.7D;
|
||||
|
||||
@Test
|
||||
public void seededStyleIgnoresTheCallerRandom() {
|
||||
IrisData data = dataOfWorld(WORLD_SEED);
|
||||
IrisGeneratorStyle style = seededSimplex(STYLE_SEED);
|
||||
|
||||
assertArrayEquals(samples(style.createNoCache(new RNG(1L), data)), samples(style.createNoCache(new RNG(2L), data)), 0D);
|
||||
assertSame(style.create(new RNG(1L), data), style.create(new RNG(2L), data));
|
||||
assertSame(style.create(1L, data), style.create(new RNG(3L), data));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void separateStyleInstancesWithTheSameSeedShareOneNoise() {
|
||||
IrisData data = dataOfWorld(WORLD_SEED);
|
||||
CNG first = seededSimplex(STYLE_SEED).setZoom(2.5D).createNoCache(new RNG(1L), data);
|
||||
CNG second = seededSimplex(STYLE_SEED).setZoom(2.5D).createNoCache(new RNG(2L), data);
|
||||
CNG otherSeed = seededSimplex(STYLE_SEED + 1).setZoom(2.5D).createNoCache(new RNG(1L), data);
|
||||
|
||||
assertArrayEquals(samples(first), samples(second), 0D);
|
||||
assertFalse(java.util.Arrays.equals(samples(first), samples(otherSeed)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededStyleFollowsTheWorldSeed() {
|
||||
IrisGeneratorStyle style = seededSimplex(STYLE_SEED);
|
||||
|
||||
double[] world = samples(style.createNoCache(new RNG(1L), dataOfWorld(WORLD_SEED)));
|
||||
double[] otherWorld = samples(style.createNoCache(new RNG(1L), dataOfWorld(OTHER_WORLD_SEED)));
|
||||
|
||||
assertFalse(java.util.Arrays.equals(world, otherWorld));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generatorCacheNeverHandsBackAnotherWorldsNoise() {
|
||||
IrisGeneratorStyle style = seededSimplex(STYLE_SEED);
|
||||
IrisData world = dataOfWorld(WORLD_SEED);
|
||||
IrisData otherWorld = dataOfWorld(OTHER_WORLD_SEED);
|
||||
|
||||
CNG first = style.create(new RNG(1L), world);
|
||||
CNG second = style.create(new RNG(1L), otherWorld);
|
||||
|
||||
assertFalse(java.util.Arrays.equals(samples(first), samples(second)));
|
||||
assertSame(first, style.create(new RNG(1L), world));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededStyleEqualsTheUnseededCopyBuiltWithTheRolledRandom() {
|
||||
for (IrisData data : new IrisData[]{dataOfWorld(WORLD_SEED), dataWithoutEngine()}) {
|
||||
long worldSeed = worldSeedOf(data);
|
||||
IrisGeneratorStyle seeded = seededSimplex(STYLE_SEED).setZoom(3D).setExponent(1.5D);
|
||||
IrisGeneratorStyle unseeded = simplex().setZoom(3D).setExponent(1.5D);
|
||||
|
||||
CNG expected = unseeded.createNoCache(new RNG(rolled(worldSeed, STYLE_SEED)), data);
|
||||
|
||||
assertArrayEquals(samples(expected), samples(seeded.createNoCache(new RNG(CALLER_SEED), data)), 0D);
|
||||
assertArrayEquals(samples(expected), samples(seeded.create(new RNG(CALLER_SEED), data)), 0D);
|
||||
assertArrayEquals(samples(expected), samples(seeded.createForLayer(new RNG(CALLER_SEED), data, 1)), 0D);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededStyleWithPlainFractureEqualsTheUnseededCopy() {
|
||||
for (IrisData data : new IrisData[]{dataOfWorld(WORLD_SEED), dataWithoutEngine()}) {
|
||||
long worldSeed = worldSeedOf(data);
|
||||
IrisGeneratorStyle seeded = seededSimplex(STYLE_SEED).setFracture(plainFracture());
|
||||
IrisGeneratorStyle unseeded = simplex().setFracture(plainFracture());
|
||||
|
||||
CNG expected = unseeded.createNoCache(new RNG(rolled(worldSeed, STYLE_SEED)), data);
|
||||
|
||||
assertArrayEquals(samples(expected), samples(seeded.createNoCache(new RNG(CALLER_SEED), data)), 0D);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededFractureRollsOnItsOwnUnderAnUnseededParent() {
|
||||
for (IrisData data : new IrisData[]{dataOfWorld(WORLD_SEED), dataWithoutEngine()}) {
|
||||
long worldSeed = worldSeedOf(data);
|
||||
IrisGeneratorStyle seededFracture = plainFracture().setSeed(FRACTURE_SEED);
|
||||
IrisGeneratorStyle actual = simplex().setFracture(seededFracture);
|
||||
|
||||
CNG expected = simplex().createNoCache(new RNG(CALLER_SEED), data);
|
||||
expected.fractureWith(plainFracture().createNoCache(new RNG(rolled(worldSeed, FRACTURE_SEED)), data), seededFracture.getMultiplier());
|
||||
|
||||
assertArrayEquals(samples(expected), samples(actual.createNoCache(new RNG(CALLER_SEED), data)), 0D);
|
||||
assertArrayEquals(samples(actual.createNoCache(new RNG(1L), data)), samples(actual.createNoCache(new RNG(1L), data)), 0D);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededFractureInsideASeededStyleRollsFromItsOwnSeed() {
|
||||
for (IrisData data : new IrisData[]{dataOfWorld(WORLD_SEED), dataWithoutEngine()}) {
|
||||
long worldSeed = worldSeedOf(data);
|
||||
IrisGeneratorStyle seededFracture = plainFracture().setSeed(FRACTURE_SEED);
|
||||
IrisGeneratorStyle actual = seededSimplex(STYLE_SEED).setFracture(seededFracture);
|
||||
|
||||
CNG expected = simplex().createNoCache(new RNG(rolled(worldSeed, STYLE_SEED)), data);
|
||||
expected.fractureWith(plainFracture().createNoCache(new RNG(rolled(worldSeed, FRACTURE_SEED)), data), seededFracture.getMultiplier());
|
||||
|
||||
assertArrayEquals(samples(expected), samples(actual.createNoCache(new RNG(CALLER_SEED), data)), 0D);
|
||||
assertArrayEquals(samples(expected), samples(actual.create(new RNG(CALLER_SEED), data)), 0D);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rollForPinsTheRolledRandomSeed() {
|
||||
IrisGeneratorStyle style = seededSimplex(510L);
|
||||
assertEquals(1582317604285492835L, style.rollFor(new RNG(1L), engineOfWorld(0L)).getSeed());
|
||||
assertEquals(1582317604285492835L, style.rollFor(new RNG(2L), null).getSeed());
|
||||
|
||||
IrisGeneratorStyle negativeSeed = seededSimplex(-7L);
|
||||
assertEquals(-8111716878092126206L, negativeSeed.rollFor(new RNG(1L), engineOfWorld(123456789L)).getSeed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rollForKeepsTheCallerRandomOfAnUnseededStyle() {
|
||||
RNG caller = new RNG(CALLER_SEED);
|
||||
|
||||
assertSame(caller, simplex().rollFor(caller, engineOfWorld(WORLD_SEED)));
|
||||
assertSame(caller, simplex().rollFor(caller, null));
|
||||
assertFalse(simplex().isSeeded());
|
||||
assertTrue(seededSimplex(0L).isSeeded());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unseededStylesHashExactlyAsBeforeTheSeedField() {
|
||||
int simplexHash = Objects.hash((Object) null, 0, 1D, 0, 1D, 0, 1D, 1D, 0D, "SIMPLEX");
|
||||
int fractureHash = Objects.hash((Object) null, 0, 1D, 0, 1D, 0, 1D, 1D, 0D, "STATIC");
|
||||
|
||||
assertEquals(simplexHash, simplex().prebakeSignature());
|
||||
assertEquals(Objects.hash((Object) null, 0, 1D, fractureHash, 1D, 0, 1D, 1D, 0D, "SIMPLEX"),
|
||||
simplex().setFracture(new IrisGeneratorStyle(NoiseStyle.STATIC)).prebakeSignature());
|
||||
assertEquals(Objects.hash("expr", 0, 2D, 0, 3D, 8, 4D, 5D, 0.5D, null),
|
||||
new IrisGeneratorStyle().setStyle(null).setExpression("expr").setMultiplier(2D).setExponent(3D)
|
||||
.setCacheSize(8).setZoom(4D).setCellularZoom(5D).setCellularFrequency(0.5D).prebakeSignature());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seedChangesTheSignatureOnlyWhenSet() {
|
||||
int unseeded = simplex().prebakeSignature();
|
||||
|
||||
assertNotEquals(unseeded, seededSimplex(STYLE_SEED).prebakeSignature());
|
||||
assertNotEquals(seededSimplex(STYLE_SEED).prebakeSignature(), seededSimplex(STYLE_SEED + 1).prebakeSignature());
|
||||
assertEquals(seededSimplex(STYLE_SEED).prebakeSignature(), seededSimplex(STYLE_SEED).prebakeSignature());
|
||||
assertNotEquals(unseeded, simplex().setFracture(plainFracture().setSeed(FRACTURE_SEED)).prebakeSignature());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seedReadsFromJsonAndDefaultsToNull() {
|
||||
Gson gson = new Gson();
|
||||
|
||||
assertEquals(Long.valueOf(510L), gson.fromJson("{\"seed\": 510}", IrisGeneratorStyle.class).getSeed());
|
||||
assertEquals(Long.valueOf(-9_007_199_254_740_993L), gson.fromJson("{\"seed\": -9007199254740993}", IrisGeneratorStyle.class).getSeed());
|
||||
assertNull(gson.fromJson("{\"zoom\": 2}", IrisGeneratorStyle.class).getSeed());
|
||||
assertNull(new IrisGeneratorStyle().getSeed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededChildStyleIgnoresTheChildScale() {
|
||||
IrisBiome biome = biomeWithChildStyle(seededSimplex(STYLE_SEED), dataOfWorld(WORLD_SEED));
|
||||
|
||||
double[] unscaled = samples(biome.getChildrenGenerator(CALLER_SEED, 1D));
|
||||
|
||||
assertArrayEquals(unscaled, samples(biome.getChildrenGenerator(CALLER_SEED, 4D)), 0D);
|
||||
assertArrayEquals(unscaled, samples(biome.getChildrenGenerator(CALLER_SEED, 0.25D)), 0D);
|
||||
assertArrayEquals(unscaled, samples(biome.getChildrenGenerator(new RNG(5L), 123, 9D)), 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unseededChildStyleStillScales() {
|
||||
IrisBiome biome = biomeWithChildStyle(simplex(), dataOfWorld(WORLD_SEED));
|
||||
|
||||
double[] unscaled = samples(biome.getChildrenGenerator(CALLER_SEED, 1D));
|
||||
double[] scaled = samples(biome.getChildrenGenerator(CALLER_SEED, 4D));
|
||||
|
||||
assertFalse(java.util.Arrays.equals(unscaled, scaled));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededCarvingChildStyleIgnoresTheShrinkFactor() {
|
||||
IrisData data = dataOfWorld(WORLD_SEED);
|
||||
|
||||
double[] unscaled = samples(carvingEntry(seededSimplex(STYLE_SEED), 1D).getChildrenGenerator(CALLER_SEED, data));
|
||||
|
||||
assertArrayEquals(unscaled, samples(carvingEntry(seededSimplex(STYLE_SEED), 4D).getChildrenGenerator(CALLER_SEED, data)), 0D);
|
||||
assertFalse(java.util.Arrays.equals(
|
||||
samples(carvingEntry(simplex(), 1D).getChildrenGenerator(CALLER_SEED, data)),
|
||||
samples(carvingEntry(simplex(), 4D).getChildrenGenerator(CALLER_SEED, data))));
|
||||
}
|
||||
|
||||
private static IrisDimensionCarvingEntry carvingEntry(IrisGeneratorStyle childStyle, double shrinkFactor) {
|
||||
return new IrisDimensionCarvingEntry().setId("deep-caves").setChildStyle(childStyle).setChildShrinkFactor(shrinkFactor);
|
||||
}
|
||||
|
||||
private static IrisBiome biomeWithChildStyle(IrisGeneratorStyle childStyle, IrisData data) {
|
||||
IrisBiome biome = new IrisBiome().setChildStyle(childStyle);
|
||||
biome.setLoader(data);
|
||||
return biome;
|
||||
}
|
||||
|
||||
private static IrisGeneratorStyle simplex() {
|
||||
return new IrisGeneratorStyle(NoiseStyle.SIMPLEX);
|
||||
}
|
||||
|
||||
private static IrisGeneratorStyle seededSimplex(long seed) {
|
||||
return simplex().setSeed(seed);
|
||||
}
|
||||
|
||||
private static IrisGeneratorStyle plainFracture() {
|
||||
return new IrisGeneratorStyle(NoiseStyle.SIMPLEX).setZoom(2D).setMultiplier(30D);
|
||||
}
|
||||
|
||||
private static Engine engineOfWorld(long worldSeed) {
|
||||
Engine engine = mock(Engine.class);
|
||||
when(engine.getSeedManager()).thenReturn(new SeedManager(worldSeed));
|
||||
return engine;
|
||||
}
|
||||
|
||||
private static IrisData dataOfWorld(long worldSeed) {
|
||||
IrisData data = mock(IrisData.class);
|
||||
Engine engine = engineOfWorld(worldSeed);
|
||||
when(data.getEngine()).thenReturn(engine);
|
||||
return data;
|
||||
}
|
||||
|
||||
private static IrisData dataWithoutEngine() {
|
||||
return mock(IrisData.class);
|
||||
}
|
||||
|
||||
private static long worldSeedOf(IrisData data) {
|
||||
Engine engine = data.getEngine();
|
||||
return engine == null ? 0L : engine.getSeedManager().getSeed();
|
||||
}
|
||||
|
||||
private static long rolled(long worldSeed, long styleSeed) {
|
||||
return mix(worldSeed ^ mix(styleSeed));
|
||||
}
|
||||
|
||||
private static long mix(long value) {
|
||||
long mixed = value + 0x9E3779B97F4A7C15L;
|
||||
mixed = (mixed ^ (mixed >>> 30)) * 0xBF58476D1CE4E5B9L;
|
||||
mixed = (mixed ^ (mixed >>> 27)) * 0x94D049BB133111EBL;
|
||||
return mixed ^ (mixed >>> 31);
|
||||
}
|
||||
|
||||
private static double[] samples(CNG generator) {
|
||||
double[] sampled = new double[GRID_SIZE * GRID_SIZE * 2];
|
||||
int cursor = 0;
|
||||
for (int xIndex = 0; xIndex < GRID_SIZE; xIndex++) {
|
||||
for (int zIndex = 0; zIndex < GRID_SIZE; zIndex++) {
|
||||
double x = (xIndex - GRID_SIZE / 2) * GRID_STEP_BLOCKS + 0.31D;
|
||||
double z = (zIndex - GRID_SIZE / 2) * GRID_STEP_BLOCKS - 0.77D;
|
||||
sampled[cursor++] = generator.noise(x, z);
|
||||
sampled[cursor++] = generator.noise(x, 41.5D, z);
|
||||
}
|
||||
}
|
||||
return sampled;
|
||||
}
|
||||
}
|
||||
@@ -84,7 +84,7 @@ public class SchemaBuilderIdentityTest {
|
||||
@ClassRule
|
||||
public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean();
|
||||
|
||||
private static final String EXPECTED_SCHEMA_DIGEST = "89641e077a85bec8a27681f2ce6166150cc37277bc6f58793081b669432782f2";
|
||||
private static final String EXPECTED_SCHEMA_DIGEST = "be9aec54b8fc72b55a7f644e7683391dba83ed148bdd3a80eafd6982367d5f48";
|
||||
|
||||
private static final List<Class<?>> SCHEMA_ROOTS = List.of(
|
||||
IrisDimension.class,
|
||||
|
||||
Reference in new issue
Block a user