|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +namespace Gameframe.Procgen |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Noise gradients used in perlin and simplex noise |
| 7 | + /// Based on the tutorial by Cat-like Coding |
| 8 | + /// Using Random Access Noise methods by Squirrel Eiserloh |
| 9 | + /// </summary> |
| 10 | + public static class NoiseGradients |
| 11 | + { |
| 12 | + private const int GradientsMask1D = 1; |
| 13 | + private const int GradientsMask2D = 7; |
| 14 | + private const int GradientsMask3D = 15; |
| 15 | + private const int SimplexGradientsMask3D = 31; |
| 16 | + |
| 17 | + private static readonly float[] Gradients1D = new[] {1f, -1f}; |
| 18 | + |
| 19 | + private static readonly Vector2[] Gradients2D = |
| 20 | + { |
| 21 | + new Vector2(1f, 0f), |
| 22 | + new Vector2(-1f, 0f), |
| 23 | + new Vector2(0f, 1f), |
| 24 | + new Vector2(0f, -1f), |
| 25 | + new Vector2(1f, 1f).normalized, |
| 26 | + new Vector2(-1f, 1f).normalized, |
| 27 | + new Vector2(1f, -1f).normalized, |
| 28 | + new Vector2(-1f, -1f).normalized |
| 29 | + }; |
| 30 | + |
| 31 | + private static readonly Vector3[] Gradients3D = |
| 32 | + { |
| 33 | + new Vector3(1f, 1f, 0f), |
| 34 | + new Vector3(-1f, 1f, 0f), |
| 35 | + new Vector3(1f, -1f, 0f), |
| 36 | + new Vector3(-1f, -1f, 0f), |
| 37 | + new Vector3(1f, 0f, 1f), |
| 38 | + new Vector3(-1f, 0f, 1f), |
| 39 | + new Vector3(1f, 0f, -1f), |
| 40 | + new Vector3(-1f, 0f, -1f), |
| 41 | + new Vector3(0f, 1f, 1f), |
| 42 | + new Vector3(0f, -1f, 1f), |
| 43 | + new Vector3(0f, 1f, -1f), |
| 44 | + new Vector3(0f, -1f, -1f), |
| 45 | + |
| 46 | + new Vector3(1f, 1f, 0f), |
| 47 | + new Vector3(-1f, 1f, 0f), |
| 48 | + new Vector3(0f, -1f, 1f), |
| 49 | + new Vector3(0f, -1f, -1f) |
| 50 | + }; |
| 51 | + |
| 52 | + private static readonly Vector3[] SimplexGradients3D = |
| 53 | + { |
| 54 | + new Vector3(1f, 1f, 0f).normalized, |
| 55 | + new Vector3(-1f, 1f, 0f).normalized, |
| 56 | + new Vector3(1f, -1f, 0f).normalized, |
| 57 | + new Vector3(-1f, -1f, 0f).normalized, |
| 58 | + new Vector3(1f, 0f, 1f).normalized, |
| 59 | + new Vector3(-1f, 0f, 1f).normalized, |
| 60 | + new Vector3(1f, 0f, -1f).normalized, |
| 61 | + new Vector3(-1f, 0f, -1f).normalized, |
| 62 | + new Vector3(0f, 1f, 1f).normalized, |
| 63 | + new Vector3(0f, -1f, 1f).normalized, |
| 64 | + new Vector3(0f, 1f, -1f).normalized, |
| 65 | + new Vector3(0f, -1f, -1f).normalized, |
| 66 | + |
| 67 | + new Vector3(1f, 1f, 0f).normalized, |
| 68 | + new Vector3(-1f, 1f, 0f).normalized, |
| 69 | + new Vector3(1f, -1f, 0f).normalized, |
| 70 | + new Vector3(-1f, -1f, 0f).normalized, |
| 71 | + new Vector3(1f, 0f, 1f).normalized, |
| 72 | + new Vector3(-1f, 0f, 1f).normalized, |
| 73 | + new Vector3(1f, 0f, -1f).normalized, |
| 74 | + new Vector3(-1f, 0f, -1f).normalized, |
| 75 | + new Vector3(0f, 1f, 1f).normalized, |
| 76 | + new Vector3(0f, -1f, 1f).normalized, |
| 77 | + new Vector3(0f, 1f, -1f).normalized, |
| 78 | + new Vector3(0f, -1f, -1f).normalized, |
| 79 | + |
| 80 | + new Vector3(1f, 1f, 1f).normalized, |
| 81 | + new Vector3(-1f, 1f, 1f).normalized, |
| 82 | + new Vector3(1f, -1f, 1f).normalized, |
| 83 | + new Vector3(-1f, -1f, 1f).normalized, |
| 84 | + new Vector3(1f, 1f, -1f).normalized, |
| 85 | + new Vector3(-1f, 1f, -1f).normalized, |
| 86 | + new Vector3(1f, -1f, -1f).normalized, |
| 87 | + new Vector3(-1f, -1f, -1f).normalized |
| 88 | + }; |
| 89 | + |
| 90 | + #region Public Methods |
| 91 | + public static float Gradient1D(int x, uint seed) |
| 92 | + { |
| 93 | + return HashToGradient1D(Hash1D(x, seed)); |
| 94 | + } |
| 95 | + public static Vector2 Gradient2D(int x, int y, uint seed) |
| 96 | + { |
| 97 | + return HashToGradient2D(Hash2D(x, y, seed)); |
| 98 | + } |
| 99 | + public static Vector3 Gradient3D(int x, int y, int z, uint seed) |
| 100 | + { |
| 101 | + return HashToGradient3D(Hash3D(x, y, z, seed)); |
| 102 | + } |
| 103 | + public static Vector3 SimplexGradient3D(int x, int y, int z, uint seed) |
| 104 | + { |
| 105 | + return HashToSimplexGradient3D(Hash3D(x, y, z, seed)); |
| 106 | + } |
| 107 | + public static float Dot2D(Vector2 g, float x, float y) |
| 108 | + { |
| 109 | + return g.x * x + g.y * y; |
| 110 | + } |
| 111 | + |
| 112 | + public static float Dot3D(Vector3 g, float x, float y, float z) |
| 113 | + { |
| 114 | + return g.x * x + g.y * y + g.z * z; |
| 115 | + } |
| 116 | + #endregion |
| 117 | + |
| 118 | + private static float HashToGradient1D(uint value) |
| 119 | + { |
| 120 | + return Gradients1D[value & GradientsMask1D]; |
| 121 | + } |
| 122 | + private static Vector2 HashToGradient2D(uint value) |
| 123 | + { |
| 124 | + return Gradients2D[value & GradientsMask2D]; |
| 125 | + } |
| 126 | + private static Vector3 HashToGradient3D(uint value) |
| 127 | + { |
| 128 | + return Gradients3D[value & GradientsMask3D]; |
| 129 | + } |
| 130 | + private static Vector3 HashToSimplexGradient3D(uint value) |
| 131 | + { |
| 132 | + return SimplexGradients3D[value & SimplexGradientsMask3D]; |
| 133 | + } |
| 134 | + private static uint Hash1D(int value, uint seed) |
| 135 | + { |
| 136 | + return SquirrelEiserloh.Get1dNoiseUint(value, seed); |
| 137 | + } |
| 138 | + private static uint Hash2D(int x, int y, uint seed) |
| 139 | + { |
| 140 | + return SquirrelEiserloh.Get2dNoiseUint(x, y, seed); |
| 141 | + } |
| 142 | + private static uint Hash3D(int x, int y, int z, uint seed) |
| 143 | + { |
| 144 | + return SquirrelEiserloh.Get3dNoiseUint(x, y, z, seed); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments