| 1 | package net.minecraft.world.level.levelgen.synth;
|
| 2 |
|
| 3 | import net.minecraft.SharedConstants;
|
| 4 | import net.minecraft.util.RandomSource;
|
| 5 |
|
| 6 | public abstract class GradientNoise implements Noise {
|
| 7 | protected static final GradientNoise.Gradient[] GRADIENT = new GradientNoise.Gradient[]{
|
| 8 | new GradientNoise.Gradient(1, 1, 0),
|
| 9 | new GradientNoise.Gradient(-1, 1, 0),
|
| 10 | new GradientNoise.Gradient(1, -1, 0),
|
| 11 | new GradientNoise.Gradient(-1, -1, 0),
|
| 12 | new GradientNoise.Gradient(1, 0, 1),
|
| 13 | new GradientNoise.Gradient(-1, 0, 1),
|
| 14 | new GradientNoise.Gradient(1, 0, -1),
|
| 15 | new GradientNoise.Gradient(-1, 0, -1),
|
| 16 | new GradientNoise.Gradient(0, 1, 1),
|
| 17 | new GradientNoise.Gradient(0, -1, 1),
|
| 18 | new GradientNoise.Gradient(0, 1, -1),
|
| 19 | new GradientNoise.Gradient(0, -1, -1),
|
| 20 | new GradientNoise.Gradient(1, 1, 0),
|
| 21 | new GradientNoise.Gradient(0, -1, 1),
|
| 22 | new GradientNoise.Gradient(-1, 1, 0),
|
| 23 | new GradientNoise.Gradient(0, -1, -1)
|
| 24 | };
|
| 25 | private static final int ROUND_OFF = 33554432;
|
| 26 | private static final double HALF_ROUND_OFF = Math.nextDown(1.6777216E7);
|
| 27 | protected final byte[] perms = new byte[256];
|
| 28 | protected final double offsetX;
|
| 29 | protected final double offsetY;
|
| 30 | protected final double offsetZ;
|
| 31 |
|
| 32 | protected GradientNoise(final RandomSource random) {
|
| 33 | this(random, 256.0);
|
| 34 | }
|
| 35 |
|
| 36 | protected GradientNoise(final RandomSource random, final double noiseOffsetScale) {
|
| 37 | this.offsetX = random.nextDouble() * noiseOffsetScale;
|
| 38 | this.offsetY = random.nextDouble() * noiseOffsetScale;
|
| 39 | this.offsetZ = random.nextDouble() * noiseOffsetScale;
|
| 40 |
|
| 41 | for (int i = 0; i < 256; i++) {
|
| 42 | this.perms[i] = (byte)i;
|
| 43 | }
|
| 44 |
|
| 45 | for (int i = 0; i < 256; i++) {
|
| 46 | int offset = random.nextInt(256 - i);
|
| 47 | byte tmp = this.perms[i];
|
| 48 | this.perms[i] = this.perms[offset + i];
|
| 49 | this.perms[offset + i] = tmp;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | protected int permute(final int x) {
|
| 54 | return this.perms[x & 0xFF] & 0xFF;
|
| 55 | }
|
| 56 |
|
| 57 | protected GradientNoise.Gradient permuteToGrad(final int x) {
|
| 58 | return GRADIENT[this.permute(x) & 15];
|
| 59 | }
|
| 60 |
|
| 61 | protected static float gradDot(final int hash, final float x, final float y, final float z) {
|
| 62 | return GRADIENT[hash & 15].dot(x, y, z);
|
| 63 | }
|
| 64 |
|
| 65 | protected static double wrap(final double x) {
|
| 66 | if (SharedConstants.DEBUG_ENABLE_FARLANDS) {
|
| 67 | return x;
|
| 68 | } else {
|
| 69 | return x >= -HALF_ROUND_OFF && x < HALF_ROUND_OFF ? x : x - Math.floor(x / 3.3554432E7 + 0.5) * 3.3554432E7;
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | protected record Gradient(int x, int y, int z) {
|
| 74 | public double dot(final double x, final double y, final double z) {
|
| 75 | return this.x * x + this.y * y + this.z * z;
|
| 76 | }
|
| 77 |
|
| 78 | public float dot(final float x, final float y, final float z) {
|
| 79 | return this.x * x + this.y * y + this.z * z;
|
| 80 | }
|
| 81 |
|
| 82 | public float dotXz(final float x, final float z) {
|
| 83 | return this.x * x + this.z * z;
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|