Give noise styles an optional seed of their own

A style with a seed is rolled from that number and the world seed and
ignores the seed it is asked for, so every reader of the same seeded
style gets one shared noise. Readers that scale coordinates before
sampling read it flat in blocks. NoiseStyle.seed(), isSeeded(),
withSeed(); the NoiseFactory contract says what a seeded style does.

Ported from v3e a1bd24186.
This commit is contained in:
piexel committed 2026-11-05 21:02:06 +01:00
1 parent 3ba62e0c47
commit 2914ed168c
2 files changed
+32 -2

No files matched your search

@@ -10,6 +10,9 @@ public interface NoiseFactory {
* stage's random stream, when the noise must line up across chunks. The host keeps what it builds for the pack * stage's random stream, when the noise must line up across chunks. The host keeps what it builds for the pack
* snapshot, so asking again for the same style and seed is cheap. * snapshot, so asking again for the same style and seed is cheap.
* *
* <p>A style with its own seed ({@link NoiseStyle#seed()}) ignores {@code seed}: the host rolls it from that number
* and the seed of the world the pack generates, so every reader of the same seeded style gets the same noise.
*
* <p>A style name the host does not know samples as {@link NoiseStyle#FLAT}, and an expression the pack does not * <p>A style name the host does not know samples as {@link NoiseStyle#FLAT}, and an expression the pack does not
* have falls back to the base style, as packs have always been read. An image map the pack does not have, or cannot * have falls back to the base style, as packs have always been read. An image map the pack does not have, or cannot
* use as noise, throws {@link IllegalArgumentException}. * use as noise, throws {@link IllegalArgumentException}.
@@ -10,6 +10,7 @@ import com.volmit.resource.PackResources;
import com.volmit.resource.ResourceId; import com.volmit.resource.ResourceId;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.OptionalLong;
/** How noise is shaped, as packs write it: {@code {"style": "SIMPLEX", "zoom": 4}}, or a snippet under /** How noise is shaped, as packs write it: {@code {"style": "SIMPLEX", "zoom": 4}}, or a snippet under
* {@code snippet/style/}. The style names one of the host's noise generators ({@link Reference#NOISE_STYLE}); an * {@code snippet/style/}. The style names one of the host's noise generators ({@link Reference#NOISE_STYLE}); an
@@ -59,6 +60,9 @@ public final class NoiseStyle {
@Desc("The power the noise is raised to: above 1 favours low values, below 1 high ones.") @Desc("The power the noise is raised to: above 1 favours low values, below 1 high ones.")
private double exponent = 1; private double exponent = 1;
@Desc("Rolls this noise from the world seed and this number, so every style with the same seed shares one noise. Child biomes, palettes and decorator placement read a seeded style flat in blocks: zoom is its only scale, so a layer's zoom, childShrinkFactor and the decorator's extra zoom don't apply.")
private Long seed;
private NoiseStyle() { private NoiseStyle() {
} }
@@ -72,6 +76,7 @@ public final class NoiseStyle {
multiplier = copy.multiplier; multiplier = copy.multiplier;
fracture = copy.fracture; fracture = copy.fracture;
exponent = copy.exponent; exponent = copy.exponent;
seed = copy.seed;
} }
/** The host's noise style {@code style}, such as {@code SIMPLEX}, with every other option at its default. */ /** The host's noise style {@code style}, such as {@code SIMPLEX}, with every other option at its default. */
@@ -88,6 +93,13 @@ public final class NoiseStyle {
return changed; return changed;
} }
/** This style rolled from {@code seed} and the world seed instead of the seed it is asked for; see {@link #seed()}. */
public NoiseStyle withSeed(long seed) {
var changed = new NoiseStyle(this);
changed.seed = seed;
return changed;
}
/** The base style's name; {@link #FLAT} where the pack wrote none. */ /** The base style's name; {@link #FLAT} where the pack wrote none. */
public String style() { public String style() {
return style; return style;
@@ -131,6 +143,21 @@ public final class NoiseStyle {
return exponent; return exponent;
} }
/** The style's own seed, when the pack gave it one. A seeded style is one noise per world: {@link NoiseFactory#sampler}
* rolls it from this number and the world seed and ignores the seed it is asked for, so every reader of a style with
* the same seed and shape gets the same values. Readers that scale coordinates before sampling (a palette layer's
* zoom, a decorator's zoom, a child biome's shrink factor) read a seeded style in plain blocks instead, so
* {@link #zoom()} is its only scale.
*/
public OptionalLong seed() {
return seed == null ? OptionalLong.empty() : OptionalLong.of(seed);
}
/** Whether the pack gave this style its own {@link #seed()}. */
public boolean isSeeded() {
return seed != null;
}
/** Whether the style gives no noise: the {@link #FLAT} base style without an expression or image map. */ /** Whether the style gives no noise: the {@link #FLAT} base style without an expression or image map. */
public boolean isFlat() { public boolean isFlat() {
return FLAT.equals(style) && expression == null && imageMap == null; return FLAT.equals(style) && expression == null && imageMap == null;
@@ -143,12 +170,12 @@ public final class NoiseStyle {
&& Double.compare(cellularZoom, that.cellularZoom) == 0 && Double.compare(zoom, that.zoom) == 0 && Double.compare(cellularZoom, that.cellularZoom) == 0 && Double.compare(zoom, that.zoom) == 0
&& Objects.equals(expression, that.expression) && Objects.equals(imageMap, that.imageMap) && Objects.equals(expression, that.expression) && Objects.equals(imageMap, that.imageMap)
&& Double.compare(multiplier, that.multiplier) == 0 && Objects.equals(fracture, that.fracture) && Double.compare(multiplier, that.multiplier) == 0 && Objects.equals(fracture, that.fracture)
&& Double.compare(exponent, that.exponent) == 0; && Double.compare(exponent, that.exponent) == 0 && Objects.equals(seed, that.seed);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(style, cellularFrequency, cellularZoom, zoom, expression, imageMap, multiplier, fracture, exponent); return Objects.hash(style, cellularFrequency, cellularZoom, zoom, expression, imageMap, multiplier, fracture, exponent, seed);
} }
@Override @Override