| 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.util.Mth;
|
| 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 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.generator.ConstantFunction;
|
| 15 |
|
| 16 | public record LerpFunction(DensityFunction alpha, DensityFunction first, DensityFunction second) implements DensityFunction {
|
| 17 | public static final MapCodec<LerpFunction> CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | DensityFunction.CODEC.fieldOf("alpha").forGetter(LerpFunction::alpha),
|
| 20 | DensityFunction.CODEC.fieldOf("first").forGetter(LerpFunction::first),
|
| 21 | DensityFunction.CODEC.fieldOf("second").forGetter(LerpFunction::second)
|
| 22 | )
|
| 23 | .apply(i, LerpFunction::new)
|
| 24 | );
|
| 25 |
|
| 26 | @Override
|
| 27 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 28 | DensitySampler alpha = this.alpha.compileSampler(context);
|
| 29 | DensitySampler first = this.first.compileSampler(context);
|
| 30 | DensitySampler second = this.second.compileSampler(context);
|
| 31 | if (this.first instanceof ConstantFunction(float firstValue)) {
|
| 32 | return new LerpFunction.ConstFirstSampler(alpha, firstValue, second);
|
| 33 | } else {
|
| 34 | return this.second instanceof ConstantFunction(float secondValue)
|
| 35 | ? new LerpFunction.ConstSecondSampler(alpha, first, secondValue)
|
| 36 | : new LerpFunction.Sampler(alpha, first, second);
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 42 | DensityFunction alpha = rule.rewrite(this.alpha);
|
| 43 | DensityFunction first = rule.rewrite(this.first);
|
| 44 | DensityFunction second = rule.rewrite(this.second);
|
| 45 | return alpha == this.alpha && first == this.first && second == this.second ? this : new LerpFunction(alpha, first, second);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public Interval range() {
|
| 50 | return Interval.lerp(this.alpha.range(), this.first.range(), this.second.range());
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public @DensityFunction.Axes int domainAxes() {
|
| 55 | return this.alpha.domainAxes() | this.first.domainAxes() | this.second.domainAxes();
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public MapCodec<LerpFunction> codec() {
|
| 60 | return CODEC;
|
| 61 | }
|
| 62 |
|
| 63 | public record ConstFirstSampler(DensitySampler alpha, float first, DensitySampler second) implements DensitySampler {
|
| 64 | @Override
|
| 65 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 66 | this.alpha.sampleVolume(context, outputBuffer, volume);
|
| 67 |
|
| 68 | try (ScopedDensityBuffer secondBuffer = context.acquireBuffer(volume)) {
|
| 69 | this.second.sampleVolume(context, secondBuffer, volume);
|
| 70 |
|
| 71 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 72 | float alpha = outputBuffer.get(i);
|
| 73 | float second = secondBuffer.get(i);
|
| 74 | if (alpha == 0.0F) {
|
| 75 | outputBuffer.set(i, this.first);
|
| 76 | } else if (alpha == 1.0F) {
|
| 77 | outputBuffer.set(i, second);
|
| 78 | } else {
|
| 79 | outputBuffer.set(i, Mth.lerp(alpha, this.first, second));
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 87 | float alpha = this.alpha.sampleValue(context, blockX, blockY, blockZ);
|
| 88 | if (alpha == 0.0F) {
|
| 89 | return this.first;
|
| 90 | } else {
|
| 91 | return alpha == 1.0F
|
| 92 | ? this.second.sampleValue(context, blockX, blockY, blockZ)
|
| 93 | : Mth.lerp(alpha, this.first, this.second.sampleValue(context, blockX, blockY, blockZ));
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | public record ConstSecondSampler(DensitySampler alpha, DensitySampler first, float second) implements DensitySampler {
|
| 99 | @Override
|
| 100 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 101 | this.alpha.sampleVolume(context, outputBuffer, volume);
|
| 102 |
|
| 103 | try (ScopedDensityBuffer firstBuffer = context.acquireBuffer(volume)) {
|
| 104 | this.first.sampleVolume(context, firstBuffer, volume);
|
| 105 |
|
| 106 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 107 | float alpha = outputBuffer.get(i);
|
| 108 | float first = firstBuffer.get(i);
|
| 109 | if (alpha == 0.0F) {
|
| 110 | outputBuffer.set(i, first);
|
| 111 | } else if (alpha == 1.0F) {
|
| 112 | outputBuffer.set(i, this.second);
|
| 113 | } else {
|
| 114 | outputBuffer.set(i, Mth.lerp(alpha, first, this.second));
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 122 | float alpha = this.alpha.sampleValue(context, blockX, blockY, blockZ);
|
| 123 | if (alpha == 0.0F) {
|
| 124 | return this.first.sampleValue(context, blockX, blockY, blockZ);
|
| 125 | } else {
|
| 126 | return alpha == 1.0F ? this.second : Mth.lerp(alpha, this.first.sampleValue(context, blockX, blockY, blockZ), this.second);
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | public record Sampler(DensitySampler alpha, DensitySampler first, DensitySampler second) implements DensitySampler {
|
| 132 | @Override
|
| 133 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 134 | this.alpha.sampleVolume(context, outputBuffer, volume);
|
| 135 |
|
| 136 | try (ScopedDensityBuffer firstBuffer = context.acquireBuffer(volume)) {
|
| 137 | this.first.sampleVolume(context, firstBuffer, volume);
|
| 138 |
|
| 139 | try (ScopedDensityBuffer secondBuffer = context.acquireBuffer(volume)) {
|
| 140 | this.second.sampleVolume(context, secondBuffer, volume);
|
| 141 |
|
| 142 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 143 | float alpha = outputBuffer.get(i);
|
| 144 | float first = firstBuffer.get(i);
|
| 145 | float second = secondBuffer.get(i);
|
| 146 | if (alpha == 0.0F) {
|
| 147 | outputBuffer.set(i, first);
|
| 148 | } else if (alpha == 1.0F) {
|
| 149 | outputBuffer.set(i, second);
|
| 150 | } else {
|
| 151 | outputBuffer.set(i, Mth.lerp(alpha, first, second));
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 160 | float alpha = this.alpha.sampleValue(context, blockX, blockY, blockZ);
|
| 161 | if (alpha == 0.0F) {
|
| 162 | return this.first.sampleValue(context, blockX, blockY, blockZ);
|
| 163 | } else {
|
| 164 | return alpha == 1.0F
|
| 165 | ? this.second.sampleValue(context, blockX, blockY, blockZ)
|
| 166 | : Mth.lerp(alpha, this.first.sampleValue(context, blockX, blockY, blockZ), this.second.sampleValue(context, blockX, blockY, blockZ));
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 |
|