| 1 | package net.minecraft.world.level.levelgen.densityfunction.op;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.util.ExtraCodecs;
|
| 6 | import net.minecraft.util.Interval;
|
| 7 | import net.minecraft.util.Mth;
|
| 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.DensitySampler;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 15 |
|
| 16 | public record InterpolatedFunction(DensityFunction input, int cellSizeXz, int cellSizeY) implements DensityFunction {
|
| 17 | public static final MapCodec<InterpolatedFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | DensityFunction.CODEC.fieldOf("input").forGetter(InterpolatedFunction::input),
|
| 20 | ExtraCodecs.POSITIVE_INT.fieldOf("cell_size_xz").forGetter(InterpolatedFunction::cellSizeXz),
|
| 21 | ExtraCodecs.POSITIVE_INT.fieldOf("cell_size_y").forGetter(InterpolatedFunction::cellSizeY)
|
| 22 | )
|
| 23 | .apply(i, InterpolatedFunction::new)
|
| 24 | );
|
| 25 |
|
| 26 | @Override
|
| 27 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 28 | return new InterpolatedFunction.Sampler(this.input.compileSampler(context), this.cellSizeXz, this.cellSizeY, 1.0F / this.cellSizeXz, 1.0F / this.cellSizeY);
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 33 | DensityFunction input = rule.rewrite(this.input);
|
| 34 | return input == this.input ? this : new InterpolatedFunction(input, this.cellSizeXz, this.cellSizeY);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public Interval range() {
|
| 39 | return this.input.range();
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public @DensityFunction.Axes int domainAxes() {
|
| 44 | return this.input.domainAxes();
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public MapCodec<InterpolatedFunction> codec() {
|
| 49 | return CODEC;
|
| 50 | }
|
| 51 |
|
| 52 | private record Sampler(DensitySampler input, int cellSizeXz, int cellSizeY, float cellSizeXzInv, float cellSizeYInv) implements DensitySampler {
|
| 53 | @Override
|
| 54 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 55 | if ((volume.stepBlockX() == this.cellSizeXz || volume.sizeX() == 1)
|
| 56 | && (volume.stepBlockY() == this.cellSizeY || volume.sizeY() == 1)
|
| 57 | && (volume.stepBlockZ() == this.cellSizeXz || volume.sizeZ() == 1)
|
| 58 | && Math.floorMod(volume.minBlockX(), this.cellSizeXz) == 0
|
| 59 | && Math.floorMod(volume.minBlockY(), this.cellSizeY) == 0
|
| 60 | && Math.floorMod(volume.minBlockZ(), this.cellSizeXz) == 0) {
|
| 61 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 62 | } else if (volume.stepBlockX() == 1 && volume.stepBlockY() == 1 && volume.stepBlockZ() == 1) {
|
| 63 | this.sampleWithBlockStep(context, outputBuffer, volume);
|
| 64 | } else {
|
| 65 | this.sampleWithNonBlockStep(context, outputBuffer, volume);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | private void sampleWithNonBlockStep(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 70 | DensityVolume blockVolume = new DensityVolume(
|
| 71 | volume.sizeX() * volume.stepBlockX(),
|
| 72 | volume.sizeY() * volume.stepBlockY(),
|
| 73 | volume.sizeZ() * volume.stepBlockZ(),
|
| 74 | volume.minBlockX(),
|
| 75 | volume.minBlockY(),
|
| 76 | volume.minBlockZ(),
|
| 77 | 1,
|
| 78 | 1,
|
| 79 | 1
|
| 80 | );
|
| 81 |
|
| 82 | try (ScopedDensityBuffer blockBuffer = context.acquireBuffer(blockVolume)) {
|
| 83 | this.sampleWithBlockStep(context, blockBuffer, blockVolume);
|
| 84 |
|
| 85 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 86 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 87 | for (int y = 0; y < volume.sizeY(); y++) {
|
| 88 | float value = blockBuffer.get(blockVolume.indexUnchecked(x * volume.stepBlockX(), y * volume.stepBlockY(), z * volume.stepBlockZ()));
|
| 89 | outputBuffer.set(volume.indexUnchecked(x, y, z), value);
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | private void sampleWithBlockStep(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 97 | int minCellX = Mth.floorDiv(volume.minBlockX(), this.cellSizeXz);
|
| 98 | int minCellY = Mth.floorDiv(volume.minBlockY(), this.cellSizeY);
|
| 99 | int minCellZ = Mth.floorDiv(volume.minBlockZ(), this.cellSizeXz);
|
| 100 | int maxCellX = Mth.floorDiv(volume.maxBlockX(), this.cellSizeXz);
|
| 101 | int maxCellY = Mth.floorDiv(volume.maxBlockY(), this.cellSizeY);
|
| 102 | int maxCellZ = Mth.floorDiv(volume.maxBlockZ(), this.cellSizeXz);
|
| 103 | int cellCountX = maxCellX - minCellX + 1;
|
| 104 | int cellCountY = maxCellY - minCellY + 1;
|
| 105 | int cellCountZ = maxCellZ - minCellZ + 1;
|
| 106 | DensityVolume cellVolume = new DensityVolume(
|
| 107 | Math.floorMod(volume.maxBlockX(), this.cellSizeXz) == 0 ? cellCountX : cellCountX + 1,
|
| 108 | Math.floorMod(volume.maxBlockY(), this.cellSizeY) == 0 ? cellCountY : cellCountY + 1,
|
| 109 | Math.floorMod(volume.maxBlockZ(), this.cellSizeXz) == 0 ? cellCountZ : cellCountZ + 1,
|
| 110 | minCellX * this.cellSizeXz,
|
| 111 | minCellY * this.cellSizeY,
|
| 112 | minCellZ * this.cellSizeXz,
|
| 113 | this.cellSizeXz,
|
| 114 | this.cellSizeY,
|
| 115 | this.cellSizeXz
|
| 116 | );
|
| 117 |
|
| 118 | try (ScopedDensityBuffer cellBuffer = context.acquireBuffer(cellVolume)) {
|
| 119 | this.input.sampleVolume(context, cellBuffer, cellVolume);
|
| 120 |
|
| 121 | for (int cellZ = 0; cellZ < cellCountZ; cellZ++) {
|
| 122 | int nextCellZ = Math.min(cellZ + 1, cellVolume.sizeZ() - 1);
|
| 123 |
|
| 124 | for (int cellX = 0; cellX < cellCountX; cellX++) {
|
| 125 | int nextCellX = Math.min(cellX + 1, cellVolume.sizeX() - 1);
|
| 126 | float v000 = cellBuffer.get(cellVolume.indexUnchecked(cellX, 0, cellZ));
|
| 127 | float v100 = cellBuffer.get(cellVolume.indexUnchecked(nextCellX, 0, cellZ));
|
| 128 | float v001 = cellBuffer.get(cellVolume.indexUnchecked(cellX, 0, nextCellZ));
|
| 129 | float v101 = cellBuffer.get(cellVolume.indexUnchecked(nextCellX, 0, nextCellZ));
|
| 130 |
|
| 131 | for (int cellY = 0; cellY < cellCountY; cellY++) {
|
| 132 | int nextCellY = Math.min(cellY + 1, cellVolume.sizeY() - 1);
|
| 133 | float v010 = cellBuffer.get(cellVolume.indexUnchecked(cellX, nextCellY, cellZ));
|
| 134 | float v110 = cellBuffer.get(cellVolume.indexUnchecked(nextCellX, nextCellY, cellZ));
|
| 135 | float v011 = cellBuffer.get(cellVolume.indexUnchecked(cellX, nextCellY, nextCellZ));
|
| 136 | float v111 = cellBuffer.get(cellVolume.indexUnchecked(nextCellX, nextCellY, nextCellZ));
|
| 137 | this.fillCell(outputBuffer, volume, cellVolume, cellX, cellY, cellZ, v000, v100, v010, v110, v001, v101, v011, v111);
|
| 138 | v000 = v010;
|
| 139 | v100 = v110;
|
| 140 | v001 = v011;
|
| 141 | v101 = v111;
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | private void fillCell(
|
| 149 | final DensityBuffer outputBuffer,
|
| 150 | final DensityVolume outputVolume,
|
| 151 | final DensityVolume cellVolume,
|
| 152 | final int cellX,
|
| 153 | final int cellY,
|
| 154 | final int cellZ,
|
| 155 | final float v000,
|
| 156 | final float v100,
|
| 157 | final float v010,
|
| 158 | final float v110,
|
| 159 | final float v001,
|
| 160 | final float v101,
|
| 161 | final float v011,
|
| 162 | final float v111
|
| 163 | ) {
|
| 164 | int cellOutputX = cellVolume.blockX(cellX) - outputVolume.minBlockX();
|
| 165 | int cellOutputY = cellVolume.blockY(cellY) - outputVolume.minBlockY();
|
| 166 | int cellOutputZ = cellVolume.blockZ(cellZ) - outputVolume.minBlockZ();
|
| 167 | int x0 = Math.max(0, -cellOutputX);
|
| 168 | int y0 = Math.max(0, -cellOutputY);
|
| 169 | int z0 = Math.max(0, -cellOutputZ);
|
| 170 | int x1 = Math.min(this.cellSizeXz, outputVolume.sizeX() - cellOutputX) - 1;
|
| 171 | int y1 = Math.min(this.cellSizeY, outputVolume.sizeY() - cellOutputY) - 1;
|
| 172 | int z1 = Math.min(this.cellSizeXz, outputVolume.sizeZ() - cellOutputZ) - 1;
|
| 173 |
|
| 174 | for (int z = z0; z <= z1; z++) {
|
| 175 | int outputZ = cellOutputZ + z;
|
| 176 | float alphaZ = z * this.cellSizeXzInv;
|
| 177 | float v00_ = Mth.lerp(alphaZ, v000, v001);
|
| 178 | float v01_ = Mth.lerp(alphaZ, v010, v011);
|
| 179 | float v10_ = Mth.lerp(alphaZ, v100, v101);
|
| 180 | float v11_ = Mth.lerp(alphaZ, v110, v111);
|
| 181 |
|
| 182 | for (int x = x0; x <= x1; x++) {
|
| 183 | int outputX = cellOutputX + x;
|
| 184 | float alphaX = x * this.cellSizeXzInv;
|
| 185 | float v_0_ = Mth.lerp(alphaX, v00_, v10_);
|
| 186 | float v_1_ = Mth.lerp(alphaX, v01_, v11_);
|
| 187 | float valueStep = (v_1_ - v_0_) * this.cellSizeYInv;
|
| 188 | float value = v_0_ + valueStep * y0;
|
| 189 | int outputIndex = outputVolume.indexUnchecked(outputX, cellOutputY + y0, outputZ);
|
| 190 |
|
| 191 | for (int y = y0; y <= y1; y++) {
|
| 192 | outputBuffer.set(outputIndex++, value);
|
| 193 | value += valueStep;
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 201 | int xInCell = Math.floorMod(blockX, this.cellSizeXz);
|
| 202 | int yInCell = Math.floorMod(blockY, this.cellSizeY);
|
| 203 | int zInCell = Math.floorMod(blockZ, this.cellSizeXz);
|
| 204 | if (xInCell == 0 && yInCell == 0 && zInCell == 0) {
|
| 205 | return this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 206 | }
|
| 207 |
|
| 208 | DensityVolume volume = new DensityVolume(2, 2, 2, blockX - xInCell, blockY - yInCell, blockZ - zInCell, this.cellSizeXz, this.cellSizeY, this.cellSizeXz);
|
| 209 |
|
| 210 | try (ScopedDensityBuffer inputBuffer = context.acquireBuffer(volume)) {
|
| 211 | this.input.sampleVolume(context, inputBuffer, volume);
|
| 212 | return Mth.lerp3(
|
| 213 | (float)xInCell / this.cellSizeXz,
|
| 214 | (float)yInCell / this.cellSizeY,
|
| 215 | (float)zInCell / this.cellSizeXz,
|
| 216 | inputBuffer.get(volume.indexUnchecked(0, 0, 0)),
|
| 217 | inputBuffer.get(volume.indexUnchecked(1, 0, 0)),
|
| 218 | inputBuffer.get(volume.indexUnchecked(0, 1, 0)),
|
| 219 | inputBuffer.get(volume.indexUnchecked(1, 1, 0)),
|
| 220 | inputBuffer.get(volume.indexUnchecked(0, 0, 1)),
|
| 221 | inputBuffer.get(volume.indexUnchecked(1, 0, 1)),
|
| 222 | inputBuffer.get(volume.indexUnchecked(0, 1, 1)),
|
| 223 | inputBuffer.get(volume.indexUnchecked(1, 1, 1))
|
| 224 | );
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 |
|