Skip to content

Commit fd13238

Browse files
Add noiseGLSL.glsl with MIT-licensed 2D noise for p5.strands
1 parent 0cd9448 commit fd13238

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
float baseNoise(vec2 st) {
2-
return fract(sin(dot(st.xy ,vec2(12.9898,78.233))) * 43758.5453123);
1+
// Based on https://github.com/patriciogonzalezvivo/lygia/blob/main/generative/noise.glsl (MIT)
2+
// Adapted for use in p5.strands
3+
4+
vec2 random2(vec2 st) {
5+
st = vec2(dot(st, vec2(127.1, 311.7)),
6+
dot(st, vec2(269.5, 183.3)));
7+
return -1.0 + 2.0 * fract(sin(st) * 43758.5453123);
38
}
49

5-
float noise(vec2 st) {
6-
float result = 0.0;
7-
for (int i = 0; i < 3; i++) {
8-
float freq = pow(2.0, float(i));
9-
float amp = pow(0.5, float(i));
10-
result += amp * baseNoise(st * freq);
11-
}
12-
return result;
10+
float baseNoise(vec2 st) {
11+
vec2 i = floor(st);
12+
vec2 f = fract(st);
13+
14+
// Four corners in 2D of a tile
15+
vec2 a = random2(i);
16+
vec2 b = random2(i + vec2(1.0, 0.0));
17+
vec2 c = random2(i + vec2(0.0, 1.0));
18+
vec2 d = random2(i + vec2(1.0, 1.0));
19+
20+
// Smooth interpolation
21+
vec2 u = f * f * (3.0 - 2.0 * f);
22+
23+
// Mix the results
24+
return mix(mix(dot(a, f - vec2(0.0, 0.0)),
25+
dot(b, f - vec2(1.0, 0.0)), u.x),
26+
mix(dot(c, f - vec2(0.0, 1.0)),
27+
dot(d, f - vec2(1.0, 1.0)), u.x), u.y);
1328
}
29+

0 commit comments

Comments
 (0)