-
Notifications
You must be signed in to change notification settings - Fork 16
/
Noise.pas
252 lines (204 loc) · 5.46 KB
/
Noise.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
unit Noise;
interface
{$G+}
{$N+}
{ based on C code by Ken Perlin }
const
DEFAULT_NOISE_SIZE = $10000;
var
NoiseSizeX: Integer;
NoiseSizeY: Integer;
NoiseSizeZ: Integer;
function Noise1 (x: Real): Real;
function Noise2 (x, y: Real): Real;
function Noise3 (x, y, z: Real): Real;
implementation
const
B = $100;
BM = $FF;
N = $1000;
NP = 12; { 2^N }
NM = $FFF;
var
p: array[0 .. B + B + 1] of Integer;
g3: array[0 .. B + B + 1, 0..2] of Real;
g2: array[0 .. B + B + 1, 0..1] of Real;
g1: array[0 .. B + B + 1] of Real;
function s_curve (t: Real): Real;
begin
s_curve := t * t * (3.0 - 2.0 * t);
end;
function lerp (t, a, b: Real): Real;
begin
lerp := a + t * (b - a);
end;
function Noise1 (x: Real): Real;
var
bx0, bx1: Integer;
rx0, rx1, sx, t, u, v, c: Real;
begin
t := x + N;
bx0 := (Round (Int (t)) mod NoiseSizeX) and BM;
bx1 := ((bx0 + 1) mod NoiseSizeX) and BM;
rx0 := t - (Int (t));
rx1 := rx0 - 1.0;
sx := s_curve (rx0);
u := rx0 * g1[p[bx0]];
v := rx1 * g1[p[bx1]];
c := 0.5 + lerp (sx, u, v);
if c < 0 then
c := 0;
if c > 0.99999999 then
c := 0.99999999;
Noise1 := c;
end;
function Noise2 (x, y: Real): Real;
type
RealArrayPtr = ^RealArray;
RealArray = array[0..1] of Real;
var
bx0, bx1, by0, by1, b00, b10, b01, b11: Integer;
rx0, rx1, ry0, ry1, sx, sy, a, b, c, t, u, v: Real;
q: RealArrayPtr;
i, j: Integer;
begin
t := x + N;
bx0 := (Round (Int (t)) mod NoiseSizeX) and BM;
bx1 := ((bx0 + 1) mod NoiseSizeX) and BM;
rx0 := t - (Int (t));
rx1 := rx0 - 1.0;
t := y + N;
by0 := (Round (Int (t)) mod NoiseSizeY) and BM;
by1 := ((by0 + 1) mod NoiseSizeY) and BM;
ry0 := t - (Int (t));
ry1 := ry0 - 1.0;
i := p[bx0];
j := p[bx1];
b00 := p[i + by0];
b10 := p[j + by0];
b01 := p[i + by1];
b11 := p[j + by1];
sx := s_curve (rx0);
sy := s_curve (ry0);
q := @g2[b00];
u := rx0 * q^[0] + ry0 * q^[1];
q := @g2[b10];
v := rx1 * q^[0] + ry0 * q^[1];
a := lerp (sx, u, v);
q := @g2[b01];
u := rx0 * q^[0] + ry1 * q^[1];
q := @g2[b11];
v := rx1 * q^[0] + ry1 * q^[1];
b := lerp (sx, u, v);
c := 0.5 + lerp (sy, a, b);
if c < 0 then
c := 0;
if c > 0.99999999 then
c := 0.99999999;
Noise2 := c;
end;
function Noise3 (x, y, z: Real): Real;
type
RealArrayPtr = ^RealArray;
RealArray = array[0..2] of Real;
var
bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11: Integer;
rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, e, t, u, v: Real;
q: RealArrayPtr;
i, j: Integer;
begin
t := x + N;
bx0 := (Round (Int (t)) mod NoiseSizeX) and BM;
bx1 := ((bx0 + 1) mod NoiseSizeX) and BM;
rx0 := t - (Int (t));
rx1 := rx0 - 1.0;
t := y + N;
by0 := (Round (Int (t)) mod NoiseSizeY) and BM;
by1 := ((by0 + 1) mod NoiseSizeY) and BM;
ry0 := t - (Int (t));
ry1 := ry0 - 1.0;
t := z + N;
bz0 := (Round (Int (t)) mod NoiseSizeZ) and BM;
bz1 := ((bz0 + 1) mod NoiseSizeZ) and BM;
rz0 := t - (Int (t));
rz1 := rz0 - 1.0;
i := p[bx0];
j := p[bx1];
b00 := p[i + by0];
b10 := p[j + by0];
b01 := p[i + by1];
b11 := p[j + by1];
t := s_curve (rx0);
sy := s_curve (ry0);
sz := s_curve (rz0);
q := @g3[b00 + bz0];
u := rx0 * q^[0] + ry0 * q^[1] + rz0 * q^[2];
q := @g3[b10 + bz0];
v := rx1 * q^[0] + ry0 * q^[1] + rz0 * q^[2];
a := lerp (t, u, v);
q := @g3[b01 + bz0];
u := rx0 * q^[0] + ry1 * q^[1] + rz0 * q^[2];
q := @g3[b11 + bz0];
v := rx1 * q^[0] + ry1 * q^[1] + rz0 * q^[2];
b := lerp (t, u, v);
c := lerp(sy, a, b);
q := @g3[b00 + bz1];
u := rx0 * q^[0] + ry0 * q^[1] + rz1 * q^[2];
q := @g3[b10 + bz1];
v := rx1 * q^[0] + ry0 * q^[1] + rz1 * q^[2];
a := lerp (t, u, v);
q := @g3[b01 + bz1];
u := rx0 * q^[0] + ry1 * q^[1] + rz1 * q^[2];
q := @g3[b11 + bz1];
v := rx1 * q^[0] + ry1 * q^[1] + rz1 * q^[2];
b := lerp (t, u, v);
d := lerp(sy, a, b);
e := 0.5 + lerp (sz, c, d);
if e < 0 then
e := 0;
if e > 0.99999999 then
e := 0.99999999;
Noise3 := e;
end;
procedure Normalize2 (v: array of Real);
var
s: Real;
begin
s := Sqrt (v[0] * v[0] + v[1] * v[1]);
v[0] := v[0] / s;
v[1] := v[1] / s;
end;
var
i, j, k: Integer;
begin
NoiseSizeX := DEFAULT_NOISE_SIZE;
NoiseSizeY := DEFAULT_NOISE_SIZE;
NoiseSizeZ := DEFAULT_NOISE_SIZE;
for i := 0 to B - 1 do
begin
p[i] := i;
g1[i] := Integer (Random (B + B) - B) / B;
for j := 0 to 1 do
g2[i, j] := Integer (Random (B + B) - B) / B;
Normalize2 (g2[i]);
for j := 0 to 2 do
g3[i, j] := Integer (Random (B + B) - B) / B;
{ normalize3 (g3[i]; }
end;
for i := B - 1 downto 0 do
begin
k := p[i];
j := Integer (Random (B));
p[i] := p[j];
p[j] := k;
end;
for i := 0 to B + 1 do
begin
p[B + i] := p[i];
g1[B + i] := g1[i];
for j := 0 to 1 do
g2[B + i, j] := g2[i, j];
for j := 0 to 2 do
g3[B + i, j] := g3[i, j];
end;
end.