From 5dd753c5899502a85a9b8f32a7a2ebf01cd9281b Mon Sep 17 00:00:00 2001 From: piexel Date: Fri, 6 Nov 2026 02:15:24 +0100 Subject: [PATCH] Studio bridge: noise sources and noise tiles The noise view's catalogue (overlay noise, pack generators and expressions, every noise style, in v3e's categories) and float tiles of any source at a seed and scale. --- .../iris/core/studio/bridge/StudioBridge.java | 41 ++++ .../iris/core/studio/noise/NoiseCategory.java | 99 ++++++++++ .../iris/core/studio/noise/NoiseSources.java | 175 ++++++++++++++++++ .../iris/core/studio/noise/NoiseTile.java | 57 ++++++ .../core/studio/noise/NoiseSourcesTest.java | 74 ++++++++ 5 files changed, 446 insertions(+) create mode 100644 core/src/main/java/art/arcane/iris/core/studio/noise/NoiseCategory.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/noise/NoiseTile.java create mode 100644 core/src/test/java/art/arcane/iris/core/studio/noise/NoiseSourcesTest.java diff --git a/core/src/main/java/art/arcane/iris/core/studio/bridge/StudioBridge.java b/core/src/main/java/art/arcane/iris/core/studio/bridge/StudioBridge.java index 4b8e0f7d0..9c4225a55 100644 --- a/core/src/main/java/art/arcane/iris/core/studio/bridge/StudioBridge.java +++ b/core/src/main/java/art/arcane/iris/core/studio/bridge/StudioBridge.java @@ -27,6 +27,8 @@ import art.arcane.iris.core.studio.map.MapPickInspection; import art.arcane.iris.core.studio.map.MapTile; import art.arcane.iris.core.studio.map.MapView; import art.arcane.iris.core.studio.assets.MinecraftAssets; +import art.arcane.iris.core.studio.noise.NoiseSources; +import art.arcane.iris.core.studio.noise.NoiseTile; import art.arcane.iris.core.studio.saved.SavedBiomeLabels; import art.arcane.iris.core.studio.saved.SavedTile; import art.arcane.iris.core.studio.saved.SavedWorld; @@ -36,6 +38,7 @@ import art.arcane.iris.engine.framework.Engine; import art.arcane.iris.engine.framework.render.RenderType; import art.arcane.iris.engine.object.world.region.IrisRegion; import art.arcane.iris.spi.IrisLogging; +import art.arcane.volmlib.util.function.NoiseProvider; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.sun.net.httpserver.Headers; @@ -91,6 +94,8 @@ public final class StudioBridge implements AutoCloseable { private static final double LARGEST_BLOCKS_PER_PIXEL = 4_096D; // Each pixel reads the chunk it lands in, so a saved world is shown no farther out than a pixel per chunk. private static final int LARGEST_SAVED_BLOCKS_PER_PIXEL = 16; + // Noise has detail finer than a block, so it may be looked at closer than the world. + private static final double SMALLEST_NOISE_BLOCKS_PER_PIXEL = 1D / 64D; private static final long ENTITY_WAIT_MILLIS = 3_000L; private static final int STATUS_OK = 200; private static final int STATUS_NO_CONTENT = 204; @@ -98,6 +103,7 @@ public final class StudioBridge implements AutoCloseable { private static final int STATUS_UNAUTHORIZED = 401; private static final int STATUS_NOT_FOUND = 404; private static final int STATUS_METHOD_NOT_ALLOWED = 405; + private static final int STATUS_UNPROCESSABLE = 422; private static final int STATUS_SERVER_ERROR = 500; private static final int STATUS_UNAVAILABLE = 503; @@ -216,6 +222,8 @@ public final class StudioBridge implements AutoCloseable { case "saved-worlds" -> sendJson(exchange, STATUS_OK, savedWorlds(exchange)); case "saved-start" -> sendJson(exchange, STATUS_OK, savedStart(exchange, query)); case "saved-tile" -> sendBytes(exchange, "application/octet-stream", savedTile(get(exchange).engine(), query)); + case "noise-sources" -> sendJson(exchange, STATUS_OK, NoiseSources.of(get(exchange).engine()).list()); + case "noise-tile" -> sendBytes(exchange, "application/octet-stream", noiseTile(get(exchange).engine(), query)); case "events" -> streamEvents(exchange); case "teleport" -> teleport(exchange, query); case "open" -> openInEditor(exchange, query); @@ -348,6 +356,31 @@ public final class StudioBridge implements AutoCloseable { return SavedTileEncoding.encode(tile, SavedBiomeLabels.of(engine).labelsOf(tile.biomeNames()), gson); } + private byte[] noiseTile(Engine engine, Query query) { + int size = query.whole("size", DEFAULT_TILE_SIZE); + double blocksPerPixel = query.number("scale"); + if (size < 1 || size > LARGEST_TILE_SIZE) { + throw new RequestError(STATUS_BAD_REQUEST, "A tile is 1 to " + LARGEST_TILE_SIZE + " pixels"); + } + if (!(blocksPerPixel >= SMALLEST_NOISE_BLOCKS_PER_PIXEL && blocksPerPixel <= LARGEST_BLOCKS_PER_PIXEL)) { + throw new RequestError(STATUS_BAD_REQUEST, "The scale is " + SMALLEST_NOISE_BLOCKS_PER_PIXEL + " to " + LARGEST_BLOCKS_PER_PIXEL + " blocks per pixel"); + } + NoiseProvider noise = noiseSampler(engine, query.text("source"), query.longNumber("seed")); + return NoiseTile.sample(noise, query.whole("x", 0), query.whole("z", 0), blocksPerPixel, size, + () -> closed || engine.isClosing() || engine.isClosed()).encode(); + } + + // A source that can't be sampled fails every tile alike, so it is told once to the studio rather than logged per tile. + private static NoiseProvider noiseSampler(Engine engine, String source, long seed) { + try { + return NoiseSources.of(engine).sampler(source, seed); + } catch (NoSuchElementException unknown) { + throw new RequestError(STATUS_NOT_FOUND, unknown.getMessage()); + } catch (IllegalStateException unsampleable) { + throw new RequestError(STATUS_UNPROCESSABLE, unsampleable.getMessage()); + } + } + private static SavedWorld savedWorld(Query query) { try { return SavedWorlds.shared().open(query.text("world")); @@ -562,6 +595,14 @@ public final class StudioBridge implements AutoCloseable { } } + long longNumber(String name) { + try { + return Long.parseLong(text(name)); + } catch (NumberFormatException malformed) { + throw new RequestError(STATUS_BAD_REQUEST, name + " must be a whole number"); + } + } + boolean has(String name) { return values.containsKey(name); } diff --git a/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseCategory.java b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseCategory.java new file mode 100644 index 000000000..078bde09b --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseCategory.java @@ -0,0 +1,99 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.studio.noise; + +import art.arcane.iris.engine.object.noise.NoiseStyle; + +/** The groups the noise view lists its sources in, in this order. */ +public enum NoiseCategory { + PACK_GENERATORS("Pack generators"), + PACK_EXPRESSIONS("Pack expressions"), + SIMPLEX("Simplex"), + PERLIN("Perlin"), + CELLULAR("Cellular"), + IRIS("Iris"), + CLOVER("Clover"), + HEXAGON("Hexagon"), + VASCULAR("Vascular"), + GLOBE("Globe"), + CUBIC("Cubic"), + FRACTAL("Fractal"), + STATIC("Static"), + NOWHERE("Nowhere"), + SIERPINSKI("Sierpinski"), + UTILITY("Utility"), + OTHER("Other"); + + private final String label; + + NoiseCategory(String label) { + this.label = label; + } + + public String label() { + return label; + } + + static NoiseCategory of(NoiseStyle style) { + String name = style.name(); + if (name.startsWith("STATIC")) { + return STATIC; + } + if (name.startsWith("IRIS")) { + return IRIS; + } + if (name.startsWith("CLOVER")) { + return CLOVER; + } + if (name.startsWith("VASCULAR")) { + return VASCULAR; + } + if (name.equals("FLAT")) { + return UTILITY; + } + if (name.startsWith("CELLULAR")) { + return CELLULAR; + } + if (name.startsWith("HEX")) { + return HEXAGON; + } + if (name.startsWith("SIERPINSKI")) { + return SIERPINSKI; + } + if (name.startsWith("NOWHERE")) { + return NOWHERE; + } + if (name.startsWith("GLOB")) { + return GLOBE; + } + if (name.startsWith("PERLIN")) { + return PERLIN; + } + if (name.startsWith("CUBIC") || (name.startsWith("FRACTAL") && name.contains("CUBIC"))) { + return CUBIC; + } + if (name.contains("SIMPLEX") && !name.startsWith("FRACTAL")) { + return SIMPLEX; + } + if (name.startsWith("FRACTAL")) { + return FRACTAL; + } + return OTHER; + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java new file mode 100644 index 000000000..8b5db0f53 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java @@ -0,0 +1,175 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.studio.noise; + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.noise.IrisShapedGeneratorStyle; +import art.arcane.iris.engine.object.noise.NoiseStyle; +import art.arcane.iris.engine.object.noise.expression.IrisExpression; +import art.arcane.iris.engine.object.terrain.IrisGenerator; +import art.arcane.iris.util.project.noise.CNG; +import art.arcane.volmlib.util.function.NoiseProvider; +import art.arcane.volmlib.util.math.RNG; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.NoSuchElementException; + +/** + * Every noise the noise view can show for a world: the dimension's overlay noise, the pack's generators and + * expressions, and each built-in noise style. + */ +public final class NoiseSources { + static final String OVERLAY_NOISE_ID = "world:overlay"; + // Generators mix their seed the way the Swing explorer did, so a seed shows the same noise in both. + private static final long GENERATOR_SEED_SALT = 3245L; + + private final List entries; + + private NoiseSources(List entries) { + this.entries = entries.stream().sorted(Comparator.comparing(Entry::category)).toList(); + } + + public static NoiseSources of(Engine engine) { + List entries = new ArrayList<>(); + addOverlayNoise(engine, entries); + addPackGenerators(engine.getData(), entries); + addPackExpressions(engine.getData(), entries); + addStyles(entries); + return new NoiseSources(entries); + } + + static NoiseSources builtIn() { + List entries = new ArrayList<>(); + addStyles(entries); + return new NoiseSources(entries); + } + + public List list() { + return entries.stream().map(Entry::source).toList(); + } + + /** The noise of a listed source at a seed, which sources that keep the world's seed ignore. */ + public NoiseProvider sampler(String id, long seed) { + return entries.stream() + .filter(entry -> entry.source().id().equals(id)) + .findFirst() + .orElseThrow(() -> new NoSuchElementException("No noise " + id)) + .factory() + .create(seed); + } + + // The overlay is summed the way the world adds it, with the world's seed, in blocks above the fluid. + private static void addOverlayNoise(Engine engine, List entries) { + List overlay = engine.getDimension().getOverlayNoise(); + if (overlay.isEmpty()) { + return; + } + BlockRange blocks = new BlockRange( + overlay.stream().mapToInt(IrisShapedGeneratorStyle::getMin).sum(), + overlay.stream().mapToInt(IrisShapedGeneratorStyle::getMax).sum()); + Source source = new Source(OVERLAY_NOISE_ID, "Overlay noise", NoiseCategory.PACK_GENERATORS.label(), false, blocks); + entries.add(new Entry(source, NoiseCategory.PACK_GENERATORS, seed -> engine.getComplex().getOverlayStream()::get)); + } + + private static void addPackGenerators(IrisData data, List entries) { + for (String key : sortedKeys(data.getGeneratorLoader().getPossibleKeys())) { + Source source = new Source("pack:" + key, displayName(key), NoiseCategory.PACK_GENERATORS.label(), true, null); + entries.add(new Entry(source, NoiseCategory.PACK_GENERATORS, seed -> { + IrisGenerator generator = data.getGeneratorLoader().load(key); + if (generator == null) { + throw new NoSuchElementException("No pack generator " + key); + } + long mixedSeed = new RNG(seed).nextParallelRNG(GENERATOR_SEED_SALT).lmax(); + return (x, z) -> generator.getHeight(x, z, mixedSeed); + })); + } + } + + private static void addPackExpressions(IrisData data, List entries) { + for (String key : sortedKeys(data.getExpressionLoader().getPossibleKeys())) { + Source source = new Source("expression:" + key, displayName(key), NoiseCategory.PACK_EXPRESSIONS.label(), true, null); + entries.add(new Entry(source, NoiseCategory.PACK_EXPRESSIONS, seed -> { + IrisExpression expression = data.getExpressionLoader().load(key); + if (expression == null) { + throw new NoSuchElementException("No pack expression " + key); + } + RNG rng = new RNG(seed); + requireParses(expression, rng); + return (x, z) -> expression.evaluate(rng, x, z); + })); + } + } + + // An expression that doesn't parse fails on every sample; checking one turns that into a single readable error. + private static void requireParses(IrisExpression expression, RNG rng) { + try { + expression.evaluate(rng, 0, 0); + } catch (NullPointerException unparsed) { + throw new IllegalStateException(expression.getLoadKey() + " doesn't parse; the server log has the details"); + } + } + + private static void addStyles(List entries) { + for (NoiseStyle style : NoiseStyle.values()) { + NoiseCategory category = NoiseCategory.of(style); + Source source = new Source("style:" + style.name(), displayName(style.name()), category.label(), true, null); + entries.add(new Entry(source, category, seed -> { + CNG noise = style.create(new RNG(seed)); + return noise::noiseFast2D; + })); + } + } + + private static List sortedKeys(String[] keys) { + if (keys == null) { + return List.of(); + } + return Arrays.stream(keys).sorted().toList(); + } + + private static String displayName(String rawName) { + String spaced = rawName.toLowerCase(Locale.ROOT).replace('_', ' ').replace('-', ' '); + return Character.toUpperCase(spaced.charAt(0)) + spaced.substring(1); + } + + /** + * One noise the view lists. + * + * @param usesSeed whether the view's seed changes it; the overlay keeps the world's + * @param blockRange where the noise is measured in blocks above the fluid, what it spans; null for noise of 0 to 1 + */ + public record Source(String id, String name, String category, boolean usesSeed, BlockRange blockRange) { + } + + public record BlockRange(double lowest, double highest) { + } + + @FunctionalInterface + private interface SamplerFactory { + NoiseProvider create(long seed); + } + + private record Entry(Source source, NoiseCategory category, SamplerFactory factory) { + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseTile.java b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseTile.java new file mode 100644 index 000000000..4efd02cc4 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseTile.java @@ -0,0 +1,57 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.studio.noise; + +import art.arcane.volmlib.util.function.NoiseProvider; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.concurrent.CancellationException; +import java.util.function.BooleanSupplier; + +/** + * A square of noise values, a pixel's value taken at its north west corner, row by row from the north. Tile (0, 0) + * starts at block (0, 0), so tiles at one scale meet without a seam. + */ +public record NoiseTile(int size, float[] values) { + public static NoiseTile sample(NoiseProvider noise, int tileX, int tileZ, double blocksPerPixel, int size, + BooleanSupplier cancelled) { + double tileBlocks = size * blocksPerPixel; + double originX = tileX * tileBlocks; + double originZ = tileZ * tileBlocks; + float[] values = new float[size * size]; + for (int row = 0; row < size; row++) { + if (cancelled.getAsBoolean()) { + throw new CancellationException("Noise tile no longer wanted"); + } + double z = originZ + row * blocksPerPixel; + for (int column = 0; column < size; column++) { + values[row * size + column] = (float) noise.noise(originX + column * blocksPerPixel, z); + } + } + return new NoiseTile(size, values); + } + + /** The values as little endian 32 bit floats, an unsampleable value as NaN. */ + public byte[] encode() { + ByteBuffer bytes = ByteBuffer.allocate(values.length * Float.BYTES).order(ByteOrder.LITTLE_ENDIAN); + bytes.asFloatBuffer().put(values); + return bytes.array(); + } +} diff --git a/core/src/test/java/art/arcane/iris/core/studio/noise/NoiseSourcesTest.java b/core/src/test/java/art/arcane/iris/core/studio/noise/NoiseSourcesTest.java new file mode 100644 index 000000000..953ce9956 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/studio/noise/NoiseSourcesTest.java @@ -0,0 +1,74 @@ +package art.arcane.iris.core.studio.noise; + +import art.arcane.iris.engine.object.noise.NoiseStyle; +import art.arcane.volmlib.util.function.NoiseProvider; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.CancellationException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class NoiseSourcesTest { + private static final long SEED = 12345L; + private static final double EXACT = 0D; + + @Test + public void listsEveryStyleGroupedInCategoryOrder() { + List sources = NoiseSources.builtIn().list(); + assertEquals(NoiseStyle.values().length, sources.size()); + List categoryOrder = Arrays.stream(NoiseCategory.values()).map(NoiseCategory::label).toList(); + List positions = sources.stream().map(source -> categoryOrder.indexOf(source.category())).toList(); + assertFalse(positions.contains(-1)); + assertEquals(positions.stream().sorted().toList(), positions); + NoiseSources.Source simplex = sources.stream().filter(source -> source.id().equals("style:SIMPLEX")).findFirst().orElseThrow(); + assertEquals("Simplex", simplex.name()); + assertEquals("Simplex", simplex.category()); + assertTrue(simplex.usesSeed()); + } + + @Test + public void aSeedPicksTheNoise() { + NoiseSources sources = NoiseSources.builtIn(); + NoiseProvider first = sources.sampler("style:SIMPLEX", SEED); + NoiseProvider again = sources.sampler("style:SIMPLEX", SEED); + NoiseProvider other = sources.sampler("style:SIMPLEX", SEED + 1); + assertEquals(first.noise(37.5, -12.25), again.noise(37.5, -12.25), EXACT); + assertNotEquals(first.noise(37.5, -12.25), other.noise(37.5, -12.25), EXACT); + } + + @Test + public void anUnknownSourceIsNotFound() { + assertThrows(NoSuchElementException.class, () -> NoiseSources.builtIn().sampler("style:NOPE", SEED)); + } + + @Test + public void tilesStartAtTheirCornerAndMeetTheirNeighbours() { + NoiseProvider position = (x, z) -> x * 1000 + z; + NoiseTile west = NoiseTile.sample(position, 0, 1, 0.5, 4, () -> false); + NoiseTile east = NoiseTile.sample(position, 1, 1, 0.5, 4, () -> false); + assertEquals(position.noise(0, 2), west.values()[0], EXACT); + assertEquals(position.noise(1.5, 2.5), west.values()[4 + 3], EXACT); + assertEquals(position.noise(2, 2), east.values()[0], EXACT); + } + + @Test + public void encodesLittleEndianFloats() { + NoiseTile tile = new NoiseTile(1, new float[]{0.25F}); + float decoded = ByteBuffer.wrap(tile.encode()).order(ByteOrder.LITTLE_ENDIAN).getFloat(); + assertEquals(0.25F, decoded, EXACT); + } + + @Test + public void stopsWhenNoLongerWanted() { + assertThrows(CancellationException.class, () -> NoiseTile.sample((x, z) -> 0, 0, 0, 1, 4, () -> true)); + } +}