Merge pull request #1264 from VolmitSoftware/fix/fracture-mirror

Stop fractured noise from mirroring across x = z
This commit is contained in:
Brian Fopiano authored and GitHub committed 2026-11-23 13:42:18 -05:00
commit 435ed10648
8 files changed
+197 -133

No files matched your search

@@ -62,6 +62,9 @@ public class IrisNoiseGenerator {
private static final long GENERATOR_SEED_SALT = 33_955_677L;
private static final int MINIMUM_FRACTURE_DEPTH = 4;
private static final int MAXIMUM_FRACTURE_NODES = 32;
// Fracture axes as CNG.reseededForAxis numbers them for a 2D read: x is axis 0, z is axis 1.
static final int X_AXIS = 0;
static final int Z_AXIS = 1;
private static final ThreadLocal<FractureSamples> FRACTURE_SAMPLES = ThreadLocal.withInitial(FractureSamples::new);
@Getter(AccessLevel.NONE)
@@ -144,6 +147,14 @@ public class IrisNoiseGenerator {
}
double getNoise(long superSeed, double xv, double zv, IrisData data, Engine engine) {
return getNoiseForAxis(superSeed, xv, zv, data, engine, X_AXIS);
}
/**
* This layer read as the fracture of one axis. Each axis samples this layer's generator reseeded for that axis at
* the same coordinates, so the x and z warps are uncorrelated instead of mirror images of each other.
*/
private double getNoiseForAxis(long superSeed, double xv, double zv, IrisData data, Engine engine, int axis) {
if (!enabled) {
return offsetY;
}
@@ -154,7 +165,7 @@ public class IrisNoiseGenerator {
samples.active = true;
try {
if (samples.prepare(this, superSeed, data, engine)) {
return samples.sample(xv, zv);
return samples.sample(xv, zv, axis);
}
} finally {
samples.clear();
@@ -169,13 +180,13 @@ public class IrisNoiseGenerator {
for (IrisNoiseGenerator i : fracture) {
if (i.isEnabled()) {
double fractureOffset = i.getOpacity() / 2D;
x += i.getNoise(superSeed + seed + g, xv, zv, data, engine) - fractureOffset;
z += i.getNoise(superSeed + seed + g, zv, xv, data, engine) - fractureOffset;
x += i.getNoiseForAxis(superSeed + seed + g, xv, zv, data, engine, X_AXIS) - fractureOffset;
z += i.getNoiseForAxis(superSeed + seed + g, xv, zv, data, engine, Z_AXIS) - fractureOffset;
}
g += 819;
}
return sample(getGenerator(superSeed, data, engine), x, z);
return sample(getGenerator(superSeed, data, engine).reseededForAxis(axis), x, z);
}
boolean supportsBatch() {
@@ -200,6 +211,10 @@ public class IrisNoiseGenerator {
}
NoiseLayerProgram compileProgram(long superSeed, IrisData data, Engine engine) {
return compileProgram(superSeed, data, engine, false);
}
private NoiseLayerProgram compileProgram(long superSeed, IrisData data, Engine engine, boolean readAsFracture) {
if (!enabled) {
return NoiseLayerProgram.disabled(this);
}
@@ -215,13 +230,14 @@ public class IrisNoiseGenerator {
int compiled = 0;
for (IrisNoiseGenerator child : fracture) {
if (child.isEnabled()) {
children[compiled] = child.compileProgram(superSeed + seed + salt, data, engine);
children[compiled] = child.compileProgram(superSeed + seed + salt, data, engine, true);
offsets[compiled++] = child.getOpacity() / 2D;
}
salt += 819;
}
CNG generator = Objects.requireNonNull(getGenerator(superSeed, data, engine), "Noise layer generator");
return NoiseLayerProgram.of(this, NoiseProgram.compile2D(generator), children, offsets);
NoiseProgram zAxisProgram = readAsFracture ? NoiseProgram.compile2D(generator.reseededForAxis(Z_AXIS)) : null;
return NoiseLayerProgram.of(this, NoiseProgram.compile2D(generator), zAxisProgram, children, offsets);
}
public KList<IrisNoiseGenerator> getAllComposites() {
@@ -357,20 +373,26 @@ public class IrisNoiseGenerator {
return false;
}
private double sample(double x, double z) {
/**
* Both axes of a fracture node read the same warped coordinates, since a reseeded generator shares the
* node's own fracture, so each level samples its generator once per axis.
*/
private double sample(double x, double z, int rootAxis) {
int leaf = size - 1;
double forward = nodes[leaf].sample(generators[leaf], x, z);
double reverse = nodes[leaf].sample(generators[leaf], z, x);
for (int index = leaf - 1; index >= 0; index--) {
double offset = nodes[index + 1].opacity / 2D;
double forwardX = x + (forward - offset);
double forwardZ = z + (reverse - offset);
forward = nodes[index].sample(generators[index], forwardX, forwardZ);
if (index > 0) {
reverse = nodes[index].sample(generators[index], forwardZ, forwardX);
}
if (leaf == 0) {
return nodes[0].sample(generators[0].reseededForAxis(rootAxis), x, z);
}
return forward;
double warpX = nodes[leaf].sample(generators[leaf], x, z);
double warpZ = nodes[leaf].sample(generators[leaf].reseededForAxis(Z_AXIS), x, z);
for (int index = leaf - 1; index > 0; index--) {
double offset = nodes[index + 1].opacity / 2D;
double warpedX = x + (warpX - offset);
double warpedZ = z + (warpZ - offset);
warpX = nodes[index].sample(generators[index], warpedX, warpedZ);
warpZ = nodes[index].sample(generators[index].reseededForAxis(Z_AXIS), warpedX, warpedZ);
}
double offset = nodes[1].opacity / 2D;
return nodes[0].sample(generators[0].reseededForAxis(rootAxis), x + (warpX - offset), z + (warpZ - offset));
}
private void clear() {
@@ -9,6 +9,7 @@ public final class NoiseLayerProgram {
private final boolean enabled;
private final double offsetY;
private final NoiseProgram program;
private final NoiseProgram zAxisProgram;
private final double zoom;
private final double opacity;
private final double offsetX;
@@ -21,10 +22,12 @@ public final class NoiseLayerProgram {
private final NoiseLayerProgram[] children;
private final double[] childOffsets;
private NoiseLayerProgram(IrisNoiseGenerator layer, NoiseProgram program, NoiseLayerProgram[] children, double[] childOffsets) {
private NoiseLayerProgram(IrisNoiseGenerator layer, NoiseProgram program, NoiseProgram zAxisProgram,
NoiseLayerProgram[] children, double[] childOffsets) {
this.enabled = program != null;
this.offsetY = layer.getOffsetY();
this.program = program;
this.zAxisProgram = zAxisProgram;
this.zoom = layer.getZoom();
this.opacity = layer.getOpacity();
this.offsetX = layer.getOffsetX();
@@ -39,11 +42,16 @@ public final class NoiseLayerProgram {
}
static NoiseLayerProgram disabled(IrisNoiseGenerator layer) {
return new NoiseLayerProgram(layer, null, new NoiseLayerProgram[0], new double[0]);
return new NoiseLayerProgram(layer, null, null, new NoiseLayerProgram[0], new double[0]);
}
static NoiseLayerProgram of(IrisNoiseGenerator layer, NoiseProgram program, NoiseLayerProgram[] children, double[] childOffsets) {
return new NoiseLayerProgram(layer, program, children, childOffsets);
/**
* zAxisProgram is the layer's generator reseeded for the z axis; only a layer read as a fracture needs it, and the
* root layer passes null.
*/
static NoiseLayerProgram of(IrisNoiseGenerator layer, NoiseProgram program, NoiseProgram zAxisProgram,
NoiseLayerProgram[] children, double[] childOffsets) {
return new NoiseLayerProgram(layer, program, zAxisProgram, children, childOffsets);
}
public void evaluate(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] out) {
@@ -53,143 +61,110 @@ public final class NoiseLayerProgram {
}
int mark = scratch.mark();
try {
fill(scratch, xs, zs, count, out);
double[] sampleX = scratch.buffer(count);
double[] sampleZ = scratch.buffer(count);
warpedSampleCoordinates(scratch, xs, zs, count, sampleX, sampleZ);
program.fill2D(scratch.noise(), sampleX, sampleZ, count, out);
applyPost(out, count);
} finally {
scratch.reset(mark);
}
}
private void fill(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] out) {
double[] sampleX = scratch.buffer(count);
double[] sampleZ = scratch.buffer(count);
if (children.length == 0) {
for (int index = 0; index < count; index++) {
sampleX[index] = (xs[index] / zoom) + offsetX;
sampleZ[index] = (zs[index] / zoom) + offsetZ;
}
} else {
System.arraycopy(xs, 0, sampleX, 0, count);
System.arraycopy(zs, 0, sampleZ, 0, count);
double[] forward = scratch.buffer(count);
double[] reverse = scratch.buffer(count);
for (int child = 0; child < children.length; child++) {
children[child].evaluatePair(scratch, xs, zs, count, forward, reverse);
double offset = childOffsets[child];
for (int index = 0; index < count; index++) {
sampleX[index] += forward[index] - offset;
sampleZ[index] += reverse[index] - offset;
}
}
for (int index = 0; index < count; index++) {
sampleX[index] = (sampleX[index] / zoom) + offsetX;
sampleZ[index] = (sampleZ[index] / zoom) + offsetZ;
}
}
program.fill2D(scratch.noise(), sampleX, sampleZ, count, out);
for (int index = 0; index < count; index++) {
out[index] = post(out[index]);
}
}
void evaluatePair(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] forward, double[] reverse) {
/** Fills this layer read as a fracture: the x warp from its generator, the z warp from its z-axis generator. */
void evaluateAxes(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] xWarp, double[] zWarp) {
if (!enabled) {
Arrays.fill(forward, 0, count, offsetY);
Arrays.fill(reverse, 0, count, offsetY);
Arrays.fill(xWarp, 0, count, offsetY);
Arrays.fill(zWarp, 0, count, offsetY);
return;
}
int mark = scratch.mark();
try {
fillPair(scratch, xs, zs, count, forward, reverse);
double[] sampleX = scratch.buffer(count);
double[] sampleZ = scratch.buffer(count);
warpedSampleCoordinates(scratch, xs, zs, count, sampleX, sampleZ);
program.fill2D(scratch.noise(), sampleX, sampleZ, count, xWarp);
zAxisProgram.fill2D(scratch.noise(), sampleX, sampleZ, count, zWarp);
applyPost(xWarp, count);
applyPost(zWarp, count);
} finally {
scratch.reset(mark);
}
}
private void fillPair(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] forward, double[] reverse) {
int pairs = count * 2;
double[] sampleX = scratch.buffer(pairs);
double[] sampleZ = scratch.buffer(pairs);
private void warpedSampleCoordinates(TerrainBatchScratch scratch, double[] xs, double[] zs, int count,
double[] sampleX, double[] sampleZ) {
System.arraycopy(xs, 0, sampleX, 0, count);
System.arraycopy(zs, 0, sampleZ, 0, count);
System.arraycopy(zs, 0, sampleX, count, count);
System.arraycopy(xs, 0, sampleZ, count, count);
if (children.length > 0) {
double[] childForward = scratch.buffer(count);
double[] childReverse = scratch.buffer(count);
double[] childX = scratch.buffer(count);
double[] childZ = scratch.buffer(count);
for (int child = 0; child < children.length; child++) {
children[child].evaluatePair(scratch, xs, zs, count, childForward, childReverse);
children[child].evaluateAxes(scratch, xs, zs, count, childX, childZ);
double offset = childOffsets[child];
for (int index = 0; index < count; index++) {
sampleX[index] += childForward[index] - offset;
sampleZ[index] += childReverse[index] - offset;
sampleX[count + index] += childReverse[index] - offset;
sampleZ[count + index] += childForward[index] - offset;
sampleX[index] += childX[index] - offset;
sampleZ[index] += childZ[index] - offset;
}
}
}
for (int index = 0; index < pairs; index++) {
for (int index = 0; index < count; index++) {
sampleX[index] = (sampleX[index] / zoom) + offsetX;
sampleZ[index] = (sampleZ[index] / zoom) + offsetZ;
}
double[] values = scratch.buffer(pairs);
program.fill2D(scratch.noise(), sampleX, sampleZ, pairs, values);
for (int index = 0; index < count; index++) {
forward[index] = post(values[index]);
reverse[index] = post(values[count + index]);
}
}
public double sample(TerrainBatchScratch scratch, double x, double z) {
if (!enabled) {
return offsetY;
}
double sampleX = x;
double sampleZ = z;
if (children.length > 0) {
int mark = scratch.mark();
try {
double[] pair = scratch.buffer(2);
for (int child = 0; child < children.length; child++) {
children[child].samplePair(scratch, x, z, pair);
double offset = childOffsets[child];
sampleX += pair[0] - offset;
sampleZ += pair[1] - offset;
}
} finally {
scratch.reset(mark);
}
int mark = scratch.mark();
try {
double[] sampleCoordinates = scratch.buffer(2);
warpedSampleCoordinate(scratch, x, z, sampleCoordinates);
return post(program.sample2D(sampleCoordinates[0], sampleCoordinates[1]));
} finally {
scratch.reset(mark);
}
return post(program.sample2D((sampleX / zoom) + offsetX, (sampleZ / zoom) + offsetZ));
}
private void samplePair(TerrainBatchScratch scratch, double x, double z, double[] out) {
private void sampleAxes(TerrainBatchScratch scratch, double x, double z, double[] out) {
if (!enabled) {
out[0] = offsetY;
out[1] = offsetY;
return;
}
double forwardX = x;
double forwardZ = z;
double reverseX = z;
double reverseZ = x;
int mark = scratch.mark();
try {
double[] sampleCoordinates = scratch.buffer(2);
warpedSampleCoordinate(scratch, x, z, sampleCoordinates);
out[0] = post(program.sample2D(sampleCoordinates[0], sampleCoordinates[1]));
out[1] = post(zAxisProgram.sample2D(sampleCoordinates[0], sampleCoordinates[1]));
} finally {
scratch.reset(mark);
}
}
private void warpedSampleCoordinate(TerrainBatchScratch scratch, double x, double z, double[] out) {
double sampleX = x;
double sampleZ = z;
if (children.length > 0) {
int mark = scratch.mark();
try {
double[] pair = scratch.buffer(2);
for (int child = 0; child < children.length; child++) {
children[child].samplePair(scratch, x, z, pair);
double offset = childOffsets[child];
forwardX += pair[0] - offset;
forwardZ += pair[1] - offset;
reverseX += pair[1] - offset;
reverseZ += pair[0] - offset;
}
} finally {
scratch.reset(mark);
double[] warp = scratch.buffer(2);
for (int child = 0; child < children.length; child++) {
children[child].sampleAxes(scratch, x, z, warp);
double offset = childOffsets[child];
sampleX += warp[0] - offset;
sampleZ += warp[1] - offset;
}
}
out[0] = post(program.sample2D((forwardX / zoom) + offsetX, (forwardZ / zoom) + offsetZ));
out[1] = post(program.sample2D((reverseX / zoom) + offsetX, (reverseZ / zoom) + offsetZ));
out[0] = (sampleX / zoom) + offsetX;
out[1] = (sampleZ / zoom) + offsetZ;
}
private void applyPost(double[] values, int count) {
for (int index = 0; index < count; index++) {
values[index] = post(values[index]);
}
}
private double post(double raw) {
@@ -143,7 +143,7 @@ public final class NoiseStyleCompiler implements StyleCompiler {
Field center = new Field.Const(-child.getOpacity() / 2.0);
String childPath = path + "/f" + index + "_" + child.getSeed();
warpX = sum(warpX, binary(Field.BinaryOp.ADD, layer(child, domain, emit, childPath + "x"), center));
warpZ = sum(warpZ, binary(Field.BinaryOp.ADD, layer(child, domain.swapped(), emit, childPath + "z"), center));
warpZ = sum(warpZ, binary(Field.BinaryOp.ADD, layer(child, domain, emit, childPath + "z"), center));
}
Domain sampled = domain.layer(positive(layer.getZoom(), "layer zoom"), layer.getOffsetX(), layer.getOffsetZ(), warpX, warpZ);
Field value = style(layer.getStyle(), layer.getOctaves(), sampled, emit, path);
@@ -208,7 +208,7 @@ public final class NoiseStyleCompiler implements StyleCompiler {
Displacement leafDisplacement = displacement;
if (warped(cng)) {
Field warpX = displacement(node(cng.getFracture(), displacement, emit, path + "/x"), cng.getFscale());
Field warpZ = displacement(node(cng.getFracture(), displacement.swapped(), emit, path + "/z"), cng.getFscale());
Field warpZ = displacement(node(cng.getFracture(), displacement, emit, path + "/z"), cng.getFscale());
leafDisplacement = displacement.warped(warpX, warpZ);
}
value = leaf(type, cng.getOct(), cng.getEffectiveScale() * multiplier, leafDisplacement, emit, path);
@@ -483,19 +483,11 @@ public final class NoiseStyleCompiler implements StyleCompiler {
sum(warpX, layerWarpX == null ? null : scaled(layerWarpX, zoom)),
sum(warpZ, layerWarpZ == null ? null : scaled(layerWarpZ, zoom)));
}
private Domain swapped() {
return new Domain(zoom, offsetZ, offsetX, warpZ, warpX);
}
}
private record Displacement(double offsetX, double offsetZ, Field warpX, Field warpZ) {
private static final Displacement NONE = new Displacement(0.0, 0.0, null, null);
private Displacement swapped() {
return new Displacement(offsetZ, offsetX, warpZ, warpX);
}
private Displacement warped(Field addedX, Field addedZ) {
return new Displacement(offsetX, offsetZ, sum(warpX, addedX), sum(warpZ, addedZ));
}
@@ -85,7 +85,7 @@ public class IrisCaveCarver3DCellKernelTest {
@Test
public void packStyleProfilesSharingFieldsMatchGoldenWriteSequence() throws Exception {
assertScenario(Scenario.PACK_SHARED, 0xdbe20a1ca87f9cc7L, 269035);
assertScenario(Scenario.PACK_SHARED, 0xb464b50d050e766dL, 288720);
}
@Test
@@ -0,0 +1,68 @@
package art.arcane.iris.generation.noise;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.volmlib.util.noise.NoiseGenerator;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public final class IrisNoiseGeneratorFractureMirrorTest {
private static final long SEED = 71L;
private static final int GRID_SIZE = 40;
private static final double GRID_STEP = 7.3D;
private static final double MIRRORED = 1.0E-12D;
private static final int ALLOWED_MIRRORED_SAMPLES = 2;
@Test
public void aCompositeFractureIsNotMirroredAcrossTheDiagonal() {
IrisNoiseGenerator fractured = diagonalSymmetricRoot().setFracture(new KList<>(fracture()));
assertTrue(mirroredSamples(fractured) <= ALLOWED_MIRRORED_SAMPLES);
}
@Test
public void theDiagonalSymmetricRootIsMirroredWithoutAFracture() {
assertTrue(mirroredSamples(diagonalSymmetricRoot()) > ALLOWED_MIRRORED_SAMPLES);
}
private static int mirroredSamples(IrisNoiseGenerator noise) {
int mirroredSamples = 0;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = i + 1; j < GRID_SIZE; j++) {
double a = i * GRID_STEP;
double b = j * GRID_STEP + 0.5D;
if (Math.abs(noise.getNoise(SEED, a, b, null) - noise.getNoise(SEED, b, a, null)) < MIRRORED) {
mirroredSamples++;
}
}
}
return mirroredSamples;
}
private static IrisNoiseGenerator diagonalSymmetricRoot() {
IrisNoiseGenerator root = new IrisNoiseGenerator().setStyle(NoiseStyle.SIMPLEX.style()).setSeed(5L);
root.getGenerator(SEED, null, null).setGenerator(new DiagonalSymmetricGenerator());
return root;
}
private static IrisNoiseGenerator fracture() {
return new IrisNoiseGenerator().setStyle(NoiseStyle.SIMPLEX.style()).setSeed(9L).setZoom(40D).setOpacity(40D);
}
private static final class DiagonalSymmetricGenerator implements NoiseGenerator {
@Override
public double noise(double x) {
return noise(x, 0D, 0D);
}
@Override
public double noise(double x, double z) {
return noise(x, z, 0D);
}
@Override
public double noise(double x, double y, double z) {
return 0.5D + (Math.sin(x * 0.11D) + Math.sin(y * 0.11D) + Math.sin(z * 0.11D)) / 6D;
}
}
}
@@ -225,6 +225,11 @@ public final class IrisNoiseGeneratorFractureTest {
private static double reference(IrisNoiseGenerator node, long superSeed, double xv, double zv,
IrisData data, Engine engine) {
return reference(node, superSeed, xv, zv, data, engine, IrisNoiseGenerator.X_AXIS);
}
private static double reference(IrisNoiseGenerator node, long superSeed, double xv, double zv,
IrisData data, Engine engine, int axis) {
if (!node.isEnabled()) {
return node.getOffsetY();
}
@@ -234,14 +239,16 @@ public final class IrisNoiseGeneratorFractureTest {
for (IrisNoiseGenerator child : node.getFracture()) {
if (child.isEnabled()) {
double offset = child.getOpacity() / 2D;
x += reference(child, superSeed + node.getSeed() + salt, xv, zv, data, engine) - offset;
z += reference(child, superSeed + node.getSeed() + salt, zv, xv, data, engine) - offset;
long childSeed = superSeed + node.getSeed() + salt;
x += reference(child, childSeed, xv, zv, data, engine, IrisNoiseGenerator.X_AXIS) - offset;
z += reference(child, childSeed, xv, zv, data, engine, IrisNoiseGenerator.Z_AXIS) - offset;
}
salt += 819;
}
double sampleX = (x / node.getZoom()) + node.getOffsetX();
double sampleZ = (z / node.getZoom()) + node.getOffsetZ();
double result = node.getGenerator(superSeed, data, engine).noiseFast2D(sampleX, sampleZ) * node.getOpacity();
CNG generator = node.getGenerator(superSeed, data, engine).reseededForAxis(axis);
double result = generator.noiseFast2D(sampleX, sampleZ) * node.getOpacity();
result = node.isNegative() ? -result + node.getOpacity() : result;
result = (node.getExponent() != 1D ? result < 0D ? -Math.pow(-result, node.getExponent())
: Math.pow(result, node.getExponent()) : result) + node.getOffsetY();
@@ -18,7 +18,7 @@ public class NoiseSeedCohesionTest {
private static final long[] SEEDS = {813753L, -935731L, Long.MIN_VALUE};
private static final double[] COORDINATES = {-29_999_983.25D, -513D, -64D, -1D, 0D, 0.125D, 17D, 64D, 8192D, 29_999_983.25D};
private static final double PINNED_SAMPLE_SCALE = 1.0E9D;
private static final long PINNED_HASH_WINDOWS = 2597318414939754011L;
private static final long PINNED_HASH_WINDOWS = 6074446696529759284L;
private static final long PINNED_HASH_MAC = -602781966673993721L;
@Test
@@ -38,17 +38,17 @@ public class IrisMathNoiseHotPathParityTest {
.setStyle(NoiseStyle.PERLIN_IRIS.style())
.setExponent(1.15D);
assertEquals(0.5084984235498856D, noiseGenerator.getNoise(9988L, 31.25D, -17.5D, null), 0D);
assertEquals(0.4646529061250786D, noiseGenerator.getNoise(9988L, 31.25D, -17.5D, null), 0D);
IrisGeneratorStyle style = NoiseStyle.IRIS_DOUBLE.style();
assertEquals(0.6224926166486215D, style.createNoCache(new RNG(112233L), null).noiseFast2D(42.5D, -19.75D), 0D);
assertEquals(0.6855680984398492D, style.createNoCache(new RNG(112233L), null).noiseFast2D(42.5D, -19.75D), 0D);
IrisGenerator generator = new IrisGenerator()
.setZoom(9.5D)
.setOpacity(0.91D)
.setComposite(new KList<IrisNoiseGenerator>().qadd(noiseGenerator));
assertEquals(0.43997118860461754D, generator.getHeight(63.0D, -27.0D, 445566L), 0D);
assertEquals(0.5023367573389593D, generator.getHeight(63.0D, -27.0D, 445566L), 0D);
}
@Test