Perlin.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using OpenMetaverse;
  29. namespace OpenSim.Region.CoreModules.World.Warp3DMap
  30. {
  31. public static class Perlin
  32. {
  33. // We use a hardcoded seed to keep the noise generation consistent between runs
  34. private const int SEED = 42;
  35. private const int SAMPLE_SIZE = 1024;
  36. private const int B = SAMPLE_SIZE;
  37. private const int BM = SAMPLE_SIZE - 1;
  38. private const int N = 0x1000;
  39. private static readonly int[] p = new int[SAMPLE_SIZE + SAMPLE_SIZE + 2];
  40. private static readonly float[,] g3 = new float[SAMPLE_SIZE + SAMPLE_SIZE + 2, 3];
  41. private static readonly float[,] g2 = new float[SAMPLE_SIZE + SAMPLE_SIZE + 2, 2];
  42. private static readonly float[] g1 = new float[SAMPLE_SIZE + SAMPLE_SIZE + 2];
  43. static Perlin()
  44. {
  45. Random rng = new Random(SEED);
  46. int i, j, k;
  47. for (i = 0; i < B; i++)
  48. {
  49. p[i] = i;
  50. g1[i] = (float)((rng.Next() % (B + B)) - B) / B;
  51. for (j = 0; j < 2; j++)
  52. g2[i, j] = (float)((rng.Next() % (B + B)) - B) / B;
  53. normalize2(g2, i);
  54. for (j = 0; j < 3; j++)
  55. g3[i, j] = (float)((rng.Next() % (B + B)) - B) / B;
  56. normalize3(g3, i);
  57. }
  58. while (--i > 0)
  59. {
  60. k = p[i];
  61. p[i] = p[j = rng.Next() % B];
  62. p[j] = k;
  63. }
  64. for (i = 0; i < B + 2; i++)
  65. {
  66. p[B + i] = p[i];
  67. g1[B + i] = g1[i];
  68. for (j = 0; j < 2; j++)
  69. g2[B + i, j] = g2[i, j];
  70. for (j = 0; j < 3; j++)
  71. g3[B + i, j] = g3[i, j];
  72. }
  73. }
  74. public static float noise1(float arg)
  75. {
  76. int bx0, bx1;
  77. float rx0, rx1, sx, t, u, v;
  78. t = arg + N;
  79. bx0 = ((int)t) & BM;
  80. bx1 = (bx0 + 1) & BM;
  81. rx0 = t - (int)t;
  82. rx1 = rx0 - 1f;
  83. sx = s_curve(rx0);
  84. u = rx0 * g1[p[bx0]];
  85. v = rx1 * g1[p[bx1]];
  86. return Utils.Lerp(u, v, sx);
  87. }
  88. public static float noise2(float x, float y)
  89. {
  90. int bx0, bx1, by0, by1, b00, b10, b01, b11;
  91. float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
  92. int i, j;
  93. t = x + N;
  94. bx0 = ((int)t) & BM;
  95. bx1 = (bx0 + 1) & BM;
  96. rx0 = t - (int)t;
  97. rx1 = rx0 - 1f;
  98. t = y + N;
  99. by0 = ((int)t) & BM;
  100. by1 = (by0 + 1) & BM;
  101. ry0 = t - (int)t;
  102. ry1 = ry0 - 1f;
  103. i = p[bx0];
  104. j = p[bx1];
  105. b00 = p[i + by0];
  106. b10 = p[j + by0];
  107. b01 = p[i + by1];
  108. b11 = p[j + by1];
  109. sx = s_curve(rx0);
  110. sy = s_curve(ry0);
  111. u = rx0 * g2[b00, 0] + ry0 * g2[b00, 1];
  112. v = rx1 * g2[b10, 0] + ry0 * g2[b10, 1];
  113. a = Utils.Lerp(u, v, sx);
  114. u = rx0 * g2[b01, 0] + ry1 * g2[b01, 1];
  115. v = rx1 * g2[b11, 0] + ry1 * g2[b11, 1];
  116. b = Utils.Lerp(u, v, sx);
  117. return Utils.Lerp(a, b, sy);
  118. }
  119. public static float noise3(float x, float y, float z)
  120. {
  121. int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
  122. float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
  123. int i, j;
  124. t = x + N;
  125. bx0 = ((int)t) & BM;
  126. bx1 = (bx0 + 1) & BM;
  127. rx0 = t - (int)t;
  128. rx1 = rx0 - 1f;
  129. t = y + N;
  130. by0 = ((int)t) & BM;
  131. by1 = (by0 + 1) & BM;
  132. ry0 = t - (int)t;
  133. ry1 = ry0 - 1f;
  134. t = z + N;
  135. bz0 = ((int)t) & BM;
  136. bz1 = (bz0 + 1) & BM;
  137. rz0 = t - (int)t;
  138. rz1 = rz0 - 1f;
  139. i = p[bx0];
  140. j = p[bx1];
  141. b00 = p[i + by0];
  142. b10 = p[j + by0];
  143. b01 = p[i + by1];
  144. b11 = p[j + by1];
  145. t = s_curve(rx0);
  146. sy = s_curve(ry0);
  147. sz = s_curve(rz0);
  148. u = rx0 * g3[b00 + bz0, 0] + ry0 * g3[b00 + bz0, 1] + rz0 * g3[b00 + bz0, 2];
  149. v = rx1 * g3[b10 + bz0, 0] + ry0 * g3[b10 + bz0, 1] + rz0 * g3[b10 + bz0, 2];
  150. a = Utils.Lerp(u, v, t);
  151. u = rx0 * g3[b01 + bz0, 0] + ry1 * g3[b01 + bz0, 1] + rz0 * g3[b01 + bz0, 2];
  152. v = rx1 * g3[b11 + bz0, 0] + ry1 * g3[b11 + bz0, 1] + rz0 * g3[b11 + bz0, 2];
  153. b = Utils.Lerp(u, v, t);
  154. c = Utils.Lerp(a, b, sy);
  155. u = rx0 * g3[b00 + bz1, 0] + ry0 * g3[b00 + bz1, 1] + rz1 * g3[b00 + bz1, 2];
  156. v = rx1 * g3[b10 + bz1, 0] + ry0 * g3[b10 + bz1, 1] + rz1 * g3[b10 + bz1, 2];
  157. a = Utils.Lerp(u, v, t);
  158. u = rx0 * g3[b01 + bz1, 0] + ry1 * g3[b01 + bz1, 1] + rz1 * g3[b01 + bz1, 2];
  159. v = rx1 * g3[b11 + bz1, 0] + ry1 * g3[b11 + bz1, 1] + rz1 * g3[b11 + bz1, 2];
  160. b = Utils.Lerp(u, v, t);
  161. d = Utils.Lerp(a, b, sy);
  162. return Utils.Lerp(c, d, sz);
  163. }
  164. public static float turbulence1(float x, float freq)
  165. {
  166. float t;
  167. float v;
  168. for (t = 0f; freq >= 1f; freq *= 0.5f)
  169. {
  170. v = freq * x;
  171. t += noise1(v) / freq;
  172. }
  173. return t;
  174. }
  175. public static float turbulence2(float x, float y, float freq)
  176. {
  177. float t;
  178. Vector2 vec;
  179. for (t = 0f; freq >= 1f; freq *= 0.5f)
  180. {
  181. vec.X = freq * x;
  182. vec.Y = freq * y;
  183. t += noise2(vec.X, vec.Y) / freq;
  184. }
  185. return t;
  186. }
  187. public static float turbulence3(float x, float y, float z, float freq)
  188. {
  189. float t;
  190. Vector3 vec;
  191. for (t = 0f; freq >= 1f; freq *= 0.5f)
  192. {
  193. vec.X = freq * x;
  194. vec.Y = freq * y;
  195. vec.Z = freq * z;
  196. t += noise3(vec.X, vec.Y, vec.Z) / freq;
  197. }
  198. return t;
  199. }
  200. private static void normalize2(float[,] v, int i)
  201. {
  202. float s;
  203. s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1]);
  204. s = 1.0f / s;
  205. v[i, 0] = v[i, 0] * s;
  206. v[i, 1] = v[i, 1] * s;
  207. }
  208. private static void normalize3(float[,] v, int i)
  209. {
  210. float s;
  211. s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1] + v[i, 2] * v[i, 2]);
  212. s = 1.0f / s;
  213. v[i, 0] = v[i, 0] * s;
  214. v[i, 1] = v[i, 1] * s;
  215. v[i, 2] = v[i, 2] * s;
  216. }
  217. private static float s_curve(float t)
  218. {
  219. return t * t * (3f - 2f * t);
  220. }
  221. }
  222. }