| 1 | package net.minecraft.world.level.levelgen.densityfunction.generator;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.util.Interval;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 9 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunctions;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 15 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 16 | import net.minecraft.world.level.levelgen.synth.Noise;
|
| 17 | import net.minecraft.world.level.levelgen.synth.NormalNoise;
|
| 18 |
|
| 19 | public record NoiseFunction(
|
| 20 | Holder<NormalNoise> noise, @Deprecated double xzScale, double yScale, DensityFunction shiftX, DensityFunction shiftY, DensityFunction shiftZ
|
| 21 | ) implements DensityFunction {
|
| 22 | public static final MapCodec<NoiseFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 23 | i -> i.group(
|
| 24 | NormalNoise.CODEC.fieldOf("noise").forGetter(NoiseFunction::noise),
|
| 25 | Codec.DOUBLE.fieldOf("xz_scale").forGetter(NoiseFunction::xzScale),
|
| 26 | Codec.DOUBLE.fieldOf("y_scale").forGetter(NoiseFunction::yScale),
|
| 27 | DensityFunction.CODEC.optionalFieldOf("shift_x", DensityFunctions.zero()).forGetter(NoiseFunction::shiftX),
|
| 28 | DensityFunction.CODEC.optionalFieldOf("shift_y", DensityFunctions.zero()).forGetter(NoiseFunction::shiftY),
|
| 29 | DensityFunction.CODEC.optionalFieldOf("shift_z", DensityFunctions.zero()).forGetter(NoiseFunction::shiftZ)
|
| 30 | )
|
| 31 | .apply(i, NoiseFunction::new)
|
| 32 | );
|
| 33 |
|
| 34 | @Override
|
| 35 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 36 | Noise noise = context.createNoiseSampler(this.noise);
|
| 37 | if (this.shiftX.equals(DensityFunctions.zero()) && this.shiftY.equals(DensityFunctions.zero()) && this.shiftZ.equals(DensityFunctions.zero())) {
|
| 38 | return new NoiseFunction.Sampler(noise, this.xzScale, this.yScale);
|
| 39 | }
|
| 40 |
|
| 41 | DensitySampler shiftX = this.shiftX.compileSampler(context);
|
| 42 | DensitySampler shiftZ = this.shiftZ.compileSampler(context);
|
| 43 | if (this.shiftY.equals(DensityFunctions.zero())) {
|
| 44 | return new NoiseFunction.ShiftedXzSampler(shiftX, shiftZ, noise, this.xzScale, this.yScale);
|
| 45 | }
|
| 46 |
|
| 47 | DensitySampler shiftY = this.shiftY.compileSampler(context);
|
| 48 | return new NoiseFunction.ShiftedXyzSampler(shiftX, shiftY, shiftZ, noise, this.xzScale, this.yScale);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 53 | DensityFunction shiftX = rule.rewrite(this.shiftX);
|
| 54 | DensityFunction shiftY = rule.rewrite(this.shiftY);
|
| 55 | DensityFunction shiftZ = rule.rewrite(this.shiftZ);
|
| 56 | return shiftX == this.shiftX && shiftY == this.shiftY && shiftZ == this.shiftZ
|
| 57 | ? this
|
| 58 | : new NoiseFunction(this.noise, this.xzScale, this.yScale, shiftX, shiftY, shiftZ);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public Interval range() {
|
| 63 | return this.noise.value().range();
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public @DensityFunction.Axes int domainAxes() {
|
| 68 | int axes = 7;
|
| 69 | if (this.yScale == 0.0) {
|
| 70 | axes &= -3;
|
| 71 | }
|
| 72 |
|
| 73 | if (this.xzScale == 0.0) {
|
| 74 | axes &= -6;
|
| 75 | }
|
| 76 |
|
| 77 | return axes | this.shiftX.domainAxes() | this.shiftY.domainAxes() | this.shiftZ.domainAxes();
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public MapCodec<NoiseFunction> codec() {
|
| 82 | return CODEC;
|
| 83 | }
|
| 84 |
|
| 85 | public record Sampler(Noise noise, double xzScale, double yScale) implements DensitySampler {
|
| 86 | @Override
|
| 87 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 88 | outputBuffer.fill(0.0F);
|
| 89 | this.noise.addToVolume(outputBuffer, volume, this.xzScale, this.yScale, 1.0F);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 94 | return this.noise.get(blockX * this.xzScale, blockY * this.yScale, blockZ * this.xzScale);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | public record ShiftedXyzSampler(DensitySampler shiftX, DensitySampler shiftY, DensitySampler shiftZ, Noise noise, double xzScale, double yScale)
|
| 99 | implements DensitySampler {
|
| 100 | @Override
|
| 101 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 102 | this.shiftX.sampleVolume(context, outputBuffer, volume);
|
| 103 |
|
| 104 | try (ScopedDensityBuffer shiftYBuffer = context.acquireBuffer(volume)) {
|
| 105 | this.shiftY.sampleVolume(context, shiftYBuffer, volume);
|
| 106 |
|
| 107 | try (ScopedDensityBuffer shiftZBuffer = context.acquireBuffer(volume)) {
|
| 108 | this.shiftZ.sampleVolume(context, shiftZBuffer, volume);
|
| 109 | int index = 0;
|
| 110 |
|
| 111 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 112 | double baseNoiseZ = volume.blockZ(z) * this.xzScale;
|
| 113 |
|
| 114 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 115 | double baseNoiseX = volume.blockX(x) * this.xzScale;
|
| 116 |
|
| 117 | for (int y = 0; y < volume.sizeY(); y++) {
|
| 118 | double noiseX = baseNoiseX + outputBuffer.get(index);
|
| 119 | double noiseY = volume.blockY(y) * this.yScale + shiftYBuffer.get(index);
|
| 120 | double noiseZ = baseNoiseZ + shiftZBuffer.get(index);
|
| 121 | outputBuffer.set(index, this.noise.get(noiseX, noiseY, noiseZ));
|
| 122 | index++;
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 132 | double x = blockX * this.xzScale + this.shiftX.sampleValue(context, blockX, blockY, blockZ);
|
| 133 | double y = blockY * this.yScale + this.shiftY.sampleValue(context, blockX, blockY, blockZ);
|
| 134 | double z = blockZ * this.xzScale + this.shiftZ.sampleValue(context, blockX, blockY, blockZ);
|
| 135 | return this.noise.get(x, y, z);
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | public record ShiftedXzSampler(DensitySampler shiftX, DensitySampler shiftZ, Noise noise, double xzScale, double yScale) implements DensitySampler {
|
| 140 | @Override
|
| 141 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 142 | this.shiftX.sampleVolume(context, outputBuffer, volume);
|
| 143 |
|
| 144 | try (ScopedDensityBuffer shiftZBuffer = context.acquireBuffer(volume)) {
|
| 145 | this.shiftZ.sampleVolume(context, shiftZBuffer, volume);
|
| 146 | int index = 0;
|
| 147 |
|
| 148 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 149 | double baseNoiseZ = volume.blockZ(z) * this.xzScale;
|
| 150 |
|
| 151 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 152 | double baseNoiseX = volume.blockX(x) * this.xzScale;
|
| 153 |
|
| 154 | for (int y = 0; y < volume.sizeY(); y++) {
|
| 155 | double noiseX = baseNoiseX + outputBuffer.get(index);
|
| 156 | double noiseY = volume.blockY(y) * this.yScale;
|
| 157 | double noiseZ = baseNoiseZ + shiftZBuffer.get(index);
|
| 158 | outputBuffer.set(index, this.noise.get(noiseX, noiseY, noiseZ));
|
| 159 | index++;
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 168 | double x = blockX * this.xzScale + this.shiftX.sampleValue(context, blockX, blockY, blockZ);
|
| 169 | double y = blockY * this.yScale;
|
| 170 | double z = blockZ * this.xzScale + this.shiftZ.sampleValue(context, blockX, blockY, blockZ);
|
| 171 | return this.noise.get(x, y, z);
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|