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

@@ -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);
}