onejs-sl 0.1.1 → 0.1.3
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 +18 -0
- package/README.md +63 -13
- package/goldens/goldens.json +1 -1
- package/lib/common.hlsl +155 -0
- package/lib/noise2d.hlsl +167 -0
- package/lib/sdf2d.hlsl +510 -0
- package/package.json +6 -3
- package/src/body.ts +62 -21
- package/src/emit/hlsl-body.ts +3 -2
- package/src/lib/glsl.ts +899 -0
- package/src/lib/hlsl.ts +891 -0
- package/src/lib/index.ts +47 -0
- package/src/lib/names.ts +77 -0
- package/src/lib/table.ts +125 -0
- package/src/lib/types.ts +23 -0
- package/src/lib/wgsl.ts +994 -0
- package/src/params.ts +113 -0
- package/src/shapes.ts +4 -4
- package/src/sl.ts +9 -1
- package/src/tables.ts +4 -1
- package/src/web.ts +53 -28
- package/src/weblib.ts +0 -1303
package/lib/sdf2d.hlsl
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
// 2D signed distance functions, from Inigo Quilez.
|
|
2
|
+
// https://iquilezles.org/articles/distfunctions2d/
|
|
3
|
+
// Ported to HLSL in Spark2D; imported here for the TextureFX shape spike.
|
|
4
|
+
// sdPolygon is deliberately absent: it needs a vertex array, which does not
|
|
5
|
+
// fit the uniform-array layer model.
|
|
6
|
+
|
|
7
|
+
// IQ writes these in GLSL, where mod() floors. HLSL fmod() truncates, so the
|
|
8
|
+
// two disagree for negative x and every shape using it is wrong over half its
|
|
9
|
+
// domain. sdStar and sdCircleWave both need the GLSL meaning.
|
|
10
|
+
float glslMod(float x, float y) { return x - y * floor(x / y); }
|
|
11
|
+
float ndot(float2 a, float2 b) { return a.x * b.x - a.y * b.y; }
|
|
12
|
+
float dot2(in float2 v) { return dot(v, v); }
|
|
13
|
+
|
|
14
|
+
float sdCircle(float2 p, float r) {
|
|
15
|
+
return length(p) - r;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
float sdRoundedBox(in float2 p, in float2 b, in float4 r) {
|
|
19
|
+
r.xy = (p.x > 0.0) ? r.xy : r.zw;
|
|
20
|
+
r.x = (p.y > 0.0) ? r.x : r.y;
|
|
21
|
+
float2 q = abs(p) - b + r.x;
|
|
22
|
+
return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r.x;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
float sdBox(in float2 p, in float2 b) {
|
|
26
|
+
float2 d = abs(p) - b;
|
|
27
|
+
return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
float sdOrientedBox(in float2 p, in float2 a, in float2 b, float th) {
|
|
31
|
+
float l = length(b - a);
|
|
32
|
+
float2 d = (b - a) / l;
|
|
33
|
+
float2 q = (p - (a + b) * 0.5);
|
|
34
|
+
q = mul(q, float2x2(d.x, -d.y, d.y, d.x));
|
|
35
|
+
q = abs(q) - float2(l, th) * 0.5;
|
|
36
|
+
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
float sdSegment(in float2 p, in float2 a, in float2 b) {
|
|
40
|
+
float2 pa = p - a, ba = b - a;
|
|
41
|
+
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
|
|
42
|
+
return length(pa - ba * h);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
float sdRhombus(in float2 p, in float2 b) {
|
|
46
|
+
p = abs(p);
|
|
47
|
+
float h = clamp(ndot(b - 2.0 * p, b) / dot(b, b), -1.0, 1.0);
|
|
48
|
+
float d = length(p - 0.5 * b * float2(1.0 - h, 1.0 + h));
|
|
49
|
+
return d * sign(p.x * b.y + p.y * b.x - b.x * b.y);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
float sdTrapezoid(in float2 p, in float r1, float r2, float he) {
|
|
53
|
+
float2 k1 = float2(r2, he);
|
|
54
|
+
float2 k2 = float2(r2 - r1, 2.0 * he);
|
|
55
|
+
p.x = abs(p.x);
|
|
56
|
+
float2 ca = float2(p.x - min(p.x, (p.y < 0.0) ? r1 : r2), abs(p.y) - he);
|
|
57
|
+
float2 cb = p - k1 + k2 * clamp(dot(k1 - p, k2) / dot2(k2), 0.0, 1.0);
|
|
58
|
+
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
|
|
59
|
+
return s * sqrt(min(dot2(ca), dot2(cb)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
float sdParallelogram(float2 p, float wi, float he, float sk) {
|
|
63
|
+
float2 e = float2(sk, he);
|
|
64
|
+
p = (p.y < 0.0) ? -p : p;
|
|
65
|
+
float2 w = p - e;
|
|
66
|
+
w.x -= clamp(w.x, -wi, wi);
|
|
67
|
+
float2 d = float2(dot(w, w), -w.y);
|
|
68
|
+
float s = p.x * e.y - p.y * e.x;
|
|
69
|
+
p = (s < 0.0) ? -p : p;
|
|
70
|
+
float2 v = p - float2(wi, 0);
|
|
71
|
+
v -= e * clamp(dot(v, e) / dot(e, e), -1.0, 1.0);
|
|
72
|
+
d = min(d, float2(dot(v, v), wi * he - abs(s)));
|
|
73
|
+
return sqrt(d.x) * sign(-d.y);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
float sdEquilateralTriangle(in float2 p, in float r) {
|
|
77
|
+
const float k = sqrt(3.0);
|
|
78
|
+
p.x = abs(p.x) - r;
|
|
79
|
+
p.y = p.y + r / k;
|
|
80
|
+
if (p.x + k * p.y > 0.0) p = float2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
|
|
81
|
+
p.x -= clamp(p.x, -2.0 * r, 0.0);
|
|
82
|
+
return -length(p) * sign(p.y);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
float sdTriangleIsosceles(float2 p, float2 q) {
|
|
86
|
+
p.x = abs(p.x);
|
|
87
|
+
float2 a = p - q * clamp(dot(p, q) / dot(q, q), 0.0, 1.0);
|
|
88
|
+
float2 b = p - q * float2(clamp(p.x / q.x, 0.0, 1.0), 1.0);
|
|
89
|
+
float s = -sign(q.y);
|
|
90
|
+
float2 d = min(float2(dot(a, a), s * (p.x * q.y - p.y * q.x)), float2(dot(b, b), s * (p.y - q.y)));
|
|
91
|
+
return -sqrt(d.x) * sign(d.y);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
float sdTriangle(float2 p, float2 p0, float2 p1, float2 p2) {
|
|
95
|
+
float2 e0 = p1 - p0;
|
|
96
|
+
float2 e1 = p2 - p1;
|
|
97
|
+
float2 e2 = p0 - p2;
|
|
98
|
+
float2 v0 = p - p0;
|
|
99
|
+
float2 v1 = p - p1;
|
|
100
|
+
float2 v2 = p - p2;
|
|
101
|
+
|
|
102
|
+
float2 pq0 = v0 - e0 * clamp(dot(v0, e0) / dot(e0, e0), 0.0, 1.0);
|
|
103
|
+
float2 pq1 = v1 - e1 * clamp(dot(v1, e1) / dot(e1, e1), 0.0, 1.0);
|
|
104
|
+
float2 pq2 = v2 - e2 * clamp(dot(v2, e2) / dot(e2, e2), 0.0, 1.0);
|
|
105
|
+
|
|
106
|
+
float s = sign(e0.x * e2.y - e0.y * e2.x);
|
|
107
|
+
float2 d = min(min(float2(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x)),
|
|
108
|
+
float2(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x))),
|
|
109
|
+
float2(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x)));
|
|
110
|
+
|
|
111
|
+
return -sqrt(d.x) * sign(d.y);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
float sdUnevenCapsule(float2 p, float r1, float r2, float h) {
|
|
115
|
+
p.x = abs(p.x);
|
|
116
|
+
float b = (r1 - r2) / h;
|
|
117
|
+
float a = sqrt(1.0 - b * b);
|
|
118
|
+
float k = dot(p, float2(-b, a));
|
|
119
|
+
|
|
120
|
+
if (k < 0.0) {
|
|
121
|
+
return length(p) - r1;
|
|
122
|
+
}
|
|
123
|
+
if (k > a * h) {
|
|
124
|
+
return length(p - float2(0.0, h)) - r2;
|
|
125
|
+
}
|
|
126
|
+
return dot(p, float2(a, b)) - r1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
float sdPentagon(in float2 p, in float r) {
|
|
130
|
+
const float3 k = float3(0.809016994, 0.587785252, 0.726542528);
|
|
131
|
+
p.x = abs(p.x);
|
|
132
|
+
p -= 2.0 * min(dot(float2(-k.x, k.y), p), 0.0) * float2(-k.x, k.y);
|
|
133
|
+
p -= 2.0 * min(dot(float2(k.x, k.y), p), 0.0) * float2(k.x, k.y);
|
|
134
|
+
p -= float2(clamp(p.x, -r * k.z, r * k.z), r);
|
|
135
|
+
return length(p) * sign(p.y);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
float sdHexagon(in float2 p, in float r) {
|
|
139
|
+
const float3 k = float3(-0.866025404, 0.5, 0.577350269);
|
|
140
|
+
p = abs(p);
|
|
141
|
+
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
|
|
142
|
+
p -= float2(clamp(p.x, -k.z * r, k.z * r), r);
|
|
143
|
+
return length(p) * sign(p.y);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
float sdOctogon(in float2 p, in float r) {
|
|
147
|
+
const float3 k = float3(-0.9238795325, 0.3826834323, 0.4142135623);
|
|
148
|
+
p = abs(p);
|
|
149
|
+
p -= 2.0 * min(dot(float2(k.x, k.y), p), 0.0) * float2(k.x, k.y);
|
|
150
|
+
p -= 2.0 * min(dot(float2(-k.x, k.y), p), 0.0) * float2(-k.x, k.y);
|
|
151
|
+
p -= float2(clamp(p.x, -k.z * r, k.z * r), r);
|
|
152
|
+
return length(p) * sign(p.y);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
float sdHexagram(float2 p, float r) {
|
|
156
|
+
const float4 k = float4(-0.5, 0.8660254038, 0.5773502692, 1.7320508076);
|
|
157
|
+
p = abs(p);
|
|
158
|
+
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
|
|
159
|
+
p -= 2.0 * min(dot(k.yx, p), 0.0) * k.yx;
|
|
160
|
+
p -= float2(clamp(p.x, r * k.z, r * k.w), r);
|
|
161
|
+
return length(p) * sign(p.y);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
float sdStar5(float2 p, float r, float rf) {
|
|
165
|
+
const float2 k1 = float2(0.809016994375, -0.587785252292);
|
|
166
|
+
const float2 k2 = float2(-k1.x, k1.y);
|
|
167
|
+
|
|
168
|
+
p.x = abs(p.x);
|
|
169
|
+
p -= 2.0 * max(dot(k1, p), 0.0) * k1;
|
|
170
|
+
p -= 2.0 * max(dot(k2, p), 0.0) * k2;
|
|
171
|
+
p.x = abs(p.x);
|
|
172
|
+
p.y -= r;
|
|
173
|
+
|
|
174
|
+
float2 ba = rf * float2(-k1.y, k1.x) - float2(0, 1);
|
|
175
|
+
float h = clamp(dot(p, ba) / dot(ba, ba), 0.0, r);
|
|
176
|
+
|
|
177
|
+
return length(p - ba * h) * sign(p.y * ba.x - p.x * ba.y);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
float sdStar(in float2 p, in float r, in int n, in float m) {
|
|
181
|
+
float an = 3.141593 / float(n);
|
|
182
|
+
float en = 3.141593 / m;
|
|
183
|
+
float2 acs = float2(cos(an), sin(an));
|
|
184
|
+
float2 ecs = float2(cos(en), sin(en));
|
|
185
|
+
float bn = glslMod(atan2(p.x, p.y), 2.0 * an) - an;
|
|
186
|
+
p = length(p) * float2(cos(bn), abs(sin(bn)));
|
|
187
|
+
p -= r * acs;
|
|
188
|
+
p += ecs * clamp(-dot(p, ecs), 0.0, r * acs.y / ecs.y);
|
|
189
|
+
return length(p) * sign(p.x);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
float sdPie(float2 p, float2 c, float r) {
|
|
193
|
+
p.x = abs(p.x);
|
|
194
|
+
float l = length(p) - r;
|
|
195
|
+
float m = length(p - c * clamp(dot(p, c), 0.0, r)); // c = sin/cos of aperture
|
|
196
|
+
return max(l, m * sign(c.y * p.x - c.x * p.y));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
float sdCutDisk(in float2 p, in float r, in float h) {
|
|
200
|
+
float w = sqrt(r * r - h * h);
|
|
201
|
+
p.x = abs(p.x);
|
|
202
|
+
float s = max((h - r) * p.x * p.x + w * w * (h + r - 2.0 * p.y), h * p.x - w * p.y);
|
|
203
|
+
return (s < 0.0) ? length(p) - r : (p.x < w) ? h - p.y : length(p - float2(w, h));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
float sdArc(float2 p, float2 sc, float ra, float rb) {
|
|
207
|
+
p.x = abs(p.x);
|
|
208
|
+
return ((sc.y * p.x > sc.x * p.y) ? length(p - sc * ra) : abs(length(p) - ra)) - rb;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
float sdRing(float2 p, float2 n, float r, float th) {
|
|
212
|
+
p.x = abs(p.x);
|
|
213
|
+
p = mul(p, float2x2(n.x, n.y, -n.y, n.x));
|
|
214
|
+
return max(abs(length(p) - r) - th * 0.5, length(float2(p.x, max(0.0, abs(r - p.y) - th * 0.5))) * sign(p.x));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
float sdHorseshoe(float2 p, float2 c, float r, float2 w) {
|
|
218
|
+
p.x = abs(p.x);
|
|
219
|
+
float l = length(p);
|
|
220
|
+
p = mul(p, float2x2(-c.x, c.y, c.y, c.x));
|
|
221
|
+
p = float2((p.y > 0.0 || p.x > 0.0) ? p.x : l * sign(-c.x), (p.x > 0.0) ? p.y : l);
|
|
222
|
+
p = float2(p.x, abs(p.y - r)) - w;
|
|
223
|
+
return length(max(p, 0.0)) + min(0.0, max(p.x, p.y));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
float sdVesica(float2 p, float r, float d) {
|
|
227
|
+
p = abs(p);
|
|
228
|
+
float b = sqrt(r * r - d * d);
|
|
229
|
+
return ((p.y - b) * d > p.x * b) ? length(p - float2(0.0, b)) : length(p - float2(-d, 0.0)) - r;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
float sdOrientedVesica(float2 p, float2 a, float2 b, float w) {
|
|
233
|
+
float r = 0.5 * length(b - a);
|
|
234
|
+
float d = 0.5 * (r * r - w * w) / w;
|
|
235
|
+
float2 v = (b - a) / r;
|
|
236
|
+
float2 c = (b + a) * 0.5;
|
|
237
|
+
float2 q = 0.5 * abs(mul((p - c), float2x2(v.y, v.x, -v.x, v.y)));
|
|
238
|
+
float3 h = (r * q.x < d * (q.y - r)) ? float3(0.0, r, 0.0) : float3(-d, 0.0, d + w);
|
|
239
|
+
return length(q - h.xy) - h.z;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
float sdMoon(float2 p, float d, float ra, float rb) {
|
|
243
|
+
p.y = abs(p.y);
|
|
244
|
+
float a = (ra * ra - rb * rb + d * d) / (2.0 * d);
|
|
245
|
+
float b = sqrt(max(ra * ra - a * a, 0.0));
|
|
246
|
+
if (d * (p.x * b - p.y * a) > d * d * max(b - p.y, 0.0))
|
|
247
|
+
return length(p - float2(a, b));
|
|
248
|
+
return max(length(p) - ra, -(length(p - float2(d, 0)) - rb));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
float sdRoundedCross(float2 p, float h) {
|
|
252
|
+
float k = 0.5 * (h + 1.0 / h);
|
|
253
|
+
p = abs(p);
|
|
254
|
+
return (p.x < 1.0 && p.y < p.x * (k - h) + h)
|
|
255
|
+
? k - sqrt(dot2(p - float2(1, k)))
|
|
256
|
+
: sqrt(min(dot2(p - float2(0, h)), dot2(p - float2(1, 0))));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Inigo Quilez's four parameter egg: a bottom circle of radius ra, a top circle
|
|
260
|
+
// of radius rb a height he above it, joined by arcs whose radius bu sets, so
|
|
261
|
+
// the sides bulge (bu below 1) or run nearly straight (bu near 1). Centred on
|
|
262
|
+
// the shape's own extent, like every other shape here, rather than on the
|
|
263
|
+
// bottom circle as the original is.
|
|
264
|
+
float sdEgg(float2 p, float he, float ra, float rb, float bu) {
|
|
265
|
+
float r = 0.5 * (he + ra + rb) / bu;
|
|
266
|
+
float da = r - ra;
|
|
267
|
+
float db = r - rb;
|
|
268
|
+
float y = (db * db - da * da - he * he) / (2.0 * he);
|
|
269
|
+
float x = sqrt(max(da * da - y * y, 0.0));
|
|
270
|
+
|
|
271
|
+
p.x = abs(p.x);
|
|
272
|
+
p.y += 0.5 * (he + rb - ra);
|
|
273
|
+
float k = p.y * x - p.x * y;
|
|
274
|
+
if (k > 0.0 && k < he * (p.x + x)) return length(p + float2(x, y)) - r;
|
|
275
|
+
return min(length(p) - ra, length(float2(p.x, p.y - he)) - rb);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
float sdHeart(float2 p) {
|
|
279
|
+
p.x = abs(p.x);
|
|
280
|
+
if (p.y + p.x > 1.0)
|
|
281
|
+
return sqrt(dot(p - float2(0.25, 0.75), p - float2(0.25, 0.75))) - sqrt(2.0) / 4.0;
|
|
282
|
+
return sqrt(min(dot(p - float2(0.00, 1.00), p - float2(0.00, 1.00)),
|
|
283
|
+
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);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
float sdCross(float2 p, float2 b, float r) {
|
|
287
|
+
p = abs(p);
|
|
288
|
+
p = (p.y > p.x) ? p.yx : p.xy;
|
|
289
|
+
float2 q = p - b;
|
|
290
|
+
float k = max(q.y, q.x);
|
|
291
|
+
float2 w = (k > 0.0) ? q : float2(b.y - p.x, -k);
|
|
292
|
+
return sign(k) * length(max(w, 0.0)) + r;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
float sdRoundedX(float2 p, float w, float r) {
|
|
296
|
+
p = abs(p);
|
|
297
|
+
return length(p - min(p.x + p.y, w) * 0.5) - r;
|
|
298
|
+
}
|
|
299
|
+
float sdEllipse(float2 p, float2 ab) {
|
|
300
|
+
p = abs(p);
|
|
301
|
+
if (p.x > p.y) {
|
|
302
|
+
p = p.yx;
|
|
303
|
+
ab = ab.yx;
|
|
304
|
+
}
|
|
305
|
+
float l = ab.y * ab.y - ab.x * ab.x;
|
|
306
|
+
float m = ab.x * p.x / l;
|
|
307
|
+
float m2 = m * m;
|
|
308
|
+
float n = ab.y * p.y / l;
|
|
309
|
+
float n2 = n * n;
|
|
310
|
+
float c = (m2 + n2 - 1.0) / 3.0;
|
|
311
|
+
float c3 = c * c * c;
|
|
312
|
+
float q = c3 + m2 * n2 * 2.0;
|
|
313
|
+
float d = c3 + m2 * n2;
|
|
314
|
+
float g = m + m * n2;
|
|
315
|
+
float co;
|
|
316
|
+
if (d < 0.0) {
|
|
317
|
+
float h = acos(q / c3) / 3.0;
|
|
318
|
+
float s = cos(h);
|
|
319
|
+
float t = sin(h) * sqrt(3.0);
|
|
320
|
+
float rx = sqrt(-c * (s + t + 2.0) + m2);
|
|
321
|
+
float ry = sqrt(-c * (s - t + 2.0) + m2);
|
|
322
|
+
co = (ry + sign(l) * rx + abs(g) / (rx * ry) - m) / 2.0;
|
|
323
|
+
} else {
|
|
324
|
+
float h = 2.0 * m * n * sqrt(d);
|
|
325
|
+
float s = sign(q + h) * pow(abs(q + h), 1.0 / 3.0);
|
|
326
|
+
float u = sign(q - h) * pow(abs(q - h), 1.0 / 3.0);
|
|
327
|
+
float rx = -s - u - c * 4.0 + 2.0 * m2;
|
|
328
|
+
float ry = (s - u) * sqrt(3.0);
|
|
329
|
+
float rm = sqrt(rx * rx + ry * ry);
|
|
330
|
+
co = (ry / sqrt(rm - rx) + 2.0 * g / rm - m) / 2.0;
|
|
331
|
+
}
|
|
332
|
+
float2 r = ab * float2(co, sqrt(1.0 - co * co));
|
|
333
|
+
return length(r - p) * sign(p.y - r.y);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
float sdParabola(float2 pos, float k) {
|
|
337
|
+
pos.x = abs(pos.x);
|
|
338
|
+
float ik = 1.0 / k;
|
|
339
|
+
float p = ik * (pos.y - 0.5 * ik) / 3.0;
|
|
340
|
+
float q = 0.25 * ik * ik * pos.x;
|
|
341
|
+
float h = q * q - p * p * p;
|
|
342
|
+
float r = sqrt(abs(h));
|
|
343
|
+
float x = (h > 0.0)
|
|
344
|
+
? pow(q + r, 1.0 / 3.0) - pow(abs(q - r), 1.0 / 3.0) * sign(r - q)
|
|
345
|
+
: 2.0 * cos(atan(r / q) / 3.0) * sqrt(p);
|
|
346
|
+
return length(pos - float2(x, k * x * x)) * sign(pos.x - x);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
float sdParabolaSegment(float2 pos, float wi, float he) {
|
|
350
|
+
pos.x = abs(pos.x);
|
|
351
|
+
float ik = wi * wi / he;
|
|
352
|
+
float p = ik * (he - pos.y - 0.5 * ik) / 3.0;
|
|
353
|
+
float q = pos.x * ik * ik * 0.25;
|
|
354
|
+
float h = q * q - p * p * p;
|
|
355
|
+
float r = sqrt(abs(h));
|
|
356
|
+
float x = (h > 0.0)
|
|
357
|
+
? pow(q + r, 1.0 / 3.0) - pow(abs(q - r), 1.0 / 3.0) * sign(r - q)
|
|
358
|
+
: 2.0 * cos(atan(r / q) / 3.0) * sqrt(p);
|
|
359
|
+
x = min(x, wi);
|
|
360
|
+
return length(pos - float2(x, he - x * x / ik)) * sign(ik * (pos.y - he) + pos.x * pos.x);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
float sdBezier(float2 pos, float2 A, float2 B, float2 C) {
|
|
364
|
+
float2 a = B - A;
|
|
365
|
+
float2 b = A - 2.0 * B + C;
|
|
366
|
+
float2 c = a * 2.0;
|
|
367
|
+
float2 d = A - pos;
|
|
368
|
+
float kk = 1.0 / dot(b, b);
|
|
369
|
+
float kx = kk * dot(a, b);
|
|
370
|
+
float ky = kk * (2.0 * dot(a, a) + dot(d, b)) / 3.0;
|
|
371
|
+
float kz = kk * dot(d, a);
|
|
372
|
+
float res = 0.0;
|
|
373
|
+
float p = ky - kx * kx;
|
|
374
|
+
float p3 = p * p * p;
|
|
375
|
+
float q = kx * (2.0 * kx * kx - 3.0 * ky) + kz;
|
|
376
|
+
float h = q * q + 4.0 * p3;
|
|
377
|
+
if (h >= 0.0) {
|
|
378
|
+
h = sqrt(h);
|
|
379
|
+
float2 x = (float2(h, -h) - q) * 0.5;
|
|
380
|
+
float2 uv = sign(x) * pow(abs(x), float2(1.0 / 3.0, 1.0 / 3.0));
|
|
381
|
+
float t = clamp(uv.x + uv.y - kx, 0.0, 1.0);
|
|
382
|
+
res = dot(d + (c + b * t) * t, d + (c + b * t) * t);
|
|
383
|
+
} else {
|
|
384
|
+
float z = sqrt(-p);
|
|
385
|
+
float v = acos(q / (p * z * 2.0)) / 3.0;
|
|
386
|
+
float m = cos(v);
|
|
387
|
+
float n = sin(v) * 1.732050808;
|
|
388
|
+
float3 t = clamp(float3(m + m, -n - m, n - m) * z - kx, 0.0, 1.0);
|
|
389
|
+
res = min(dot(d + (c + b * t.x) * t.x, d + (c + b * t.x) * t.x),
|
|
390
|
+
dot(d + (c + b * t.y) * t.y, d + (c + b * t.y) * t.y));
|
|
391
|
+
}
|
|
392
|
+
return sqrt(res);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
float sdBlobbyCross(float2 pos, float he) {
|
|
396
|
+
pos = abs(pos);
|
|
397
|
+
pos = float2(abs(pos.x - pos.y), 1.0 - pos.x - pos.y) / sqrt(2.0);
|
|
398
|
+
float p = (he - pos.y - 0.25 / he) / (6.0 * he);
|
|
399
|
+
float q = pos.x / (he * he * 16.0);
|
|
400
|
+
float h = q * q - p * p * p;
|
|
401
|
+
float x;
|
|
402
|
+
if (h > 0.0) {
|
|
403
|
+
float r = sqrt(h);
|
|
404
|
+
x = pow(q + r, 1.0 / 3.0) - pow(abs(q - r), 1.0 / 3.0) * sign(r - q);
|
|
405
|
+
} else {
|
|
406
|
+
float r = sqrt(p);
|
|
407
|
+
x = 2.0 * r * cos(acos(q / (p * r)) / 3.0);
|
|
408
|
+
}
|
|
409
|
+
x = min(x, sqrt(2.0) / 2.0);
|
|
410
|
+
float2 z = float2(x, he * (1.0 - 2.0 * x * x)) - pos;
|
|
411
|
+
return length(z) * sign(z.y);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
float sdTunnel(float2 p, float2 wh) {
|
|
415
|
+
p.x = abs(p.x);
|
|
416
|
+
p.y = -p.y;
|
|
417
|
+
float2 q = p - wh;
|
|
418
|
+
float d1 = dot(float2(max(q.x, 0.0), q.y), float2(max(q.x, 0.0), q.y));
|
|
419
|
+
q.x = (p.y > 0.0) ? q.x : length(p) - wh.x;
|
|
420
|
+
float d2 = dot(float2(q.x, max(q.y, 0.0)), float2(q.x, max(q.y, 0.0)));
|
|
421
|
+
float d = sqrt(min(d1, d2));
|
|
422
|
+
return (max(q.x, q.y) < 0.0) ? -d : d;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
float sdStairs(float2 p, float2 wh, float n) {
|
|
426
|
+
float2 ba = wh * n;
|
|
427
|
+
float d = min(dot(p - float2(clamp(p.x, 0.0, ba.x), 0.0), p - float2(clamp(p.x, 0.0, ba.x), 0.0)),
|
|
428
|
+
dot(p - float2(ba.x, clamp(p.y, 0.0, ba.y)), p - float2(ba.x, clamp(p.y, 0.0, ba.y))));
|
|
429
|
+
float s = sign(max(-p.y, p.x - ba.x));
|
|
430
|
+
float dia = length(wh);
|
|
431
|
+
p = mul(p, float2x2(wh.x, -wh.y, wh.y, wh.x)) / dia;
|
|
432
|
+
float id = clamp(round(p.x / dia), 0.0, n - 1.0);
|
|
433
|
+
p.x = p.x - id * dia;
|
|
434
|
+
p = mul(p, float2x2(wh.x, wh.y, -wh.y, wh.x)) / dia;
|
|
435
|
+
float hh = wh.y / 2.0;
|
|
436
|
+
p.y -= hh;
|
|
437
|
+
if (p.y > hh * sign(p.x)) s = 1.0;
|
|
438
|
+
p = (id < 0.5 || p.x > 0.0) ? p : -p;
|
|
439
|
+
d = min(d, dot(p - float2(0.0, clamp(p.y, -hh, hh)), p - float2(0.0, clamp(p.y, -hh, hh))));
|
|
440
|
+
d = min(d, dot(p - float2(clamp(p.x, 0.0, wh.x), hh), p - float2(clamp(p.x, 0.0, wh.x), hh)));
|
|
441
|
+
return sqrt(d) * s;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
float sdQuadraticCircle(float2 p) {
|
|
445
|
+
p = abs(p);
|
|
446
|
+
if (p.y > p.x) p = p.yx;
|
|
447
|
+
float a = p.x - p.y;
|
|
448
|
+
float b = p.x + p.y;
|
|
449
|
+
float c = (2.0 * b - 1.0) / 3.0;
|
|
450
|
+
float h = a * a + c * c * c;
|
|
451
|
+
float t;
|
|
452
|
+
if (h >= 0.0) {
|
|
453
|
+
h = sqrt(h);
|
|
454
|
+
t = sign(h - a) * pow(abs(h - a), 1.0 / 3.0) - pow(h + a, 1.0 / 3.0);
|
|
455
|
+
} else {
|
|
456
|
+
float z = sqrt(-c);
|
|
457
|
+
float v = acos(a / (c * z)) / 3.0;
|
|
458
|
+
t = -z * (cos(v) + sin(v) * 1.732050808);
|
|
459
|
+
}
|
|
460
|
+
t *= 0.5;
|
|
461
|
+
float2 w = float2(-t, t) + 0.75 - t * t - p;
|
|
462
|
+
return length(w) * sign(a * a * 0.5 + b - 1.5);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
float sdHyberbola(float2 p, float k, float he) {
|
|
466
|
+
p = abs(p);
|
|
467
|
+
p = float2(p.x - p.y, p.x + p.y) / sqrt(2.0);
|
|
468
|
+
float x2 = p.x * p.x / 16.0;
|
|
469
|
+
float y2 = p.y * p.y / 16.0;
|
|
470
|
+
float r = k * (4.0 * k - p.x * p.y) / 12.0;
|
|
471
|
+
float q = (x2 - y2) * k * k;
|
|
472
|
+
float h = q * q + r * r * r;
|
|
473
|
+
float u;
|
|
474
|
+
if (h < 0.0) {
|
|
475
|
+
float m = sqrt(-r);
|
|
476
|
+
u = m * cos(acos(q / (r * m)) / 3.0);
|
|
477
|
+
} else {
|
|
478
|
+
float m = pow(sqrt(h) - q, 1.0 / 3.0);
|
|
479
|
+
u = (m - r / m) / 2.0;
|
|
480
|
+
}
|
|
481
|
+
float w = sqrt(u + x2);
|
|
482
|
+
float b = k * p.y - x2 * p.x * 2.0;
|
|
483
|
+
float t = p.x / 4.0 - w + sqrt(2.0 * x2 - u + b / w / 4.0);
|
|
484
|
+
t = max(t, sqrt(he * he * 0.5 + k) - he / sqrt(2.0));
|
|
485
|
+
return length(p - float2(t, k / t)) * sign(p.x * p.y < k ? 1.0 : -1.0);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
float sdCoolS(float2 p) {
|
|
489
|
+
float six = (p.y < 0.0) ? -p.x : p.x;
|
|
490
|
+
p.x = abs(p.x);
|
|
491
|
+
p.y = abs(p.y) - 0.2;
|
|
492
|
+
float rex = p.x - min(round(p.x / 0.4), 0.4);
|
|
493
|
+
float aby = abs(p.y - 0.2) - 0.6;
|
|
494
|
+
float d = dot2(float2(six, -p.y) - clamp(0.5 * (six - p.y), 0.0, 0.2));
|
|
495
|
+
d = min(d, dot2(float2(p.x, -aby) - clamp(0.5 * (p.x - aby), 0.0, 0.4)));
|
|
496
|
+
d = min(d, dot2(float2(rex, p.y - clamp(p.y, 0.0, 0.4))));
|
|
497
|
+
float s = 2.0 * p.x + aby + abs(aby + 0.4) - 0.4;
|
|
498
|
+
return sqrt(d) * sign(s);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
float sdCircleWave(float2 p, float tb, float ra) {
|
|
502
|
+
tb = 3.1415927 * 5.0 / 6.0 * max(tb, 0.0001);
|
|
503
|
+
float2 co = ra * float2(sin(tb), cos(tb));
|
|
504
|
+
p.x = abs(glslMod(p.x, co.x * 4.0) - co.x * 2.0);
|
|
505
|
+
float2 p1 = p;
|
|
506
|
+
float2 p2 = float2(abs(p.x - 2.0 * co.x), -p.y + 2.0 * co.y);
|
|
507
|
+
float d1 = ((co.y * p1.x > co.x * p1.y) ? length(p1 - co) : abs(length(p1) - ra));
|
|
508
|
+
float d2 = ((co.y * p2.x > co.x * p2.y) ? length(p2 - co) : abs(length(p2) - ra));
|
|
509
|
+
return min(d1, d2);
|
|
510
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "onejs-sl",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "The OneJS shader language: a small typed language for per pixel programs, compiled to a portable IR and emitted as HLSL, WGSL, GLSL ES or a VM buffer",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"./vm": "./src/vm.ts",
|
|
13
13
|
"./emit/hlsl-body": "./src/emit/hlsl-body.ts",
|
|
14
14
|
"./emit/unity": "./src/emit/unity.ts",
|
|
15
|
-
"./emit/web": "./src/emit/web.ts"
|
|
15
|
+
"./emit/web": "./src/emit/web.ts",
|
|
16
|
+
"./goldens.json": "./goldens/goldens.json"
|
|
16
17
|
},
|
|
17
18
|
"sideEffects": false,
|
|
18
19
|
"files": [
|
|
@@ -20,6 +21,7 @@
|
|
|
20
21
|
"!src/**/*.test.ts",
|
|
21
22
|
"!src/fixtures",
|
|
22
23
|
"goldens/goldens.json",
|
|
24
|
+
"lib/*.hlsl",
|
|
23
25
|
"CHANGELOG.md"
|
|
24
26
|
],
|
|
25
27
|
"keywords": [
|
|
@@ -46,7 +48,8 @@
|
|
|
46
48
|
"lint": "eslint .",
|
|
47
49
|
"test": "vitest run && node quickjs/run.mjs",
|
|
48
50
|
"test:quickjs": "node quickjs/run.mjs",
|
|
49
|
-
"goldens": "node goldens/run.mjs"
|
|
51
|
+
"goldens": "node goldens/run.mjs",
|
|
52
|
+
"lib": "node lib/write.mjs"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
55
|
"@eslint/js": "^10.0.1",
|