Skip to content

Commit 92e95a2

Browse files
committed
2d cellular noise
1 parent 6d925db commit 92e95a2

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

Assets/Demoscene/Projects/2018-07-12-Lava/Lava.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ float _Power;
5353

5454
inline float DistanceFunction(float3 pos)
5555
{
56-
float2 c = cellular(float3(pos.xz, _Time.y));
56+
float2 c = cellular(float2(pos.xz));
5757
float h = c.y - c.x;
5858
h = pow(h, _Power);
5959

60-
float2 c2 = cellular(float3(3.0 * pos.xz, 0.0));
60+
float2 c2 = cellular(float2(3.0 * pos.xz));
6161
h += 0.5 * pow((c2.y - c2.x), _Power);
6262

6363
h += 0.001 * snoise(50.0 * pos.xz);

Assets/Demoscene/Shaders/Includes/Noise.cginc

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,62 @@ float snoise(vec2 v) {
8080
}
8181

8282

83+
// https://thebookofshaders.com/edit.php#12/2d-cnoise.frag
84+
85+
// Permutation polynomial: (34x^2 + x) mod 289
86+
// vec3 permute(vec3 x) {
87+
// return mod((34.0 * x + 1.0) * x, 289.0);
88+
// }
89+
90+
// Cellular noise, returning F1 and F2 in a vec2.
91+
// Standard 3x3 search window for good F1 and F2 values
92+
vec2 cellular(vec2 P) {
93+
#define K 0.142857142857 // 1/7
94+
#define Ko 0.428571428571 // 3/7
95+
#define jitter 1.0 // Less gives more regular pattern
96+
vec2 Pi = mod(floor(P), 289.0);
97+
vec2 Pf = fract(P);
98+
vec3 oi = vec3(-1.0, 0.0, 1.0);
99+
vec3 of = vec3(-0.5, 0.5, 1.5);
100+
vec3 px = permute(Pi.x + oi);
101+
vec3 p = permute(px.x + Pi.y + oi); // p11, p12, p13
102+
vec3 ox = fract(p*K) - Ko;
103+
vec3 oy = mod(floor(p*K),7.0)*K - Ko;
104+
vec3 dx = Pf.x + 0.5 + jitter*ox;
105+
vec3 dy = Pf.y - of + jitter*oy;
106+
vec3 d1 = dx * dx + dy * dy; // d11, d12 and d13, squared
107+
p = permute(px.y + Pi.y + oi); // p21, p22, p23
108+
ox = fract(p*K) - Ko;
109+
oy = mod(floor(p*K),7.0)*K - Ko;
110+
dx = Pf.x - 0.5 + jitter*ox;
111+
dy = Pf.y - of + jitter*oy;
112+
vec3 d2 = dx * dx + dy * dy; // d21, d22 and d23, squared
113+
p = permute(px.z + Pi.y + oi); // p31, p32, p33
114+
ox = fract(p*K) - Ko;
115+
oy = mod(floor(p*K),7.0)*K - Ko;
116+
dx = Pf.x - 1.5 + jitter*ox;
117+
dy = Pf.y - of + jitter*oy;
118+
vec3 d3 = dx * dx + dy * dy; // d31, d32 and d33, squared
119+
// Sort out the two smallest distances (F1, F2)
120+
vec3 d1a = min(d1, d2);
121+
d2 = max(d1, d2); // Swap to keep candidates for F2
122+
d2 = min(d2, d3); // neither F1 nor F2 are now in d3
123+
d1 = min(d1a, d2); // F1 is now in d1
124+
d2 = max(d1a, d2); // Swap to keep candidates for F2
125+
d1.xy = (d1.x < d1.y) ? d1.xy : d1.yx; // Swap if smaller
126+
d1.xz = (d1.x < d1.z) ? d1.xz : d1.zx; // F1 is in d1.x
127+
d1.yz = min(d1.yz, d2.yz); // F2 is now not in d2.yz
128+
d1.y = min(d1.y, d1.z); // nor in d1.z
129+
d1.y = min(d1.y, d2.x); // F2 is in d1.y, we're done.
130+
return sqrt(d1.xy);
131+
}
132+
133+
83134
// 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-
//}
135+
136+
// vec3 permute(vec3 x) {
137+
// return mod((34.0 * x + 1.0) * x, 289.0);
138+
// }
87139

88140
// Cellular noise, returning F1 and F2 in a vec2.
89141
// 3x3x3 search region for good F2 everywhere, but a lot

0 commit comments

Comments
 (0)