31 namespace OpenSim.Region.CoreModules.World.Warp3DMap
33 public static class Perlin
36 private const int SEED = 42;
38 private const int SAMPLE_SIZE = 1024;
39 private const int B = SAMPLE_SIZE;
40 private const int BM = SAMPLE_SIZE - 1;
41 private const int N = 0x1000;
43 private static readonly
int[] p =
new int[SAMPLE_SIZE + SAMPLE_SIZE + 2];
44 private static readonly
float[,] g3 =
new float[SAMPLE_SIZE + SAMPLE_SIZE + 2, 3];
45 private static readonly
float[,] g2 =
new float[SAMPLE_SIZE + SAMPLE_SIZE + 2, 2];
46 private static readonly
float[] g1 =
new float[SAMPLE_SIZE + SAMPLE_SIZE + 2];
50 Random rng =
new Random(SEED);
53 for (i = 0; i < B; i++)
56 g1[i] = (float)((rng.Next() % (B + B)) - B) / B;
58 for (j = 0; j < 2; j++)
59 g2[i, j] = (
float)((rng.Next() % (B + B)) - B) / B;
62 for (j = 0; j < 3; j++)
63 g3[i, j] = (
float)((rng.Next() % (B + B)) - B) / B;
70 p[i] = p[j = rng.Next() % B];
74 for (i = 0; i < B + 2; i++)
78 for (j = 0; j < 2; j++)
79 g2[B + i, j] = g2[i, j];
80 for (j = 0; j < 3; j++)
81 g3[B + i, j] = g3[i, j];
85 public static float noise1(
float arg)
88 float rx0, rx1, sx, t, u, v;
101 return Utils.Lerp(u, v, sx);
104 public static float noise2(
float x,
float y)
106 int bx0, bx1, by0, by1, b00, b10, b01, b11;
107 float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
112 bx1 = (bx0 + 1) & BM;
118 by1 = (by0 + 1) & BM;
133 u = rx0 * g2[b00, 0] + ry0 * g2[b00, 1];
134 v = rx1 * g2[b10, 0] + ry0 * g2[b10, 1];
135 a = Utils.Lerp(u, v, sx);
137 u = rx0 * g2[b01, 0] + ry1 * g2[b01, 1];
138 v = rx1 * g2[b11, 0] + ry1 * g2[b11, 1];
139 b = Utils.Lerp(u, v, sx);
141 return Utils.Lerp(a, b, sy);
144 public static float noise3(
float x,
float y,
float z)
146 int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
147 float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
152 bx1 = (bx0 + 1) & BM;
158 by1 = (by0 + 1) & BM;
164 bz1 = (bz0 + 1) & BM;
180 u = rx0 * g3[b00 + bz0, 0] + ry0 * g3[b00 + bz0, 1] + rz0 * g3[b00 + bz0, 2];
181 v = rx1 * g3[b10 + bz0, 0] + ry0 * g3[b10 + bz0, 1] + rz0 * g3[b10 + bz0, 2];
182 a = Utils.Lerp(u, v, t);
184 u = rx0 * g3[b01 + bz0, 0] + ry1 * g3[b01 + bz0, 1] + rz0 * g3[b01 + bz0, 2];
185 v = rx1 * g3[b11 + bz0, 0] + ry1 * g3[b11 + bz0, 1] + rz0 * g3[b11 + bz0, 2];
186 b = Utils.Lerp(u, v, t);
188 c = Utils.Lerp(a, b, sy);
190 u = rx0 * g3[b00 + bz1, 0] + ry0 * g3[b00 + bz1, 1] + rz1 * g3[b00 + bz1, 2];
191 v = rx1 * g3[b10 + bz1, 0] + ry0 * g3[b10 + bz1, 1] + rz1 * g3[b10 + bz1, 2];
192 a = Utils.Lerp(u, v, t);
194 u = rx0 * g3[b01 + bz1, 0] + ry1 * g3[b01 + bz1, 1] + rz1 * g3[b01 + bz1, 2];
195 v = rx1 * g3[b11 + bz1, 0] + ry1 * g3[b11 + bz1, 1] + rz1 * g3[b11 + bz1, 2];
196 b = Utils.Lerp(u, v, t);
198 d = Utils.Lerp(a, b, sy);
199 return Utils.Lerp(c, d, sz);
202 public static float turbulence1(
float x,
float freq)
207 for (t = 0f; freq >= 1f; freq *= 0.5f)
210 t += noise1(v) / freq;
215 public static float turbulence2(
float x,
float y,
float freq)
220 for (t = 0f; freq >= 1f; freq *= 0.5f)
224 t += noise2(vec.X, vec.Y) / freq;
229 public static float turbulence3(
float x,
float y,
float z,
float freq)
234 for (t = 0f; freq >= 1f; freq *= 0.5f)
239 t += noise3(vec.X, vec.Y, vec.Z) / freq;
244 private static void normalize2(
float[,] v,
int i)
248 s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1]);
250 v[i, 0] = v[i, 0] * s;
251 v[i, 1] = v[i, 1] * s;
254 private static void normalize3(
float[,] v,
int i)
258 s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1] + v[i, 2] * v[i, 2]);
261 v[i, 0] = v[i, 0] * s;
262 v[i, 1] = v[i, 1] * s;
263 v[i, 2] = v[i, 2] * s;
266 private static float s_curve(
float t)
268 return t * t * (3f - 2f * t);
Interactive OpenSim region server