Guest User

Untitled

a guest
Jul 1st, 2016
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sing UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SimplexNoiseGenerator
  5. {
  6. private int[] A = new int[3];
  7. private float s, u, v, w;
  8. private int i, j, k;
  9. private float onethird = 0.333333333f;
  10. private float onesixth = 0.166666667f;
  11. private int[] T;
  12.  
  13. public SimplexNoiseGenerator()
  14. {
  15. if (T == null)
  16. {
  17. System.Random rand = new System.Random();
  18. T = new int[8];
  19. for (int q = 0; q < 8; q++)
  20. T[q] = rand.Next();
  21. }
  22. }
  23.  
  24. public SimplexNoiseGenerator(string seed)
  25. {
  26. T = new int[8];
  27. string[] seed_parts = seed.Split(new char[] { ' ' });
  28.  
  29. for (int q = 0; q < 8; q++)
  30. {
  31. int b;
  32. try
  33. {
  34. b = int.Parse(seed_parts[q]);
  35. }
  36. catch
  37. {
  38. b = 0x0;
  39. }
  40. T[q] = b;
  41. }
  42. }
  43.  
  44. public SimplexNoiseGenerator(int[] seed)
  45. { // {0x16, 0x38, 0x32, 0x2c, 0x0d, 0x13, 0x07, 0x2a}
  46. T = seed;
  47. }
  48.  
  49. public string GetSeed()
  50. {
  51. string seed = "";
  52.  
  53. for (int q = 0; q < 8; q++)
  54. {
  55. seed += T[q].ToString();
  56. if (q < 7)
  57. seed += " ";
  58. }
  59.  
  60. return seed;
  61. }
  62.  
  63. public float coherentNoise(float x, float y, float z, int octaves = 1, int multiplier = 25, float amplitude = 0.5f, float lacunarity = 2, float persistence = 0.9f)
  64. {
  65. Vector3 v3 = new Vector3(x, y, z) / multiplier;
  66. float val = 0;
  67. for (int n = 0; n < octaves; n++)
  68. {
  69. val += noise(v3.x, v3.y, v3.z) * amplitude;
  70. v3 *= lacunarity;
  71. amplitude *= persistence;
  72. }
  73. return val;
  74. }
  75.  
  76. public int getDensity(Vector3 loc)
  77. {
  78. float val = coherentNoise(loc.x, loc.y, loc.z);
  79. return (int)Mathf.Lerp(0, 255, val);
  80. }
  81.  
  82. // Simplex Noise Generator
  83. public float noise(float x, float y, float z)
  84. {
  85. s = (x + y + z) * onethird;
  86. i = fastfloor(x + s);
  87. j = fastfloor(y + s);
  88. k = fastfloor(z + s);
  89.  
  90. s = (i + j + k) * onesixth;
  91. u = x - i + s;
  92. v = y - j + s;
  93. w = z - k + s;
  94.  
  95. A[0] = 0; A[1] = 0; A[2] = 0;
  96.  
  97. int hi = u >= w ? u >= v ? 0 : 1 : v >= w ? 1 : 2;
  98. int lo = u < w ? u < v ? 0 : 1 : v < w ? 1 : 2;
  99.  
  100. return kay(hi) + kay(3 - hi - lo) + kay(lo) + kay(0);
  101. }
  102.  
  103. float kay(int a)
  104. {
  105. s = (A[0] + A[1] + A[2]) * onesixth;
  106. float x = u - A[0] + s;
  107. float y = v - A[1] + s;
  108. float z = w - A[2] + s;
  109. float t = 0.6f - x * x - y * y - z * z;
  110. int h = shuffle(i + A[0], j + A[1], k + A[2]);
  111. A[a]++;
  112. if (t < 0) return 0;
  113. int b5 = h >> 5 & 1;
  114. int b4 = h >> 4 & 1;
  115. int b3 = h >> 3 & 1;
  116. int b2 = h >> 2 & 1;
  117. int b1 = h & 3;
  118.  
  119. float p = b1 == 1 ? x : b1 == 2 ? y : z;
  120. float q = b1 == 1 ? y : b1 == 2 ? z : x;
  121. float r = b1 == 1 ? z : b1 == 2 ? x : y;
  122.  
  123. p = b5 == b3 ? -p : p;
  124. q = b5 == b4 ? -q : q;
  125. r = b5 != (b4 ^ b3) ? -r : r;
  126. t *= t;
  127. return 8 * t * t * (p + (b1 == 0 ? q + r : b2 == 0 ? q : r));
  128. }
  129.  
  130. int shuffle(int i, int j, int k)
  131. {
  132. return b(i, j, k, 0) + b(j, k, i, 1) + b(k, i, j, 2) + b(i, j, k, 3) + b(j, k, i, 4) + b(k, i, j, 5) + b(i, j, k, 6) + b(j, k, i, 7);
  133. }
  134.  
  135. int b(int i, int j, int k, int B)
  136. {
  137. return T[b(i, B) << 2 | b(j, B) << 1 | b(k, B)];
  138. }
  139.  
  140. int b(int N, int B)
  141. {
  142. return N >> B & 1;
  143. }
  144.  
  145. int fastfloor(float n)
  146. {
  147. return n > 0 ? (int)n : (int)n - 1;
  148. }
  149. }
  150.  
  151. ---------------End of Noise Generator Class. This is supposed to generate values for various [x,y,z] points from -1 to 1.--------------
  152.  
  153. public class MapGenerator : MonoBehaviour {
  154.  
  155. public int resolution;
  156. public float scale;
  157. public int octaves;
  158. public float persistence, lacunarity;
  159.  
  160. public void GenerateMap()
  161. {
  162. SimplexNoiseGenerator gen = new SimplexNoiseGenerator();
  163. float[, ,] noiseMap = new float[resolution, resolution, resolution];
  164. for (int x = 0; x < resolution; x++)
  165. {
  166. for (int y = 0; y < resolution; y++)
  167. {
  168. for (int z = 0; z < resolution; z++)
  169. {
  170. float amplitude = 1;
  171. amplitude *= persistence;
  172. noiseMap[x,y,z] = gen.coherentNoise((float)x, (float)y, (float)z);
  173. }
  174. }
  175. }
  176.  
  177. MapDisplay mapDisplay = FindObjectOfType<MapDisplay>();
  178. mapDisplay.DrawNoiseMap(noiseMap);
  179. }
  180. }
  181.  
  182. --------------------------The MapGenerator class fills the 3D array with values from SimplexNoiseGenerator------------------------
RAW Paste Data