perlin 0.1.0pre1-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/LICENSE +21 -0
- data/README.md +33 -0
- data/Rakefile +85 -0
- data/examples/chunk_in_2d.rb +22 -0
- data/examples/chunk_in_3d.rb +24 -0
- data/examples/index_in_2d.rb +23 -0
- data/examples/index_in_3d.rb +25 -0
- data/examples/ogl/README.md +3 -0
- data/examples/ogl/run.rb +200 -0
- data/ext/perlin/classic.c +160 -0
- data/ext/perlin/classic.h +24 -0
- data/ext/perlin/extconf.rb +15 -0
- data/ext/perlin/generator.c +292 -0
- data/ext/perlin/generator.h +35 -0
- data/ext/perlin/perlin.c +27 -0
- data/ext/perlin/perlin.h +20 -0
- data/ext/perlin/simplex.c +475 -0
- data/ext/perlin/simplex.h +196 -0
- data/lib/perlin.rb +13 -0
- data/lib/perlin/1.9/perlin.so +0 -0
- data/lib/perlin/generator.rb +161 -0
- data/lib/perlin/version.rb +5 -0
- metadata +127 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
/*
|
2
|
+
* class Perlin::Generator
|
3
|
+
*/
|
4
|
+
|
5
|
+
#ifndef GENERATOR_H
|
6
|
+
#define GENERATOR_H
|
7
|
+
|
8
|
+
#include <ruby.h>
|
9
|
+
|
10
|
+
#include "classic.h"
|
11
|
+
#include "simplex.h"
|
12
|
+
|
13
|
+
extern long seed;
|
14
|
+
|
15
|
+
// Arbitrary number used to add an extra "seed" dimension for Simplex noise.
|
16
|
+
// Seed 2D noise by offsetting the 3rd dimension.
|
17
|
+
// Seed 3D noise by seeding with the 4th dimension.
|
18
|
+
#define SEED_OFFSET -12354.1123f
|
19
|
+
|
20
|
+
VALUE Perlin_Generator_set_seed(const VALUE self, const VALUE seed);
|
21
|
+
VALUE Perlin_Generator_set_persistence(const VALUE self, const VALUE persistence);
|
22
|
+
VALUE Perlin_Generator_set_octave(const VALUE self, const VALUE octave);
|
23
|
+
VALUE Perlin_Generator_set_classic(const VALUE self, const VALUE classic);
|
24
|
+
|
25
|
+
VALUE Perlin_Generator_run(const int argc, const VALUE *argv, const VALUE self);
|
26
|
+
VALUE Perlin_Generator_run2d(const VALUE self, const VALUE x, const VALUE y);
|
27
|
+
VALUE Perlin_Generator_run3d(const VALUE self, const VALUE x, const VALUE y, const VALUE z);
|
28
|
+
|
29
|
+
VALUE Perlin_Generator_chunk(const int argc, const VALUE *argv, const VALUE self);
|
30
|
+
VALUE Perlin_Generator_chunk2d(const VALUE self, const VALUE x, const VALUE y, const VALUE steps_x, const VALUE steps_y, VALUE interval);
|
31
|
+
VALUE Perlin_Generator_chunk3d(const VALUE self, const VALUE x, const VALUE y, const VALUE z, const VALUE steps_x, const VALUE steps_y, const VALUE steps_z, const VALUE interval);
|
32
|
+
|
33
|
+
VALUE Perlin_Generator_init(const VALUE self, const VALUE seed, const VALUE persistence, const VALUE octave, const VALUE classic);
|
34
|
+
|
35
|
+
#endif // GENERATOR_H
|
data/ext/perlin/perlin.c
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
/*
|
2
|
+
Ruby module that is built according to the Perlin Noise function
|
3
|
+
located at http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include "perlin.h"
|
7
|
+
|
8
|
+
void Init_perlin() {
|
9
|
+
VALUE jm_Module = rb_define_module("Perlin");
|
10
|
+
VALUE rb_cPerlin = rb_define_class_under(jm_Module, "Generator", rb_cObject);
|
11
|
+
|
12
|
+
rb_define_method(rb_cPerlin, "initialize_", Perlin_Generator_init, 4);
|
13
|
+
|
14
|
+
rb_define_method(rb_cPerlin, "seed=", Perlin_Generator_set_seed, 1);
|
15
|
+
rb_define_method(rb_cPerlin, "persistence=", Perlin_Generator_set_persistence, 1);
|
16
|
+
rb_define_method(rb_cPerlin, "octave=", Perlin_Generator_set_octave, 1);
|
17
|
+
rb_define_method(rb_cPerlin, "classic=", Perlin_Generator_set_classic, 1);
|
18
|
+
|
19
|
+
rb_define_method(rb_cPerlin, "[]", Perlin_Generator_run, -1);
|
20
|
+
rb_define_method(rb_cPerlin, "run2d", Perlin_Generator_run2d, 2);
|
21
|
+
rb_define_method(rb_cPerlin, "run3d", Perlin_Generator_run3d, 3);
|
22
|
+
|
23
|
+
rb_define_method(rb_cPerlin, "chunk", Perlin_Generator_chunk, -1);
|
24
|
+
rb_define_method(rb_cPerlin, "chunk2d", Perlin_Generator_chunk2d, 5);
|
25
|
+
rb_define_method(rb_cPerlin, "chunk3d", Perlin_Generator_chunk3d, 7);
|
26
|
+
}
|
27
|
+
|
data/ext/perlin/perlin.h
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
/*
|
2
|
+
* module Perlin
|
3
|
+
*
|
4
|
+
* Ruby module that is built according to the Perlin Noise function
|
5
|
+
* located at http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
|
6
|
+
*
|
7
|
+
*/
|
8
|
+
|
9
|
+
|
10
|
+
#ifndef PERLIN_H
|
11
|
+
#define PERLIN_H
|
12
|
+
|
13
|
+
#include <ruby.h>
|
14
|
+
|
15
|
+
VALUE rb_cPerlin;
|
16
|
+
|
17
|
+
#include "generator.h"
|
18
|
+
|
19
|
+
#endif // PERLIN_H
|
20
|
+
|
@@ -0,0 +1,475 @@
|
|
1
|
+
/* Copyright (c) 2007-2012 Eliot Eshelman
|
2
|
+
*
|
3
|
+
* This program is free software: you can redistribute it and/or modify
|
4
|
+
* it under the terms of the GNU General Public License as published by
|
5
|
+
* the Free Software Foundation, either version 3 of the License, or
|
6
|
+
* (at your option) any later version.
|
7
|
+
*
|
8
|
+
* This program is distributed in the hope that it will be useful,
|
9
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
* GNU General Public License for more details.
|
12
|
+
*
|
13
|
+
* You should have received a copy of the GNU General Public License
|
14
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
15
|
+
*
|
16
|
+
*/
|
17
|
+
|
18
|
+
|
19
|
+
#include <math.h>
|
20
|
+
|
21
|
+
#include "simplex.h"
|
22
|
+
|
23
|
+
|
24
|
+
/* 2D, 3D and 4D Simplex Noise functions return 'random' values in (-1, 1).
|
25
|
+
|
26
|
+
This algorithm was originally designed by Ken Perlin, but my code has been
|
27
|
+
adapted from the implementation written by Stefan Gustavson (stegu@itn.liu.se)
|
28
|
+
|
29
|
+
Raw Simplex noise functions return the value generated by Ken's algorithm.
|
30
|
+
|
31
|
+
Scaled Raw Simplex noise functions adjust the range of values returned from the
|
32
|
+
traditional (-1, 1) to whichever bounds are passed to the function.
|
33
|
+
|
34
|
+
Multi-Octave Simplex noise functions compine multiple noise values to create a
|
35
|
+
more complex result. Each successive layer of noise is adjusted and scaled.
|
36
|
+
|
37
|
+
Scaled Multi-Octave Simplex noise functions scale the values returned from the
|
38
|
+
traditional (-1,1) range to whichever range is passed to the function.
|
39
|
+
|
40
|
+
In many cases, you may think you only need a 1D noise function, but in practice
|
41
|
+
2D is almost always better. For instance, if you're using the current frame
|
42
|
+
number as the parameter for the noise, all objects will end up with the same
|
43
|
+
noise value at each frame. By adding a second parameter on the second
|
44
|
+
dimension, you can ensure that each gets a unique noise value and they don't
|
45
|
+
all look identical.
|
46
|
+
*/
|
47
|
+
|
48
|
+
|
49
|
+
// 2D Multi-octave Simplex noise.
|
50
|
+
//
|
51
|
+
// For each octave, a higher frequency/lower amplitude function will be added to the original.
|
52
|
+
// The higher the persistence [0-1], the more of each succeeding octave will be added.
|
53
|
+
float octave_noise_2d( const float octaves, const float persistence, const float scale, const float x, const float y ) {
|
54
|
+
float total = 0;
|
55
|
+
float frequency = scale;
|
56
|
+
float amplitude = 1;
|
57
|
+
|
58
|
+
// We have to keep track of the largest possible amplitude,
|
59
|
+
// because each octave adds more, and we need a value in [-1, 1].
|
60
|
+
float maxAmplitude = 0;
|
61
|
+
|
62
|
+
for( int i=0; i < octaves; i++ ) {
|
63
|
+
total += raw_noise_2d( x * frequency, y * frequency ) * amplitude;
|
64
|
+
|
65
|
+
frequency *= 2;
|
66
|
+
maxAmplitude += amplitude;
|
67
|
+
amplitude *= persistence;
|
68
|
+
}
|
69
|
+
|
70
|
+
return total / maxAmplitude;
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
// 3D Multi-octave Simplex noise.
|
75
|
+
//
|
76
|
+
// For each octave, a higher frequency/lower amplitude function will be added to the original.
|
77
|
+
// The higher the persistence [0-1], the more of each succeeding octave will be added.
|
78
|
+
float octave_noise_3d( const float octaves, const float persistence, const float scale, const float x, const float y, const float z ) {
|
79
|
+
float total = 0;
|
80
|
+
float frequency = scale;
|
81
|
+
float amplitude = 1;
|
82
|
+
|
83
|
+
// We have to keep track of the largest possible amplitude,
|
84
|
+
// because each octave adds more, and we need a value in [-1, 1].
|
85
|
+
float maxAmplitude = 0;
|
86
|
+
|
87
|
+
for( int i=0; i < octaves; i++ ) {
|
88
|
+
total += raw_noise_3d( x * frequency, y * frequency, z * frequency ) * amplitude;
|
89
|
+
|
90
|
+
frequency *= 2;
|
91
|
+
maxAmplitude += amplitude;
|
92
|
+
amplitude *= persistence;
|
93
|
+
}
|
94
|
+
|
95
|
+
return total / maxAmplitude;
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
// 4D Multi-octave Simplex noise.
|
100
|
+
//
|
101
|
+
// For each octave, a higher frequency/lower amplitude function will be added to the original.
|
102
|
+
// The higher the persistence [0-1], the more of each succeeding octave will be added.
|
103
|
+
float octave_noise_4d( const float octaves, const float persistence, const float scale, const float x, const float y, const float z, const float w ) {
|
104
|
+
float total = 0;
|
105
|
+
float frequency = scale;
|
106
|
+
float amplitude = 1;
|
107
|
+
|
108
|
+
// We have to keep track of the largest possible amplitude,
|
109
|
+
// because each octave adds more, and we need a value in [-1, 1].
|
110
|
+
float maxAmplitude = 0;
|
111
|
+
|
112
|
+
for( int i=0; i < octaves; i++ ) {
|
113
|
+
total += raw_noise_4d( x * frequency, y * frequency, z * frequency, w * frequency ) * amplitude;
|
114
|
+
|
115
|
+
frequency *= 2;
|
116
|
+
maxAmplitude += amplitude;
|
117
|
+
amplitude *= persistence;
|
118
|
+
}
|
119
|
+
|
120
|
+
return total / maxAmplitude;
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
// 2D Scaled Multi-octave Simplex noise.
|
126
|
+
//
|
127
|
+
// Returned value will be between loBound and hiBound.
|
128
|
+
float scaled_octave_noise_2d( const float octaves, const float persistence, const float scale, const float loBound, const float hiBound, const float x, const float y ) {
|
129
|
+
return octave_noise_2d(octaves, persistence, scale, x, y) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
|
130
|
+
}
|
131
|
+
|
132
|
+
|
133
|
+
// 3D Scaled Multi-octave Simplex noise.
|
134
|
+
//
|
135
|
+
// Returned value will be between loBound and hiBound.
|
136
|
+
float scaled_octave_noise_3d( const float octaves, const float persistence, const float scale, const float loBound, const float hiBound, const float x, const float y, const float z ) {
|
137
|
+
return octave_noise_3d(octaves, persistence, scale, x, y, z) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
|
138
|
+
}
|
139
|
+
|
140
|
+
// 4D Scaled Multi-octave Simplex noise.
|
141
|
+
//
|
142
|
+
// Returned value will be between loBound and hiBound.
|
143
|
+
float scaled_octave_noise_4d( const float octaves, const float persistence, const float scale, const float loBound, const float hiBound, const float x, const float y, const float z, const float w ) {
|
144
|
+
return octave_noise_4d(octaves, persistence, scale, x, y, z, w) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
// 2D Scaled Simplex raw noise.
|
150
|
+
//
|
151
|
+
// Returned value will be between loBound and hiBound.
|
152
|
+
float scaled_raw_noise_2d( const float loBound, const float hiBound, const float x, const float y ) {
|
153
|
+
return raw_noise_2d(x, y) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
|
154
|
+
}
|
155
|
+
|
156
|
+
|
157
|
+
// 3D Scaled Simplex raw noise.
|
158
|
+
//
|
159
|
+
// Returned value will be between loBound and hiBound.
|
160
|
+
float scaled_raw_noise_3d( const float loBound, const float hiBound, const float x, const float y, const float z ) {
|
161
|
+
return raw_noise_3d(x, y, z) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
|
162
|
+
}
|
163
|
+
|
164
|
+
// 4D Scaled Simplex raw noise.
|
165
|
+
//
|
166
|
+
// Returned value will be between loBound and hiBound.
|
167
|
+
float scaled_raw_noise_4d( const float loBound, const float hiBound, const float x, const float y, const float z, const float w ) {
|
168
|
+
return raw_noise_4d(x, y, z, w) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2;
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
// 2D raw Simplex noise
|
174
|
+
float raw_noise_2d( const float x, const float y ) {
|
175
|
+
// Noise contributions from the three corners
|
176
|
+
float n0, n1, n2;
|
177
|
+
|
178
|
+
// Skew the input space to determine which simplex cell we're in
|
179
|
+
float F2 = 0.5 * (sqrtf(3.0) - 1.0);
|
180
|
+
// Hairy factor for 2D
|
181
|
+
float s = (x + y) * F2;
|
182
|
+
int i = fastfloor( x + s );
|
183
|
+
int j = fastfloor( y + s );
|
184
|
+
|
185
|
+
float G2 = (3.0 - sqrtf(3.0)) / 6.0;
|
186
|
+
float t = (i + j) * G2;
|
187
|
+
// Unskew the cell origin back to (x,y) space
|
188
|
+
float X0 = i-t;
|
189
|
+
float Y0 = j-t;
|
190
|
+
// The x,y distances from the cell origin
|
191
|
+
float x0 = x-X0;
|
192
|
+
float y0 = y-Y0;
|
193
|
+
|
194
|
+
// For the 2D case, the simplex shape is an equilateral triangle.
|
195
|
+
// Determine which simplex we are in.
|
196
|
+
int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
|
197
|
+
if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
|
198
|
+
else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1)
|
199
|
+
|
200
|
+
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
|
201
|
+
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
|
202
|
+
// c = (3-sqrt(3))/6
|
203
|
+
float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
|
204
|
+
float y1 = y0 - j1 + G2;
|
205
|
+
float x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
|
206
|
+
float y2 = y0 - 1.0 + 2.0 * G2;
|
207
|
+
|
208
|
+
// Work out the hashed gradient indices of the three simplex corners
|
209
|
+
int ii = i & 255;
|
210
|
+
int jj = j & 255;
|
211
|
+
int gi0 = perm[ii+perm[jj]] % 12;
|
212
|
+
int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
|
213
|
+
int gi2 = perm[ii+1+perm[jj+1]] % 12;
|
214
|
+
|
215
|
+
// Calculate the contribution from the three corners
|
216
|
+
float t0 = 0.5 - x0*x0-y0*y0;
|
217
|
+
if(t0<0) n0 = 0.0;
|
218
|
+
else {
|
219
|
+
t0 *= t0;
|
220
|
+
n0 = t0 * t0 * dot2(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
|
221
|
+
}
|
222
|
+
|
223
|
+
float t1 = 0.5 - x1*x1-y1*y1;
|
224
|
+
if(t1<0) n1 = 0.0;
|
225
|
+
else {
|
226
|
+
t1 *= t1;
|
227
|
+
n1 = t1 * t1 * dot2(grad3[gi1], x1, y1);
|
228
|
+
}
|
229
|
+
|
230
|
+
float t2 = 0.5 - x2*x2-y2*y2;
|
231
|
+
if(t2<0) n2 = 0.0;
|
232
|
+
else {
|
233
|
+
t2 *= t2;
|
234
|
+
n2 = t2 * t2 * dot2(grad3[gi2], x2, y2);
|
235
|
+
}
|
236
|
+
|
237
|
+
// Add contributions from each corner to get the final noise value.
|
238
|
+
// The result is scaled to return values in the interval [-1,1].
|
239
|
+
return 70.0 * (n0 + n1 + n2);
|
240
|
+
}
|
241
|
+
|
242
|
+
|
243
|
+
// 3D raw Simplex noise
|
244
|
+
float raw_noise_3d( const float x, const float y, const float z ) {
|
245
|
+
float n0, n1, n2, n3; // Noise contributions from the four corners
|
246
|
+
|
247
|
+
// Skew the input space to determine which simplex cell we're in
|
248
|
+
float F3 = 1.0/3.0;
|
249
|
+
float s = (x+y+z)*F3; // Very nice and simple skew factor for 3D
|
250
|
+
int i = fastfloor(x+s);
|
251
|
+
int j = fastfloor(y+s);
|
252
|
+
int k = fastfloor(z+s);
|
253
|
+
|
254
|
+
float G3 = 1.0/6.0; // Very nice and simple unskew factor, too
|
255
|
+
float t = (i+j+k)*G3;
|
256
|
+
float X0 = i-t; // Unskew the cell origin back to (x,y,z) space
|
257
|
+
float Y0 = j-t;
|
258
|
+
float Z0 = k-t;
|
259
|
+
float x0 = x-X0; // The x,y,z distances from the cell origin
|
260
|
+
float y0 = y-Y0;
|
261
|
+
float z0 = z-Z0;
|
262
|
+
|
263
|
+
// For the 3D case, the simplex shape is a slightly irregular tetrahedron.
|
264
|
+
// Determine which simplex we are in.
|
265
|
+
int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
|
266
|
+
int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
|
267
|
+
|
268
|
+
if(x0>=y0) {
|
269
|
+
if(y0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order
|
270
|
+
else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order
|
271
|
+
else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order
|
272
|
+
}
|
273
|
+
else { // x0<y0
|
274
|
+
if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; } // Z Y X order
|
275
|
+
else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; } // Y Z X order
|
276
|
+
else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; } // Y X Z order
|
277
|
+
}
|
278
|
+
|
279
|
+
// A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
|
280
|
+
// a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
|
281
|
+
// a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
|
282
|
+
// c = 1/6.
|
283
|
+
float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
|
284
|
+
float y1 = y0 - j1 + G3;
|
285
|
+
float z1 = z0 - k1 + G3;
|
286
|
+
float x2 = x0 - i2 + 2.0*G3; // Offsets for third corner in (x,y,z) coords
|
287
|
+
float y2 = y0 - j2 + 2.0*G3;
|
288
|
+
float z2 = z0 - k2 + 2.0*G3;
|
289
|
+
float x3 = x0 - 1.0 + 3.0*G3; // Offsets for last corner in (x,y,z) coords
|
290
|
+
float y3 = y0 - 1.0 + 3.0*G3;
|
291
|
+
float z3 = z0 - 1.0 + 3.0*G3;
|
292
|
+
|
293
|
+
// Work out the hashed gradient indices of the four simplex corners
|
294
|
+
int ii = i & 255;
|
295
|
+
int jj = j & 255;
|
296
|
+
int kk = k & 255;
|
297
|
+
int gi0 = perm[ii+perm[jj+perm[kk]]] % 12;
|
298
|
+
int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1]]] % 12;
|
299
|
+
int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2]]] % 12;
|
300
|
+
int gi3 = perm[ii+1+perm[jj+1+perm[kk+1]]] % 12;
|
301
|
+
|
302
|
+
// Calculate the contribution from the four corners
|
303
|
+
float t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
|
304
|
+
if(t0<0) n0 = 0.0;
|
305
|
+
else {
|
306
|
+
t0 *= t0;
|
307
|
+
n0 = t0 * t0 * dot3(grad3[gi0], x0, y0, z0);
|
308
|
+
}
|
309
|
+
|
310
|
+
float t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
|
311
|
+
if(t1<0) n1 = 0.0;
|
312
|
+
else {
|
313
|
+
t1 *= t1;
|
314
|
+
n1 = t1 * t1 * dot3(grad3[gi1], x1, y1, z1);
|
315
|
+
}
|
316
|
+
|
317
|
+
float t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
|
318
|
+
if(t2<0) n2 = 0.0;
|
319
|
+
else {
|
320
|
+
t2 *= t2;
|
321
|
+
n2 = t2 * t2 * dot3(grad3[gi2], x2, y2, z2);
|
322
|
+
}
|
323
|
+
|
324
|
+
float t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
|
325
|
+
if(t3<0) n3 = 0.0;
|
326
|
+
else {
|
327
|
+
t3 *= t3;
|
328
|
+
n3 = t3 * t3 * dot3(grad3[gi3], x3, y3, z3);
|
329
|
+
}
|
330
|
+
|
331
|
+
// Add contributions from each corner to get the final noise value.
|
332
|
+
// The result is scaled to stay just inside [-1,1]
|
333
|
+
return 32.0*(n0 + n1 + n2 + n3);
|
334
|
+
}
|
335
|
+
|
336
|
+
|
337
|
+
// 4D raw Simplex noise
|
338
|
+
float raw_noise_4d( const float x, const float y, const float z, const float w ) {
|
339
|
+
// The skewing and unskewing factors are hairy again for the 4D case
|
340
|
+
float F4 = (sqrtf(5.0)-1.0)/4.0;
|
341
|
+
float G4 = (5.0-sqrtf(5.0))/20.0;
|
342
|
+
float n0, n1, n2, n3, n4; // Noise contributions from the five corners
|
343
|
+
|
344
|
+
// Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
|
345
|
+
float s = (x + y + z + w) * F4; // Factor for 4D skewing
|
346
|
+
int i = fastfloor(x + s);
|
347
|
+
int j = fastfloor(y + s);
|
348
|
+
int k = fastfloor(z + s);
|
349
|
+
int l = fastfloor(w + s);
|
350
|
+
float t = (i + j + k + l) * G4; // Factor for 4D unskewing
|
351
|
+
float X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
|
352
|
+
float Y0 = j - t;
|
353
|
+
float Z0 = k - t;
|
354
|
+
float W0 = l - t;
|
355
|
+
|
356
|
+
float x0 = x - X0; // The x,y,z,w distances from the cell origin
|
357
|
+
float y0 = y - Y0;
|
358
|
+
float z0 = z - Z0;
|
359
|
+
float w0 = w - W0;
|
360
|
+
|
361
|
+
// For the 4D case, the simplex is a 4D shape I won't even try to describe.
|
362
|
+
// To find out which of the 24 possible simplices we're in, we need to
|
363
|
+
// determine the magnitude ordering of x0, y0, z0 and w0.
|
364
|
+
// The method below is a good way of finding the ordering of x,y,z,w and
|
365
|
+
// then find the correct traversal order for the simplex we're in.
|
366
|
+
// First, six pair-wise comparisons are performed between each possible pair
|
367
|
+
// of the four coordinates, and the results are used to add up binary bits
|
368
|
+
// for an integer index.
|
369
|
+
int c1 = (x0 > y0) ? 32 : 0;
|
370
|
+
int c2 = (x0 > z0) ? 16 : 0;
|
371
|
+
int c3 = (y0 > z0) ? 8 : 0;
|
372
|
+
int c4 = (x0 > w0) ? 4 : 0;
|
373
|
+
int c5 = (y0 > w0) ? 2 : 0;
|
374
|
+
int c6 = (z0 > w0) ? 1 : 0;
|
375
|
+
int c = c1 + c2 + c3 + c4 + c5 + c6;
|
376
|
+
|
377
|
+
int i1, j1, k1, l1; // The integer offsets for the second simplex corner
|
378
|
+
int i2, j2, k2, l2; // The integer offsets for the third simplex corner
|
379
|
+
int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
|
380
|
+
|
381
|
+
// simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
|
382
|
+
// Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
|
383
|
+
// impossible. Only the 24 indices which have non-zero entries make any sense.
|
384
|
+
// We use a thresholding to set the coordinates in turn from the largest magnitude.
|
385
|
+
// The number 3 in the "simplex" array is at the position of the largest coordinate.
|
386
|
+
i1 = simplex[c][0]>=3 ? 1 : 0;
|
387
|
+
j1 = simplex[c][1]>=3 ? 1 : 0;
|
388
|
+
k1 = simplex[c][2]>=3 ? 1 : 0;
|
389
|
+
l1 = simplex[c][3]>=3 ? 1 : 0;
|
390
|
+
// The number 2 in the "simplex" array is at the second largest coordinate.
|
391
|
+
i2 = simplex[c][0]>=2 ? 1 : 0;
|
392
|
+
j2 = simplex[c][1]>=2 ? 1 : 0;
|
393
|
+
k2 = simplex[c][2]>=2 ? 1 : 0;
|
394
|
+
l2 = simplex[c][3]>=2 ? 1 : 0;
|
395
|
+
// The number 1 in the "simplex" array is at the second smallest coordinate.
|
396
|
+
i3 = simplex[c][0]>=1 ? 1 : 0;
|
397
|
+
j3 = simplex[c][1]>=1 ? 1 : 0;
|
398
|
+
k3 = simplex[c][2]>=1 ? 1 : 0;
|
399
|
+
l3 = simplex[c][3]>=1 ? 1 : 0;
|
400
|
+
// The fifth corner has all coordinate offsets = 1, so no need to look that up.
|
401
|
+
|
402
|
+
float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
|
403
|
+
float y1 = y0 - j1 + G4;
|
404
|
+
float z1 = z0 - k1 + G4;
|
405
|
+
float w1 = w0 - l1 + G4;
|
406
|
+
float x2 = x0 - i2 + 2.0*G4; // Offsets for third corner in (x,y,z,w) coords
|
407
|
+
float y2 = y0 - j2 + 2.0*G4;
|
408
|
+
float z2 = z0 - k2 + 2.0*G4;
|
409
|
+
float w2 = w0 - l2 + 2.0*G4;
|
410
|
+
float x3 = x0 - i3 + 3.0*G4; // Offsets for fourth corner in (x,y,z,w) coords
|
411
|
+
float y3 = y0 - j3 + 3.0*G4;
|
412
|
+
float z3 = z0 - k3 + 3.0*G4;
|
413
|
+
float w3 = w0 - l3 + 3.0*G4;
|
414
|
+
float x4 = x0 - 1.0 + 4.0*G4; // Offsets for last corner in (x,y,z,w) coords
|
415
|
+
float y4 = y0 - 1.0 + 4.0*G4;
|
416
|
+
float z4 = z0 - 1.0 + 4.0*G4;
|
417
|
+
float w4 = w0 - 1.0 + 4.0*G4;
|
418
|
+
|
419
|
+
// Work out the hashed gradient indices of the five simplex corners
|
420
|
+
int ii = i & 255;
|
421
|
+
int jj = j & 255;
|
422
|
+
int kk = k & 255;
|
423
|
+
int ll = l & 255;
|
424
|
+
int gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32;
|
425
|
+
int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32;
|
426
|
+
int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32;
|
427
|
+
int gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32;
|
428
|
+
int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32;
|
429
|
+
|
430
|
+
// Calculate the contribution from the five corners
|
431
|
+
float t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;
|
432
|
+
if(t0<0) n0 = 0.0;
|
433
|
+
else {
|
434
|
+
t0 *= t0;
|
435
|
+
n0 = t0 * t0 * dot4(grad4[gi0], x0, y0, z0, w0);
|
436
|
+
}
|
437
|
+
|
438
|
+
float t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;
|
439
|
+
if(t1<0) n1 = 0.0;
|
440
|
+
else {
|
441
|
+
t1 *= t1;
|
442
|
+
n1 = t1 * t1 * dot4(grad4[gi1], x1, y1, z1, w1);
|
443
|
+
}
|
444
|
+
|
445
|
+
float t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;
|
446
|
+
if(t2<0) n2 = 0.0;
|
447
|
+
else {
|
448
|
+
t2 *= t2;
|
449
|
+
n2 = t2 * t2 * dot4(grad4[gi2], x2, y2, z2, w2);
|
450
|
+
}
|
451
|
+
|
452
|
+
float t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;
|
453
|
+
if(t3<0) n3 = 0.0;
|
454
|
+
else {
|
455
|
+
t3 *= t3;
|
456
|
+
n3 = t3 * t3 * dot4(grad4[gi3], x3, y3, z3, w3);
|
457
|
+
}
|
458
|
+
|
459
|
+
float t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;
|
460
|
+
if(t4<0) n4 = 0.0;
|
461
|
+
else {
|
462
|
+
t4 *= t4;
|
463
|
+
n4 = t4 * t4 * dot4(grad4[gi4], x4, y4, z4, w4);
|
464
|
+
}
|
465
|
+
|
466
|
+
// Sum up and scale the result to cover the range [-1,1]
|
467
|
+
return 27.0 * (n0 + n1 + n2 + n3 + n4);
|
468
|
+
}
|
469
|
+
|
470
|
+
|
471
|
+
int fastfloor( const float x ) { return x > 0 ? (int) x : (int) x - 1; }
|
472
|
+
|
473
|
+
float dot2( const int* g, const float x, const float y ) { return g[0]*x + g[1]*y; }
|
474
|
+
float dot3( const int* g, const float x, const float y, const float z ) { return g[0]*x + g[1]*y + g[2]*z; }
|
475
|
+
float dot4( const int* g, const float x, const float y, const float z, const float w ) { return g[0]*x + g[1]*y + g[2]*z + g[3]*w; }
|