Realtime Fractal Renderer Documentation
simplexnoise1234.cl
Go to the documentation of this file.
1/* SimplexNoise1234, Simplex noise with true analytic
2 * derivative in 1D to 4D.
3 *
4 * Author: Stefan Gustavson, 2003-2005
5 * Contact: stefan.gustavson@liu.se
6 *
7 * This code was GPL licensed until February 2011.
8 * As the original author of this code, I hereby
9 * release it into the public domain.
10 * Please feel free to use it for whatever you want.
11 * Credit is appreciated where appropriate, and I also
12 * appreciate being told where this code finds any use,
13 * but you may do as you like.
14 */
15
16 /*
17 * This implementation is "Simplex Noise" as presented by
18 * Ken Perlin at a relatively obscure and not often cited course
19 * session "Real-Time Shading" at Siggraph 2001 (before real
20 * time shading actually took off), under the title "hardware noise".
21 * The 3D function is numerically equivalent to his Java reference
22 * code available in the PDF course notes, although I re-implemented
23 * it from scratch to get more readable code. The 1D, 2D and 4D cases
24 * were implemented from scratch by me from Ken Perlin's text.
25 *
26 * This file has no dependencies on any other file, not even its own
27 * header file. The header file is made for use by external code only.
28 */
29
30float snoise1(float x);
31float snoise2(float x, float y);
32float snoise3(float x, float y, float z);
33float snoise4(float x, float y, float z, float w);
34
35#define FASTFLOOR(x) (((int)(x) <= (x)) ? ((int)x) : (((int)x) - 1))
36
37//---------------------------------------------------------------------
38// Static data
39
40/*
41 * Permutation table. This is just a random jumble of all numbers 0-255,
42 * repeated twice to avoid wrapping the index at 255 for each lookup.
43 * This needs to be exactly the same for all instances on all platforms,
44 * so it's easiest to just keep it as static explicit data.
45 * This also removes the need for any initialisation of this class.
46 *
47 * Note that making this an int[] instead of a char[] might make the
48 * code run faster on platforms with a high penalty for unaligned single
49 * byte addressing. Intel x86 is generally single-byte-friendly, but
50 * some other CPUs are faster with 4-aligned reads.
51 * However, a char[] is smaller, which avoids cache trashing, and that
52 * is probably the most important aspect on most architectures.
53 * This array is accessed a *lot* by the noise functions.
54 * A vector-valued noise over 3D accesses it 96 times, and a
55 * float-valued 4D noise 64 times. We want this to fit in the cache!
56 */
57__constant unsigned char perm[512] = { 151, 160, 137, 91, 90, 15,
58 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
59 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
60 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
61 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
62 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
63 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
64 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
65 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
66 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
67 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
68 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
69 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
70 151, 160, 137, 91, 90, 15,
71 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
72 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
73 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
74 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
75 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
76 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
77 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
78 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
79 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
80 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
81 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
82 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180 };
83
84//---------------------------------------------------------------------
85
86/*
87 * Helper functions to compute gradients-dot-residualvectors (1D to 4D)
88 * Note that these generate gradients of more than unit length. To make
89 * a close match with the value range of classic Perlin noise, the final
90 * noise values need to be rescaled to fit nicely within [-1,1].
91 * (The simplex noise functions as such also have different scaling.)
92 * Note also that these noise functions are the most practical and useful
93 * signed version of Perlin noise. To return values according to the
94 * RenderMan specification from the SL noise() and pnoise() functions,
95 * the noise values need to be scaled and offset to [0,1], like this:
96 * float SLnoise = (noise(x,y,z) + 1.0) * 0.5;
97 */
98
99float grad1(int hash, float x)
100{
101 int h = hash & 15;
102 float grad = 1.0f + (h & 7); // Gradient value 1.0, 2.0, ..., 8.0
103 if (h & 8)
104 grad = -grad; // Set a random sign for the gradient
105 return (grad * x); // Multiply the gradient with the distance
106}
107
108float grad2(int hash, float x, float y)
109{
110 int h = hash & 7; // Convert low 3 bits of hash code
111 float u = h < 4 ? x : y; // into 8 simple gradient directions,
112 float v = h < 4 ? y : x; // and compute the dot product with (x,y).
113 return ((h & 1) ? -u : u) + ((h & 2) ? -2.0f * v : 2.0f * v);
114}
115
116float grad3(int hash, float x, float y, float z)
117{
118 int h = hash & 15; // Convert low 4 bits of hash code into 12 simple
119 float u = h < 8 ? x : y; // gradient directions, and compute dot product.
120 float v = h < 4 ? y : h == 12 || h == 14 ? x
121 : z; // Fix repeats at h = 12 to 15
122 return ((h & 1) ? -u : u) + ((h & 2) ? -v : v);
123}
124
125float grad4(int hash, float x, float y, float z, float t)
126{
127 int h = hash & 31; // Convert low 5 bits of hash code into 32 simple
128 float u = h < 24 ? x : y; // gradient directions, and compute dot product.
129 float v = h < 16 ? y : z;
130 float w = h < 8 ? z : t;
131 return ((h & 1) ? -u : u) + ((h & 2) ? -v : v) + ((h & 4) ? -w : w);
132}
133
134// A lookup table to traverse the simplex around a given point in 4D.
135// Details can be found where this table is used, in the 4D noise method.
136/* TODO: This should not be required, backport it from Bill's GLSL code! */
137__constant unsigned char simplex[64][4] = {
138 {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0}, {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0}, {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0} };
139
140// 1D simplex noise
141float snoise1(float x)
142{
143
144 int i0 = FASTFLOOR(x);
145 int i1 = i0 + 1;
146 float x0 = x - i0;
147 float x1 = x0 - 1.0f;
148
149 float n0, n1;
150
151 float t0 = 1.0f - x0 * x0;
152 // if(t0 < 0.0f) t0 = 0.0f; // this never happens for the 1D case
153 t0 *= t0;
154 n0 = t0 * t0 * grad1(perm[i0 & 0xff], x0);
155
156 float t1 = 1.0f - x1 * x1;
157 // if(t1 < 0.0f) t1 = 0.0f; // this never happens for the 1D case
158 t1 *= t1;
159 n1 = t1 * t1 * grad1(perm[i1 & 0xff], x1);
160 // The maximum value of this noise is 8*(3/4)^4 = 2.53125
161 // A factor of 0.395 would scale to fit exactly within [-1,1], but
162 // we want to match PRMan's 1D noise, so we scale it down some more.
163 return 0.25f * (n0 + n1);
164}
165
166// 2D simplex noise
167float snoise2(float x, float y)
168{
169
170#define F2 0.366025403f // F2 = 0.5*(sqrt(3.0)-1.0)
171#define G2 0.211324865f // G2 = (3.0-Math.sqrt(3.0))/6.0
172
173 float n0, n1, n2; // Noise contributions from the three corners
174
175 // Skew the input space to determine which simplex cell we're in
176 float s = (x + y) * F2; // Hairy factor for 2D
177 float xs = x + s;
178 float ys = y + s;
179 int i = FASTFLOOR(xs);
180 int j = FASTFLOOR(ys);
181
182 float t = (float)(i + j) * G2;
183 float X0 = i - t; // Unskew the cell origin back to (x,y) space
184 float Y0 = j - t;
185 float x0 = x - X0; // The x,y distances from the cell origin
186 float y0 = y - Y0;
187
188 // For the 2D case, the simplex shape is an equilateral triangle.
189 // Determine which simplex we are in.
190 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
191 if (x0 > y0)
192 {
193 i1 = 1;
194 j1 = 0;
195 } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
196 else
197 {
198 i1 = 0;
199 j1 = 1;
200 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
201
202 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
203 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
204 // c = (3-sqrt(3))/6
205
206 float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
207 float y1 = y0 - j1 + G2;
208 float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
209 float y2 = y0 - 1.0f + 2.0f * G2;
210
211 // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
212 int ii = i & 0xff;
213 int jj = j & 0xff;
214
215 // Calculate the contribution from the three corners
216 float t0 = 0.5f - x0 * x0 - y0 * y0;
217 if (t0 < 0.0f)
218 n0 = 0.0f;
219 else
220 {
221 t0 *= t0;
222 n0 = t0 * t0 * grad2(perm[ii + perm[jj]], x0, y0);
223 }
224
225 float t1 = 0.5f - x1 * x1 - y1 * y1;
226 if (t1 < 0.0f)
227 n1 = 0.0f;
228 else
229 {
230 t1 *= t1;
231 n1 = t1 * t1 * grad2(perm[ii + i1 + perm[jj + j1]], x1, y1);
232 }
233
234 float t2 = 0.5f - x2 * x2 - y2 * y2;
235 if (t2 < 0.0f)
236 n2 = 0.0f;
237 else
238 {
239 t2 *= t2;
240 n2 = t2 * t2 * grad2(perm[ii + 1 + perm[jj + 1]], x2, y2);
241 }
242
243 // Add contributions from each corner to get the final noise value.
244 // The result is scaled to return values in the interval [-1,1].
245 return 40.0f * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
246}
247
248// 3D simplex noise
249float snoise3(float x, float y, float z)
250{
251
252 // Simple skewing factors for the 3D case
253#define F3 0.333333333f
254#define G3 0.166666667f
255
256 float n0, n1, n2, n3; // Noise contributions from the four corners
257
258 // Skew the input space to determine which simplex cell we're in
259 float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D
260 float xs = x + s;
261 float ys = y + s;
262 float zs = z + s;
263 int i = FASTFLOOR(xs);
264 int j = FASTFLOOR(ys);
265 int k = FASTFLOOR(zs);
266
267 float t = (float)(i + j + k) * G3;
268 float X0 = i - t; // Unskew the cell origin back to (x,y,z) space
269 float Y0 = j - t;
270 float Z0 = k - t;
271 float x0 = x - X0; // The x,y,z distances from the cell origin
272 float y0 = y - Y0;
273 float z0 = z - Z0;
274
275 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
276 // Determine which simplex we are in.
277 int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
278 int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
279
280 /* This code would benefit from a backport from the GLSL version! */
281 if (x0 >= y0)
282 {
283 if (y0 >= z0)
284 {
285 i1 = 1;
286 j1 = 0;
287 k1 = 0;
288 i2 = 1;
289 j2 = 1;
290 k2 = 0;
291 } // X Y Z order
292 else if (x0 >= z0)
293 {
294 i1 = 1;
295 j1 = 0;
296 k1 = 0;
297 i2 = 1;
298 j2 = 0;
299 k2 = 1;
300 } // X Z Y order
301 else
302 {
303 i1 = 0;
304 j1 = 0;
305 k1 = 1;
306 i2 = 1;
307 j2 = 0;
308 k2 = 1;
309 } // Z X Y order
310 }
311 else
312 { // x0<y0
313 if (y0 < z0)
314 {
315 i1 = 0;
316 j1 = 0;
317 k1 = 1;
318 i2 = 0;
319 j2 = 1;
320 k2 = 1;
321 } // Z Y X order
322 else if (x0 < z0)
323 {
324 i1 = 0;
325 j1 = 1;
326 k1 = 0;
327 i2 = 0;
328 j2 = 1;
329 k2 = 1;
330 } // Y Z X order
331 else
332 {
333 i1 = 0;
334 j1 = 1;
335 k1 = 0;
336 i2 = 1;
337 j2 = 1;
338 k2 = 0;
339 } // Y X Z order
340 }
341
342 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
343 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
344 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
345 // c = 1/6.
346
347 float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
348 float y1 = y0 - j1 + G3;
349 float z1 = z0 - k1 + G3;
350 float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x,y,z) coords
351 float y2 = y0 - j2 + 2.0f * G3;
352 float z2 = z0 - k2 + 2.0f * G3;
353 float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x,y,z) coords
354 float y3 = y0 - 1.0f + 3.0f * G3;
355 float z3 = z0 - 1.0f + 3.0f * G3;
356
357 // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
358 int ii = i & 0xff;
359 int jj = j & 0xff;
360 int kk = k & 0xff;
361
362 // Calculate the contribution from the four corners
363 float t0 = 0.5f - x0 * x0 - y0 * y0 - z0 * z0;
364 if (t0 < 0.0f)
365 n0 = 0.0f;
366 else
367 {
368 t0 *= t0;
369 n0 = t0 * t0 * grad3(perm[ii + perm[jj + perm[kk]]], x0, y0, z0);
370 }
371
372 float t1 = 0.5f - x1 * x1 - y1 * y1 - z1 * z1;
373 if (t1 < 0.0f)
374 n1 = 0.0f;
375 else
376 {
377 t1 *= t1;
378 n1 = t1 * t1 * grad3(perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], x1, y1, z1);
379 }
380
381 float t2 = 0.5f - x2 * x2 - y2 * y2 - z2 * z2;
382 if (t2 < 0.0f)
383 n2 = 0.0f;
384 else
385 {
386 t2 *= t2;
387 n2 = t2 * t2 * grad3(perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], x2, y2, z2);
388 }
389
390 float t3 = 0.5f - x3 * x3 - y3 * y3 - z3 * z3;
391 if (t3 < 0.0f)
392 n3 = 0.0f;
393 else
394 {
395 t3 *= t3;
396 n3 = t3 * t3 * grad3(perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], x3, y3, z3);
397 }
398
399 // Add contributions from each corner to get the final noise value.
400 // The result is scaled to stay just inside [-1,1]
401 return 72.0f * (n0 + n1 + n2 + n3);
402}
403
404// 4D simplex noise
405float snoise4(float x, float y, float z, float w)
406{
407
408 // The skewing and unskewing factors are hairy again for the 4D case
409#define F4 0.309016994f // F4 = (Math.sqrt(5.0)-1.0)/4.0
410#define G4 0.138196601f // G4 = (5.0-Math.sqrt(5.0))/20.0
411
412 float n0, n1, n2, n3, n4; // Noise contributions from the five corners
413
414 // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
415 float s = (x + y + z + w) * F4; // Factor for 4D skewing
416 float xs = x + s;
417 float ys = y + s;
418 float zs = z + s;
419 float ws = w + s;
420 int i = FASTFLOOR(xs);
421 int j = FASTFLOOR(ys);
422 int k = FASTFLOOR(zs);
423 int l = FASTFLOOR(ws);
424
425 float t = (i + j + k + l) * G4; // Factor for 4D unskewing
426 float X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
427 float Y0 = j - t;
428 float Z0 = k - t;
429 float W0 = l - t;
430
431 float x0 = x - X0; // The x,y,z,w distances from the cell origin
432 float y0 = y - Y0;
433 float z0 = z - Z0;
434 float w0 = w - W0;
435
436 // For the 4D case, the simplex is a 4D shape I won't even try to describe.
437 // To find out which of the 24 possible simplices we're in, we need to
438 // determine the magnitude ordering of x0, y0, z0 and w0.
439 // The method below is a good way of finding the ordering of x,y,z,w and
440 // then find the correct traversal order for the simplex we’re in.
441 // First, six pair-wise comparisons are performed between each possible pair
442 // of the four coordinates, and the results are used to add up binary bits
443 // for an integer index.
444 int c1 = (x0 > y0) ? 32 : 0;
445 int c2 = (x0 > z0) ? 16 : 0;
446 int c3 = (y0 > z0) ? 8 : 0;
447 int c4 = (x0 > w0) ? 4 : 0;
448 int c5 = (y0 > w0) ? 2 : 0;
449 int c6 = (z0 > w0) ? 1 : 0;
450 int c = c1 + c2 + c3 + c4 + c5 + c6;
451
452 int i1, j1, k1, l1; // The integer offsets for the second simplex corner
453 int i2, j2, k2, l2; // The integer offsets for the third simplex corner
454 int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
455
456 // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
457 // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
458 // impossible. Only the 24 indices which have non-zero entries make any sense.
459 // We use a thresholding to set the coordinates in turn from the largest magnitude.
460 // The number 3 in the "simplex" array is at the position of the largest coordinate.
461 i1 = simplex[c][0] >= 3 ? 1 : 0;
462 j1 = simplex[c][1] >= 3 ? 1 : 0;
463 k1 = simplex[c][2] >= 3 ? 1 : 0;
464 l1 = simplex[c][3] >= 3 ? 1 : 0;
465 // The number 2 in the "simplex" array is at the second largest coordinate.
466 i2 = simplex[c][0] >= 2 ? 1 : 0;
467 j2 = simplex[c][1] >= 2 ? 1 : 0;
468 k2 = simplex[c][2] >= 2 ? 1 : 0;
469 l2 = simplex[c][3] >= 2 ? 1 : 0;
470 // The number 1 in the "simplex" array is at the second smallest coordinate.
471 i3 = simplex[c][0] >= 1 ? 1 : 0;
472 j3 = simplex[c][1] >= 1 ? 1 : 0;
473 k3 = simplex[c][2] >= 1 ? 1 : 0;
474 l3 = simplex[c][3] >= 1 ? 1 : 0;
475 // The fifth corner has all coordinate offsets = 1, so no need to look that up.
476
477 float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
478 float y1 = y0 - j1 + G4;
479 float z1 = z0 - k1 + G4;
480 float w1 = w0 - l1 + G4;
481 float x2 = x0 - i2 + 2.0f * G4; // Offsets for third corner in (x,y,z,w) coords
482 float y2 = y0 - j2 + 2.0f * G4;
483 float z2 = z0 - k2 + 2.0f * G4;
484 float w2 = w0 - l2 + 2.0f * G4;
485 float x3 = x0 - i3 + 3.0f * G4; // Offsets for fourth corner in (x,y,z,w) coords
486 float y3 = y0 - j3 + 3.0f * G4;
487 float z3 = z0 - k3 + 3.0f * G4;
488 float w3 = w0 - l3 + 3.0f * G4;
489 float x4 = x0 - 1.0f + 4.0f * G4; // Offsets for last corner in (x,y,z,w) coords
490 float y4 = y0 - 1.0f + 4.0f * G4;
491 float z4 = z0 - 1.0f + 4.0f * G4;
492 float w4 = w0 - 1.0f + 4.0f * G4;
493
494 // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
495 int ii = i & 0xff;
496 int jj = j & 0xff;
497 int kk = k & 0xff;
498 int ll = l & 0xff;
499
500 // Calculate the contribution from the five corners
501 float t0 = 0.5f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
502 if (t0 < 0.0f)
503 n0 = 0.0f;
504 else
505 {
506 t0 *= t0;
507 n0 = t0 * t0 * grad4(perm[ii + perm[jj + perm[kk + perm[ll]]]], x0, y0, z0, w0);
508 }
509
510 float t1 = 0.5f - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
511 if (t1 < 0.0f)
512 n1 = 0.0f;
513 else
514 {
515 t1 *= t1;
516 n1 = t1 * t1 * grad4(perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]], x1, y1, z1, w1);
517 }
518
519 float t2 = 0.5f - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
520 if (t2 < 0.0f)
521 n2 = 0.0f;
522 else
523 {
524 t2 *= t2;
525 n2 = t2 * t2 * grad4(perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]], x2, y2, z2, w2);
526 }
527
528 float t3 = 0.5f - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
529 if (t3 < 0.0f)
530 n3 = 0.0f;
531 else
532 {
533 t3 *= t3;
534 n3 = t3 * t3 * grad4(perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]], x3, y3, z3, w3);
535 }
536
537 float t4 = 0.5f - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
538 if (t4 < 0.0f)
539 n4 = 0.0f;
540 else
541 {
542 t4 *= t4;
543 n4 = t4 * t4 * grad4(perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]], x4, y4, z4, w4);
544 }
545
546 // Sum up and scale the result to cover the range [-1,1]
547 return 62.0f * (n0 + n1 + n2 + n3 + n4);
548}
549//---------------------------------------------------------------------
__constant unsigned char simplex[64][4]
#define F2
float snoise2(float x, float y)
float grad2(int hash, float x, float y)
float snoise1(float x)
#define G4
float snoise4(float x, float y, float z, float w)
#define F3
#define F4
#define G3
float grad3(int hash, float x, float y, float z)
__constant unsigned char perm[512]
float snoise3(float x, float y, float z)
float grad4(int hash, float x, float y, float z, float t)
#define FASTFLOOR(x)
#define G2
float grad1(int hash, float x)