Perlin.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 bx, by, 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. rx0 = t - (int)t;
  95. bx = ((int)t) & BM;
  96. i = p[bx];
  97. bx = (bx + 1) & BM;
  98. j = p[bx];
  99. t = y + N;
  100. ry0 = t - (int)t;
  101. by = ((int)t) & BM;
  102. b00 = p[i + by];
  103. b10 = p[j + by];
  104. by = (by + 1) & BM;
  105. b01 = p[i + by];
  106. b11 = p[j + by];
  107. sx = s_curve(rx0);
  108. u = rx0 * g2[b00, 0] + ry0 * g2[b00, 1];
  109. rx1 = rx0 - 1f;
  110. v = rx1 * g2[b10, 0] + ry0 * g2[b10, 1];
  111. a = Utils.Lerp(u, v, sx);
  112. ry1 = ry0 - 1f;
  113. u = rx0 * g2[b01, 0] + ry1 * g2[b01, 1];
  114. v = rx1 * g2[b11, 0] + ry1 * g2[b11, 1];
  115. b = Utils.Lerp(u, v, sx);
  116. sy = s_curve(ry0);
  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. for (t = 0f; freq >= 1f; freq *= 0.5f)
  168. {
  169. t += noise1(freq * x) / freq;
  170. }
  171. return t;
  172. }
  173. public static float turbulence2(float x, float y, float freq)
  174. {
  175. float t;
  176. for (t = 0f; freq >= 1f; freq *= 0.5f)
  177. t += noise2(freq * x, freq * y) / freq;
  178. return t;
  179. }
  180. public static float turbulence3(float x, float y, float z, float freq)
  181. {
  182. float t;
  183. for (t = 0f; freq >= 1f; freq *= 0.5f)
  184. {
  185. t += noise3(freq * x, freq * y, freq * z) / freq;
  186. }
  187. return t;
  188. }
  189. private static void normalize2(float[,] v, int i)
  190. {
  191. float s;
  192. float a = v[i, 0];
  193. float b = v[i, 1];
  194. s = (float)Math.Sqrt(a * a + b * b);
  195. s = 1.0f / s;
  196. v[i, 0] = a * s;
  197. v[i, 1] = b * s;
  198. }
  199. private static void normalize3(float[,] v, int i)
  200. {
  201. float s;
  202. float a = v[i, 0];
  203. float b = v[i, 1];
  204. float c = v[i, 2];
  205. s = (float)Math.Sqrt(a * a + b * b + c * c);
  206. s = 1.0f / s;
  207. v[i, 0] = a * s;
  208. v[i, 1] = b * s;
  209. v[i, 2] = c * s;
  210. }
  211. private static float s_curve(float t)
  212. {
  213. return t * t * (3f - 2f * t);
  214. }
  215. }
  216. }