| 1 |
{ |
| 2 |
Copyright 2009-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Generating noise. } |
| 17 |
unit CastleInternalNoise; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
{ Noise for 2D coords, resulting in float 0..1 range. |
| 24 |
|
| 25 |
This is the interpolated integer noise. That is, |
| 26 |
values on integer grid points (when X, Y are ints) |
| 27 |
come from the really independent "integer noise". |
| 28 |
Values between integer points are interpolated. |
| 29 |
|
| 30 |
Suffix describes interpolation method. |
| 31 |
|
| 32 |
@groupBegin } |
| 33 |
function InterpolatedNoise2D_None(const X, Y: Single; const Seed: Cardinal): Single; |
| 34 |
function InterpolatedNoise2D_Linear(const X, Y: Single; const Seed: Cardinal): Single; |
| 35 |
function InterpolatedNoise2D_Cosine(const X, Y: Single; const Seed: Cardinal): Single; |
| 36 |
function InterpolatedNoise2D_Spline(const X, Y: Single; const Seed: Cardinal): Single; |
| 37 |
{ @groupEnd } |
| 38 |
|
| 39 |
{ Noise for 2D coords, resulting in float 0..1 range, additionally blurred. |
| 40 |
|
| 41 |
Blurring smooths the noise by averaging values a little with neighbors |
| 42 |
(just like blur on normal 2D images). This helps for 2D noise |
| 43 |
to be less vertical/horizontal oriented. |
| 44 |
|
| 45 |
BlurredInterpolatedNoise* functions first blur |
| 46 |
the IntegerNoise, and then blurred noise is interpolated. |
| 47 |
Results are the same as the other way around (first interpolate IntegerNoise, |
| 48 |
and only then blur it), and marginally faster. |
| 49 |
|
| 50 |
@groupBegin } |
| 51 |
function BlurredInterpolatedNoise2D_None(const X, Y: Single; const Seed: Cardinal): Single; |
| 52 |
function BlurredInterpolatedNoise2D_Linear(const X, Y: Single; const Seed: Cardinal): Single; |
| 53 |
function BlurredInterpolatedNoise2D_Cosine(const X, Y: Single; const Seed: Cardinal): Single; |
| 54 |
function BlurredInterpolatedNoise2D_Spline(const X, Y: Single; const Seed: Cardinal): Single; |
| 55 |
{ @groupEnd } |
| 56 |
|
| 57 |
implementation |
| 58 |
|
| 59 |
uses Math, CastleCurves; |
| 60 |
|
| 61 |
{ Integer noise -------------------------------------------------------------- } |
| 62 |
|
| 63 |
{ Make integer noise: for 3 given Int32s, generate random UInt32 value. |
| 64 |
|
| 65 |
It is designed to fill uniformly the UInt32 range. |
| 66 |
So if you want float result in 0 ... 1, just divide by High(UInt32), |
| 67 |
which is exactly what IntegerNoise (without Core suffix) does. |
| 68 |
|
| 69 |
If you want noise based on 2D coords, just pass Z = 0 -- it's Ok, |
| 70 |
noise will still nicely fill whole range. |
| 71 |
|
| 72 |
Seed chooses prime numbers that are "seeds" for this noise. |
| 73 |
Any Cardinal value is Ok, although actually we have only ~100 sets |
| 74 |
of prime numbers prepared, so Seed = i*100+k may be equal |
| 75 |
to Seed = k. } |
| 76 |
|
| 77 |
{ This integer noise implementation is very close to the one on |
| 78 |
http://freespace.virgin.net/hugo.elias/models/m_perlin.htm, |
| 79 |
and it follows Blender's cellNoiseU. Especially Seed = 0 |
| 80 |
values are equal. } |
| 81 |
|
| 82 |
const |
| 83 |
Primes: array [0..99] of array [0..4] of UInt32 = ( |
| 84 |
(1301, 314159, 15731, 789221, 1376312589), |
| 85 |
{ Following rows were generated. See gen_primes.lpr to know how } |
| 86 |
(1847, 423307, 10139, 1249603, 1665650897), |
| 87 |
(1987, 728471, 14081, 2490529, 1392026473), |
| 88 |
(1523, 311111, 12149, 4335337, 1415538263), |
| 89 |
(1097, 727427, 18637, 3364037, 1150632649), |
| 90 |
(1279, 535991, 19889, 1354247, 1372899589), |
| 91 |
(1637, 546841, 15859, 4067467, 1842430829), |
| 92 |
(1801, 609101, 14699, 1722163, 1301061851), |
| 93 |
(1571, 678157, 12569, 1476983, 1425758801), |
| 94 |
(1277, 770447, 12569, 3671453, 1160210171), |
| 95 |
(1693, 247337, 14533, 2917181, 1096009073), |
| 96 |
(1997, 472631, 15101, 2206927, 1308414101), |
| 97 |
(1381, 243343, 13399, 3593581, 1473006989), |
| 98 |
(1087, 343787, 19121, 1666303, 1827806587), |
| 99 |
(1511, 432457, 19889, 4688323, 1140647509), |
| 100 |
(1423, 638423, 10859, 2792261, 1388850959), |
| 101 |
(1693, 306941, 12227, 2114197, 1486745171), |
| 102 |
(1811, 459623, 12097, 1701967, 1789608661), |
| 103 |
(1931, 333997, 16453, 1909399, 1963309847), |
| 104 |
(1471, 663031, 19333, 3723271, 1123370333), |
| 105 |
(1181, 511991, 12527, 4514173, 1844831603), |
| 106 |
(1847, 752681, 10061, 3083083, 1107775621), |
| 107 |
(1279, 209371, 10597, 4558963, 1469510213), |
| 108 |
(1129, 255349, 12071, 3496249, 1937407343), |
| 109 |
(1361, 275201, 19751, 2685509, 1380662681), |
| 110 |
(1979, 258697, 10781, 971171, 1622753981), |
| 111 |
(1993, 279269, 12301, 3882007, 1789168301), |
| 112 |
(1511, 756191, 13121, 2518079, 1145597183), |
| 113 |
(1907, 476849, 14947, 739759, 1025324617), |
| 114 |
(1187, 783269, 18679, 1988659, 1565677307), |
| 115 |
(1693, 522127, 16127, 643619, 1325160337), |
| 116 |
(1973, 282377, 10657, 4793471, 1182127627), |
| 117 |
(1061, 206519, 19687, 2211817, 1290819697), |
| 118 |
(1487, 483139, 13399, 3299017, 1620163709), |
| 119 |
(1733, 399353, 18131, 3896413, 1782206801), |
| 120 |
(1823, 539837, 17509, 2924893, 1579678643), |
| 121 |
(1279, 662281, 11117, 3868649, 1460807707), |
| 122 |
(1307, 556573, 18701, 4909981, 1020040097), |
| 123 |
(1399, 783227, 11617, 1585261, 1683233333), |
| 124 |
(1511, 489329, 16417, 977507, 1076020889), |
| 125 |
(1861, 535663, 15527, 3241877, 1316557103), |
| 126 |
(1493, 576151, 15493, 1359857, 1995392447), |
| 127 |
(1741, 690583, 15451, 4715257, 1544226283), |
| 128 |
(1549, 597643, 15391, 2167259, 1505011861), |
| 129 |
(1049, 230189, 16741, 4954721, 1113820843), |
| 130 |
(1721, 492799, 13757, 4611911, 1099761457), |
| 131 |
(1709, 731681, 10831, 1725359, 1372268377), |
| 132 |
(1319, 221773, 15013, 587341, 1923567649), |
| 133 |
(1823, 755861, 17891, 3275137, 1842351779), |
| 134 |
(1087, 624467, 10499, 4473671, 1755489077), |
| 135 |
(1259, 410587, 10723, 827941, 1921405481), |
| 136 |
(1987, 215921, 16217, 1807891, 1523633599), |
| 137 |
(1879, 409379, 13043, 2535179, 1004391049), |
| 138 |
(1033, 629711, 13469, 3431009, 1323242267), |
| 139 |
(1597, 636739, 12517, 541339, 1590396649), |
| 140 |
(1091, 770207, 10987, 3032399, 1573672669), |
| 141 |
(1931, 729037, 10007, 2094601, 1848962197), |
| 142 |
(1567, 722983, 12377, 2413097, 1767850663), |
| 143 |
(1663, 511991, 14173, 1883237, 1388814047), |
| 144 |
(1069, 351359, 10937, 2999231, 1701603977), |
| 145 |
(1361, 376171, 17359, 3707813, 1047300581), |
| 146 |
(1319, 248509, 17729, 1920613, 1897665247), |
| 147 |
(1453, 423649, 11971, 3806837, 1481850761), |
| 148 |
(1481, 441307, 14591, 1897277, 1468411541), |
| 149 |
(1061, 341027, 16319, 4835749, 1510244311), |
| 150 |
(1259, 629059, 13613, 3925729, 1965058607), |
| 151 |
(1409, 602593, 18287, 878651, 1292754097), |
| 152 |
(1289, 488729, 16889, 2448613, 1527555457), |
| 153 |
(1597, 555419, 18119, 2529953, 1988884747), |
| 154 |
(1973, 205267, 15199, 684599, 1125570263), |
| 155 |
(1949, 665069, 16229, 4194847, 1152730927), |
| 156 |
(1657, 299857, 16979, 1749569, 1194919007), |
| 157 |
(1361, 740303, 13577, 3384569, 1406711543), |
| 158 |
(1277, 431521, 11527, 2202329, 1269718669), |
| 159 |
(1783, 767093, 11897, 4346663, 1887886951), |
| 160 |
(1091, 534739, 13859, 3246707, 1197746239), |
| 161 |
(1747, 218549, 18787, 4891483, 1205730121), |
| 162 |
(1201, 693697, 13249, 4457093, 1947146767), |
| 163 |
(1847, 703819, 14627, 2416369, 1977370267), |
| 164 |
(1019, 423103, 16691, 811861, 1843870841), |
| 165 |
(1559, 301703, 13877, 2401253, 1568621947), |
| 166 |
(1117, 585691, 14387, 635729, 1326978659), |
| 167 |
(1151, 374837, 16823, 4959883, 1427600791), |
| 168 |
(1181, 428863, 18587, 1630987, 1781740633), |
| 169 |
(1997, 766439, 12781, 826663, 1219929743), |
| 170 |
(1213, 486971, 17929, 2152307, 1617558353), |
| 171 |
(1657, 513101, 12527, 2052311, 1951551751), |
| 172 |
(1277, 657061, 12583, 4566997, 1855642709), |
| 173 |
(1831, 585283, 10243, 549169, 1251554477), |
| 174 |
(1973, 381103, 17231, 3439193, 1736060657), |
| 175 |
(1193, 541349, 18583, 1150117, 1746547661), |
| 176 |
(1607, 635483, 12721, 1968679, 1468711331), |
| 177 |
(1087, 792443, 19949, 2665717, 1775479577), |
| 178 |
(1709, 238529, 10091, 3150529, 1260068023), |
| 179 |
(1319, 639299, 16901, 3629719, 1706624761), |
| 180 |
(1873, 629567, 11261, 1476551, 1447471297), |
| 181 |
(1979, 608459, 18637, 4424353, 1941211913), |
| 182 |
(1237, 631753, 11329, 3096277, 1995626081), |
| 183 |
(1531, 588011, 19597, 4746457, 1208747233), |
| 184 |
(1201, 219977, 10937, 2862599, 1111733503) |
| 185 |
); |
| 186 |
|
| 187 |
function IntegerNoiseCore(const X, Y, Z: Int32; const Seed: Cardinal): UInt32; |
| 188 |
type |
| 189 |
TUInt32Array = array [0..High(Integer) div SizeOf(UInt32) - 1] of UInt32; |
| 190 |
PUInt32Array = ^TUInt32Array; |
| 191 |
var |
| 192 |
N: UInt32; |
| 193 |
PPrimes: PUInt32Array; |
| 194 |
{$I norqcheckbegin.inc} |
| 195 |
begin |
| 196 |
{ Choose our primes row from Primes table. } |
| 197 |
PPrimes := @Primes[Seed mod (High(Primes)+1)]; |
| 198 |
|
| 199 |
N := UInt32(x) + UInt32(y) * PPrimes^[0] + UInt32(z) * PPrimes^[1]; |
| 200 |
N := N xor (N shl 13); |
| 201 |
Result := (n * (n * n * PPrimes^[2] + PPrimes^[3]) + PPrimes^[4]); |
| 202 |
end; |
| 203 |
{$I norqcheckend.inc} |
| 204 |
|
| 205 |
function IntegerNoise(const X, Y, Z: Int32; const Seed: Cardinal): Single; overload; |
| 206 |
begin |
| 207 |
Result := IntegerNoiseCore(X, Y, Z, Seed) / High(UInt32); |
| 208 |
end; |
| 209 |
|
| 210 |
function IntegerNoise(const X, Y: Int32; const Seed: Cardinal): Single; overload; |
| 211 |
begin |
| 212 |
Result := IntegerNoiseCore(X, Y, 0, Seed) / High(UInt32); |
| 213 |
end; |
| 214 |
|
| 215 |
{ Interpolated noise for 2D coords ------------------------------------------- } |
| 216 |
|
| 217 |
function InterpolatedNoise2D_None(const X, Y: Single; const Seed: Cardinal): Single; |
| 218 |
begin |
| 219 |
Result := IntegerNoise(Round(X), Round(Y), Seed); |
| 220 |
end; |
| 221 |
|
| 222 |
function InterpolatedNoise2D_Linear(const X, Y: Single; const Seed: Cardinal): Single; |
| 223 |
{$I castleinternalnoise_interpolatednoise2d_linear_cosine.inc} |
| 224 |
|
| 225 |
function InterpolatedNoise2D_Cosine(const X, Y: Single; const Seed: Cardinal): Single; |
| 226 |
{$define InterpolatedNoise2D_Cosine} |
| 227 |
{$I castleinternalnoise_interpolatednoise2d_linear_cosine.inc} |
| 228 |
{$undef InterpolatedNoise2D_Cosine} |
| 229 |
|
| 230 |
function InterpolatedNoise2D_Spline(const X, Y: Single; const Seed: Cardinal): Single; |
| 231 |
{$I castleinternalnoise_interpolatednoise2d_spline.inc} |
| 232 |
|
| 233 |
{ BlurredInterpolatedNoise* -------------------------------------------------- } |
| 234 |
|
| 235 |
function BlurredIntegerNoise(const X, Y: Int32; const Seed: Cardinal): Single; |
| 236 |
begin |
| 237 |
Result := |
| 238 |
IntegerNoise(X , Y , Seed) / 4 + |
| 239 |
( IntegerNoise(X - 1, Y , Seed) + |
| 240 |
IntegerNoise(X + 1, Y , Seed) + |
| 241 |
IntegerNoise(X , Y - 1, Seed) + |
| 242 |
IntegerNoise(X , Y + 1, Seed) ) / 8 + |
| 243 |
( IntegerNoise(X - 1, Y - 1, Seed) + |
| 244 |
IntegerNoise(X - 1, Y + 1, Seed) + |
| 245 |
IntegerNoise(X + 1, Y - 1, Seed) + |
| 246 |
IntegerNoise(X + 1, Y + 1, Seed) ) / 16; |
| 247 |
end; |
| 248 |
|
| 249 |
function BlurredInterpolatedNoise2D_None(const X, Y: Single; const Seed: Cardinal): Single; |
| 250 |
begin |
| 251 |
Result := BlurredIntegerNoise(Round(X), Round(Y), Seed); |
| 252 |
end; |
| 253 |
|
| 254 |
{$define IntegerNoise := BlurredIntegerNoise} |
| 255 |
|
| 256 |
function BlurredInterpolatedNoise2D_Linear(const X, Y: Single; const Seed: Cardinal): Single; |
| 257 |
{$I castleinternalnoise_interpolatednoise2d_linear_cosine.inc} |
| 258 |
|
| 259 |
function BlurredInterpolatedNoise2D_Cosine(const X, Y: Single; const Seed: Cardinal): Single; |
| 260 |
{$define InterpolatedNoise2D_Cosine} |
| 261 |
{$I castleinternalnoise_interpolatednoise2d_linear_cosine.inc} |
| 262 |
{$undef InterpolatedNoise2D_Cosine} |
| 263 |
|
| 264 |
function BlurredInterpolatedNoise2D_Spline(const X, Y: Single; const Seed: Cardinal): Single; |
| 265 |
{$I castleinternalnoise_interpolatednoise2d_spline.inc} |
| 266 |
|
| 267 |
{$undef IntegerNoise} |
| 268 |
|
| 269 |
end. |