-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgradientNoise.glsl
166 lines (141 loc) · 6.47 KB
/
gradientNoise.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 2D Gradient noise.
// @param scale Number of tiles, must be integer for tileable results, range: [2, inf]
// @param seed Seed to randomize result, range: [0, inf], default: 0.0
// @return Value of the noise, range: [-1, 1]
float gradientNoise(vec2 pos, vec2 scale, float seed)
{
pos *= scale;
vec4 i = floor(pos).xyxy + vec2(0.0, 1.0).xxyy;
vec4 f = (pos.xyxy - i.xyxy) - vec2(0.0, 1.0).xxyy;
i = mod(i, scale.xyxy) + seed;
vec4 hashX, hashY;
smultiHash2D(i, hashX, hashY);
vec4 gradients = hashX * f.xzxz + hashY * f.yyww;
vec2 u = noiseInterpolate(f.xy);
vec2 g = mix(gradients.xz, gradients.yw, u.x);
return 1.4142135623730950 * mix(g.x, g.y, u.y);
}
// 2D Gradient noise with gradients transform (i.e. can be used to rotate the gradients).
// @param scale Number of tiles, must be integer for tileable results, range: [2, inf]
// @param transform Transform matrix for the noise gradients.
// @param seed Seed to randomize result, range: [0, inf], default: 0.0
// @return Value of the noise, range: [-1, 1]
float gradientNoise(vec2 pos, vec2 scale, mat2 transform, float seed)
{
pos *= scale;
vec4 i = floor(pos).xyxy + vec2(0.0, 1.0).xxyy;
vec4 f = (pos.xyxy - i.xyxy) - vec2(0.0, 1.0).xxyy;
i = mod(i, scale.xyxy) + seed;
vec4 hashX, hashY;
smultiHash2D(i, hashX, hashY);
// transform gradients
vec4 m = vec4(transform);
vec4 rh = vec4(hashX.x, hashY.x, hashX.y, hashY.y);
rh = rh.xxzz * m.xyxy + rh.yyww * m.zwzw;
hashX.xy = rh.xz;
hashY.xy = rh.yw;
rh = vec4(hashX.z, hashY.z, hashX.w, hashY.w);
rh = rh.xxzz * m.xyxy + rh.yyww * m.zwzw;
hashX.zw = rh.xz;
hashY.zw = rh.yw;
vec4 gradients = hashX * f.xzxz + hashY * f.yyww;
vec2 u = noiseInterpolate(f.xy);
vec2 g = mix(gradients.xz, gradients.yw, u.x);
return 1.4142135623730950 * mix(g.x, g.y, u.y);
}
float gradientNoise(vec2 pos, vec2 scale, float rotation, float seed)
{
vec2 sinCos = vec2(sin(rotation), cos(rotation));
return gradientNoise(pos, scale, mat2(sinCos.y, sinCos.x, sinCos.x, sinCos.y), seed);
}
// 2D Gradient noise with derivatives.
// @param scale Number of tiles, must be integer for tileable results, range: [2, inf]
// @param seed Seed to randomize result, range: [0, inf], default: 0.0
// @return x = value of the noise, yz = derivative of the noise, range: [-1, 1]
vec3 gradientNoised(vec2 pos, vec2 scale, float seed)
{
// gradient noise with derivatives based on Inigo Quilez
pos *= scale;
vec4 i = floor(pos).xyxy + vec2(0.0, 1.0).xxyy;
vec4 f = (pos.xyxy - i.xyxy) - vec2(0.0, 1.0).xxyy;
i = mod(i, scale.xyxy) + seed;
vec4 hashX, hashY;
smultiHash2D(i, hashX, hashY);
vec2 a = vec2(hashX.x, hashY.x);
vec2 b = vec2(hashX.y, hashY.y);
vec2 c = vec2(hashX.z, hashY.z);
vec2 d = vec2(hashX.w, hashY.w);
vec4 gradients = hashX * f.xzxz + hashY * f.yyww;
vec4 udu = noiseInterpolateDu(f.xy);
vec2 u = udu.xy;
vec2 g = mix(gradients.xz, gradients.yw, u.x);
vec2 dxdy = a + u.x * (b - a) + u.y * (c - a) + u.x * u.y * (a - b - c + d);
dxdy += udu.zw * (u.yx * (gradients.x - gradients.y - gradients.z + gradients.w) + gradients.yz - gradients.x);
return vec3(mix(g.x, g.y, u.y) * 1.4142135623730950, dxdy);
}
// 2D Gradient noise with gradients transform (i.e. can be used to rotate the gradients) and derivatives.
// @param scale Number of tiles, must be integer for tileable results, range: [2, inf]
// @param transform Transform matrix for the noise gradients.
// @param seed Seed to randomize result, range: [0, inf], default: 0.0
// @return x = value of the noise, yz = derivative of the noise, range: [-1, 1]
vec3 gradientNoised(vec2 pos, vec2 scale, mat2 transform, float seed)
{
// gradient noise with derivatives based on Inigo Quilez
pos *= scale;
vec4 i = floor(pos).xyxy + vec2(0.0, 1.0).xxyy;
vec4 f = (pos.xyxy - i.xyxy) - vec2(0.0, 1.0).xxyy;
i = mod(i, scale.xyxy) + seed;
vec4 hashX, hashY;
smultiHash2D(i, hashX, hashY);
// transform gradients
vec4 m = vec4(transform);
vec4 rh = vec4(hashX.x, hashY.x, hashX.y, hashY.y);
rh = rh.xxzz * m.xyxy + rh.yyww * m.zwzw;
hashX.xy = rh.xz;
hashY.xy = rh.yw;
rh = vec4(hashX.z, hashY.z, hashX.w, hashY.w);
rh = rh.xxzz * m.xyxy + rh.yyww * m.zwzw;
hashX.zw = rh.xz;
hashY.zw = rh.yw;
vec2 a = vec2(hashX.x, hashY.x);
vec2 b = vec2(hashX.y, hashY.y);
vec2 c = vec2(hashX.z, hashY.z);
vec2 d = vec2(hashX.w, hashY.w);
vec4 gradients = hashX * f.xzxz + hashY * f.yyww;
vec4 udu = noiseInterpolateDu(f.xy);
vec2 u = udu.xy;
vec2 g = mix(gradients.xz, gradients.yw, u.x);
vec2 dxdy = a + u.x * (b - a) + u.y * (c - a) + u.x * u.y * (a - b - c + d);
dxdy += udu.zw * (u.yx * (gradients.x - gradients.y - gradients.z + gradients.w) + gradients.yz - gradients.x);
return vec3(mix(g.x, g.y, u.y) * 1.4142135623730950, dxdy);
}
// 2D Gradient noise with gradients rotation and derivatives.
// @param scale Number of tiles, must be integer for tileable results, range: [2, inf]
// @param rotation Rotation for the noise gradients, useful to animate flow, range: [0, PI]
// @param seed Seed to randomize result, range: [0, inf], default: 0.0
// @return x = value of the noise, yz = derivative of the noise, range: [-1, 1]
vec3 gradientNoised(vec2 pos, vec2 scale, float rotation, float seed)
{
vec2 sinCos = vec2(sin(rotation), cos(rotation));
return gradientNoised(pos, scale, mat2(sinCos.y, sinCos.x, sinCos.x, sinCos.y), seed);
}
// Variant of 2D Gradient noise with disorder/jitter for the gradients.
// @param scale Number of tiles, must be integer for tileable results, range: [2, inf]
// @param disoder Jitter factor for the noise gradients,, range: [0, 1.0]
// @param seed Seed to randomize result, range: [0, inf], default: 0.0
// @return x = value of the noise, yz = derivative of the noise, range: [-1, 1]
float gradientNoiseDisorder(vec2 pos, vec2 scale, float disoder, float seed)
{
pos *= scale;
vec4 i = floor(pos).xyxy + vec2(0.0, 1.0).xxyy;
vec4 f = (pos.xyxy - i.xyxy) - vec2(0.0, 1.0).xxyy;
i = mod(i, scale.xyxy) + seed;
vec4 hashX, hashY;
multiHash2D(i, hashX, hashY);
hashX = (hashX * disoder) * 2.0 - 1.0;
hashY = (hashY * disoder) * 2.0 - 1.0;
vec4 gradients = hashX * f.xzxz + hashY * f.yyww;
vec2 u = noiseInterpolate(f.xy);
vec2 g = mix(gradients.xz, gradients.yw, u.x);
return 1.4142135623730950 * mix(g.x, g.y, u.y);
}