Sample terrain height noise tiles on a world-fixed lattice

Asking the world's height at every pixel of a noise tile is too slow for the 3D view. Tiles of
the terrain height source now sample a power-of-two lattice fixed in the world and fill in between
along a Catmull-Rom curve, so tiles meet without a seam. Other noise sources are still sampled at
every pixel, and no pins moved.

v3: 1622f6dee 58382a46d bdea50e75
This commit is contained in:
piexel committed 2026-11-10 12:54:37 +01:00
1 parent 2366c87659
commit 80ecd143da
9 files changed
+247 -31

No files matched your search

@@ -501,15 +501,17 @@ public final class StudioBridge implements AutoCloseable {
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,
NoiseSources sources = NoiseSources.of(engine);
String source = query.text("source");
NoiseProvider noise = noiseSampler(sources, source, query.longNumber("seed"));
return NoiseTile.sample(noise, query.whole("x", 0), query.whole("z", 0), blocksPerPixel, size, sources.isSmooth(source),
() -> 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) {
private static NoiseProvider noiseSampler(NoiseSources sources, String source, long seed) {
try {
return NoiseSources.of(engine).sampler(source, seed);
return sources.sampler(source, seed);
} catch (NoSuchElementException unknown) {
throw new RequestError(STATUS_NOT_FOUND, unknown.getMessage());
} catch (IllegalStateException unsampleable) {
@@ -18,6 +18,7 @@
package art.arcane.iris.core.studio.map;
import art.arcane.iris.core.studio.noise.CatmullRom;
import art.arcane.iris.util.project.stream.ProceduralStream;
/**
@@ -78,17 +79,8 @@ final class HeightLattice {
double[] rows = new double[CURVE_POINTS];
for (int row = 0; row < CURVE_POINTS; row++) {
int offset = (baseZ - 1 + row) * pointsPerSide + baseX - 1;
rows[row] = catmullRom(points[offset], points[offset + 1], points[offset + 2], points[offset + 3], fractionX);
rows[row] = CatmullRom.between(points[offset], points[offset + 1], points[offset + 2], points[offset + 3], fractionX);
}
return catmullRom(rows[0], rows[1], rows[2], rows[3], fractionZ);
}
private static double catmullRom(double before, double start, double end, double after, double fraction) {
double squared = fraction * fraction;
double cubed = squared * fraction;
return 0.5D * (2D * start
+ (end - before) * fraction
+ (2D * before - 5D * start + 4D * end - after) * squared
+ (3D * start - before - 3D * end + after) * cubed);
return CatmullRom.between(rows[0], rows[1], rows[2], rows[3], fractionZ);
}
}
@@ -0,0 +1,35 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.studio.noise;
/** The curve through four evenly spaced samples that passes through the middle two. */
public final class CatmullRom {
private CatmullRom() {
}
/** The curve between {@code start} and {@code end}, {@code fraction} of the way from one to the other. */
public static double between(double before, double start, double end, double after, double fraction) {
double squared = fraction * fraction;
double cubed = squared * fraction;
return 0.5D * (2D * start
+ (end - before) * fraction
+ (2D * before - 5D * start + 4D * end - after) * squared
+ (3D * start - before - 3D * end + after) * cubed);
}
}
@@ -0,0 +1,86 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.studio.noise;
import art.arcane.volmlib.util.function.NoiseProvider;
import java.util.function.BooleanSupplier;
/**
* Tile values for noise too slow to sample at every pixel: the noise is sampled on a lattice fixed in the world and
* filled in between along a Catmull-Rom curve. A pixel's value depends only on where it is in the world, so tiles of
* one scale meet without a seam and tiles of nearby scales share their lattice points.
*/
final class NoiseLattice {
static final int LATTICE_PIXELS = 4;
static final double SMALLEST_SPACING_BLOCKS = 1D;
private static final int POINTS_BEFORE_CELL = 1;
private static final int POINTS_AFTER_CELL = 2;
private static final int CURVE_POINTS = 4;
private NoiseLattice() {
}
/** A power of two of blocks, at least {@link #LATTICE_PIXELS} pixels wide and at least a block, so a pixel never asks closer than the world's own grain. */
static double spacingBlocks(double blocksPerPixel) {
double wanted = Math.max(LATTICE_PIXELS * blocksPerPixel, SMALLEST_SPACING_BLOCKS);
double spacing = SMALLEST_SPACING_BLOCKS;
while (spacing < wanted) {
spacing *= 2D;
}
return spacing;
}
static float[] values(NoiseProvider noise, double originX, double originZ, double blocksPerPixel, int size, BooleanSupplier cancelled) {
double spacing = spacingBlocks(blocksPerPixel);
double reach = (size - 1) * blocksPerPixel;
int firstColumn = (int) Math.floor(originX / spacing) - POINTS_BEFORE_CELL;
int firstRow = (int) Math.floor(originZ / spacing) - POINTS_BEFORE_CELL;
int pointsAcross = (int) Math.floor((originX + reach) / spacing) + POINTS_AFTER_CELL - firstColumn + 1;
int pointsDown = (int) Math.floor((originZ + reach) / spacing) + POINTS_AFTER_CELL - firstRow + 1;
double[] points = new double[pointsAcross * pointsDown];
for (int row = 0; row < pointsDown; row++) {
NoiseTile.requireWanted(cancelled);
for (int column = 0; column < pointsAcross; column++) {
points[row * pointsAcross + column] = noise.noise((firstColumn + column) * spacing, (firstRow + row) * spacing);
}
}
float[] values = new float[size * size];
double[] curveRows = new double[CURVE_POINTS];
for (int pixelRow = 0; pixelRow < size; pixelRow++) {
NoiseTile.requireWanted(cancelled);
double latticeZ = (originZ + pixelRow * blocksPerPixel) / spacing;
int cellZ = (int) Math.floor(latticeZ);
double fractionZ = latticeZ - cellZ;
for (int pixelColumn = 0; pixelColumn < size; pixelColumn++) {
double latticeX = (originX + pixelColumn * blocksPerPixel) / spacing;
int cellX = (int) Math.floor(latticeX);
double fractionX = latticeX - cellX;
for (int curveRow = 0; curveRow < CURVE_POINTS; curveRow++) {
int offset = (cellZ - firstRow - POINTS_BEFORE_CELL + curveRow) * pointsAcross
+ cellX - firstColumn - POINTS_BEFORE_CELL;
curveRows[curveRow] = CatmullRom.between(points[offset], points[offset + 1], points[offset + 2], points[offset + 3], fractionX);
}
values[pixelRow * size + pixelColumn] = (float) CatmullRom.between(curveRows[0], curveRows[1], curveRows[2], curveRows[3], fractionZ);
}
}
return values;
}
}
@@ -74,12 +74,19 @@ public final class NoiseSources {
/** 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 entryOf(id).factory().create(seed);
}
/** Whether tiles of a listed source are drawn from samples on a lattice, smoothly in between, because sampling every pixel is too slow. */
public boolean isSmooth(String id) {
return entryOf(id).source().smooth();
}
private Entry entryOf(String id) {
return entries.stream()
.filter(entry -> entry.source().id().equals(id))
.findFirst()
.orElseThrow(() -> new NoSuchElementException("No noise " + id))
.factory()
.create(seed);
.orElseThrow(() -> new NoSuchElementException("No noise " + id));
}
// The overlay is summed the way the world adds it, with the world's seed, in blocks above the fluid.
@@ -91,7 +98,7 @@ public final class NoiseSources {
BlockRange blocks = new BlockRange(
overlay.stream().mapToInt(IrisBiomeGeneratorLink::getMin).sum(),
overlay.stream().mapToInt(IrisBiomeGeneratorLink::getMax).sum());
Source source = new Source(OVERLAY_NOISE_ID, "Overlay noise", NoiseCategory.PACK_GENERATORS.label(), false, blocks);
Source source = new Source(OVERLAY_NOISE_ID, "Overlay noise", NoiseCategory.PACK_GENERATORS.label(), false, false, blocks);
entries.add(new Entry(source, NoiseCategory.PACK_GENERATORS, seed -> engine.getComplex().getOverlayStream()::get));
}
@@ -99,14 +106,14 @@ public final class NoiseSources {
private static void addTerrainHeight(Engine engine, List<Entry> entries) {
double fluidHeight = engine.getComplex().getFluidHeight();
BlockRange blocks = new BlockRange(-fluidHeight, engine.getHeight() - fluidHeight);
Source source = new Source(TERRAIN_HEIGHT_ID, "Terrain height", NoiseCategory.PACK_GENERATORS.label(), false, blocks);
Source source = new Source(TERRAIN_HEIGHT_ID, "Terrain height", NoiseCategory.PACK_GENERATORS.label(), false, true, blocks);
entries.add(new Entry(source, NoiseCategory.PACK_GENERATORS,
seed -> (x, z) -> engine.getComplex().getHeightStream().getDouble(x, z) - fluidHeight));
}
private static void addPackGenerators(IrisData data, List<Entry> entries) {
for (String key : sortedKeys(data.getGeneratorLoader().getPossibleKeys())) {
Source source = new Source("pack:" + key, displayName(key), NoiseCategory.PACK_GENERATORS.label(), true, null);
Source source = new Source("pack:" + key, displayName(key), NoiseCategory.PACK_GENERATORS.label(), true, false, null);
entries.add(new Entry(source, NoiseCategory.PACK_GENERATORS, seed -> {
IrisGenerator generator = data.getGeneratorLoader().load(key);
if (generator == null) {
@@ -124,7 +131,7 @@ public final class NoiseSources {
Map<String, RNG> worldRandoms = ExpressionSeeds.of(engine);
for (String key : sortedKeys(data.getExpressionLoader().getPossibleKeys())) {
RNG worldRandom = worldRandoms.get(key);
Source source = new Source("expression:" + key, displayName(key), NoiseCategory.PACK_EXPRESSIONS.label(), worldRandom == null, null);
Source source = new Source("expression:" + key, displayName(key), NoiseCategory.PACK_EXPRESSIONS.label(), worldRandom == null, false, null);
entries.add(new Entry(source, NoiseCategory.PACK_EXPRESSIONS, seed -> {
IrisExpression expression = data.getExpressionLoader().load(key);
if (expression == null) {
@@ -149,7 +156,7 @@ public final class NoiseSources {
private static void addStyles(List<Entry> 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);
Source source = new Source("style:" + style.name(), displayName(style.name()), category.label(), true, false, null);
entries.add(new Entry(source, category, seed -> {
CNG noise = style.create(new RNG(seed));
return noise::noiseFast2D;
@@ -173,9 +180,10 @@ public final class NoiseSources {
* One noise the view lists.
*
* @param usesSeed whether the view's seed changes it; the overlay and the expressions the world uses keep the world's
* @param smooth whether tiles are sampled on a world-fixed lattice and smoothed between its points, for noise too slow to ask at every pixel
* @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 Source(String id, String name, String category, boolean usesSeed, boolean smooth, BlockRange blockRange) {
}
public record BlockRange(double lowest, double highest) {
@@ -30,16 +30,18 @@ import java.util.function.BooleanSupplier;
* starts at block (0, 0), so tiles at one scale meet without a seam.
*/
public record NoiseTile(int size, float[] values) {
/** @param smooth whether to sample a lattice and fill in between, for noise too slow to ask at every pixel */
public static NoiseTile sample(NoiseProvider noise, int tileX, int tileZ, double blocksPerPixel, int size,
BooleanSupplier cancelled) {
boolean smooth, BooleanSupplier cancelled) {
double tileBlocks = size * blocksPerPixel;
double originX = tileX * tileBlocks;
double originZ = tileZ * tileBlocks;
if (smooth) {
return new NoiseTile(size, NoiseLattice.values(noise, originX, originZ, blocksPerPixel, size, cancelled));
}
float[] values = new float[size * size];
for (int row = 0; row < size; row++) {
if (cancelled.getAsBoolean()) {
throw new CancellationException("Noise tile no longer wanted");
}
requireWanted(cancelled);
double z = originZ + row * blocksPerPixel;
for (int column = 0; column < size; column++) {
values[row * size + column] = (float) noise.noise(originX + column * blocksPerPixel, z);
@@ -48,6 +50,12 @@ public record NoiseTile(int size, float[] values) {
return new NoiseTile(size, values);
}
static void requireWanted(BooleanSupplier cancelled) {
if (cancelled.getAsBoolean()) {
throw new CancellationException("Noise tile no longer wanted");
}
}
/** 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);
@@ -0,0 +1,82 @@
package art.arcane.iris.core.studio.noise;
import art.arcane.volmlib.util.function.NoiseProvider;
import org.junit.Test;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class NoiseLatticeTest {
private static final double EXACT = 0D;
private static final int TILE_SIZE = 32;
private static final double HILL_WAVELENGTH_BLOCKS = 384D;
private static final double HILL_HEIGHT_BLOCKS = 40D;
private static final double SMOOTH_TOLERANCE_BLOCKS = 0.5D;
private static final double FLOAT_TOLERANCE = 1e-4D;
private static final NoiseProvider HILLS = (x, z) ->
HILL_HEIGHT_BLOCKS * Math.sin(x * 2 * Math.PI / HILL_WAVELENGTH_BLOCKS) * Math.cos(z * 2 * Math.PI / HILL_WAVELENGTH_BLOCKS);
@Test
public void spacingIsAPowerOfTwoOfAtLeastFourPixelsAndOneBlock() {
assertEquals(4D, NoiseLattice.spacingBlocks(1D), EXACT);
assertEquals(16D, NoiseLattice.spacingBlocks(3D), EXACT);
assertEquals(1D, NoiseLattice.spacingBlocks(0.0625D), EXACT);
assertEquals(1D, NoiseLattice.spacingBlocks(0.25D), EXACT);
assertEquals(2D, NoiseLattice.spacingBlocks(0.5D), EXACT);
}
@Test
public void latticePointsAreTheSourceExactly() {
NoiseProvider ramps = (x, z) -> x * x - 3 * z;
float[] values = NoiseLattice.values(ramps, -64, 32, 1D, TILE_SIZE, () -> false);
for (int pixelRow = 0; pixelRow < TILE_SIZE; pixelRow += 4) {
for (int pixelColumn = 0; pixelColumn < TILE_SIZE; pixelColumn += 4) {
assertEquals(ramps.noise(-64 + pixelColumn, 32 + pixelRow), values[pixelRow * TILE_SIZE + pixelColumn], FLOAT_TOLERANCE);
}
}
}
@Test
public void smoothNoiseStaysCloseToTheExactNoiseAtEveryScale() {
for (double blocksPerPixel : new double[]{0.0625D, 0.5D, 1D, 4D}) {
NoiseTile tile = NoiseTile.sample(HILLS, -3, 2, blocksPerPixel, TILE_SIZE, true, () -> false);
NoiseTile exact = NoiseTile.sample(HILLS, -3, 2, blocksPerPixel, TILE_SIZE, false, () -> false);
for (int pixel = 0; pixel < tile.values().length; pixel++) {
assertEquals("scale " + blocksPerPixel, exact.values()[pixel], tile.values()[pixel], SMOOTH_TOLERANCE_BLOCKS);
}
}
}
@Test
public void tilesOfTheSameScaleMeetWithoutASeam() {
NoiseTile whole = NoiseTile.sample(HILLS, -1, 0, 1D, 2 * TILE_SIZE, true, () -> false);
NoiseTile west = NoiseTile.sample(HILLS, -2, 0, 1D, TILE_SIZE, true, () -> false);
NoiseTile east = NoiseTile.sample(HILLS, -1, 0, 1D, TILE_SIZE, true, () -> false);
for (int row = 0; row < TILE_SIZE; row++) {
for (int column = 0; column < TILE_SIZE; column++) {
assertEquals(whole.values()[row * 2 * TILE_SIZE + column], west.values()[row * TILE_SIZE + column], FLOAT_TOLERANCE);
assertEquals(whole.values()[row * 2 * TILE_SIZE + TILE_SIZE + column], east.values()[row * TILE_SIZE + column], FLOAT_TOLERANCE);
}
}
}
@Test
public void aTileAsksFarFewerPointsThanItHasPixels() {
AtomicInteger asked = new AtomicInteger();
NoiseProvider counting = (x, z) -> {
asked.incrementAndGet();
return 0;
};
NoiseLattice.values(counting, 0, 0, 1D, TILE_SIZE, () -> false);
assertTrue(asked.get() * 8 < TILE_SIZE * TILE_SIZE);
}
@Test
public void stopsWhenNoLongerWanted() {
assertThrows(CancellationException.class, () -> NoiseLattice.values((x, z) -> 0, 0, 0, 1D, TILE_SIZE, () -> true));
}
}
@@ -33,6 +33,7 @@ public class NoiseSourcesTest {
assertEquals("Simplex", simplex.name());
assertEquals("Simplex", simplex.category());
assertTrue(simplex.usesSeed());
assertFalse(simplex.smooth());
}
@Test
@@ -53,8 +54,8 @@ public class NoiseSourcesTest {
@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);
NoiseTile west = NoiseTile.sample(position, 0, 1, 0.5, 4, false, () -> false);
NoiseTile east = NoiseTile.sample(position, 1, 1, 0.5, 4, false, () -> 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);
@@ -69,6 +70,6 @@ public class NoiseSourcesTest {
@Test
public void stopsWhenNoLongerWanted() {
assertThrows(CancellationException.class, () -> NoiseTile.sample((x, z) -> 0, 0, 0, 1, 4, () -> true));
assertThrows(CancellationException.class, () -> NoiseTile.sample((x, z) -> 0, 0, 0, 1, 4, false, () -> true));
}
}
@@ -13,6 +13,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -58,6 +59,7 @@ public class TerrainHeightSourceTest {
assertEquals("Terrain height", terrain.name());
assertFalse(terrain.usesSeed());
assertTrue(terrain.smooth());
assertEquals(-FLUID_HEIGHT, terrain.blockRange().lowest(), EXACT);
assertEquals(WORLD_HEIGHT - FLUID_HEIGHT, terrain.blockRange().highest(), EXACT);
}