PerlinNoise.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. [Serializable]
  5. public class PerlinNoise
  6. {
  7. const int SIZE = 256;
  8. int[] m_perm = new int[SIZE+SIZE];
  9. public Texture2D m_permTable1D, m_permTable2D, m_gradient2D, m_gradient3D, m_gradient4D;
  10. public Texture2D GetPermutationTable1D() { return m_permTable1D; }
  11. public Texture2D GetPermutationTable2D() { return m_permTable2D; }
  12. public Texture2D GetGradient2D() { return m_gradient2D; }
  13. public Texture2D GetGradient3D() { return m_gradient3D; }
  14. public Texture2D GetGradient4D() { return m_gradient4D; }
  15. public PerlinNoise(int seed)
  16. {
  17. #if UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3
  18. UnityEngine.Random.seed = seed;
  19. #else
  20. UnityEngine.Random.InitState(seed);
  21. #endif
  22. int i, j, k;
  23. for (i = 0 ; i < SIZE ; i++)
  24. {
  25. m_perm[i] = i;
  26. }
  27. while (--i != 0)
  28. {
  29. k = m_perm[i];
  30. j = UnityEngine.Random.Range(0, SIZE);
  31. m_perm[i] = m_perm[j];
  32. m_perm[j] = k;
  33. }
  34. for (i = 0 ; i < SIZE; i++)
  35. {
  36. m_perm[SIZE + i] = m_perm[i];
  37. }
  38. }
  39. public void LoadResourcesFor2DNoise()
  40. {
  41. LoadPermTable1D();
  42. LoadGradient2D();
  43. }
  44. public void LoadResourcesFor3DNoise()
  45. {
  46. LoadPermTable2D();
  47. LoadGradient3D();
  48. }
  49. public void LoadResourcesFor4DNoise()
  50. {
  51. LoadPermTable1D();
  52. LoadPermTable2D();
  53. LoadGradient4D();
  54. }
  55. void LoadPermTable1D()
  56. {
  57. if(m_permTable1D) return;
  58. m_permTable1D = new Texture2D(SIZE, 1, TextureFormat.Alpha8, false, true);
  59. m_permTable1D.hideFlags = HideFlags.DontSave;
  60. m_permTable1D.filterMode = FilterMode.Point;
  61. m_permTable1D.wrapMode = TextureWrapMode.Repeat;
  62. for(int x = 0; x < SIZE; x++)
  63. {
  64. m_permTable1D.SetPixel(x, 1, new Color(0,0,0,(float)m_perm[x]/(float)(SIZE-1)));
  65. }
  66. m_permTable1D.Apply();
  67. }
  68. void LoadPermTable2D()
  69. {
  70. if(m_permTable2D) return;
  71. m_permTable2D = new Texture2D(SIZE, SIZE, TextureFormat.ARGB32, false, true);
  72. m_permTable2D.hideFlags = HideFlags.DontSave;
  73. m_permTable2D.filterMode = FilterMode.Point;
  74. m_permTable2D.wrapMode = TextureWrapMode.Repeat;
  75. for(int x = 0; x < SIZE; x++)
  76. {
  77. for(int y = 0; y < SIZE; y++)
  78. {
  79. int A = m_perm[x] + y;
  80. int AA = m_perm[A];
  81. int AB = m_perm[A + 1];
  82. int B = m_perm[x + 1] + y;
  83. int BA = m_perm[B];
  84. int BB = m_perm[B + 1];
  85. m_permTable2D.SetPixel(x, y, new Color((float)AA/255.0f, (float)AB/255.0f, (float)BA/255.0f, (float)BB/255.0f) );
  86. }
  87. }
  88. m_permTable2D.Apply();
  89. }
  90. void LoadGradient2D()
  91. {
  92. if(m_gradient2D) return;
  93. m_gradient2D = new Texture2D(8, 1, TextureFormat.RGB24, false, true);
  94. m_gradient2D.hideFlags = HideFlags.DontSave;
  95. m_gradient2D.filterMode = FilterMode.Point;
  96. m_gradient2D.wrapMode = TextureWrapMode.Repeat;
  97. for(int i = 0; i < 8; i++)
  98. {
  99. float R = (GRADIENT2[i*2+0] + 1.0f) * 0.5f;
  100. float G = (GRADIENT2[i*2+1] + 1.0f) * 0.5f;
  101. m_gradient2D.SetPixel(i, 0, new Color(R, G, 0, 1));
  102. }
  103. m_gradient2D.Apply();
  104. }
  105. void LoadGradient3D()
  106. {
  107. if(m_gradient3D) return;
  108. m_gradient3D = new Texture2D(SIZE, 1, TextureFormat.RGB24, false, true);
  109. m_gradient3D.hideFlags = HideFlags.DontSave;
  110. m_gradient3D.filterMode = FilterMode.Point;
  111. m_gradient3D.wrapMode = TextureWrapMode.Repeat;
  112. for(int i = 0; i < SIZE; i++)
  113. {
  114. int idx = m_perm[i] % 16;
  115. float R = (GRADIENT3[idx*3+0] + 1.0f) * 0.5f;
  116. float G = (GRADIENT3[idx*3+1] + 1.0f) * 0.5f;
  117. float B = (GRADIENT3[idx*3+2] + 1.0f) * 0.5f;
  118. m_gradient3D.SetPixel(i, 0, new Color(R, G, B, 1));
  119. }
  120. m_gradient3D.Apply();
  121. }
  122. void LoadGradient4D()
  123. {
  124. if(m_gradient4D) return;
  125. m_gradient4D = new Texture2D(SIZE, 1, TextureFormat.ARGB32, false, true);
  126. m_gradient4D.hideFlags = HideFlags.DontSave;
  127. m_gradient4D.filterMode = FilterMode.Point;
  128. m_gradient4D.wrapMode = TextureWrapMode.Repeat;
  129. for(int i = 0; i < SIZE; i++)
  130. {
  131. int idx = m_perm[i] % 32;
  132. float R = (GRADIENT4[idx*4+0] + 1.0f) * 0.5f;
  133. float G = (GRADIENT4[idx*4+1] + 1.0f) * 0.5f;
  134. float B = (GRADIENT4[idx*4+2] + 1.0f) * 0.5f;
  135. float A = (GRADIENT4[idx*4+3] + 1.0f) * 0.5f;
  136. m_gradient4D.SetPixel(i, 0, new Color(R, G, B, A));
  137. }
  138. m_gradient4D.Apply();
  139. }
  140. static float[] GRADIENT2 = new float[]
  141. {
  142. 0, 1,
  143. 1, 1,
  144. 1, 0,
  145. 1, -1,
  146. 0, -1,
  147. -1, -1,
  148. -1, 0,
  149. -1, 1,
  150. };
  151. static float[] GRADIENT3 = new float[]
  152. {
  153. 1,1,0,
  154. -1,1,0,
  155. 1,-1,0,
  156. -1,-1,0,
  157. 1,0,1,
  158. -1,0,1,
  159. 1,0,-1,
  160. -1,0,-1,
  161. 0,1,1,
  162. 0,-1,1,
  163. 0,1,-1,
  164. 0,-1,-1,
  165. 1,1,0,
  166. 0,-1,1,
  167. -1,1,0,
  168. 0,-1,-1,
  169. };
  170. static float[] GRADIENT4 = new float[]
  171. {
  172. 0, -1, -1, -1,
  173. 0, -1, -1, 1,
  174. 0, -1, 1, -1,
  175. 0, -1, 1, 1,
  176. 0, 1, -1, -1,
  177. 0, 1, -1, 1,
  178. 0, 1, 1, -1,
  179. 0, 1, 1, 1,
  180. -1, -1, 0, -1,
  181. -1, 1, 0, -1,
  182. 1, -1, 0, -1,
  183. 1, 1, 0, -1,
  184. -1, -1, 0, 1,
  185. -1, 1, 0, 1,
  186. 1, -1, 0, 1,
  187. 1, 1, 0, 1,
  188. -1, 0, -1, -1,
  189. 1, 0, -1, -1,
  190. -1, 0, -1, 1,
  191. 1, 0, -1, 1,
  192. -1, 0, 1, -1,
  193. 1, 0, 1, -1,
  194. -1, 0, 1, 1,
  195. 1, 0, 1, 1,
  196. 0, -1, -1, 0,
  197. 0, -1, -1, 0,
  198. 0, -1, 1, 0,
  199. 0, -1, 1, 0,
  200. 0, 1, -1, 0,
  201. 0, 1, -1, 0,
  202. 0, 1, 1, 0,
  203. 0, 1, 1, 0,
  204. };
  205. }