|
3 | 3 |
|
4 | 4 | #include "PortingFromGLSL.cginc"
|
5 | 5 |
|
6 |
| -// https://thebookofshaders.com/edit.php#12/3d-cnoise.frag |
7 |
| -vec3 permute(vec3 x) { |
8 |
| - return mod((34.0 * x + 1.0) * x, 289.0); |
| 6 | +// https://thebookofshaders.com/edit.php#11/2d-snoise-clear.frag |
| 7 | + |
| 8 | +// Some useful functions |
| 9 | +vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } |
| 10 | +vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } |
| 11 | +vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); } |
| 12 | + |
| 13 | +// |
| 14 | +// Description : GLSL 2D simplex noise function |
| 15 | +// Author : Ian McEwan, Ashima Arts |
| 16 | +// Maintainer : ijm |
| 17 | +// Lastmod : 20110822 (ijm) |
| 18 | +// License : |
| 19 | +// Copyright (C) 2011 Ashima Arts. All rights reserved. |
| 20 | +// Distributed under the MIT License. See LICENSE file. |
| 21 | +// https://github.com/ashima/webgl-noise |
| 22 | +// |
| 23 | +float snoise(vec2 v) { |
| 24 | + |
| 25 | + // Precompute values for skewed triangular grid |
| 26 | + const vec4 C = vec4(0.211324865405187, |
| 27 | + // (3.0-sqrt(3.0))/6.0 |
| 28 | + 0.366025403784439, |
| 29 | + // 0.5*(sqrt(3.0)-1.0) |
| 30 | + -0.577350269189626, |
| 31 | + // -1.0 + 2.0 * C.x |
| 32 | + 0.024390243902439); |
| 33 | + // 1.0 / 41.0 |
| 34 | + |
| 35 | + // First corner (x0) |
| 36 | + vec2 i = floor(v + dot(v, C.yy)); |
| 37 | + vec2 x0 = v - i + dot(i, C.xx); |
| 38 | + |
| 39 | + // Other two corners (x1, x2) |
| 40 | + vec2 i1 = vec2(0.0, 0.0); |
| 41 | + i1 = (x0.x > x0.y)? vec2(1.0, 0.0):vec2(0.0, 1.0); |
| 42 | + vec2 x1 = x0.xy + C.xx - i1; |
| 43 | + vec2 x2 = x0.xy + C.zz; |
| 44 | + |
| 45 | + // Do some permutations to avoid |
| 46 | + // truncation effects in permutation |
| 47 | + i = mod289(i); |
| 48 | + vec3 p = permute( |
| 49 | + permute( i.y + vec3(0.0, i1.y, 1.0)) |
| 50 | + + i.x + vec3(0.0, i1.x, 1.0 )); |
| 51 | + |
| 52 | + vec3 m = max(0.5 - vec3( |
| 53 | + dot(x0,x0), |
| 54 | + dot(x1,x1), |
| 55 | + dot(x2,x2) |
| 56 | + ), 0.0); |
| 57 | + |
| 58 | + m = m*m ; |
| 59 | + m = m*m ; |
| 60 | + |
| 61 | + // Gradients: |
| 62 | + // 41 pts uniformly over a line, mapped onto a diamond |
| 63 | + // The ring size 17*17 = 289 is close to a multiple |
| 64 | + // of 41 (41*7 = 287) |
| 65 | + |
| 66 | + vec3 x = 2.0 * fract(p * C.www) - 1.0; |
| 67 | + vec3 h = abs(x) - 0.5; |
| 68 | + vec3 ox = floor(x + 0.5); |
| 69 | + vec3 a0 = x - ox; |
| 70 | + |
| 71 | + // Normalise gradients implicitly by scaling m |
| 72 | + // Approximation of: m *= inversesqrt(a0*a0 + h*h); |
| 73 | + m *= 1.79284291400159 - 0.85373472095314 * (a0*a0+h*h); |
| 74 | + |
| 75 | + // Compute final noise value at P |
| 76 | + vec3 g = vec3(0.0, 0.0, 0.0); |
| 77 | + g.x = a0.x * x0.x + h.x * x0.y; |
| 78 | + g.yz = a0.yz * vec2(x1.x,x2.x) + h.yz * vec2(x1.y,x2.y); |
| 79 | + return 130.0 * dot(m, g); |
9 | 80 | }
|
10 | 81 |
|
| 82 | + |
| 83 | +// https://thebookofshaders.com/edit.php#12/3d-cnoise.frag |
| 84 | +//vec3 permute(vec3 x) { |
| 85 | +// return mod((34.0 * x + 1.0) * x, 289.0); |
| 86 | +//} |
| 87 | + |
11 | 88 | // Cellular noise, returning F1 and F2 in a vec2.
|
12 | 89 | // 3x3x3 search region for good F2 everywhere, but a lot
|
13 | 90 | // slower than the 2x2x2 version.
|
|
0 commit comments