| 1 | package net.minecraft.world.level.levelgen.synth;
|
| 2 |
|
| 3 | import net.minecraft.util.Interval;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.util.RandomSource;
|
| 6 |
|
| 7 | public class SimplexNoise extends GradientNoise {
|
| 8 | public static final Interval RANGE = Interval.ofSymmetric(2.0F);
|
| 9 | public static final double STANDARD_DEVIATION = 0.42544;
|
| 10 | private static final double SQRT_3 = Math.sqrt(3.0);
|
| 11 | private static final double F2 = 0.5 * (SQRT_3 - 1.0);
|
| 12 | private static final double G2 = (3.0 - SQRT_3) / 6.0;
|
| 13 |
|
| 14 | public SimplexNoise(final RandomSource random) {
|
| 15 | super(random);
|
| 16 | }
|
| 17 |
|
| 18 | public SimplexNoise(final RandomSource random, final boolean discardNoiseOffset) {
|
| 19 | super(random, discardNoiseOffset ? 0.0 : 256.0);
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public Interval range() {
|
| 24 | return RANGE;
|
| 25 | }
|
| 26 |
|
| 27 | private double getCornerNoise3D(final int index, final double x, final double y, final double z, final double base) {
|
| 28 | double t0 = base - x * x - y * y - z * z;
|
| 29 | double n0;
|
| 30 | if (t0 < 0.0) {
|
| 31 | n0 = 0.0;
|
| 32 | } else {
|
| 33 | t0 *= t0;
|
| 34 | n0 = t0 * t0 * GRADIENT[index].dot(x, y, z);
|
| 35 | }
|
| 36 |
|
| 37 | return n0;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public float get(final double _xin, final double _yin) {
|
| 42 | double xin = _xin + this.offsetX;
|
| 43 | double yin = _yin + this.offsetY;
|
| 44 | double s = (xin + yin) * F2;
|
| 45 | int i = Mth.floor(xin + s);
|
| 46 | int j = Mth.floor(yin + s);
|
| 47 | double t = (i + j) * G2;
|
| 48 | double X0 = i - t;
|
| 49 | double Y0 = j - t;
|
| 50 | double x0 = xin - X0;
|
| 51 | double y0 = yin - Y0;
|
| 52 | int i1;
|
| 53 | int j1;
|
| 54 | if (x0 > y0) {
|
| 55 | i1 = 1;
|
| 56 | j1 = 0;
|
| 57 | } else {
|
| 58 | i1 = 0;
|
| 59 | j1 = 1;
|
| 60 | }
|
| 61 |
|
| 62 | double x1 = x0 - i1 + G2;
|
| 63 | double y1 = y0 - j1 + G2;
|
| 64 | double x2 = x0 - 1.0 + 2.0 * G2;
|
| 65 | double y2 = y0 - 1.0 + 2.0 * G2;
|
| 66 | int ii = i & 0xFF;
|
| 67 | int jj = j & 0xFF;
|
| 68 | int gi0 = this.permute(ii + this.permute(jj)) % 12;
|
| 69 | int gi1 = this.permute(ii + i1 + this.permute(jj + j1)) % 12;
|
| 70 | int gi2 = this.permute(ii + 1 + this.permute(jj + 1)) % 12;
|
| 71 | double n0 = this.getCornerNoise3D(gi0, x0, y0, 0.0, 0.5);
|
| 72 | double n1 = this.getCornerNoise3D(gi1, x1, y1, 0.0, 0.5);
|
| 73 | double n2 = this.getCornerNoise3D(gi2, x2, y2, 0.0, 0.5);
|
| 74 | return (float)(70.0 * (n0 + n1 + n2));
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public float get(final double _xin, final double _yin, final double _zin) {
|
| 79 | double xin = _xin + this.offsetX;
|
| 80 | double yin = _yin + this.offsetY;
|
| 81 | double zin = _zin + this.offsetZ;
|
| 82 | double F3 = 0.3333333333333333;
|
| 83 | double s = (xin + yin + zin) * 0.3333333333333333;
|
| 84 | int i = Mth.floor(xin + s);
|
| 85 | int j = Mth.floor(yin + s);
|
| 86 | int k = Mth.floor(zin + s);
|
| 87 | double G3 = 0.16666666666666666;
|
| 88 | double t = (i + j + k) * 0.16666666666666666;
|
| 89 | double X0 = i - t;
|
| 90 | double Y0 = j - t;
|
| 91 | double Z0 = k - t;
|
| 92 | double x0 = xin - X0;
|
| 93 | double y0 = yin - Y0;
|
| 94 | double z0 = zin - Z0;
|
| 95 | int i1;
|
| 96 | int j1;
|
| 97 | int k1;
|
| 98 | int i2;
|
| 99 | int j2;
|
| 100 | int k2;
|
| 101 | if (x0 >= y0) {
|
| 102 | if (y0 >= z0) {
|
| 103 | i1 = 1;
|
| 104 | j1 = 0;
|
| 105 | k1 = 0;
|
| 106 | i2 = 1;
|
| 107 | j2 = 1;
|
| 108 | k2 = 0;
|
| 109 | } else if (x0 >= z0) {
|
| 110 | i1 = 1;
|
| 111 | j1 = 0;
|
| 112 | k1 = 0;
|
| 113 | i2 = 1;
|
| 114 | j2 = 0;
|
| 115 | k2 = 1;
|
| 116 | } else {
|
| 117 | i1 = 0;
|
| 118 | j1 = 0;
|
| 119 | k1 = 1;
|
| 120 | i2 = 1;
|
| 121 | j2 = 0;
|
| 122 | k2 = 1;
|
| 123 | }
|
| 124 | } else if (y0 < z0) {
|
| 125 | i1 = 0;
|
| 126 | j1 = 0;
|
| 127 | k1 = 1;
|
| 128 | i2 = 0;
|
| 129 | j2 = 1;
|
| 130 | k2 = 1;
|
| 131 | } else if (x0 < z0) {
|
| 132 | i1 = 0;
|
| 133 | j1 = 1;
|
| 134 | k1 = 0;
|
| 135 | i2 = 0;
|
| 136 | j2 = 1;
|
| 137 | k2 = 1;
|
| 138 | } else {
|
| 139 | i1 = 0;
|
| 140 | j1 = 1;
|
| 141 | k1 = 0;
|
| 142 | i2 = 1;
|
| 143 | j2 = 1;
|
| 144 | k2 = 0;
|
| 145 | }
|
| 146 |
|
| 147 | double x1 = x0 - i1 + 0.16666666666666666;
|
| 148 | double y1 = y0 - j1 + 0.16666666666666666;
|
| 149 | double z1 = z0 - k1 + 0.16666666666666666;
|
| 150 | double x2 = x0 - i2 + 0.3333333333333333;
|
| 151 | double y2 = y0 - j2 + 0.3333333333333333;
|
| 152 | double z2 = z0 - k2 + 0.3333333333333333;
|
| 153 | double x3 = x0 - 1.0 + 0.5;
|
| 154 | double y3 = y0 - 1.0 + 0.5;
|
| 155 | double z3 = z0 - 1.0 + 0.5;
|
| 156 | int ii = i & 0xFF;
|
| 157 | int jj = j & 0xFF;
|
| 158 | int kk = k & 0xFF;
|
| 159 | int gi0 = this.permute(ii + this.permute(jj + this.permute(kk))) % 12;
|
| 160 | int gi1 = this.permute(ii + i1 + this.permute(jj + j1 + this.permute(kk + k1))) % 12;
|
| 161 | int gi2 = this.permute(ii + i2 + this.permute(jj + j2 + this.permute(kk + k2))) % 12;
|
| 162 | int gi3 = this.permute(ii + 1 + this.permute(jj + 1 + this.permute(kk + 1))) % 12;
|
| 163 | double n0 = this.getCornerNoise3D(gi0, x0, y0, z0, 0.6);
|
| 164 | double n1 = this.getCornerNoise3D(gi1, x1, y1, z1, 0.6);
|
| 165 | double n2 = this.getCornerNoise3D(gi2, x2, y2, z2, 0.6);
|
| 166 | double n3 = this.getCornerNoise3D(gi3, x3, y3, z3, 0.6);
|
| 167 | return (float)(32.0 * (n0 + n1 + n2 + n3));
|
| 168 | }
|
| 169 | }
|
| 170 |
|