| 1 | package net.minecraft.world.level.levelgen.synth;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.util.Interval;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 9 |
|
| 10 | public class NoiseStack implements Noise {
|
| 11 | protected final NoiseStack.Layer[] layers;
|
| 12 | private final Interval range;
|
| 13 |
|
| 14 | private NoiseStack(final NoiseStack.Layer[] layers) {
|
| 15 | this.layers = layers;
|
| 16 | Interval range = Interval.ofExact(0.0F);
|
| 17 |
|
| 18 | for (NoiseStack.Layer layer : layers) {
|
| 19 | Interval layerRange = Interval.mul(layer.noise.range(), Interval.ofExact(layer.amplitude));
|
| 20 | range = Interval.add(range, layerRange);
|
| 21 | }
|
| 22 |
|
| 23 | this.range = range;
|
| 24 | }
|
| 25 |
|
| 26 | public static NoiseStack.Builder builder() {
|
| 27 | return new NoiseStack.Builder();
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public Interval range() {
|
| 32 | return this.range;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public float get(final double x, final double y, final double z) {
|
| 37 | float value = 0.0F;
|
| 38 |
|
| 39 | for (NoiseStack.Layer layer : this.layers) {
|
| 40 | double frequency = layer.frequency;
|
| 41 | value += layer.amplitude * layer.noise.get(x * frequency, y * frequency, z * frequency);
|
| 42 | }
|
| 43 |
|
| 44 | return value;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public float get(final double x, final double y) {
|
| 49 | float value = 0.0F;
|
| 50 |
|
| 51 | for (NoiseStack.Layer layer : this.layers) {
|
| 52 | double frequency = layer.frequency;
|
| 53 | value += layer.amplitude * layer.noise.get(x * frequency, y * frequency);
|
| 54 | }
|
| 55 |
|
| 56 | return value;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void addToVolume(final DensityBuffer buffer, final DensityVolume volume, final double xzScale, final double yScale, final float amplitude) {
|
| 61 | for (NoiseStack.Layer layer : this.layers) {
|
| 62 | double frequency = layer.frequency;
|
| 63 | layer.noise.addToVolume(buffer, volume, xzScale * frequency, yScale * frequency, amplitude * layer.amplitude);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @VisibleForTesting
|
| 68 | public Noise getLayer(final int index) {
|
| 69 | return this.layers[index].noise;
|
| 70 | }
|
| 71 |
|
| 72 | public static class Builder {
|
| 73 | private final List<NoiseStack.Layer> layers = new ArrayList<>();
|
| 74 |
|
| 75 | private Builder() {
|
| 76 | }
|
| 77 |
|
| 78 | public NoiseStack.Builder add(final Noise noise, final double frequency, final float amplitude) {
|
| 79 | this.layers.add(new NoiseStack.Layer(noise, frequency, amplitude));
|
| 80 | return this;
|
| 81 | }
|
| 82 |
|
| 83 | public NoiseStack.Builder addStack(final NoiseStack stack, final double frequency, final float amplitude) {
|
| 84 | for (NoiseStack.Layer layer : stack.layers) {
|
| 85 | this.layers.add(new NoiseStack.Layer(layer.noise, layer.frequency * frequency, layer.amplitude * amplitude));
|
| 86 | }
|
| 87 |
|
| 88 | return this;
|
| 89 | }
|
| 90 |
|
| 91 | public NoiseStack build() {
|
| 92 | NoiseStack.Layer[] layers = this.layers.toArray(NoiseStack.Layer[]::new);
|
| 93 | if (this.layers.stream().allMatch(layer -> layer.noise.getClass() == PerlinNoise.class)) {
|
| 94 | return new NoiseStack.Perlin(layers);
|
| 95 | } else {
|
| 96 | return this.layers.stream().allMatch(layer -> layer.noise.getClass() == SmearedPerlinNoise.class)
|
| 97 | ? new NoiseStack.SmearedPerlin(layers)
|
| 98 | : new NoiseStack(layers);
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | protected record Layer(Noise noise, double frequency, float amplitude) {
|
| 104 | }
|
| 105 |
|
| 106 | private static class Perlin extends NoiseStack {
|
| 107 | private Perlin(final NoiseStack.Layer[] layers) {
|
| 108 | super(layers);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public float get(final double x, final double y, final double z) {
|
| 113 | float value = 0.0F;
|
| 114 |
|
| 115 | for (NoiseStack.Layer layer : this.layers) {
|
| 116 | double frequency = layer.frequency;
|
| 117 | value += layer.amplitude * layer.noise.get(x * frequency, y * frequency, z * frequency);
|
| 118 | }
|
| 119 |
|
| 120 | return value;
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void addToVolume(final DensityBuffer buffer, final DensityVolume volume, final double xzScale, final double yScale, final float amplitude) {
|
| 125 | for (NoiseStack.Layer layer : this.layers) {
|
| 126 | double frequency = layer.frequency;
|
| 127 | layer.noise.addToVolume(buffer, volume, xzScale * frequency, yScale * frequency, amplitude * layer.amplitude);
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | private static class SmearedPerlin extends NoiseStack {
|
| 133 | private SmearedPerlin(final NoiseStack.Layer[] layers) {
|
| 134 | super(layers);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public float get(final double x, final double y, final double z) {
|
| 139 | float value = 0.0F;
|
| 140 |
|
| 141 | for (NoiseStack.Layer layer : this.layers) {
|
| 142 | double frequency = layer.frequency;
|
| 143 | value += layer.amplitude * layer.noise.get(x * frequency, y * frequency, z * frequency);
|
| 144 | }
|
| 145 |
|
| 146 | return value;
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public void addToVolume(final DensityBuffer buffer, final DensityVolume volume, final double xzScale, final double yScale, final float amplitude) {
|
| 151 | for (NoiseStack.Layer layer : this.layers) {
|
| 152 | double frequency = layer.frequency;
|
| 153 | layer.noise.addToVolume(buffer, volume, xzScale * frequency, yScale * frequency, amplitude * layer.amplitude);
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 |
|