MegaPerlin.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using UnityEngine;
  2. public class MegaPerlin
  3. {
  4. private static MegaPerlin instance;
  5. public MegaPerlin()
  6. {
  7. if ( instance != null )
  8. {
  9. Debug.LogError("Cannot have two instances of ImprovedPerlin.");
  10. return;
  11. }
  12. instance = this;
  13. for ( int i = 0; i < 256; i++ )
  14. p[256 + i] = p[i] = permutation[i];
  15. }
  16. public static MegaPerlin Instance
  17. {
  18. get
  19. {
  20. if ( instance == null )
  21. new MegaPerlin();
  22. return instance;
  23. }
  24. }
  25. //int X,Y,Z = 0;
  26. //float u,v,w = 0.0f;
  27. //int A, AA, AB, B, BA, BB = 0;
  28. //float floorx, floory, floorz = 0.0f;
  29. static int[] p = new int[512];
  30. static int[] permutation = { 151,160,137,91,90,15,
  31. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  32. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  33. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  34. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  35. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  36. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  37. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  38. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  39. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  40. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  41. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  42. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
  43. };
  44. public int Perm(int i)
  45. {
  46. return p[i & 255];
  47. }
  48. public float Noise(float x)
  49. {
  50. // Compute the cell coordinates
  51. int X = (int)Mathf.Floor(x) & 255;
  52. // Retrieve the decimal part of the cell
  53. x -= (float)Mathf.Floor(x);
  54. float u = fade(x);
  55. return lerp(u, grad(p[X], x), grad(p[X + 1], x - 1));
  56. }
  57. public float Noise(float x, float y)
  58. {
  59. // Compute the cell coordinates
  60. int X = (int)Mathf.Floor(x) & 255;
  61. int Y = (int)Mathf.Floor(y) & 255;
  62. // Retrieve the decimal part of the cell
  63. x -= (float)Mathf.Floor(x);
  64. y -= (float)Mathf.Floor(y);
  65. // Smooth the curve
  66. float u = fade(x);
  67. float v = fade(y);
  68. // Fetch some randoms values from the table
  69. int A = p[X] + Y;
  70. int B = p[X + 1] + Y;
  71. // Interpolate between directions
  72. return lerp(v, lerp(u, grad(p[A], x, y), grad(p[B], x - 1, y)), lerp(u, grad(p[A + 1], x, y - 1), grad(p[B + 1], x - 1, y - 1)));
  73. }
  74. public float Noise(float x, float y, float z)
  75. {
  76. // FIND UNIT CUBE THAT CONTAINS POINT
  77. float floorx = Mathf.Floor(x);
  78. float floory = Mathf.Floor(y);
  79. float floorz = Mathf.Floor(z);
  80. int X = (int)floorx & 255;
  81. int Y = (int)floory & 255;
  82. int Z = (int)floorz & 255;
  83. // FIND RELATIVE X,Y,Z OF POINT IN CUBE.
  84. x -= floorx;
  85. y -= floory;
  86. z -= floorz;
  87. // COMPUTE FADE CURVES FOR EACH X,Y,Z
  88. float u = fade(x);
  89. float v = fade(y);
  90. float w = fade(z);
  91. // HASH COORDINATES OF THE 8 CUBE CORNERS
  92. int A = p[X]+Y;
  93. int AA = p[A]+Z;
  94. int AB = p[A+1]+Z;
  95. int B = p[X+1]+Y;
  96. int BA = p[B]+Z;
  97. int BB = p[B+1]+Z;
  98. // ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
  99. return lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z)), lerp(u, grad(p[AB], x, y - 1, z),
  100. grad(p[BB], x - 1, y - 1, z))), lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1 , y, z - 1)),
  101. lerp(u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1 , y - 1 , z - 1))));
  102. }
  103. private float fade(float t)
  104. {
  105. return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
  106. }
  107. private float lerp(float t, float a, float b)
  108. {
  109. return a + t * (b - a);
  110. }
  111. private static float grad(int hash, float x)
  112. {
  113. return (hash & 1) == 0 ? x : -x;
  114. }
  115. private float grad(int hash, float x, float y)
  116. {
  117. // Fetch the last 3 bits
  118. int h = hash & 3;
  119. float u = (h & 2) == 0 ? x : -x;
  120. float v = (h & 1) == 0 ? y : -y;
  121. return u + v;
  122. }
  123. private float grad(int hash, float x, float y, float z)
  124. {
  125. int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
  126. float u = h < 8 ? x : y; // INTO 12 GRADIENT DIRECTIONS.
  127. float v = h < 4 ? y : h == 12 || h == 14 ? x : z;
  128. return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0.0f ? v : -v);
  129. }
  130. public float fBm1(float x, float H, float lacunarity, float octaves)
  131. {
  132. float value = 0.0f;
  133. float frequency = 1.0f;
  134. for ( int i = 0; i < (int)octaves; i++ )
  135. {
  136. value = Noise(x) * Mathf.Pow(frequency, -H);
  137. x *= lacunarity;
  138. frequency *= lacunarity;
  139. }
  140. float remainder = octaves - (int)octaves;
  141. if ( remainder != 0.0f )
  142. value += remainder * Noise(x) * Mathf.Pow(frequency, -H);
  143. return value;
  144. }
  145. public float fBm1(Vector3 vertex, float H, float lacunarity, float octaves)
  146. {
  147. return fBm1(vertex.x, vertex.y, vertex.z, H, lacunarity, octaves);
  148. }
  149. public float fBm1(float x, float y, float z, float H, float lacunarity, float octaves)
  150. {
  151. float value = 0.0f;
  152. float frequency = 1.0f;
  153. for ( int i = 0; i < octaves; i++ )
  154. {
  155. value += Noise(x, y, z) * Mathf.Pow(frequency, -H);
  156. x *= lacunarity;
  157. y *= lacunarity;
  158. z *= lacunarity;
  159. frequency *= lacunarity;
  160. }
  161. float remainder = octaves - (int)octaves;
  162. if ( remainder != 0.0f )
  163. value += remainder * Noise(x, y, z) * Mathf.Pow(frequency, -H);
  164. return value;
  165. }
  166. }