|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace MineCase.Algorithm.Noise |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Implementation for Improved Perlin Noise (http://mrl.nyu.edu/~perlin/noise/) |
| 9 | + /// </summary> |
| 10 | + public class PerlinNoise : INoise |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Permutation |
| 14 | + /// </summary> |
| 15 | + private readonly int[] _p = new int[512]; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="PerlinNoise"/> class. |
| 19 | + /// </summary> |
| 20 | + /// <param name="seed">Seed for generating permutaion.</param> |
| 21 | + public PerlinNoise(int seed) |
| 22 | + { |
| 23 | + var random = new Random(seed); |
| 24 | + for (int i = 0; i < 256; i++) |
| 25 | + _p[i + 256] = _p[i] = random.Next(0, 256); |
| 26 | + } |
| 27 | + |
| 28 | + public double Noise(double x, double y, double z) |
| 29 | + { |
| 30 | + var xcoord = Split(x); |
| 31 | + var ycoord = Split(y); |
| 32 | + var zcoord = Split(z); |
| 33 | + |
| 34 | + var u = Fade(xcoord.remainder); |
| 35 | + var v = Fade(ycoord.remainder); |
| 36 | + var w = Fade(zcoord.remainder); |
| 37 | + |
| 38 | + int aaa, aba, aab, abb, baa, bba, bab, bbb; |
| 39 | + aaa = _p[_p[_p[xcoord.integer] + ycoord.integer] + zcoord.integer]; |
| 40 | + aba = _p[_p[_p[xcoord.integer] + ycoord.integer + 1] + zcoord.integer]; |
| 41 | + aab = _p[_p[_p[xcoord.integer] + ycoord.integer] + zcoord.integer + 1]; |
| 42 | + abb = _p[_p[_p[xcoord.integer] + ycoord.integer + 1] + zcoord.integer + 1]; |
| 43 | + baa = _p[_p[_p[xcoord.integer + 1] + ycoord.integer] + zcoord.integer]; |
| 44 | + bba = _p[_p[_p[xcoord.integer + 1] + ycoord.integer + 1] + zcoord.integer]; |
| 45 | + bab = _p[_p[_p[xcoord.integer + 1] + ycoord.integer] + zcoord.integer + 1]; |
| 46 | + bbb = _p[_p[_p[xcoord.integer + 1] + ycoord.integer + 1] + zcoord.integer + 1]; |
| 47 | + |
| 48 | + double x1, x2, y1, y2; |
| 49 | + x1 = Lerp( |
| 50 | + Grad(aaa, xcoord.remainder, ycoord.remainder, zcoord.remainder), |
| 51 | + Grad(baa, xcoord.remainder - 1, ycoord.remainder, zcoord.remainder), |
| 52 | + u); |
| 53 | + x2 = Lerp( |
| 54 | + Grad(aba, xcoord.remainder, ycoord.remainder - 1, zcoord.remainder), |
| 55 | + Grad(bba, xcoord.remainder - 1, ycoord.remainder - 1, zcoord.remainder), |
| 56 | + u); |
| 57 | + y1 = Lerp(x1, x2, v); |
| 58 | + |
| 59 | + x1 = Lerp( |
| 60 | + Grad(aab, xcoord.remainder, ycoord.remainder, zcoord.remainder - 1), |
| 61 | + Grad(bab, xcoord.remainder - 1, ycoord.remainder, zcoord.remainder - 1), |
| 62 | + u); |
| 63 | + x2 = Lerp( |
| 64 | + Grad(abb, xcoord.remainder, ycoord.remainder - 1, zcoord.remainder - 1), |
| 65 | + Grad(bbb, xcoord.remainder - 1, ycoord.remainder - 1, zcoord.remainder - 1), |
| 66 | + u); |
| 67 | + y2 = Lerp(x1, x2, v); |
| 68 | + |
| 69 | + return (Lerp(y1, y2, w) + 1) / 2; |
| 70 | + } |
| 71 | + |
| 72 | + private static (int integer, double remainder) Split(double value) |
| 73 | + { |
| 74 | + var integer = (int)value; |
| 75 | + return (integer % 256, value - integer); |
| 76 | + } |
| 77 | + |
| 78 | + private static double Fade(double t) |
| 79 | + { |
| 80 | + // 6t^5 - 15t^4 + 10t^3 |
| 81 | + return t * t * t * (t * (t * 6 - 15) + 10); |
| 82 | + } |
| 83 | + |
| 84 | + // Source: http://riven8192.blogspot.com/2010/08/calculate-perlinnoise-twice-as-fast.html |
| 85 | + public static double Grad(int hash, double x, double y, double z) |
| 86 | + { |
| 87 | + switch (hash & 0xF) |
| 88 | + { |
| 89 | + case 0x0: return x + y; |
| 90 | + case 0x1: return -x + y; |
| 91 | + case 0x2: return x - y; |
| 92 | + case 0x3: return -x - y; |
| 93 | + case 0x4: return x + z; |
| 94 | + case 0x5: return -x + z; |
| 95 | + case 0x6: return x - z; |
| 96 | + case 0x7: return -x - z; |
| 97 | + case 0x8: return y + z; |
| 98 | + case 0x9: return -y + z; |
| 99 | + case 0xA: return y - z; |
| 100 | + case 0xB: return -y - z; |
| 101 | + case 0xC: return y + x; |
| 102 | + case 0xD: return -y + z; |
| 103 | + case 0xE: return y - x; |
| 104 | + case 0xF: return -y - z; |
| 105 | + default: throw new ArgumentException(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public static double Lerp(double a, double b, double x) => |
| 110 | + a + x * (b - a); |
| 111 | + } |
| 112 | +} |
0 commit comments