| 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.Interval;
|
| 6 | import net.minecraft.world.level.levelgen.blending.Blender;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 9 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 13 |
|
| 14 | public record BlendDensityFunction(DensityFunction input) implements DensityFunction {
|
| 15 | public static final MapCodec<BlendDensityFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 16 | i -> i.group(DensityFunction.CODEC.fieldOf("input").forGetter(BlendDensityFunction::input)).apply(i, BlendDensityFunction::new)
|
| 17 | );
|
| 18 |
|
| 19 | @Override
|
| 20 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 21 | return new BlendDensityFunction.Sampler(this.input.compileSampler(context));
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 26 | DensityFunction input = rule.rewrite(this.input);
|
| 27 | return input == this.input ? this : new BlendDensityFunction(input);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public Interval range() {
|
| 32 | return this.input.range();
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public @DensityFunction.Axes int domainAxes() {
|
| 37 | return this.input.domainAxes();
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public MapCodec<BlendDensityFunction> codec() {
|
| 42 | return CODEC;
|
| 43 | }
|
| 44 |
|
| 45 | private record Sampler(DensitySampler input) implements DensitySampler {
|
| 46 | @Override
|
| 47 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 48 | this.input.sampleVolume(context, outputBuffer, volume);
|
| 49 | Blender blender = context.getField(Blender.CONTEXT_KEY);
|
| 50 | if (blender != null && !blender.isEmpty()) {
|
| 51 | int index = 0;
|
| 52 |
|
| 53 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 54 | int blockZ = volume.blockZ(z);
|
| 55 |
|
| 56 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 57 | int blockX = volume.blockX(x);
|
| 58 |
|
| 59 | for (int y = 0; y < volume.sizeY(); y++) {
|
| 60 | int blockY = volume.blockY(y);
|
| 61 | outputBuffer.set(index, blender.blendDensity(blockX, blockY, blockZ, outputBuffer.get(index)));
|
| 62 | index++;
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 71 | float input = this.input.sampleValue(context, blockX, blockY, blockZ);
|
| 72 | Blender blender = context.getField(Blender.CONTEXT_KEY);
|
| 73 | return blender != null && !blender.isEmpty() ? blender.blendDensity(blockX, blockY, blockZ, input) : input;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|