NoiseSampler.signed: the sample mapped to -1..1, which a host may answer from a per-thread coordinate memo

This commit is contained in:
piexel committed 2026-10-10 18:06:46 +02:00
1 parent d2d746734f
commit 24eeeab615
4 files changed
+55

No files matched your search

@@ -9,4 +9,18 @@ public interface NoiseSampler {
double sample(double worldX, double worldZ);
double sample(double worldX, double y, double worldZ);
/** {@link #sample(double, double)} mapped from 0..1 to -1..1 ({@code sample * 2 - 1}), for noise that is added up
* and compared around zero. A host may answer a repeated read of the same coordinates from a per-thread memo, so a
* caller that samples the same field at the same points again and again (a carver testing densities) should read
* this instead of mapping {@link #sample(double, double)} itself.
*/
default double signed(double worldX, double worldZ) {
return sample(worldX, worldZ) * 2D - 1D;
}
/** The three-dimensional counterpart of {@link #signed(double, double)}. */
default double signed(double worldX, double y, double worldZ) {
return sample(worldX, y, worldZ) * 2D - 1D;
}
}
@@ -0,0 +1,35 @@
package com.volmit.noise;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class NoiseSamplerTest {
private static final double EXACT = 0D;
@Test
void signedMapsTheUnitSampleToMinusOneAndOne() {
NoiseSampler noise = new NoiseSampler() {
public double sample(double worldX, double worldZ) { return worldX / 10D; }
public double sample(double worldX, double y, double worldZ) { return y / 10D; }
};
assertEquals(-1D, noise.signed(0D, 5D), EXACT);
assertEquals(1D, noise.signed(10D, 5D), EXACT);
assertEquals(0.5D * 2D - 1D, noise.signed(5D, 0D), EXACT);
assertEquals(-1D, noise.signed(3D, 0D, 3D), EXACT);
assertEquals(0.3D * 2D - 1D, noise.signed(3D, 3D, 3D), EXACT);
}
@Test
void aHostMayAnswerSignedItself() {
NoiseSampler noise = new NoiseSampler() {
public double sample(double worldX, double worldZ) { return 0.25D; }
public double sample(double worldX, double y, double worldZ) { return 0.25D; }
@Override public double signed(double worldX, double worldZ) { return 7D; }
};
assertEquals(7D, noise.signed(1D, 1D), EXACT);
assertEquals(0.25D * 2D - 1D, noise.signed(1D, 1D, 1D), EXACT);
}
}