diff options
author | onefang | 2020-09-10 21:20:23 +1000 |
---|---|---|
committer | onefang | 2020-09-10 21:20:23 +1000 |
commit | 2940f325436f5b07dcfbac03545b3eb96e20ffcf (patch) | |
tree | 71babe10d710dac41431d20c8a22387d48f5130b /addon-modules/OpenSim.Modules.Warp3DCachedImageModule/src/Perlin.cs | |
parent | Another ini file I forgot in the last commit. (diff) | |
download | opensim-SC-2940f325436f5b07dcfbac03545b3eb96e20ffcf.zip opensim-SC-2940f325436f5b07dcfbac03545b3eb96e20ffcf.tar.gz opensim-SC-2940f325436f5b07dcfbac03545b3eb96e20ffcf.tar.bz2 opensim-SC-2940f325436f5b07dcfbac03545b3eb96e20ffcf.tar.xz |
Warp3DCachedImageModule from Christopher Latza.
From -
https://clatza.dev/OpenSim/OpenSim.Modules.Warp3DCachedImageModule.git
Commit ad0aa59f53ae77c85a7c745d9af5aa70187568ba on 17/5/20 1:37 AM
Diffstat (limited to '')
-rw-r--r-- | addon-modules/OpenSim.Modules.Warp3DCachedImageModule/src/Perlin.cs | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/addon-modules/OpenSim.Modules.Warp3DCachedImageModule/src/Perlin.cs b/addon-modules/OpenSim.Modules.Warp3DCachedImageModule/src/Perlin.cs new file mode 100644 index 0000000..2279b76 --- /dev/null +++ b/addon-modules/OpenSim.Modules.Warp3DCachedImageModule/src/Perlin.cs | |||
@@ -0,0 +1,264 @@ | |||
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 | |||
28 | using System; | ||
29 | using OpenMetaverse; | ||
30 | |||
31 | namespace 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; | ||
89 | |||
90 | t = arg + N; | ||
91 | bx0 = ((int)t) & BM; | ||
92 | bx1 = (bx0 + 1) & BM; | ||
93 | rx0 = t - (int)t; | ||
94 | rx1 = rx0 - 1f; | ||
95 | |||
96 | sx = s_curve(rx0); | ||
97 | |||
98 | u = rx0 * g1[p[bx0]]; | ||
99 | v = rx1 * g1[p[bx1]]; | ||
100 | |||
101 | return Utils.Lerp(u, v, sx); | ||
102 | } | ||
103 | |||
104 | public static float noise2(float x, float y) | ||
105 | { | ||
106 | int bx, by, b00, b10, b01, b11; | ||
107 | float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v; | ||
108 | int i, j; | ||
109 | |||
110 | t = x + N; | ||
111 | rx0 = t - (int)t; | ||
112 | bx = ((int)t) & BM; | ||
113 | i = p[bx]; | ||
114 | bx = (bx + 1) & BM; | ||
115 | j = p[bx]; | ||
116 | |||
117 | t = y + N; | ||
118 | ry0 = t - (int)t; | ||
119 | by = ((int)t) & BM; | ||
120 | b00 = p[i + by]; | ||
121 | b10 = p[j + by]; | ||
122 | |||
123 | by = (by + 1) & BM; | ||
124 | b01 = p[i + by]; | ||
125 | b11 = p[j + by]; | ||
126 | |||
127 | sx = s_curve(rx0); | ||
128 | u = rx0 * g2[b00, 0] + ry0 * g2[b00, 1]; | ||
129 | rx1 = rx0 - 1f; | ||
130 | v = rx1 * g2[b10, 0] + ry0 * g2[b10, 1]; | ||
131 | a = Utils.Lerp(u, v, sx); | ||
132 | |||
133 | ry1 = ry0 - 1f; | ||
134 | u = rx0 * g2[b01, 0] + ry1 * g2[b01, 1]; | ||
135 | v = rx1 * g2[b11, 0] + ry1 * g2[b11, 1]; | ||
136 | b = Utils.Lerp(u, v, sx); | ||
137 | |||
138 | sy = s_curve(ry0); | ||
139 | return Utils.Lerp(a, b, sy); | ||
140 | } | ||
141 | |||
142 | public static float noise3(float x, float y, float z) | ||
143 | { | ||
144 | int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; | ||
145 | float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v; | ||
146 | int i, j; | ||
147 | |||
148 | t = x + N; | ||
149 | bx0 = ((int)t) & BM; | ||
150 | bx1 = (bx0 + 1) & BM; | ||
151 | rx0 = t - (int)t; | ||
152 | rx1 = rx0 - 1f; | ||
153 | |||
154 | t = y + N; | ||
155 | by0 = ((int)t) & BM; | ||
156 | by1 = (by0 + 1) & BM; | ||
157 | ry0 = t - (int)t; | ||
158 | ry1 = ry0 - 1f; | ||
159 | |||
160 | t = z + N; | ||
161 | bz0 = ((int)t) & BM; | ||
162 | bz1 = (bz0 + 1) & BM; | ||
163 | rz0 = t - (int)t; | ||
164 | rz1 = rz0 - 1f; | ||
165 | |||
166 | i = p[bx0]; | ||
167 | j = p[bx1]; | ||
168 | |||
169 | b00 = p[i + by0]; | ||
170 | b10 = p[j + by0]; | ||
171 | b01 = p[i + by1]; | ||
172 | b11 = p[j + by1]; | ||
173 | |||
174 | t = s_curve(rx0); | ||
175 | sy = s_curve(ry0); | ||
176 | sz = s_curve(rz0); | ||
177 | |||
178 | u = rx0 * g3[b00 + bz0, 0] + ry0 * g3[b00 + bz0, 1] + rz0 * g3[b00 + bz0, 2]; | ||
179 | v = rx1 * g3[b10 + bz0, 0] + ry0 * g3[b10 + bz0, 1] + rz0 * g3[b10 + bz0, 2]; | ||
180 | a = Utils.Lerp(u, v, t); | ||
181 | |||
182 | u = rx0 * g3[b01 + bz0, 0] + ry1 * g3[b01 + bz0, 1] + rz0 * g3[b01 + bz0, 2]; | ||
183 | v = rx1 * g3[b11 + bz0, 0] + ry1 * g3[b11 + bz0, 1] + rz0 * g3[b11 + bz0, 2]; | ||
184 | b = Utils.Lerp(u, v, t); | ||
185 | |||
186 | c = Utils.Lerp(a, b, sy); | ||
187 | |||
188 | u = rx0 * g3[b00 + bz1, 0] + ry0 * g3[b00 + bz1, 1] + rz1 * g3[b00 + bz1, 2]; | ||
189 | v = rx1 * g3[b10 + bz1, 0] + ry0 * g3[b10 + bz1, 1] + rz1 * g3[b10 + bz1, 2]; | ||
190 | a = Utils.Lerp(u, v, t); | ||
191 | |||
192 | u = rx0 * g3[b01 + bz1, 0] + ry1 * g3[b01 + bz1, 1] + rz1 * g3[b01 + bz1, 2]; | ||
193 | v = rx1 * g3[b11 + bz1, 0] + ry1 * g3[b11 + bz1, 1] + rz1 * g3[b11 + bz1, 2]; | ||
194 | b = Utils.Lerp(u, v, t); | ||
195 | |||
196 | d = Utils.Lerp(a, b, sy); | ||
197 | return Utils.Lerp(c, d, sz); | ||
198 | } | ||
199 | |||
200 | public static float turbulence1(float x, float freq) | ||
201 | { | ||
202 | float t; | ||
203 | |||
204 | for (t = 0f; freq >= 1f; freq *= 0.5f) | ||
205 | { | ||
206 | t += noise1(freq * x) / freq; | ||
207 | } | ||
208 | return t; | ||
209 | } | ||
210 | |||
211 | public static float turbulence2(float x, float y, float freq) | ||
212 | { | ||
213 | float t; | ||
214 | |||
215 | for (t = 0f; freq >= 1f; freq *= 0.5f) | ||
216 | t += noise2(freq * x, freq * y) / freq; | ||
217 | |||
218 | return t; | ||
219 | } | ||
220 | |||
221 | public static float turbulence3(float x, float y, float z, float freq) | ||
222 | { | ||
223 | float t; | ||
224 | |||
225 | for (t = 0f; freq >= 1f; freq *= 0.5f) | ||
226 | { | ||
227 | t += noise3(freq * x, freq * y, freq * z) / freq; | ||
228 | } | ||
229 | return t; | ||
230 | } | ||
231 | |||
232 | private static void normalize2(float[,] v, int i) | ||
233 | { | ||
234 | float s; | ||
235 | float a = v[i, 0]; | ||
236 | float b = v[i, 1]; | ||
237 | |||
238 | s = (float)Math.Sqrt(a * a + b * b); | ||
239 | s = 1.0f / s; | ||
240 | v[i, 0] = a * s; | ||
241 | v[i, 1] = b * s; | ||
242 | } | ||
243 | |||
244 | private static void normalize3(float[,] v, int i) | ||
245 | { | ||
246 | float s; | ||
247 | float a = v[i, 0]; | ||
248 | float b = v[i, 1]; | ||
249 | float c = v[i, 2]; | ||
250 | |||
251 | s = (float)Math.Sqrt(a * a + b * b + c * c); | ||
252 | s = 1.0f / s; | ||
253 | |||
254 | v[i, 0] = a * s; | ||
255 | v[i, 1] = b * s; | ||
256 | v[i, 2] = c * s; | ||
257 | } | ||
258 | |||
259 | private static float s_curve(float t) | ||
260 | { | ||
261 | return t * t * (3f - 2f * t); | ||
262 | } | ||
263 | } | ||
264 | } | ||