aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/World/Warp3DMap/Perlin.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Warp3DMap/Perlin.cs')
-rw-r--r--OpenSim/Region/CoreModules/World/Warp3DMap/Perlin.cs273
1 files changed, 273 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/World/Warp3DMap/Perlin.cs b/OpenSim/Region/CoreModules/World/Warp3DMap/Perlin.cs
new file mode 100644
index 0000000..af59d7a
--- /dev/null
+++ b/OpenSim/Region/CoreModules/World/Warp3DMap/Perlin.cs
@@ -0,0 +1,273 @@
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
28using System;
29using OpenMetaverse;
30
31namespace OpenSim.Region.CoreModules.World.Warp3DMap
32{
33 public static class Perlin
34 {
35 // We use a hardcoded seed to keep the noise generation consistent between runs
36 private const int SEED = 42;
37
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;
42
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];
47
48 static Perlin()
49 {
50 Random rng = new Random(SEED);
51 int i, j, k;
52
53 for (i = 0; i < B; i++)
54 {
55 p[i] = i;
56 g1[i] = (float)((rng.Next() % (B + B)) - B) / B;
57
58 for (j = 0; j < 2; j++)
59 g2[i, j] = (float)((rng.Next() % (B + B)) - B) / B;
60 normalize2(g2, i);
61
62 for (j = 0; j < 3; j++)
63 g3[i, j] = (float)((rng.Next() % (B + B)) - B) / B;
64 normalize3(g3, i);
65 }
66
67 while (--i > 0)
68 {
69 k = p[i];
70 p[i] = p[j = rng.Next() % B];
71 p[j] = k;
72 }
73
74 for (i = 0; i < B + 2; i++)
75 {
76 p[B + i] = p[i];
77 g1[B + i] = g1[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];
82 }
83 }
84
85 public static float noise1(float arg)
86 {
87 int bx0, bx1;
88 float rx0, rx1, sx, t, u, v, a;
89
90 a = arg;
91
92 t = arg + N;
93 bx0 = ((int)t) & BM;
94 bx1 = (bx0 + 1) & BM;
95 rx0 = t - (int)t;
96 rx1 = rx0 - 1f;
97
98 sx = s_curve(rx0);
99
100 u = rx0 * g1[p[bx0]];
101 v = rx1 * g1[p[bx1]];
102
103 return Utils.Lerp(u, v, sx);
104 }
105
106 public static float noise2(float x, float y)
107 {
108 int bx0, bx1, by0, by1, b00, b10, b01, b11;
109 float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
110 int i, j;
111
112 t = x + N;
113 bx0 = ((int)t) & BM;
114 bx1 = (bx0 + 1) & BM;
115 rx0 = t - (int)t;
116 rx1 = rx0 - 1f;
117
118 t = y + N;
119 by0 = ((int)t) & BM;
120 by1 = (by0 + 1) & BM;
121 ry0 = t - (int)t;
122 ry1 = ry0 - 1f;
123
124 i = p[bx0];
125 j = p[bx1];
126
127 b00 = p[i + by0];
128 b10 = p[j + by0];
129 b01 = p[i + by1];
130 b11 = p[j + by1];
131
132 sx = s_curve(rx0);
133 sy = s_curve(ry0);
134
135 u = rx0 * g2[b00, 0] + ry0 * g2[b00, 1];
136 v = rx1 * g2[b10, 0] + ry0 * g2[b10, 1];
137 a = Utils.Lerp(u, v, sx);
138
139 u = rx0 * g2[b01, 0] + ry1 * g2[b01, 1];
140 v = rx1 * g2[b11, 0] + ry1 * g2[b11, 1];
141 b = Utils.Lerp(u, v, sx);
142
143 return Utils.Lerp(a, b, sy);
144 }
145
146 public static float noise3(float x, float y, float z)
147 {
148 int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
149 float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
150 int i, j;
151
152 t = x + N;
153 bx0 = ((int)t) & BM;
154 bx1 = (bx0 + 1) & BM;
155 rx0 = t - (int)t;
156 rx1 = rx0 - 1f;
157
158 t = y + N;
159 by0 = ((int)t) & BM;
160 by1 = (by0 + 1) & BM;
161 ry0 = t - (int)t;
162 ry1 = ry0 - 1f;
163
164 t = z + N;
165 bz0 = ((int)t) & BM;
166 bz1 = (bz0 + 1) & BM;
167 rz0 = t - (int)t;
168 rz1 = rz0 - 1f;
169
170 i = p[bx0];
171 j = p[bx1];
172
173 b00 = p[i + by0];
174 b10 = p[j + by0];
175 b01 = p[i + by1];
176 b11 = p[j + by1];
177
178 t = s_curve(rx0);
179 sy = s_curve(ry0);
180 sz = s_curve(rz0);
181
182 u = rx0 * g3[b00 + bz0, 0] + ry0 * g3[b00 + bz0, 1] + rz0 * g3[b00 + bz0, 2];
183 v = rx1 * g3[b10 + bz0, 0] + ry0 * g3[b10 + bz0, 1] + rz0 * g3[b10 + bz0, 2];
184 a = Utils.Lerp(u, v, t);
185
186 u = rx0 * g3[b01 + bz0, 0] + ry1 * g3[b01 + bz0, 1] + rz0 * g3[b01 + bz0, 2];
187 v = rx1 * g3[b11 + bz0, 0] + ry1 * g3[b11 + bz0, 1] + rz0 * g3[b11 + bz0, 2];
188 b = Utils.Lerp(u, v, t);
189
190 c = Utils.Lerp(a, b, sy);
191
192 u = rx0 * g3[b00 + bz1, 0] + ry0 * g3[b00 + bz1, 1] + rz1 * g3[b00 + bz1, 2];
193 v = rx1 * g3[b10 + bz1, 0] + ry0 * g3[b10 + bz1, 1] + rz1 * g3[b10 + bz1, 2];
194 a = Utils.Lerp(u, v, t);
195
196 u = rx0 * g3[b01 + bz1, 0] + ry1 * g3[b01 + bz1, 1] + rz1 * g3[b01 + bz1, 2];
197 v = rx1 * g3[b11 + bz1, 0] + ry1 * g3[b11 + bz1, 1] + rz1 * g3[b11 + bz1, 2];
198 b = Utils.Lerp(u, v, t);
199
200 d = Utils.Lerp(a, b, sy);
201 return Utils.Lerp(c, d, sz);
202 }
203
204 public static float turbulence1(float x, float freq)
205 {
206 float t;
207 float v;
208
209 for (t = 0f; freq >= 1f; freq *= 0.5f)
210 {
211 v = freq * x;
212 t += noise1(v) / freq;
213 }
214 return t;
215 }
216
217 public static float turbulence2(float x, float y, float freq)
218 {
219 float t;
220 Vector2 vec;
221
222 for (t = 0f; freq >= 1f; freq *= 0.5f)
223 {
224 vec.X = freq * x;
225 vec.Y = freq * y;
226 t += noise2(vec.X, vec.Y) / freq;
227 }
228 return t;
229 }
230
231 public static float turbulence3(float x, float y, float z, float freq)
232 {
233 float t;
234 Vector3 vec;
235
236 for (t = 0f; freq >= 1f; freq *= 0.5f)
237 {
238 vec.X = freq * x;
239 vec.Y = freq * y;
240 vec.Z = freq * z;
241 t += noise3(vec.X, vec.Y, vec.Z) / freq;
242 }
243 return t;
244 }
245
246 private static void normalize2(float[,] v, int i)
247 {
248 float s;
249
250 s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1]);
251 s = 1.0f / s;
252 v[i, 0] = v[i, 0] * s;
253 v[i, 1] = v[i, 1] * s;
254 }
255
256 private static void normalize3(float[,] v, int i)
257 {
258 float s;
259
260 s = (float)Math.Sqrt(v[i, 0] * v[i, 0] + v[i, 1] * v[i, 1] + v[i, 2] * v[i, 2]);
261 s = 1.0f / s;
262
263 v[i, 0] = v[i, 0] * s;
264 v[i, 1] = v[i, 1] * s;
265 v[i, 2] = v[i, 2] * s;
266 }
267
268 private static float s_curve(float t)
269 {
270 return t * t * (3f - 2f * t);
271 }
272 }
273}