onejs-sl 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +96 -7
- package/goldens/goldens.json +480 -0
- package/lib/common.hlsl +155 -0
- package/lib/noise2d.hlsl +167 -0
- package/lib/sdf2d.hlsl +510 -0
- package/package.json +9 -3
- package/src/body.ts +243 -0
- package/src/core.ts +1 -1
- package/src/emit/hlsl-body.ts +7 -0
- package/src/encode.ts +3 -24
- package/src/hlsl.ts +34 -143
- package/src/ir.ts +49 -2
- package/src/lang/lower.ts +6 -3
- package/src/lib/glsl.ts +899 -0
- package/src/lib/hlsl.ts +891 -0
- package/src/lib/index.ts +47 -0
- package/src/lib/table.ts +125 -0
- package/src/lib/types.ts +23 -0
- package/src/lib/wgsl.ts +994 -0
- package/src/serial.ts +3 -1
- package/src/shapes.ts +4 -4
- package/src/sl.ts +27 -1
- package/src/web.ts +53 -28
- package/src/weblib.ts +0 -1303
package/src/lib/glsl.ts
ADDED
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
// GENERATED by lib/generate.ts from lib/*.hlsl. Do not edit: change the
|
|
2
|
+
// .hlsl and run `npm run lib`. src/lib/lib.test.ts fails when this is stale.
|
|
3
|
+
|
|
4
|
+
/** GLSL ES 3.00, by `LIB_FUNCTIONS` index. */
|
|
5
|
+
export const LIB_GLSL: ReadonlyArray<string> = [
|
|
6
|
+
`
|
|
7
|
+
float glslMod(float x, float y) {
|
|
8
|
+
return x - y * floor(x / y);
|
|
9
|
+
}
|
|
10
|
+
`,
|
|
11
|
+
`
|
|
12
|
+
float ndot(vec2 a, vec2 b) {
|
|
13
|
+
return a.x * b.x - a.y * b.y;
|
|
14
|
+
}
|
|
15
|
+
`,
|
|
16
|
+
`
|
|
17
|
+
float dot2(vec2 v) {
|
|
18
|
+
return dot(v, v);
|
|
19
|
+
}
|
|
20
|
+
`,
|
|
21
|
+
`
|
|
22
|
+
float sdCircle(vec2 p, float r) {
|
|
23
|
+
return length(p) - r;
|
|
24
|
+
}
|
|
25
|
+
`,
|
|
26
|
+
`
|
|
27
|
+
float sdRoundedBox(vec2 p, vec2 b, vec4 r) {
|
|
28
|
+
r.xy = (p.x > 0.0) ? r.xy : r.zw;
|
|
29
|
+
r.x = (p.y > 0.0) ? r.x : r.y;
|
|
30
|
+
vec2 q = abs(p) - b + r.x;
|
|
31
|
+
return min(max(q.x, q.y), 0.0) + length(max(q, vec2(0.0))) - r.x;
|
|
32
|
+
}
|
|
33
|
+
`,
|
|
34
|
+
`
|
|
35
|
+
float sdBox(vec2 p, vec2 b) {
|
|
36
|
+
vec2 d = abs(p) - b;
|
|
37
|
+
return length(max(d, vec2(0.0))) + min(max(d.x, d.y), 0.0);
|
|
38
|
+
}
|
|
39
|
+
`,
|
|
40
|
+
`
|
|
41
|
+
float sdOrientedBox(vec2 p, vec2 a, vec2 b, float th) {
|
|
42
|
+
float l = length(b - a);
|
|
43
|
+
vec2 d = (b - a) / l;
|
|
44
|
+
vec2 q = (p - (a + b) * 0.5);
|
|
45
|
+
q = vec2(q.x * d.x + q.y * d.y, q.x * (-d.y) + q.y * d.x);
|
|
46
|
+
q = abs(q) - vec2(l, th) * 0.5;
|
|
47
|
+
return length(max(q, vec2(0.0))) + min(max(q.x, q.y), 0.0);
|
|
48
|
+
}
|
|
49
|
+
`,
|
|
50
|
+
`
|
|
51
|
+
float sdSegment(vec2 p, vec2 a, vec2 b) {
|
|
52
|
+
vec2 pa = p - a;
|
|
53
|
+
vec2 ba = b - a;
|
|
54
|
+
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
|
|
55
|
+
return length(pa - ba * h);
|
|
56
|
+
}
|
|
57
|
+
`,
|
|
58
|
+
`
|
|
59
|
+
float sdRhombus(vec2 p, vec2 b) {
|
|
60
|
+
p = abs(p);
|
|
61
|
+
float h = clamp(ndot(b - 2.0 * p, b) / dot(b, b), -1.0, 1.0);
|
|
62
|
+
float d = length(p - 0.5 * b * vec2(1.0 - h, 1.0 + h));
|
|
63
|
+
return d * sign(p.x * b.y + p.y * b.x - b.x * b.y);
|
|
64
|
+
}
|
|
65
|
+
`,
|
|
66
|
+
`
|
|
67
|
+
float sdTrapezoid(vec2 p, float r1, float r2, float he) {
|
|
68
|
+
vec2 k1 = vec2(r2, he);
|
|
69
|
+
vec2 k2 = vec2(r2 - r1, 2.0 * he);
|
|
70
|
+
p.x = abs(p.x);
|
|
71
|
+
vec2 ca = vec2(p.x - min(p.x, (p.y < 0.0) ? r1 : r2), abs(p.y) - he);
|
|
72
|
+
vec2 cb = p - k1 + k2 * clamp(dot(k1 - p, k2) / dot2(k2), 0.0, 1.0);
|
|
73
|
+
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
|
|
74
|
+
return s * sqrt(min(dot2(ca), dot2(cb)));
|
|
75
|
+
}
|
|
76
|
+
`,
|
|
77
|
+
`
|
|
78
|
+
float sdParallelogram(vec2 p, float wi, float he, float sk) {
|
|
79
|
+
vec2 e = vec2(sk, he);
|
|
80
|
+
p = (p.y < 0.0) ? -p : p;
|
|
81
|
+
vec2 w = p - e;
|
|
82
|
+
w.x -= clamp(w.x, -wi, wi);
|
|
83
|
+
vec2 d = vec2(dot(w, w), -w.y);
|
|
84
|
+
float s = p.x * e.y - p.y * e.x;
|
|
85
|
+
p = (s < 0.0) ? -p : p;
|
|
86
|
+
vec2 v = p - vec2(wi, 0.0);
|
|
87
|
+
v -= e * clamp(dot(v, e) / dot(e, e), -1.0, 1.0);
|
|
88
|
+
d = min(d, vec2(dot(v, v), wi * he - abs(s)));
|
|
89
|
+
return sqrt(d.x) * sign(-d.y);
|
|
90
|
+
}
|
|
91
|
+
`,
|
|
92
|
+
`
|
|
93
|
+
float sdEquilateralTriangle(vec2 p, float r) {
|
|
94
|
+
float k = sqrt(3.0);
|
|
95
|
+
p.x = abs(p.x) - r;
|
|
96
|
+
p.y = p.y + r / k;
|
|
97
|
+
if (p.x + k * p.y > 0.0) {
|
|
98
|
+
p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
|
|
99
|
+
}
|
|
100
|
+
p.x -= clamp(p.x, -2.0 * r, 0.0);
|
|
101
|
+
return -length(p) * sign(p.y);
|
|
102
|
+
}
|
|
103
|
+
`,
|
|
104
|
+
`
|
|
105
|
+
float sdTriangleIsosceles(vec2 p, vec2 q) {
|
|
106
|
+
p.x = abs(p.x);
|
|
107
|
+
vec2 a = p - q * clamp(dot(p, q) / dot(q, q), 0.0, 1.0);
|
|
108
|
+
vec2 b = p - q * vec2(clamp(p.x / q.x, 0.0, 1.0), 1.0);
|
|
109
|
+
float s = -sign(q.y);
|
|
110
|
+
vec2 d = min(vec2(dot(a, a), s * (p.x * q.y - p.y * q.x)), vec2(dot(b, b), s * (p.y - q.y)));
|
|
111
|
+
return -sqrt(d.x) * sign(d.y);
|
|
112
|
+
}
|
|
113
|
+
`,
|
|
114
|
+
`
|
|
115
|
+
float sdTriangle(vec2 p, vec2 p0, vec2 p1, vec2 p2) {
|
|
116
|
+
vec2 e0 = p1 - p0;
|
|
117
|
+
vec2 e1 = p2 - p1;
|
|
118
|
+
vec2 e2 = p0 - p2;
|
|
119
|
+
vec2 v0 = p - p0;
|
|
120
|
+
vec2 v1 = p - p1;
|
|
121
|
+
vec2 v2 = p - p2;
|
|
122
|
+
vec2 pq0 = v0 - e0 * clamp(dot(v0, e0) / dot(e0, e0), 0.0, 1.0);
|
|
123
|
+
vec2 pq1 = v1 - e1 * clamp(dot(v1, e1) / dot(e1, e1), 0.0, 1.0);
|
|
124
|
+
vec2 pq2 = v2 - e2 * clamp(dot(v2, e2) / dot(e2, e2), 0.0, 1.0);
|
|
125
|
+
float s = sign(e0.x * e2.y - e0.y * e2.x);
|
|
126
|
+
vec2 d = min(min(vec2(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x)), vec2(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x))), vec2(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x)));
|
|
127
|
+
return -sqrt(d.x) * sign(d.y);
|
|
128
|
+
}
|
|
129
|
+
`,
|
|
130
|
+
`
|
|
131
|
+
float sdUnevenCapsule(vec2 p, float r1, float r2, float h) {
|
|
132
|
+
p.x = abs(p.x);
|
|
133
|
+
float b = (r1 - r2) / h;
|
|
134
|
+
float a = sqrt(1.0 - b * b);
|
|
135
|
+
float k = dot(p, vec2(-b, a));
|
|
136
|
+
if (k < 0.0) {
|
|
137
|
+
return length(p) - r1;
|
|
138
|
+
}
|
|
139
|
+
if (k > a * h) {
|
|
140
|
+
return length(p - vec2(0.0, h)) - r2;
|
|
141
|
+
}
|
|
142
|
+
return dot(p, vec2(a, b)) - r1;
|
|
143
|
+
}
|
|
144
|
+
`,
|
|
145
|
+
`
|
|
146
|
+
float sdPentagon(vec2 p, float r) {
|
|
147
|
+
vec3 k = vec3(0.809016994, 0.587785252, 0.726542528);
|
|
148
|
+
p.x = abs(p.x);
|
|
149
|
+
p -= 2.0 * min(dot(vec2(-k.x, k.y), p), 0.0) * vec2(-k.x, k.y);
|
|
150
|
+
p -= 2.0 * min(dot(vec2(k.x, k.y), p), 0.0) * vec2(k.x, k.y);
|
|
151
|
+
p -= vec2(clamp(p.x, -r * k.z, r * k.z), r);
|
|
152
|
+
return length(p) * sign(p.y);
|
|
153
|
+
}
|
|
154
|
+
`,
|
|
155
|
+
`
|
|
156
|
+
float sdHexagon(vec2 p, float r) {
|
|
157
|
+
vec3 k = vec3(-0.866025404, 0.5, 0.577350269);
|
|
158
|
+
p = abs(p);
|
|
159
|
+
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
|
|
160
|
+
p -= vec2(clamp(p.x, -k.z * r, k.z * r), r);
|
|
161
|
+
return length(p) * sign(p.y);
|
|
162
|
+
}
|
|
163
|
+
`,
|
|
164
|
+
`
|
|
165
|
+
float sdOctogon(vec2 p, float r) {
|
|
166
|
+
vec3 k = vec3(-0.9238795325, 0.3826834323, 0.4142135623);
|
|
167
|
+
p = abs(p);
|
|
168
|
+
p -= 2.0 * min(dot(vec2(k.x, k.y), p), 0.0) * vec2(k.x, k.y);
|
|
169
|
+
p -= 2.0 * min(dot(vec2(-k.x, k.y), p), 0.0) * vec2(-k.x, k.y);
|
|
170
|
+
p -= vec2(clamp(p.x, -k.z * r, k.z * r), r);
|
|
171
|
+
return length(p) * sign(p.y);
|
|
172
|
+
}
|
|
173
|
+
`,
|
|
174
|
+
`
|
|
175
|
+
float sdHexagram(vec2 p, float r) {
|
|
176
|
+
vec4 k = vec4(-0.5, 0.8660254038, 0.5773502692, 1.7320508076);
|
|
177
|
+
p = abs(p);
|
|
178
|
+
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
|
|
179
|
+
p -= 2.0 * min(dot(k.yx, p), 0.0) * k.yx;
|
|
180
|
+
p -= vec2(clamp(p.x, r * k.z, r * k.w), r);
|
|
181
|
+
return length(p) * sign(p.y);
|
|
182
|
+
}
|
|
183
|
+
`,
|
|
184
|
+
`
|
|
185
|
+
float sdStar5(vec2 p, float r, float rf) {
|
|
186
|
+
vec2 k1 = vec2(0.809016994375, -0.587785252292);
|
|
187
|
+
vec2 k2 = vec2(-k1.x, k1.y);
|
|
188
|
+
p.x = abs(p.x);
|
|
189
|
+
p -= 2.0 * max(dot(k1, p), 0.0) * k1;
|
|
190
|
+
p -= 2.0 * max(dot(k2, p), 0.0) * k2;
|
|
191
|
+
p.x = abs(p.x);
|
|
192
|
+
p.y -= r;
|
|
193
|
+
vec2 ba = rf * vec2(-k1.y, k1.x) - vec2(0.0, 1.0);
|
|
194
|
+
float h = clamp(dot(p, ba) / dot(ba, ba), 0.0, r);
|
|
195
|
+
return length(p - ba * h) * sign(p.y * ba.x - p.x * ba.y);
|
|
196
|
+
}
|
|
197
|
+
`,
|
|
198
|
+
`
|
|
199
|
+
float sdStar(vec2 p, float r, int n, float m) {
|
|
200
|
+
float an = 3.141593 / float(n);
|
|
201
|
+
float en = 3.141593 / m;
|
|
202
|
+
vec2 acs = vec2(cos(an), sin(an));
|
|
203
|
+
vec2 ecs = vec2(cos(en), sin(en));
|
|
204
|
+
float bn = glslMod(atan(p.x, p.y), 2.0 * an) - an;
|
|
205
|
+
p = length(p) * vec2(cos(bn), abs(sin(bn)));
|
|
206
|
+
p -= r * acs;
|
|
207
|
+
p += ecs * clamp(-dot(p, ecs), 0.0, r * acs.y / ecs.y);
|
|
208
|
+
return length(p) * sign(p.x);
|
|
209
|
+
}
|
|
210
|
+
`,
|
|
211
|
+
`
|
|
212
|
+
float sdPie(vec2 p, vec2 c, float r) {
|
|
213
|
+
p.x = abs(p.x);
|
|
214
|
+
float l = length(p) - r;
|
|
215
|
+
float m = length(p - c * clamp(dot(p, c), 0.0, r));
|
|
216
|
+
return max(l, m * sign(c.y * p.x - c.x * p.y));
|
|
217
|
+
}
|
|
218
|
+
`,
|
|
219
|
+
`
|
|
220
|
+
float sdCutDisk(vec2 p, float r, float h) {
|
|
221
|
+
float w = sqrt(r * r - h * h);
|
|
222
|
+
p.x = abs(p.x);
|
|
223
|
+
float s = max((h - r) * p.x * p.x + w * w * (h + r - 2.0 * p.y), h * p.x - w * p.y);
|
|
224
|
+
return (s < 0.0) ? length(p) - r : (p.x < w) ? h - p.y : length(p - vec2(w, h));
|
|
225
|
+
}
|
|
226
|
+
`,
|
|
227
|
+
`
|
|
228
|
+
float sdArc(vec2 p, vec2 sc, float ra, float rb) {
|
|
229
|
+
p.x = abs(p.x);
|
|
230
|
+
return ((sc.y * p.x > sc.x * p.y) ? length(p - sc * ra) : abs(length(p) - ra)) - rb;
|
|
231
|
+
}
|
|
232
|
+
`,
|
|
233
|
+
`
|
|
234
|
+
float sdRing(vec2 p, vec2 n, float r, float th) {
|
|
235
|
+
p.x = abs(p.x);
|
|
236
|
+
p = vec2(p.x * n.x + p.y * (-n.y), p.x * n.y + p.y * n.x);
|
|
237
|
+
return max(abs(length(p) - r) - th * 0.5, length(vec2(p.x, max(0.0, abs(r - p.y) - th * 0.5))) * sign(p.x));
|
|
238
|
+
}
|
|
239
|
+
`,
|
|
240
|
+
`
|
|
241
|
+
float sdHorseshoe(vec2 p, vec2 c, float r, vec2 w) {
|
|
242
|
+
p.x = abs(p.x);
|
|
243
|
+
float l = length(p);
|
|
244
|
+
p = vec2(p.x * (-c.x) + p.y * c.y, p.x * c.y + p.y * c.x);
|
|
245
|
+
p = vec2((p.y > 0.0 || p.x > 0.0) ? p.x : l * sign(-c.x), (p.x > 0.0) ? p.y : l);
|
|
246
|
+
p = vec2(p.x, abs(p.y - r)) - w;
|
|
247
|
+
return length(max(p, vec2(0.0))) + min(0.0, max(p.x, p.y));
|
|
248
|
+
}
|
|
249
|
+
`,
|
|
250
|
+
`
|
|
251
|
+
float sdVesica(vec2 p, float r, float d) {
|
|
252
|
+
p = abs(p);
|
|
253
|
+
float b = sqrt(r * r - d * d);
|
|
254
|
+
return ((p.y - b) * d > p.x * b) ? length(p - vec2(0.0, b)) : length(p - vec2(-d, 0.0)) - r;
|
|
255
|
+
}
|
|
256
|
+
`,
|
|
257
|
+
`
|
|
258
|
+
float sdOrientedVesica(vec2 p, vec2 a, vec2 b, float w) {
|
|
259
|
+
float r = 0.5 * length(b - a);
|
|
260
|
+
float d = 0.5 * (r * r - w * w) / w;
|
|
261
|
+
vec2 v = (b - a) / r;
|
|
262
|
+
vec2 c = (b + a) * 0.5;
|
|
263
|
+
vec2 q = 0.5 * abs(vec2((p - c).x * v.y + (p - c).y * (-v.x), (p - c).x * v.x + (p - c).y * v.y));
|
|
264
|
+
vec3 h = (r * q.x < d * (q.y - r)) ? vec3(0.0, r, 0.0) : vec3(-d, 0.0, d + w);
|
|
265
|
+
return length(q - h.xy) - h.z;
|
|
266
|
+
}
|
|
267
|
+
`,
|
|
268
|
+
`
|
|
269
|
+
float sdMoon(vec2 p, float d, float ra, float rb) {
|
|
270
|
+
p.y = abs(p.y);
|
|
271
|
+
float a = (ra * ra - rb * rb + d * d) / (2.0 * d);
|
|
272
|
+
float b = sqrt(max(ra * ra - a * a, 0.0));
|
|
273
|
+
if (d * (p.x * b - p.y * a) > d * d * max(b - p.y, 0.0)) {
|
|
274
|
+
return length(p - vec2(a, b));
|
|
275
|
+
}
|
|
276
|
+
return max(length(p) - ra, -(length(p - vec2(d, 0.0)) - rb));
|
|
277
|
+
}
|
|
278
|
+
`,
|
|
279
|
+
`
|
|
280
|
+
float sdRoundedCross(vec2 p, float h) {
|
|
281
|
+
float k = 0.5 * (h + 1.0 / h);
|
|
282
|
+
p = abs(p);
|
|
283
|
+
return (p.x < 1.0 && p.y < p.x * (k - h) + h) ? k - sqrt(dot2(p - vec2(1.0, k))) : sqrt(min(dot2(p - vec2(0.0, h)), dot2(p - vec2(1.0, 0.0))));
|
|
284
|
+
}
|
|
285
|
+
`,
|
|
286
|
+
`
|
|
287
|
+
float sdEgg(vec2 p, float he, float ra, float rb, float bu) {
|
|
288
|
+
float r = 0.5 * (he + ra + rb) / bu;
|
|
289
|
+
float da = r - ra;
|
|
290
|
+
float db = r - rb;
|
|
291
|
+
float y = (db * db - da * da - he * he) / (2.0 * he);
|
|
292
|
+
float x = sqrt(max(da * da - y * y, 0.0));
|
|
293
|
+
p.x = abs(p.x);
|
|
294
|
+
p.y += 0.5 * (he + rb - ra);
|
|
295
|
+
float k = p.y * x - p.x * y;
|
|
296
|
+
if (k > 0.0 && k < he * (p.x + x)) {
|
|
297
|
+
return length(p + vec2(x, y)) - r;
|
|
298
|
+
}
|
|
299
|
+
return min(length(p) - ra, length(vec2(p.x, p.y - he)) - rb);
|
|
300
|
+
}
|
|
301
|
+
`,
|
|
302
|
+
`
|
|
303
|
+
float sdHeart(vec2 p) {
|
|
304
|
+
p.x = abs(p.x);
|
|
305
|
+
if (p.y + p.x > 1.0) {
|
|
306
|
+
return sqrt(dot(p - vec2(0.25, 0.75), p - vec2(0.25, 0.75))) - sqrt(2.0) / 4.0;
|
|
307
|
+
}
|
|
308
|
+
return sqrt(min(dot(p - vec2(0.00, 1.00), p - vec2(0.00, 1.00)), dot(p - 0.5 * max(p.x + p.y, 0.0), p - 0.5 * max(p.x + p.y, 0.0)))) * sign(p.x - p.y);
|
|
309
|
+
}
|
|
310
|
+
`,
|
|
311
|
+
`
|
|
312
|
+
float sdCross(vec2 p, vec2 b, float r) {
|
|
313
|
+
p = abs(p);
|
|
314
|
+
p = (p.y > p.x) ? p.yx : p.xy;
|
|
315
|
+
vec2 q = p - b;
|
|
316
|
+
float k = max(q.y, q.x);
|
|
317
|
+
vec2 w = (k > 0.0) ? q : vec2(b.y - p.x, -k);
|
|
318
|
+
return sign(k) * length(max(w, vec2(0.0))) + r;
|
|
319
|
+
}
|
|
320
|
+
`,
|
|
321
|
+
`
|
|
322
|
+
float sdRoundedX(vec2 p, float w, float r) {
|
|
323
|
+
p = abs(p);
|
|
324
|
+
return length(p - min(p.x + p.y, w) * 0.5) - r;
|
|
325
|
+
}
|
|
326
|
+
`,
|
|
327
|
+
`
|
|
328
|
+
float sdEllipse(vec2 p, vec2 ab) {
|
|
329
|
+
p = abs(p);
|
|
330
|
+
if (p.x > p.y) {
|
|
331
|
+
p = p.yx;
|
|
332
|
+
ab = ab.yx;
|
|
333
|
+
}
|
|
334
|
+
float l = ab.y * ab.y - ab.x * ab.x;
|
|
335
|
+
float m = ab.x * p.x / l;
|
|
336
|
+
float m2 = m * m;
|
|
337
|
+
float n = ab.y * p.y / l;
|
|
338
|
+
float n2 = n * n;
|
|
339
|
+
float c = (m2 + n2 - 1.0) / 3.0;
|
|
340
|
+
float c3 = c * c * c;
|
|
341
|
+
float q = c3 + m2 * n2 * 2.0;
|
|
342
|
+
float d = c3 + m2 * n2;
|
|
343
|
+
float g = m + m * n2;
|
|
344
|
+
float co;
|
|
345
|
+
if (d < 0.0) {
|
|
346
|
+
float h = acos(q / c3) / 3.0;
|
|
347
|
+
float s = cos(h);
|
|
348
|
+
float t = sin(h) * sqrt(3.0);
|
|
349
|
+
float rx = sqrt(-c * (s + t + 2.0) + m2);
|
|
350
|
+
float ry = sqrt(-c * (s - t + 2.0) + m2);
|
|
351
|
+
co = (ry + sign(l) * rx + abs(g) / (rx * ry) - m) / 2.0;
|
|
352
|
+
} else {
|
|
353
|
+
float h = 2.0 * m * n * sqrt(d);
|
|
354
|
+
float s = sign(q + h) * pow(abs(q + h), 1.0 / 3.0);
|
|
355
|
+
float u = sign(q - h) * pow(abs(q - h), 1.0 / 3.0);
|
|
356
|
+
float rx = -s - u - c * 4.0 + 2.0 * m2;
|
|
357
|
+
float ry = (s - u) * sqrt(3.0);
|
|
358
|
+
float rm = sqrt(rx * rx + ry * ry);
|
|
359
|
+
co = (ry / sqrt(rm - rx) + 2.0 * g / rm - m) / 2.0;
|
|
360
|
+
}
|
|
361
|
+
vec2 r = ab * vec2(co, sqrt(1.0 - co * co));
|
|
362
|
+
return length(r - p) * sign(p.y - r.y);
|
|
363
|
+
}
|
|
364
|
+
`,
|
|
365
|
+
`
|
|
366
|
+
float sdParabola(vec2 pos, float k) {
|
|
367
|
+
pos.x = abs(pos.x);
|
|
368
|
+
float ik = 1.0 / k;
|
|
369
|
+
float p = ik * (pos.y - 0.5 * ik) / 3.0;
|
|
370
|
+
float q = 0.25 * ik * ik * pos.x;
|
|
371
|
+
float h = q * q - p * p * p;
|
|
372
|
+
float r = sqrt(abs(h));
|
|
373
|
+
float x = (h > 0.0) ? pow(q + r, 1.0 / 3.0) - pow(abs(q - r), 1.0 / 3.0) * sign(r - q) : 2.0 * cos(atan(r / q) / 3.0) * sqrt(p);
|
|
374
|
+
return length(pos - vec2(x, k * x * x)) * sign(pos.x - x);
|
|
375
|
+
}
|
|
376
|
+
`,
|
|
377
|
+
`
|
|
378
|
+
float sdParabolaSegment(vec2 pos, float wi, float he) {
|
|
379
|
+
pos.x = abs(pos.x);
|
|
380
|
+
float ik = wi * wi / he;
|
|
381
|
+
float p = ik * (he - pos.y - 0.5 * ik) / 3.0;
|
|
382
|
+
float q = pos.x * ik * ik * 0.25;
|
|
383
|
+
float h = q * q - p * p * p;
|
|
384
|
+
float r = sqrt(abs(h));
|
|
385
|
+
float x = (h > 0.0) ? pow(q + r, 1.0 / 3.0) - pow(abs(q - r), 1.0 / 3.0) * sign(r - q) : 2.0 * cos(atan(r / q) / 3.0) * sqrt(p);
|
|
386
|
+
x = min(x, wi);
|
|
387
|
+
return length(pos - vec2(x, he - x * x / ik)) * sign(ik * (pos.y - he) + pos.x * pos.x);
|
|
388
|
+
}
|
|
389
|
+
`,
|
|
390
|
+
`
|
|
391
|
+
float sdBezier(vec2 pos, vec2 A, vec2 B, vec2 C) {
|
|
392
|
+
vec2 a = B - A;
|
|
393
|
+
vec2 b = A - 2.0 * B + C;
|
|
394
|
+
vec2 c = a * 2.0;
|
|
395
|
+
vec2 d = A - pos;
|
|
396
|
+
float kk = 1.0 / dot(b, b);
|
|
397
|
+
float kx = kk * dot(a, b);
|
|
398
|
+
float ky = kk * (2.0 * dot(a, a) + dot(d, b)) / 3.0;
|
|
399
|
+
float kz = kk * dot(d, a);
|
|
400
|
+
float res = 0.0;
|
|
401
|
+
float p = ky - kx * kx;
|
|
402
|
+
float p3 = p * p * p;
|
|
403
|
+
float q = kx * (2.0 * kx * kx - 3.0 * ky) + kz;
|
|
404
|
+
float h = q * q + 4.0 * p3;
|
|
405
|
+
if (h >= 0.0) {
|
|
406
|
+
h = sqrt(h);
|
|
407
|
+
vec2 x = (vec2(h, -h) - q) * 0.5;
|
|
408
|
+
vec2 uv = sign(x) * pow(abs(x), vec2(1.0 / 3.0, 1.0 / 3.0));
|
|
409
|
+
float t = clamp(uv.x + uv.y - kx, 0.0, 1.0);
|
|
410
|
+
res = dot(d + (c + b * t) * t, d + (c + b * t) * t);
|
|
411
|
+
} else {
|
|
412
|
+
float z = sqrt(-p);
|
|
413
|
+
float v = acos(q / (p * z * 2.0)) / 3.0;
|
|
414
|
+
float m = cos(v);
|
|
415
|
+
float n = sin(v) * 1.732050808;
|
|
416
|
+
vec3 t = clamp(vec3(m + m, -n - m, n - m) * z - kx, vec3(0.0), vec3(1.0));
|
|
417
|
+
res = min(dot(d + (c + b * t.x) * t.x, d + (c + b * t.x) * t.x), dot(d + (c + b * t.y) * t.y, d + (c + b * t.y) * t.y));
|
|
418
|
+
}
|
|
419
|
+
return sqrt(res);
|
|
420
|
+
}
|
|
421
|
+
`,
|
|
422
|
+
`
|
|
423
|
+
float sdBlobbyCross(vec2 pos, float he) {
|
|
424
|
+
pos = abs(pos);
|
|
425
|
+
pos = vec2(abs(pos.x - pos.y), 1.0 - pos.x - pos.y) / sqrt(2.0);
|
|
426
|
+
float p = (he - pos.y - 0.25 / he) / (6.0 * he);
|
|
427
|
+
float q = pos.x / (he * he * 16.0);
|
|
428
|
+
float h = q * q - p * p * p;
|
|
429
|
+
float x;
|
|
430
|
+
if (h > 0.0) {
|
|
431
|
+
float r = sqrt(h);
|
|
432
|
+
x = pow(q + r, 1.0 / 3.0) - pow(abs(q - r), 1.0 / 3.0) * sign(r - q);
|
|
433
|
+
} else {
|
|
434
|
+
float r = sqrt(p);
|
|
435
|
+
x = 2.0 * r * cos(acos(q / (p * r)) / 3.0);
|
|
436
|
+
}
|
|
437
|
+
x = min(x, sqrt(2.0) / 2.0);
|
|
438
|
+
vec2 z = vec2(x, he * (1.0 - 2.0 * x * x)) - pos;
|
|
439
|
+
return length(z) * sign(z.y);
|
|
440
|
+
}
|
|
441
|
+
`,
|
|
442
|
+
`
|
|
443
|
+
float sdTunnel(vec2 p, vec2 wh) {
|
|
444
|
+
p.x = abs(p.x);
|
|
445
|
+
p.y = -p.y;
|
|
446
|
+
vec2 q = p - wh;
|
|
447
|
+
float d1 = dot(vec2(max(q.x, 0.0), q.y), vec2(max(q.x, 0.0), q.y));
|
|
448
|
+
q.x = (p.y > 0.0) ? q.x : length(p) - wh.x;
|
|
449
|
+
float d2 = dot(vec2(q.x, max(q.y, 0.0)), vec2(q.x, max(q.y, 0.0)));
|
|
450
|
+
float d = sqrt(min(d1, d2));
|
|
451
|
+
return (max(q.x, q.y) < 0.0) ? -d : d;
|
|
452
|
+
}
|
|
453
|
+
`,
|
|
454
|
+
`
|
|
455
|
+
float sdStairs(vec2 p, vec2 wh, float n) {
|
|
456
|
+
vec2 ba = wh * n;
|
|
457
|
+
float d = min(dot(p - vec2(clamp(p.x, 0.0, ba.x), 0.0), p - vec2(clamp(p.x, 0.0, ba.x), 0.0)), dot(p - vec2(ba.x, clamp(p.y, 0.0, ba.y)), p - vec2(ba.x, clamp(p.y, 0.0, ba.y))));
|
|
458
|
+
float s = sign(max(-p.y, p.x - ba.x));
|
|
459
|
+
float dia = length(wh);
|
|
460
|
+
p = vec2(p.x * wh.x + p.y * wh.y, p.x * (-wh.y) + p.y * wh.x) / dia;
|
|
461
|
+
float id = clamp(round(p.x / dia), 0.0, n - 1.0);
|
|
462
|
+
p.x = p.x - id * dia;
|
|
463
|
+
p = vec2(p.x * wh.x + p.y * (-wh.y), p.x * wh.y + p.y * wh.x) / dia;
|
|
464
|
+
float hh = wh.y / 2.0;
|
|
465
|
+
p.y -= hh;
|
|
466
|
+
if (p.y > hh * sign(p.x)) {
|
|
467
|
+
s = 1.0;
|
|
468
|
+
}
|
|
469
|
+
p = (id < 0.5 || p.x > 0.0) ? p : -p;
|
|
470
|
+
d = min(d, dot(p - vec2(0.0, clamp(p.y, -hh, hh)), p - vec2(0.0, clamp(p.y, -hh, hh))));
|
|
471
|
+
d = min(d, dot(p - vec2(clamp(p.x, 0.0, wh.x), hh), p - vec2(clamp(p.x, 0.0, wh.x), hh)));
|
|
472
|
+
return sqrt(d) * s;
|
|
473
|
+
}
|
|
474
|
+
`,
|
|
475
|
+
`
|
|
476
|
+
float sdQuadraticCircle(vec2 p) {
|
|
477
|
+
p = abs(p);
|
|
478
|
+
if (p.y > p.x) {
|
|
479
|
+
p = p.yx;
|
|
480
|
+
}
|
|
481
|
+
float a = p.x - p.y;
|
|
482
|
+
float b = p.x + p.y;
|
|
483
|
+
float c = (2.0 * b - 1.0) / 3.0;
|
|
484
|
+
float h = a * a + c * c * c;
|
|
485
|
+
float t;
|
|
486
|
+
if (h >= 0.0) {
|
|
487
|
+
h = sqrt(h);
|
|
488
|
+
t = sign(h - a) * pow(abs(h - a), 1.0 / 3.0) - pow(h + a, 1.0 / 3.0);
|
|
489
|
+
} else {
|
|
490
|
+
float z = sqrt(-c);
|
|
491
|
+
float v = acos(a / (c * z)) / 3.0;
|
|
492
|
+
t = -z * (cos(v) + sin(v) * 1.732050808);
|
|
493
|
+
}
|
|
494
|
+
t *= 0.5;
|
|
495
|
+
vec2 w = vec2(-t, t) + 0.75 - t * t - p;
|
|
496
|
+
return length(w) * sign(a * a * 0.5 + b - 1.5);
|
|
497
|
+
}
|
|
498
|
+
`,
|
|
499
|
+
`
|
|
500
|
+
float sdHyberbola(vec2 p, float k, float he) {
|
|
501
|
+
p = abs(p);
|
|
502
|
+
p = vec2(p.x - p.y, p.x + p.y) / sqrt(2.0);
|
|
503
|
+
float x2 = p.x * p.x / 16.0;
|
|
504
|
+
float y2 = p.y * p.y / 16.0;
|
|
505
|
+
float r = k * (4.0 * k - p.x * p.y) / 12.0;
|
|
506
|
+
float q = (x2 - y2) * k * k;
|
|
507
|
+
float h = q * q + r * r * r;
|
|
508
|
+
float u;
|
|
509
|
+
if (h < 0.0) {
|
|
510
|
+
float m = sqrt(-r);
|
|
511
|
+
u = m * cos(acos(q / (r * m)) / 3.0);
|
|
512
|
+
} else {
|
|
513
|
+
float m = pow(sqrt(h) - q, 1.0 / 3.0);
|
|
514
|
+
u = (m - r / m) / 2.0;
|
|
515
|
+
}
|
|
516
|
+
float w = sqrt(u + x2);
|
|
517
|
+
float b = k * p.y - x2 * p.x * 2.0;
|
|
518
|
+
float t = p.x / 4.0 - w + sqrt(2.0 * x2 - u + b / w / 4.0);
|
|
519
|
+
t = max(t, sqrt(he * he * 0.5 + k) - he / sqrt(2.0));
|
|
520
|
+
return length(p - vec2(t, k / t)) * sign(p.x * p.y < k ? 1.0 : -1.0);
|
|
521
|
+
}
|
|
522
|
+
`,
|
|
523
|
+
`
|
|
524
|
+
float sdCoolS(vec2 p) {
|
|
525
|
+
float six = (p.y < 0.0) ? -p.x : p.x;
|
|
526
|
+
p.x = abs(p.x);
|
|
527
|
+
p.y = abs(p.y) - 0.2;
|
|
528
|
+
float rex = p.x - min(round(p.x / 0.4), 0.4);
|
|
529
|
+
float aby = abs(p.y - 0.2) - 0.6;
|
|
530
|
+
float d = dot2(vec2(six, -p.y) - clamp(0.5 * (six - p.y), 0.0, 0.2));
|
|
531
|
+
d = min(d, dot2(vec2(p.x, -aby) - clamp(0.5 * (p.x - aby), 0.0, 0.4)));
|
|
532
|
+
d = min(d, dot2(vec2(rex, p.y - clamp(p.y, 0.0, 0.4))));
|
|
533
|
+
float s = 2.0 * p.x + aby + abs(aby + 0.4) - 0.4;
|
|
534
|
+
return sqrt(d) * sign(s);
|
|
535
|
+
}
|
|
536
|
+
`,
|
|
537
|
+
`
|
|
538
|
+
float sdCircleWave(vec2 p, float tb, float ra) {
|
|
539
|
+
tb = 3.1415927 * 5.0 / 6.0 * max(tb, 0.0001);
|
|
540
|
+
vec2 co = ra * vec2(sin(tb), cos(tb));
|
|
541
|
+
p.x = abs(glslMod(p.x, co.x * 4.0) - co.x * 2.0);
|
|
542
|
+
vec2 p1 = p;
|
|
543
|
+
vec2 p2 = vec2(abs(p.x - 2.0 * co.x), -p.y + 2.0 * co.y);
|
|
544
|
+
float d1 = ((co.y * p1.x > co.x * p1.y) ? length(p1 - co) : abs(length(p1) - ra));
|
|
545
|
+
float d2 = ((co.y * p2.x > co.x * p2.y) ? length(p2 - co) : abs(length(p2) - ra));
|
|
546
|
+
return min(d1, d2);
|
|
547
|
+
}
|
|
548
|
+
`,
|
|
549
|
+
`
|
|
550
|
+
float onejsHash21(vec2 p, float seed) {
|
|
551
|
+
p = fract(p * vec2(123.34, 456.21) + seed * 0.1731);
|
|
552
|
+
p += dot(p, p + 45.32);
|
|
553
|
+
return fract(p.x * p.y);
|
|
554
|
+
}
|
|
555
|
+
`,
|
|
556
|
+
`
|
|
557
|
+
float onejsVNoise(vec2 p, float seed) {
|
|
558
|
+
vec2 i = floor(p);
|
|
559
|
+
vec2 f = fract(p);
|
|
560
|
+
f = f * f * (3.0 - 2.0 * f);
|
|
561
|
+
float a = onejsHash21(i, seed);
|
|
562
|
+
float b = onejsHash21(i + vec2(1.0, 0.0), seed);
|
|
563
|
+
float c = onejsHash21(i + vec2(0.0, 1.0), seed);
|
|
564
|
+
float d = onejsHash21(i + vec2(1.0, 1.0), seed);
|
|
565
|
+
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
|
|
566
|
+
}
|
|
567
|
+
`,
|
|
568
|
+
`
|
|
569
|
+
float onejsFbm(vec2 p, float seed, int octaves, float lacunarity, float gain) {
|
|
570
|
+
float sum = 0.0;
|
|
571
|
+
float amp = 0.5;
|
|
572
|
+
float norm = 0.0;
|
|
573
|
+
for (int o = 0; o < 4; o++) {
|
|
574
|
+
if (o >= octaves) {
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
sum += onejsVNoise(p, seed + float(o) * 19.0) * amp;
|
|
578
|
+
norm += amp;
|
|
579
|
+
p *= lacunarity;
|
|
580
|
+
amp *= gain;
|
|
581
|
+
}
|
|
582
|
+
return sum / max(norm, 1e-4);
|
|
583
|
+
}
|
|
584
|
+
`,
|
|
585
|
+
`
|
|
586
|
+
float onejsFbm(vec2 p, float seed, int octaves) {
|
|
587
|
+
return onejsFbm(p, seed, octaves, 2.0, 0.5);
|
|
588
|
+
}
|
|
589
|
+
`,
|
|
590
|
+
`
|
|
591
|
+
vec2 onejsMod289(vec2 x) {
|
|
592
|
+
return x - floor(x * (1.0 / 289.0)) * 289.0;
|
|
593
|
+
}
|
|
594
|
+
`,
|
|
595
|
+
`
|
|
596
|
+
vec3 onejsMod289(vec3 x) {
|
|
597
|
+
return x - floor(x * (1.0 / 289.0)) * 289.0;
|
|
598
|
+
}
|
|
599
|
+
`,
|
|
600
|
+
`
|
|
601
|
+
vec3 onejsPermute(vec3 x) {
|
|
602
|
+
return onejsMod289(((x * 34.0) + 1.0) * x);
|
|
603
|
+
}
|
|
604
|
+
`,
|
|
605
|
+
`
|
|
606
|
+
float onejsSimplexRaw(vec2 v) {
|
|
607
|
+
vec4 C = vec4(0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439);
|
|
608
|
+
vec2 i = floor(v + dot(v, C.yy));
|
|
609
|
+
vec2 x0 = v - i + dot(i, C.xx);
|
|
610
|
+
vec2 i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
|
|
611
|
+
vec4 x12 = x0.xyxy + C.xxzz;
|
|
612
|
+
x12.xy -= i1;
|
|
613
|
+
i = onejsMod289(i);
|
|
614
|
+
vec3 p = onejsPermute(onejsPermute(i.y + vec3(0.0, i1.y, 1.0)) + i.x + vec3(0.0, i1.x, 1.0));
|
|
615
|
+
vec3 m = max(0.5 - vec3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), vec3(0.0));
|
|
616
|
+
m = m * m;
|
|
617
|
+
m = m * m;
|
|
618
|
+
vec3 x = 2.0 * fract(p * C.www) - 1.0;
|
|
619
|
+
vec3 h = abs(x) - 0.5;
|
|
620
|
+
vec3 ox = floor(x + 0.5);
|
|
621
|
+
vec3 a0 = x - ox;
|
|
622
|
+
m *= 1.79284291400159 - 0.85373472095314 * (a0 * a0 + h * h);
|
|
623
|
+
vec3 g;
|
|
624
|
+
g.x = a0.x * x0.x + h.x * x0.y;
|
|
625
|
+
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
|
|
626
|
+
return 130.0 * dot(m, g);
|
|
627
|
+
}
|
|
628
|
+
`,
|
|
629
|
+
`
|
|
630
|
+
float onejsSimplex(vec2 p, float seed) {
|
|
631
|
+
return onejsSimplexRaw(p + seed * 137.13) * 0.5 + 0.5;
|
|
632
|
+
}
|
|
633
|
+
`,
|
|
634
|
+
`
|
|
635
|
+
float onejsFbmSimplex(vec2 p, float seed, int octaves, float lacunarity, float gain) {
|
|
636
|
+
float sum = 0.0;
|
|
637
|
+
float amp = 0.5;
|
|
638
|
+
float norm = 0.0;
|
|
639
|
+
for (int o = 0; o < 4; o++) {
|
|
640
|
+
if (o >= octaves) {
|
|
641
|
+
break;
|
|
642
|
+
}
|
|
643
|
+
sum += onejsSimplex(p, seed + float(o) * 19.0) * amp;
|
|
644
|
+
norm += amp;
|
|
645
|
+
p *= lacunarity;
|
|
646
|
+
amp *= gain;
|
|
647
|
+
}
|
|
648
|
+
return sum / max(norm, 1e-4);
|
|
649
|
+
}
|
|
650
|
+
`,
|
|
651
|
+
`
|
|
652
|
+
float onejsTurbulence(vec2 p, float seed, int octaves, float lacunarity, float gain) {
|
|
653
|
+
float sum = 0.0;
|
|
654
|
+
float amp = 0.5;
|
|
655
|
+
float norm = 0.0;
|
|
656
|
+
for (int o = 0; o < 4; o++) {
|
|
657
|
+
if (o >= octaves) {
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
sum += abs(onejsSimplexRaw(p + (seed + float(o) * 19.0) * 137.13)) * amp;
|
|
661
|
+
norm += amp;
|
|
662
|
+
p *= lacunarity;
|
|
663
|
+
amp *= gain;
|
|
664
|
+
}
|
|
665
|
+
return sum / max(norm, 1e-4);
|
|
666
|
+
}
|
|
667
|
+
`,
|
|
668
|
+
`
|
|
669
|
+
float onejsRidged(vec2 p, float seed, int octaves, float lacunarity, float gain) {
|
|
670
|
+
float sum = 0.0;
|
|
671
|
+
float amp = 0.5;
|
|
672
|
+
float norm = 0.0;
|
|
673
|
+
for (int o = 0; o < 4; o++) {
|
|
674
|
+
if (o >= octaves) {
|
|
675
|
+
break;
|
|
676
|
+
}
|
|
677
|
+
float r = 1.0 - abs(onejsSimplexRaw(p + (seed + float(o) * 19.0) * 137.13));
|
|
678
|
+
sum += r * r * amp;
|
|
679
|
+
norm += amp;
|
|
680
|
+
p *= lacunarity;
|
|
681
|
+
amp *= gain;
|
|
682
|
+
}
|
|
683
|
+
return sum / max(norm, 1e-4);
|
|
684
|
+
}
|
|
685
|
+
`,
|
|
686
|
+
`
|
|
687
|
+
float onejsFbmKind(int kind, vec2 p, float seed, int octaves, float lacunarity, float gain) {
|
|
688
|
+
if (kind == 2) {
|
|
689
|
+
return onejsTurbulence(p, seed, octaves, lacunarity, gain);
|
|
690
|
+
}
|
|
691
|
+
if (kind == 3) {
|
|
692
|
+
return onejsRidged(p, seed, octaves, lacunarity, gain);
|
|
693
|
+
}
|
|
694
|
+
return kind == 1 ? onejsFbmSimplex(p, seed, octaves, lacunarity, gain) : onejsFbm(p, seed, octaves, lacunarity, gain);
|
|
695
|
+
}
|
|
696
|
+
`,
|
|
697
|
+
`
|
|
698
|
+
float sl_hash21(vec2 p) {
|
|
699
|
+
p = fract(p * vec2(123.34, 456.21));
|
|
700
|
+
p += dot(p, p + 45.32);
|
|
701
|
+
return fract(p.x * p.y);
|
|
702
|
+
}
|
|
703
|
+
`,
|
|
704
|
+
`
|
|
705
|
+
float sl_valueNoise(vec2 p) {
|
|
706
|
+
return onejsVNoise(p, 0.0);
|
|
707
|
+
}
|
|
708
|
+
`,
|
|
709
|
+
`
|
|
710
|
+
float sl_simplex(vec2 p) {
|
|
711
|
+
return onejsSimplex(p, 0.0);
|
|
712
|
+
}
|
|
713
|
+
`,
|
|
714
|
+
`
|
|
715
|
+
float sl_fbm(vec2 p, int octaves, int kind) {
|
|
716
|
+
return onejsFbmKind(kind, p, 0.0, clamp(octaves, 1, 4), 2.0, 0.5);
|
|
717
|
+
}
|
|
718
|
+
`,
|
|
719
|
+
`
|
|
720
|
+
vec3 sl_hsv2rgb(vec3 c) {
|
|
721
|
+
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
|
|
722
|
+
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
|
|
723
|
+
return c.z * mix(K.xxx, clamp(p - K.xxx, vec3(0.0), vec3(1.0)), vec3(c.y));
|
|
724
|
+
}
|
|
725
|
+
`,
|
|
726
|
+
`
|
|
727
|
+
float sl_sdfDistance(int id, vec2 p, vec4 a, vec2 b) {
|
|
728
|
+
switch (id) {
|
|
729
|
+
case 0:
|
|
730
|
+
return sdCircle(p, a.x);
|
|
731
|
+
case 1:
|
|
732
|
+
return sdRoundedBox(p, a.xy, vec4(a.z, a.w, b.x, b.y));
|
|
733
|
+
case 2:
|
|
734
|
+
return sdBox(p, a.xy);
|
|
735
|
+
case 3:
|
|
736
|
+
return sdOrientedBox(p, a.xy, a.zw, b.x);
|
|
737
|
+
case 4:
|
|
738
|
+
return sdSegment(p, a.xy, a.zw);
|
|
739
|
+
case 5:
|
|
740
|
+
return sdRhombus(p, a.xy);
|
|
741
|
+
case 6:
|
|
742
|
+
return sdTrapezoid(p, a.x, a.y, a.z);
|
|
743
|
+
case 7:
|
|
744
|
+
return sdParallelogram(p, a.x, a.y, a.z);
|
|
745
|
+
case 8:
|
|
746
|
+
return sdEquilateralTriangle(p, a.x);
|
|
747
|
+
case 9:
|
|
748
|
+
return sdTriangleIsosceles(p, a.xy);
|
|
749
|
+
case 10:
|
|
750
|
+
return sdTriangle(p, a.xy, a.zw, b.xy);
|
|
751
|
+
case 11:
|
|
752
|
+
return sdUnevenCapsule(p, a.x, a.y, a.z);
|
|
753
|
+
case 12:
|
|
754
|
+
return sdPentagon(p, a.x);
|
|
755
|
+
case 13:
|
|
756
|
+
return sdHexagon(p, a.x);
|
|
757
|
+
case 14:
|
|
758
|
+
return sdOctogon(p, a.x);
|
|
759
|
+
case 15:
|
|
760
|
+
return sdHexagram(p, a.x);
|
|
761
|
+
case 16:
|
|
762
|
+
return sdStar5(p, a.x, a.y);
|
|
763
|
+
case 17:
|
|
764
|
+
return sdStar(p, a.x, int(a.y), a.z);
|
|
765
|
+
case 18:
|
|
766
|
+
return sdPie(p, a.xy, a.z);
|
|
767
|
+
case 19:
|
|
768
|
+
return sdCutDisk(p, a.x, a.y);
|
|
769
|
+
case 20:
|
|
770
|
+
return sdArc(p, a.xy, a.z, a.w);
|
|
771
|
+
case 21:
|
|
772
|
+
return sdRing(p, a.xy, a.z, a.w);
|
|
773
|
+
case 22:
|
|
774
|
+
return sdHorseshoe(p, a.xy, a.z, vec2(a.w, b.x));
|
|
775
|
+
case 23:
|
|
776
|
+
return sdVesica(p, a.x, a.y);
|
|
777
|
+
case 24:
|
|
778
|
+
return sdOrientedVesica(p, a.xy, a.zw, b.x);
|
|
779
|
+
case 25:
|
|
780
|
+
return sdMoon(p, a.x, a.y, a.z);
|
|
781
|
+
case 26:
|
|
782
|
+
return sdRoundedCross(p, a.x);
|
|
783
|
+
case 27:
|
|
784
|
+
return sdEgg(p, a.x, a.y, a.z, a.w);
|
|
785
|
+
case 28:
|
|
786
|
+
return sdHeart(p);
|
|
787
|
+
case 29:
|
|
788
|
+
return sdCross(p, a.xy, a.z);
|
|
789
|
+
case 30:
|
|
790
|
+
return sdRoundedX(p, a.x, a.y);
|
|
791
|
+
case 31:
|
|
792
|
+
return sdEllipse(p, a.xy);
|
|
793
|
+
case 32:
|
|
794
|
+
return sdParabola(p, a.x);
|
|
795
|
+
case 33:
|
|
796
|
+
return sdParabolaSegment(p, a.x, a.y);
|
|
797
|
+
case 34:
|
|
798
|
+
return sdBezier(p, a.xy, a.zw, b.xy);
|
|
799
|
+
case 35:
|
|
800
|
+
return sdBlobbyCross(p, a.x);
|
|
801
|
+
case 36:
|
|
802
|
+
return sdTunnel(p, a.xy);
|
|
803
|
+
case 37:
|
|
804
|
+
return sdStairs(p, a.xy, a.z);
|
|
805
|
+
case 38:
|
|
806
|
+
return sdQuadraticCircle(p);
|
|
807
|
+
case 39:
|
|
808
|
+
return sdHyberbola(p, a.x, a.y);
|
|
809
|
+
case 40:
|
|
810
|
+
return sdCoolS(p);
|
|
811
|
+
case 41:
|
|
812
|
+
return sdCircleWave(p, a.x, a.y);
|
|
813
|
+
default:
|
|
814
|
+
return 1e6;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
`,
|
|
818
|
+
`
|
|
819
|
+
float sl_voronoi(vec2 p) {
|
|
820
|
+
vec2 cell = floor(p);
|
|
821
|
+
vec2 f = fract(p);
|
|
822
|
+
float best = 8.0;
|
|
823
|
+
for (int y = -1; y <= 1; y++) {
|
|
824
|
+
for (int x = -1; x <= 1; x++) {
|
|
825
|
+
vec2 o = vec2(float(x), float(y));
|
|
826
|
+
vec2 jitter = vec2(sl_hash21(cell + o), sl_hash21(cell + o + 37.7));
|
|
827
|
+
best = min(best, length(o + jitter - f));
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
return best;
|
|
831
|
+
}
|
|
832
|
+
`,
|
|
833
|
+
`
|
|
834
|
+
float sl_luminance(vec3 c) {
|
|
835
|
+
return dot(c, vec3(0.2126, 0.7152, 0.0722));
|
|
836
|
+
}
|
|
837
|
+
`,
|
|
838
|
+
`
|
|
839
|
+
vec3 sl_gammaToLinear(vec3 c) {
|
|
840
|
+
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
|
|
841
|
+
}
|
|
842
|
+
`,
|
|
843
|
+
`
|
|
844
|
+
float sl_toLinear_gamma(float c) {
|
|
845
|
+
return c;
|
|
846
|
+
}
|
|
847
|
+
float sl_toLinear_linear(float c) {
|
|
848
|
+
return sl_gammaToLinear(vec3(c, 0.0, 0.0)).x;
|
|
849
|
+
}
|
|
850
|
+
float sl_toLinear(float c) {
|
|
851
|
+
if (sl_Opt.x > 0.5) {
|
|
852
|
+
return sl_toLinear_linear(c);
|
|
853
|
+
}
|
|
854
|
+
return sl_toLinear_gamma(c);
|
|
855
|
+
}
|
|
856
|
+
`,
|
|
857
|
+
`
|
|
858
|
+
vec2 sl_toLinear_gamma(vec2 c) {
|
|
859
|
+
return c;
|
|
860
|
+
}
|
|
861
|
+
vec2 sl_toLinear_linear(vec2 c) {
|
|
862
|
+
return sl_gammaToLinear(vec3(c, 0.0)).xy;
|
|
863
|
+
}
|
|
864
|
+
vec2 sl_toLinear(vec2 c) {
|
|
865
|
+
if (sl_Opt.x > 0.5) {
|
|
866
|
+
return sl_toLinear_linear(c);
|
|
867
|
+
}
|
|
868
|
+
return sl_toLinear_gamma(c);
|
|
869
|
+
}
|
|
870
|
+
`,
|
|
871
|
+
`
|
|
872
|
+
vec3 sl_toLinear_gamma(vec3 c) {
|
|
873
|
+
return c;
|
|
874
|
+
}
|
|
875
|
+
vec3 sl_toLinear_linear(vec3 c) {
|
|
876
|
+
return sl_gammaToLinear(c);
|
|
877
|
+
}
|
|
878
|
+
vec3 sl_toLinear(vec3 c) {
|
|
879
|
+
if (sl_Opt.x > 0.5) {
|
|
880
|
+
return sl_toLinear_linear(c);
|
|
881
|
+
}
|
|
882
|
+
return sl_toLinear_gamma(c);
|
|
883
|
+
}
|
|
884
|
+
`,
|
|
885
|
+
`
|
|
886
|
+
vec4 sl_toLinear_gamma(vec4 c) {
|
|
887
|
+
return c;
|
|
888
|
+
}
|
|
889
|
+
vec4 sl_toLinear_linear(vec4 c) {
|
|
890
|
+
return vec4(sl_gammaToLinear(c.rgb), c.a);
|
|
891
|
+
}
|
|
892
|
+
vec4 sl_toLinear(vec4 c) {
|
|
893
|
+
if (sl_Opt.x > 0.5) {
|
|
894
|
+
return sl_toLinear_linear(c);
|
|
895
|
+
}
|
|
896
|
+
return sl_toLinear_gamma(c);
|
|
897
|
+
}
|
|
898
|
+
`,
|
|
899
|
+
]
|