| 1 | package net.minecraft.world.level.levelgen.synth;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import net.minecraft.util.Interval;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 9 |
|
| 10 | public class PerlinNoise extends GradientNoise {
|
| 11 | public static final Interval RANGE = Interval.ofSymmetric(2.0F);
|
| 12 | public static final double STANDARD_DEVIATION = 0.2702247831245211;
|
| 13 |
|
| 14 | public PerlinNoise(final RandomSource random) {
|
| 15 | super(random);
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | public Interval range() {
|
| 20 | return RANGE;
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public float get(final double x, final double y) {
|
| 25 | return this.get(wrap(x), 0.0, wrap(y));
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public float get(final double _x, final double _y, final double _z) {
|
| 30 | double x = wrap(_x) + this.offsetX;
|
| 31 | double y = wrap(_y) + this.offsetY;
|
| 32 | double z = wrap(_z) + this.offsetZ;
|
| 33 | int floorX = Mth.floor(x);
|
| 34 | int floorY = Mth.floor(y);
|
| 35 | int floorZ = Mth.floor(z);
|
| 36 | float relativeX = (float)(x - floorX);
|
| 37 | float relativeY = (float)(y - floorY);
|
| 38 | float relativeZ = (float)(z - floorZ);
|
| 39 | return this.sampleAndLerp(floorX, floorY, floorZ, relativeX, relativeY, relativeZ, relativeY);
|
| 40 | }
|
| 41 |
|
| 42 | public float noiseWithDerivative(final double _x, final double _y, final double _z, final float[] derivativeOut) {
|
| 43 | double x = wrap(_x) + this.offsetX;
|
| 44 | double y = wrap(_y) + this.offsetY;
|
| 45 | double z = wrap(_z) + this.offsetZ;
|
| 46 | int floorX = Mth.floor(x);
|
| 47 | int floorY = Mth.floor(y);
|
| 48 | int floorZ = Mth.floor(z);
|
| 49 | float relativeX = (float)(x - floorX);
|
| 50 | float relativeY = (float)(y - floorY);
|
| 51 | float relativeZ = (float)(z - floorZ);
|
| 52 | return this.sampleWithDerivative(floorX, floorY, floorZ, relativeX, relativeY, relativeZ, derivativeOut);
|
| 53 | }
|
| 54 |
|
| 55 | protected float sampleAndLerp(
|
| 56 | final int x, final int y, final int z, final float relativeX, final float relativeY, final float relativeZ, final float originalRelativeY
|
| 57 | ) {
|
| 58 | int x0 = this.permute(x);
|
| 59 | int x1 = this.permute(x + 1);
|
| 60 | int xy00 = this.permute(x0 + y);
|
| 61 | int xy01 = this.permute(x0 + y + 1);
|
| 62 | int xy10 = this.permute(x1 + y);
|
| 63 | int xy11 = this.permute(x1 + y + 1);
|
| 64 | float d000 = gradDot(this.permute(xy00 + z), relativeX, relativeY, relativeZ);
|
| 65 | float d100 = gradDot(this.permute(xy10 + z), relativeX - 1.0F, relativeY, relativeZ);
|
| 66 | float d010 = gradDot(this.permute(xy01 + z), relativeX, relativeY - 1.0F, relativeZ);
|
| 67 | float d110 = gradDot(this.permute(xy11 + z), relativeX - 1.0F, relativeY - 1.0F, relativeZ);
|
| 68 | float d001 = gradDot(this.permute(xy00 + z + 1), relativeX, relativeY, relativeZ - 1.0F);
|
| 69 | float d101 = gradDot(this.permute(xy10 + z + 1), relativeX - 1.0F, relativeY, relativeZ - 1.0F);
|
| 70 | float d011 = gradDot(this.permute(xy01 + z + 1), relativeX, relativeY - 1.0F, relativeZ - 1.0F);
|
| 71 | float d111 = gradDot(this.permute(xy11 + z + 1), relativeX - 1.0F, relativeY - 1.0F, relativeZ - 1.0F);
|
| 72 | float xAlpha = Mth.smoothstep(relativeX);
|
| 73 | float yAlpha = Mth.smoothstep(originalRelativeY);
|
| 74 | float zAlpha = Mth.smoothstep(relativeZ);
|
| 75 | return Mth.lerp3(xAlpha, yAlpha, zAlpha, d000, d100, d010, d110, d001, d101, d011, d111);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void addToVolume(final DensityBuffer buffer, final DensityVolume volume, final double xzScale, final double yScale, final float amplitude) {
|
| 80 | float d000xz = 0.0F;
|
| 81 | float d100xz = 0.0F;
|
| 82 | float d010xz = 0.0F;
|
| 83 | float d110xz = 0.0F;
|
| 84 | float d001xz = 0.0F;
|
| 85 | float d101xz = 0.0F;
|
| 86 | float d011xz = 0.0F;
|
| 87 | float d111xz = 0.0F;
|
| 88 | float g000y = 0.0F;
|
| 89 | float g100y = 0.0F;
|
| 90 | float g010y = 0.0F;
|
| 91 | float g110y = 0.0F;
|
| 92 | float g001y = 0.0F;
|
| 93 | float g101y = 0.0F;
|
| 94 | float g011y = 0.0F;
|
| 95 | float g111y = 0.0F;
|
| 96 | int index = 0;
|
| 97 |
|
| 98 | for (int indexZ = 0; indexZ < volume.sizeZ(); indexZ++) {
|
| 99 | double z = wrap(volume.blockZ(indexZ) * xzScale) + this.offsetZ;
|
| 100 | int floorZ = Mth.floor(z);
|
| 101 | float relativeZ = (float)(z - floorZ);
|
| 102 | float alphaZ = Mth.smoothstep(relativeZ);
|
| 103 |
|
| 104 | for (int indexX = 0; indexX < volume.sizeX(); indexX++) {
|
| 105 | double x = wrap(volume.blockX(indexX) * xzScale) + this.offsetX;
|
| 106 | int floorX = Mth.floor(x);
|
| 107 | float relativeX = (float)(x - floorX);
|
| 108 | int x0 = this.permute(floorX);
|
| 109 | int x1 = this.permute(floorX + 1);
|
| 110 | float alphaX = Mth.smoothstep(relativeX);
|
| 111 | int lastFloorY = Integer.MIN_VALUE;
|
| 112 |
|
| 113 | for (int indexY = 0; indexY < volume.sizeY(); indexY++) {
|
| 114 | double y = wrap(volume.blockY(indexY) * yScale) + this.offsetY;
|
| 115 | int floorY = Mth.floor(y);
|
| 116 | float relativeY = (float)(y - floorY);
|
| 117 | float alphaY = Mth.smoothstep(relativeY);
|
| 118 | if (lastFloorY != floorY) {
|
| 119 | int xy00 = this.permute(x0 + floorY);
|
| 120 | int xy01 = this.permute(x0 + floorY + 1);
|
| 121 | int xy10 = this.permute(x1 + floorY);
|
| 122 | int xy11 = this.permute(x1 + floorY + 1);
|
| 123 | GradientNoise.Gradient g000 = this.permuteToGrad(xy00 + floorZ);
|
| 124 | d000xz = g000.dotXz(relativeX, relativeZ);
|
| 125 | g000y = g000.y();
|
| 126 | GradientNoise.Gradient g100 = this.permuteToGrad(xy10 + floorZ);
|
| 127 | d100xz = g100.dotXz(relativeX - 1.0F, relativeZ);
|
| 128 | g100y = g100.y();
|
| 129 | GradientNoise.Gradient g010 = this.permuteToGrad(xy01 + floorZ);
|
| 130 | d010xz = g010.dotXz(relativeX, relativeZ);
|
| 131 | g010y = g010.y();
|
| 132 | GradientNoise.Gradient g110 = this.permuteToGrad(xy11 + floorZ);
|
| 133 | d110xz = g110.dotXz(relativeX - 1.0F, relativeZ);
|
| 134 | g110y = g110.y();
|
| 135 | GradientNoise.Gradient g001 = this.permuteToGrad(xy00 + floorZ + 1);
|
| 136 | d001xz = g001.dotXz(relativeX, relativeZ - 1.0F);
|
| 137 | g001y = g001.y();
|
| 138 | GradientNoise.Gradient g101 = this.permuteToGrad(xy10 + floorZ + 1);
|
| 139 | d101xz = g101.dotXz(relativeX - 1.0F, relativeZ - 1.0F);
|
| 140 | g101y = g101.y();
|
| 141 | GradientNoise.Gradient g011 = this.permuteToGrad(xy01 + floorZ + 1);
|
| 142 | d011xz = g011.dotXz(relativeX, relativeZ - 1.0F);
|
| 143 | g011y = g011.y();
|
| 144 | GradientNoise.Gradient g111 = this.permuteToGrad(xy11 + floorZ + 1);
|
| 145 | d111xz = g111.dotXz(relativeX - 1.0F, relativeZ - 1.0F);
|
| 146 | g111y = g111.y();
|
| 147 | lastFloorY = floorY;
|
| 148 | }
|
| 149 |
|
| 150 | buffer.addTo(
|
| 151 | index,
|
| 152 | amplitude
|
| 153 | * Mth.lerp3(
|
| 154 | alphaX,
|
| 155 | alphaY,
|
| 156 | alphaZ,
|
| 157 | d000xz + g000y * relativeY,
|
| 158 | d100xz + g100y * relativeY,
|
| 159 | d010xz + g010y * (relativeY - 1.0F),
|
| 160 | d110xz + g110y * (relativeY - 1.0F),
|
| 161 | d001xz + g001y * relativeY,
|
| 162 | d101xz + g101y * relativeY,
|
| 163 | d011xz + g011y * (relativeY - 1.0F),
|
| 164 | d111xz + g111y * (relativeY - 1.0F)
|
| 165 | )
|
| 166 | );
|
| 167 | index++;
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private float sampleWithDerivative(final int x, final int y, final int z, final float xr, final float yr, final float zr, final float[] derivativeOut) {
|
| 174 | int x0 = this.permute(x);
|
| 175 | int x1 = this.permute(x + 1);
|
| 176 | int xy00 = this.permute(x0 + y);
|
| 177 | int xy01 = this.permute(x0 + y + 1);
|
| 178 | int xy10 = this.permute(x1 + y);
|
| 179 | int xy11 = this.permute(x1 + y + 1);
|
| 180 | GradientNoise.Gradient g000 = this.permuteToGrad(xy00 + z);
|
| 181 | GradientNoise.Gradient g100 = this.permuteToGrad(xy10 + z);
|
| 182 | GradientNoise.Gradient g010 = this.permuteToGrad(xy01 + z);
|
| 183 | GradientNoise.Gradient g110 = this.permuteToGrad(xy11 + z);
|
| 184 | GradientNoise.Gradient g001 = this.permuteToGrad(xy00 + z + 1);
|
| 185 | GradientNoise.Gradient g101 = this.permuteToGrad(xy10 + z + 1);
|
| 186 | GradientNoise.Gradient g011 = this.permuteToGrad(xy01 + z + 1);
|
| 187 | GradientNoise.Gradient g111 = this.permuteToGrad(xy11 + z + 1);
|
| 188 | float d000 = g000.dot(xr, yr, zr);
|
| 189 | float d100 = g100.dot(xr - 1.0F, yr, zr);
|
| 190 | float d010 = g010.dot(xr, yr - 1.0F, zr);
|
| 191 | float d110 = g110.dot(xr - 1.0F, yr - 1.0F, zr);
|
| 192 | float d001 = g001.dot(xr, yr, zr - 1.0F);
|
| 193 | float d101 = g101.dot(xr - 1.0F, yr, zr - 1.0F);
|
| 194 | float d011 = g011.dot(xr, yr - 1.0F, zr - 1.0F);
|
| 195 | float d111 = g111.dot(xr - 1.0F, yr - 1.0F, zr - 1.0F);
|
| 196 | float xAlpha = Mth.smoothstep(xr);
|
| 197 | float yAlpha = Mth.smoothstep(yr);
|
| 198 | float zAlpha = Mth.smoothstep(zr);
|
| 199 | float d1x = Mth.lerp3(xAlpha, yAlpha, zAlpha, g000.x(), g100.x(), g010.x(), g110.x(), g001.x(), g101.x(), g011.x(), g111.x());
|
| 200 | float d1y = Mth.lerp3(xAlpha, yAlpha, zAlpha, g000.y(), g100.y(), g010.y(), g110.y(), g001.y(), g101.y(), g011.y(), g111.y());
|
| 201 | float d1z = Mth.lerp3(xAlpha, yAlpha, zAlpha, g000.z(), g100.z(), g010.z(), g110.z(), g001.z(), g101.z(), g011.z(), g111.z());
|
| 202 | float d2x = Mth.lerp2(yAlpha, zAlpha, d100 - d000, d110 - d010, d101 - d001, d111 - d011);
|
| 203 | float d2y = Mth.lerp2(zAlpha, xAlpha, d010 - d000, d011 - d001, d110 - d100, d111 - d101);
|
| 204 | float d2z = Mth.lerp2(xAlpha, yAlpha, d001 - d000, d101 - d100, d011 - d010, d111 - d110);
|
| 205 | float xSD = Mth.smoothstepDerivative(xr);
|
| 206 | float ySD = Mth.smoothstepDerivative(yr);
|
| 207 | float zSD = Mth.smoothstepDerivative(zr);
|
| 208 | float dX = d1x + xSD * d2x;
|
| 209 | float dY = d1y + ySD * d2y;
|
| 210 | float dZ = d1z + zSD * d2z;
|
| 211 | derivativeOut[0] += dX;
|
| 212 | derivativeOut[1] += dY;
|
| 213 | derivativeOut[2] += dZ;
|
| 214 | return Mth.lerp3(xAlpha, yAlpha, zAlpha, d000, d100, d010, d110, d001, d101, d011, d111);
|
| 215 | }
|
| 216 |
|
| 217 | @VisibleForTesting
|
| 218 | public void parityConfigString(final StringBuilder sb) {
|
| 219 | NoiseUtils.parityNoiseOctaveConfigString(sb, this.offsetX, this.offsetY, this.offsetZ, this.perms);
|
| 220 | }
|
| 221 | }
|
| 222 |
|