Skip to content

Commit 738fc3f

Browse files
Create noise3DGLSL.glsl to add 3D noise(vec3) support to p5.strands
1 parent d6310a6 commit 738fc3f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Based on https://thebookofshaders.com/13/ and https://github.com/patriciogonzalezvivo/lygia
2+
// MIT licensed, adapted for p5.strands
3+
4+
vec3 random3(vec3 p) {
5+
return fract(sin(vec3(
6+
dot(p, vec3(127.1, 311.7, 74.7)),
7+
dot(p, vec3(269.5, 183.3, 246.1)),
8+
dot(p, vec3(113.5, 271.9, 124.6))
9+
)) * 43758.5453123);
10+
}
11+
12+
float baseNoise(vec3 p) {
13+
vec3 i = floor(p);
14+
vec3 f = fract(p);
15+
16+
// Compute corner vectors
17+
vec3 a = random3(i);
18+
vec3 b = random3(i + vec3(1, 0, 0));
19+
vec3 c = random3(i + vec3(0, 1, 0));
20+
vec3 d = random3(i + vec3(1, 1, 0));
21+
vec3 e = random3(i + vec3(0, 0, 1));
22+
vec3 f1 = random3(i + vec3(1, 0, 1));
23+
vec3 g = random3(i + vec3(0, 1, 1));
24+
vec3 h = random3(i + vec3(1, 1, 1));
25+
26+
vec3 u = f * f * (3.0 - 2.0 * f);
27+
28+
return mix(
29+
mix(
30+
mix(dot(a, f - vec3(0, 0, 0)), dot(b, f - vec3(1, 0, 0)), u.x),
31+
mix(dot(c, f - vec3(0, 1, 0)), dot(d, f - vec3(1, 1, 0)), u.x), u.y
32+
),
33+
mix(
34+
mix(dot(e, f - vec3(0, 0, 1)), dot(f1, f - vec3(1, 0, 1)), u.x),
35+
mix(dot(g, f - vec3(0, 1, 1)), dot(h, f - vec3(1, 1, 1)), u.x), u.y
36+
),
37+
u.z
38+
);
39+
}
40+
41+
float noise(vec3 p) {
42+
float result = 0.0;
43+
float amplitude = 1.0;
44+
float frequency = 1.0;
45+
46+
for (int i = 0; i < 4; i++) {
47+
result += amplitude * baseNoise(p * frequency);
48+
frequency *= 2.0;
49+
amplitude *= 0.5;
50+
}
51+
return result;
52+
}

0 commit comments

Comments
 (0)