From d7c5526eabb0ca0b102177a1493feb171a2f5185 Mon Sep 17 00:00:00 2001 From: Piexel <107539181+piexelated@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:44:03 +0100 Subject: [PATCH] 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 --- .../iris/generation/biome/IrisBiome.java | 3 +- .../biome/IrisBiomeLayerGenerator.java | 8 +- .../biome/IrisBiomePaletteLayer.java | 10 +- .../generation/decoration/IrisDecorator.java | 15 +- .../generation/noise/GeneratorProgram.java | 8 +- .../iris/generation/noise/IrisGenerator.java | 22 +- .../generation/noise/IrisGeneratorStyle.java | 38 ++- .../stage/IrisFloatingChildBiomeModifier.java | 2 +- .../terrain/IrisDimensionCarvingEntry.java | 8 +- .../terrain/IrisMaterialPalette.java | 7 +- .../compile/noise/NoiseStyleCompiler.java | 22 +- .../compile/terrain/GeneratorCompiler.java | 6 + .../biome/IrisBiomeLayerSlopeSourceTest.java | 2 + .../biome/IrisSurfaceLayerFallbackTest.java | 1 + .../noise/IrisGeneratorExponentTest.java | 133 ++++++++ .../noise/IrisGeneratorStyleSeedTest.java | 288 ++++++++++++++++++ .../schema/SchemaBuilderIdentityTest.java | 2 +- 17 files changed, 550 insertions(+), 25 deletions(-) create mode 100644 core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorExponentTest.java create mode 100644 core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorStyleSeedTest.java diff --git a/core/src/main/java/art/arcane/iris/generation/biome/IrisBiome.java b/core/src/main/java/art/arcane/iris/generation/biome/IrisBiome.java index 3dde27044..0ae7d696a 100644 --- a/core/src/main/java/art/arcane/iris/generation/biome/IrisBiome.java +++ b/core/src/main/java/art/arcane/iris/generation/biome/IrisBiome.java @@ -478,7 +478,8 @@ public class IrisBiome extends IrisRegistrant implements Rarity { } public CNG getChildrenGenerator(PackRuntime runtime, long seed, double scale) { - return runtime.biome(this).childrenGenerator(seed, scale); + double childScale = getChildStyle().isSeeded() ? 1 : scale; + return runtime.biome(this).childrenGenerator(seed, childScale); } CNG createChildrenGenerator(long seed, double scale, PackRuntime runtime) { diff --git a/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomeLayerGenerator.java b/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomeLayerGenerator.java index 03484f70c..952d0493c 100644 --- a/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomeLayerGenerator.java +++ b/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomeLayerGenerator.java @@ -231,10 +231,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, biomeRuntime.surfaceLayer(layerIndex))); + (wx + offset) / blocksPerSample, offset, (wz - offset) / blocksPerSample, biomeRuntime.surfaceLayer(layerIndex))); } catch (Throwable error) { GenerationFailures.skipPlacement(error, "surface layer", "block", (int) wx, height - depth, (int) wz, biome.getLoadKey()); } @@ -337,11 +337,11 @@ final class IrisBiomeLayerGenerator { private static void appendLayer(KList blocks, IrisBiomePaletteLayer layer, PaletteLayerRuntime layerRuntime, int layerIndex, int thickness, double x, double z, RNG random, int maxDepth) { - 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, layerRuntime)); + (x + offset) / blocksPerSample, offset, (z - offset) / blocksPerSample, layerRuntime)); } catch (Throwable error) { GenerationFailures.skipPlacement(error, "surface layer", "block", (int) x, -1, (int) z, "none"); } diff --git a/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomePaletteLayer.java b/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomePaletteLayer.java index f4192a042..b91dc12e4 100644 --- a/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomePaletteLayer.java +++ b/core/src/main/java/art/arcane/iris/generation/biome/IrisBiomePaletteLayer.java @@ -112,11 +112,19 @@ public class IrisBiomePaletteLayer { private NativeBlockState fit(KList localBlockData, PaletteLayerRuntime layerRuntime, RNG parent, int signature, double x, double y, double z) { - double localZoom = zoom; CNG generator = layerRuntime.layerGenerator(layerGeneratorSeed(parent, signature, localBlockData.size())); + if (style.isSeeded()) { + return generator.fit(localBlockData, x, z); + } + double localZoom = zoom; return generator.fit(localBlockData, x / localZoom, y / localZoom, z / localZoom); } + /** 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, PackRuntime runtime) { return getLayerGenerator(rng, 0, runtime); } diff --git a/core/src/main/java/art/arcane/iris/generation/decoration/IrisDecorator.java b/core/src/main/java/art/arcane/iris/generation/decoration/IrisDecorator.java index 70574f844..6add17ca5 100644 --- a/core/src/main/java/art/arcane/iris/generation/decoration/IrisDecorator.java +++ b/core/src/main/java/art/arcane/iris/generation/decoration/IrisDecorator.java @@ -169,11 +169,17 @@ public class IrisDecorator { if (getBlockData(decoratorRuntime.packRuntime().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 decoratorRuntime.layerGenerator(rng.getSeed()).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, PackRuntime runtime) { if (!passesChanceGate(rng, x, z, runtime)) { return null; @@ -204,8 +210,9 @@ public class IrisDecorator { return getBlockData100(b, rng, x, y, z, runtime); } - double xx = x / style.getZoom(); - double zz = z / style.getZoom(); + double blocksPerSample = chanceBlocksPerSample(); + double xx = x / blocksPerSample; + double zz = z / blocksPerSample; if (getGenerator(rng, runtime).fitDouble(0D, 1D, xx, zz) <= chance) { //Exclude y from here if (topPalette.size() == 1) { diff --git a/core/src/main/java/art/arcane/iris/generation/noise/GeneratorProgram.java b/core/src/main/java/art/arcane/iris/generation/noise/GeneratorProgram.java index bc4daf274..cba7732c1 100644 --- a/core/src/main/java/art/arcane/iris/generation/noise/GeneratorProgram.java +++ b/core/src/main/java/art/arcane/iris/generation/noise/GeneratorProgram.java @@ -15,6 +15,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; @@ -36,6 +37,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(); @@ -122,7 +124,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; } @@ -302,7 +305,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) { diff --git a/core/src/main/java/art/arcane/iris/generation/noise/IrisGenerator.java b/core/src/main/java/art/arcane/iris/generation/noise/IrisGenerator.java index 300a3fa20..c0019a056 100644 --- a/core/src/main/java/art/arcane/iris/generation/noise/IrisGenerator.java +++ b/core/src/main/java/art/arcane/iris/generation/noise/IrisGenerator.java @@ -62,6 +62,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) @@ -178,6 +181,7 @@ public class IrisGenerator extends IrisRegistrant { if (!Double.isFinite(surfaceDetail) || surfaceDetail < 0D || surfaceDetail > 1D) { throw new IllegalArgumentException("Generator surfaceDetail must be finite and between 0 and 1"); } + requireValidExponent(); if (composite.isEmpty()) { return 0D; } @@ -204,6 +208,7 @@ public class IrisGenerator extends IrisRegistrant { if (!Double.isFinite(surfaceDetail) || surfaceDetail < 0D || surfaceDetail > 1D) { throw new IllegalArgumentException("Generator surfaceDetail must be finite and between 0 and 1"); } + requireValidExponent(); if (!supportsBatch()) { throw new IllegalStateException("Generator " + getLoadKey() + " cannot be compiled into a batch program"); } @@ -298,7 +303,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; @@ -310,6 +316,20 @@ 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 void requireValidExponent() { + if (!Double.isFinite(exponent) || exponent <= 0D) { + throw new IllegalArgumentException("Generator exponent must be finite and above 0"); + } + } + private double cachedSurfaceHeight(double x, double z, long superSeed, SurfaceCacheStripe[] stripes, PackRuntime runtime) { if (x != (int) x || z != (int) z) { return sampleHeight(x, z, superSeed, runtime); diff --git a/core/src/main/java/art/arcane/iris/generation/noise/IrisGeneratorStyle.java b/core/src/main/java/art/arcane/iris/generation/noise/IrisGeneratorStyle.java index 7cbdff5ff..3a1d81c55 100644 --- a/core/src/main/java/art/arcane/iris/generation/noise/IrisGeneratorStyle.java +++ b/core/src/main/java/art/arcane/iris/generation/noise/IrisGeneratorStyle.java @@ -83,6 +83,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; @@ -119,7 +121,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() { @@ -163,10 +192,11 @@ public class IrisGeneratorStyle { } } - private CNG createNoCache(RNG rng, PackRuntime runtime, int octaveMultiplier, int fallbackCacheSize, + private CNG createNoCache(RNG callerRandom, PackRuntime runtime, int octaveMultiplier, int fallbackCacheSize, boolean quietCacheLog) { IrisData data = runtime.data(); Engine engine = runtime.engine(); + RNG rng = rollFor(callerRandom, engine); requirePositiveFinite("zoom", zoom); requirePositiveFinite("exponent", exponent); if (!Double.isFinite(cellularFrequency) || cellularFrequency < 0D) { @@ -231,8 +261,8 @@ public class IrisGeneratorStyle { return create(rng.getSeed(), runtime); } - public CNG create(long seed, PackRuntime runtime) { - return runtime.styleGenerator(this, seed); + public CNG create(long callerSeed, PackRuntime runtime) { + return runtime.styleGenerator(this, isSeeded() ? rolledSeed(runtime.engine()) : callerSeed); } @SuppressWarnings("BooleanMethodIsAlwaysInverted") diff --git a/core/src/main/java/art/arcane/iris/generation/stage/IrisFloatingChildBiomeModifier.java b/core/src/main/java/art/arcane/iris/generation/stage/IrisFloatingChildBiomeModifier.java index 2c1802e4b..e7c4e94ca 100644 --- a/core/src/main/java/art/arcane/iris/generation/stage/IrisFloatingChildBiomeModifier.java +++ b/core/src/main/java/art/arcane/iris/generation/stage/IrisFloatingChildBiomeModifier.java @@ -102,7 +102,7 @@ public class IrisFloatingChildBiomeModifier extends EngineAssignedModifier getTile(RNG rng, double x, double y, double z, IrisData rdata) { diff --git a/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/noise/NoiseStyleCompiler.java b/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/noise/NoiseStyleCompiler.java index a49a886d5..bd987a5c7 100644 --- a/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/noise/NoiseStyleCompiler.java +++ b/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/noise/NoiseStyleCompiler.java @@ -8,6 +8,7 @@ import art.arcane.iris.generation.noise.IrisStyledRange; import art.arcane.iris.pack.datapack.DataVersion; import art.arcane.iris.pack.datapack.export.ir.CostClass; import art.arcane.iris.pack.datapack.export.ir.ExportDiagnostic; +import art.arcane.iris.pack.datapack.export.ir.ExportLimit; import art.arcane.iris.pack.datapack.export.ir.Field; import art.arcane.iris.pack.datapack.export.ir.NoiseDef; import art.arcane.iris.pack.datapack.export.lower.ExportCapabilities; @@ -23,6 +24,7 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -37,6 +39,8 @@ public final class NoiseStyleCompiler implements StyleCompiler { private final CellularStyleLowering cellular; private final StyleCompiler selectors; + private final List diagnostics; + private final Set reportedSeeds = ConcurrentHashMap.newKeySet(); private final Map leaves = new ConcurrentHashMap<>(); private final Map levels = new ConcurrentHashMap<>(); private final PackRuntime runtime = PackRuntime.standalone(null); @@ -44,17 +48,20 @@ public final class NoiseStyleCompiler implements StyleCompiler { public NoiseStyleCompiler(CellularStyleLowering cellular) { this.cellular = Objects.requireNonNull(cellular, "cellular"); this.selectors = this; + this.diagnostics = null; } - private NoiseStyleCompiler(Function cellular, StyleCompiler selectors) { + private NoiseStyleCompiler(Function cellular, StyleCompiler selectors, + List diagnostics) { this.cellular = Objects.requireNonNull(cellular.apply(this), "cellular"); this.selectors = selectors == null ? this : selectors; + this.diagnostics = diagnostics; } public static NoiseStyleCompiler create(List diagnostics) { Objects.requireNonNull(diagnostics, "diagnostics"); - NoiseStyleCompiler selectors = new NoiseStyleCompiler(nested -> new DefaultCellularStyleLowering(diagnostics, nested::compile, CellularMode.QUADRANT_2X2), null); - return new NoiseStyleCompiler(nested -> new DefaultCellularStyleLowering(diagnostics, nested::compile, CellularMode.GRID_3X3), selectors); + NoiseStyleCompiler selectors = new NoiseStyleCompiler(nested -> new DefaultCellularStyleLowering(diagnostics, nested::compile, CellularMode.QUADRANT_2X2), null, diagnostics); + return new NoiseStyleCompiler(nested -> new DefaultCellularStyleLowering(diagnostics, nested::compile, CellularMode.GRID_3X3), selectors, diagnostics); } public static CNG generator(IrisGeneratorStyle style) { @@ -67,6 +74,14 @@ public final class NoiseStyleCompiler implements StyleCompiler { return style(Objects.requireNonNull(style, "style"), 1, Domain.IDENTITY, emit, "s"); } + private void reportSeed(IrisGeneratorStyle style) { + if (diagnostics == null || style == null || !style.isSeeded() || !reportedSeeds.add(String.valueOf(style.getSeed()))) { + return; + } + diagnostics.add(new ExportDiagnostic(ExportDiagnostic.Severity.WARNING, ExportLimit.NOISE_STYLE, "", + "Noise style seed " + style.getSeed() + " is not exported; the style uses its exported salt instead")); + } + @Override public StyleCompiler selectors() { return selectors; @@ -182,6 +197,7 @@ public final class NoiseStyleCompiler implements StyleCompiler { } private Field style(IrisGeneratorStyle style, int octaveMultiplier, Domain domain, Emit emit, String path) { + reportSeed(style); IrisGeneratorStyle prepared = prepared(Objects.requireNonNull(style, "noise style"), domain.zoom()); String stylePath = path + "/" + styleName(style); CNG root = build(prepared, octaveMultiplier, runtime); diff --git a/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/terrain/GeneratorCompiler.java b/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/terrain/GeneratorCompiler.java index 19dbdf98b..c36052adc 100644 --- a/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/terrain/GeneratorCompiler.java +++ b/core/src/main/java/art/arcane/iris/pack/datapack/export/compile/terrain/GeneratorCompiler.java @@ -101,6 +101,9 @@ public final class GeneratorCompiler { } public Field style(IrisGeneratorStyle style, double zoom, String path) { + if (style != null && style.isSeeded()) { + report(path, "Noise style seed " + style.getSeed() + " is not exported; the style uses its exported salt instead"); + } return styles.compile(zoomed(style, zoom), salt(path), registry, capabilities); } @@ -119,6 +122,9 @@ public final class GeneratorCompiler { if (generator.hasCliffs()) { value = new Field.Round(Field.RoundMode.ROUND, value, cliffStep(generator, root, path, resource, name)); } + if (generator.getExponent() != 1.0) { + report(resource, "Generator exponent " + generator.getExponent() + " is not exported; the uncurved height is used"); + } if (generator.getCellFractureHeight() != 0.0) { report(resource, "Generator cell fracture is not exported; the unfractured height is used"); } diff --git a/core/src/test/java/art/arcane/iris/generation/biome/IrisBiomeLayerSlopeSourceTest.java b/core/src/test/java/art/arcane/iris/generation/biome/IrisBiomeLayerSlopeSourceTest.java index 9335920c3..41539fb12 100644 --- a/core/src/test/java/art/arcane/iris/generation/biome/IrisBiomeLayerSlopeSourceTest.java +++ b/core/src/test/java/art/arcane/iris/generation/biome/IrisBiomeLayerSlopeSourceTest.java @@ -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)); diff --git a/core/src/test/java/art/arcane/iris/generation/biome/IrisSurfaceLayerFallbackTest.java b/core/src/test/java/art/arcane/iris/generation/biome/IrisSurfaceLayerFallbackTest.java index c6f7c8102..76b5006bc 100644 --- a/core/src/test/java/art/arcane/iris/generation/biome/IrisSurfaceLayerFallbackTest.java +++ b/core/src/test/java/art/arcane/iris/generation/biome/IrisSurfaceLayerFallbackTest.java @@ -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); diff --git a/core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorExponentTest.java b/core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorExponentTest.java new file mode 100644 index 000000000..c5532e1db --- /dev/null +++ b/core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorExponentTest.java @@ -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 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)); + } +} diff --git a/core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorStyleSeedTest.java b/core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorStyleSeedTest.java new file mode 100644 index 000000000..c1c40b0bb --- /dev/null +++ b/core/src/test/java/art/arcane/iris/generation/noise/IrisGeneratorStyleSeedTest.java @@ -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; + } +} diff --git a/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java b/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java index 5d3ae2867..aff78d2e8 100644 --- a/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java +++ b/core/src/test/java/art/arcane/iris/pack/schema/SchemaBuilderIdentityTest.java @@ -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> SCHEMA_ROOTS = List.of( IrisDimension.class,