Add style seed and generator exponent (#1268)

- Add seed to noise styles, rolled from the world seed
- Read seeded styles flat in child, palette and decorator lookups
- Add exponent curve to generators on scalar and batch paths
- Reject generator exponents at or below zero
- Warn in datapack export when seed or exponent is dropped
This commit is contained in:
Piexel authored and GitHub committed 2026-11-27 13:44:03 -05:00
1 parent 4c90095361
commit d7c5526eab
17 files changed
+550 -25

No files matched your search

@@ -65,6 +65,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));
@@ -106,6 +107,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));
@@ -34,6 +34,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), MockPackData.runtimeOf(data))).thenReturn(heightGenerator);
@@ -0,0 +1,133 @@
package art.arcane.iris.generation.noise;
import art.arcane.iris.generation.runtime.PackRuntime;
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.assertThrows;
import static org.junit.Assert.assertTrue;
public class IrisGeneratorExponentTest {
private static final PackRuntime RUNTIME = PackRuntime.standalone(null);
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(RUNTIME, xs[index], zs[index], SUPER_SEED) * OPACITY;
assertBits(original, unset.getHeight(RUNTIME, xs[index], zs[index], SUPER_SEED));
assertBits(original, explicit.getHeight(RUNTIME, 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(RUNTIME, SUPER_SEED);
TerrainBatchScratch scratch = new TerrainBatchScratch();
double[] batched = batch(curved);
for (int index = 0; index < POINT_COUNT; index++) {
double expected = signedPow(combined.getHeight(RUNTIME, xs[index], zs[index], SUPER_SEED), exponent) * OPACITY;
String point = "exponent " + exponent + " multiplicitive " + multiplicitive + " point " + index;
assertEquals(point + " scalar", expected, curved.getHeight(RUNTIME, 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(RUNTIME, xs[index], zs[index], SUPER_SEED) != curved.getHeight(RUNTIME, 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 exponentAtOrBelowZeroIsRejectedOnEveryPath() {
for (double exponent : new double[]{0D, -1D, Double.NaN, Double.POSITIVE_INFINITY}) {
IrisGenerator broken = generator(false, OPACITY).setExponent(exponent);
assertThrows(IllegalArgumentException.class, () -> broken.getHeight(RUNTIME, 1D, 1D, SUPER_SEED));
assertThrows(IllegalArgumentException.class, () -> broken.compileProgram(RUNTIME, SUPER_SEED));
}
}
@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(RUNTIME, 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,288 @@
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.PackRuntime;
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() {
PackRuntime 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() {
PackRuntime 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);
PackRuntime world = dataOfWorld(WORLD_SEED);
PackRuntime 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 (PackRuntime data : new PackRuntime[]{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 (PackRuntime data : new PackRuntime[]{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 (PackRuntime data : new PackRuntime[]{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 (PackRuntime data : new PackRuntime[]{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() {
PackRuntime runtime = dataOfWorld(WORLD_SEED);
IrisBiome biome = biomeWithChildStyle(seededSimplex(STYLE_SEED));
double[] unscaled = samples(biome.getChildrenGenerator(runtime, CALLER_SEED, 1D));
assertArrayEquals(unscaled, samples(biome.getChildrenGenerator(runtime, CALLER_SEED, 4D)), 0D);
assertArrayEquals(unscaled, samples(biome.getChildrenGenerator(runtime, CALLER_SEED, 0.25D)), 0D);
assertArrayEquals(unscaled, samples(biome.getChildrenGenerator(runtime, new RNG(5L), 123, 9D)), 0D);
}
@Test
public void unseededChildStyleStillScales() {
PackRuntime runtime = dataOfWorld(WORLD_SEED);
IrisBiome biome = biomeWithChildStyle(simplex());
double[] unscaled = samples(biome.getChildrenGenerator(runtime, CALLER_SEED, 1D));
double[] scaled = samples(biome.getChildrenGenerator(runtime, CALLER_SEED, 4D));
assertFalse(java.util.Arrays.equals(unscaled, scaled));
}
@Test
public void seededCarvingChildStyleIgnoresTheShrinkFactor() {
PackRuntime 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) {
return new IrisBiome().setChildStyle(childStyle);
}
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 PackRuntime dataOfWorld(long worldSeed) {
return PackRuntime.forEngine(engineOfWorld(worldSeed), mock(IrisData.class));
}
private static PackRuntime dataWithoutEngine() {
return PackRuntime.standalone(mock(IrisData.class));
}
private static long worldSeedOf(PackRuntime data) {
Engine engine = data.engine();
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 = "907c3f1d5a33281be801a23d86c4b8e1b5d933ddfc3445b179ece7898aa65d46";
private static final String EXPECTED_SCHEMA_DIGEST = "1a10c843c6dbe0f60b2dd2cd3a48c04407f30b0ade212cc69a6ba1c4188f25ce";
private static final List<Class<?>> SCHEMA_ROOTS = List.of(
IrisDimension.class,