@klex/mascot 0.3.0 → 0.5.0
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/README.md +13 -0
- package/package.json +2 -1
- package/src/emotes.ts +3 -1
- package/src/eyes.tsx +13 -19
- package/src/glide.ts +108 -21
- package/src/index.ts +11 -0
- package/src/inflate/atlas.ts +74 -0
- package/src/inflate/params.ts +302 -0
- package/src/inflate/renderer.ts +762 -0
- package/src/inflate/shaders.ts +447 -0
- package/src/inflate/shared.ts +39 -0
- package/src/inflate/support.ts +54 -0
- package/src/inflate/turn.ts +198 -0
- package/src/inflate/use-inflated-body.ts +229 -0
- package/src/klex.tsx +145 -33
- package/src/outline.ts +56 -0
- package/src/presets.ts +35 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { bodyContour } from '../geometry';
|
|
2
|
+
import type { BodyShape, InflateBump, InflateProfile } from '../presets';
|
|
3
|
+
|
|
4
|
+
export type KlexMaterial = 'plush' | 'rubber' | 'jelly';
|
|
5
|
+
|
|
6
|
+
export const MATERIALS = [
|
|
7
|
+
{ id: 'plush', name: 'Plush', description: 'Velvet sheen and fine fur.' },
|
|
8
|
+
{
|
|
9
|
+
id: 'rubber',
|
|
10
|
+
name: 'Rubber',
|
|
11
|
+
description: 'Soft-touch with a broad gloss.',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
id: 'jelly',
|
|
15
|
+
name: 'Jelly',
|
|
16
|
+
description: 'Glossy and slightly translucent.',
|
|
17
|
+
},
|
|
18
|
+
] as const satisfies readonly {
|
|
19
|
+
id: KlexMaterial;
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
}[];
|
|
23
|
+
|
|
24
|
+
export const EDGE_PROFILES = [
|
|
25
|
+
{ id: 'pillow', name: 'Pillow' },
|
|
26
|
+
{ id: 'dome', name: 'Dome' },
|
|
27
|
+
{ id: 'gummy', name: 'Gummy' },
|
|
28
|
+
] as const satisfies readonly { id: InflateProfile; name: string }[];
|
|
29
|
+
|
|
30
|
+
/** Overrides for the shape's own inflate fields. */
|
|
31
|
+
export type InflateOptions = {
|
|
32
|
+
puffiness?: number;
|
|
33
|
+
edgeProfile?: InflateProfile;
|
|
34
|
+
bumps?: readonly InflateBump[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type ResolvedInflate = {
|
|
38
|
+
puffiness: number;
|
|
39
|
+
edgeProfile: InflateProfile;
|
|
40
|
+
bumps: readonly InflateBump[];
|
|
41
|
+
/** Largest inscribed radius of the rest outline. */
|
|
42
|
+
innerRadius: number;
|
|
43
|
+
/** Height at the plateau, in frame units. */
|
|
44
|
+
amplitude: number;
|
|
45
|
+
/** Distance from the edge where the profile reaches its plateau. */
|
|
46
|
+
edgeRadius: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const MAX_BUMPS = 4;
|
|
50
|
+
const PROFILE_IDS: Record<InflateProfile, number> = {
|
|
51
|
+
dome: 0,
|
|
52
|
+
pillow: 1,
|
|
53
|
+
gummy: 2,
|
|
54
|
+
};
|
|
55
|
+
export const profileId = (profile: InflateProfile) => PROFILE_IDS[profile];
|
|
56
|
+
|
|
57
|
+
const lerp = (from: number, to: number, t: number) => from + (to - from) * t;
|
|
58
|
+
const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
|
|
59
|
+
|
|
60
|
+
export function resolveInflate(
|
|
61
|
+
shape: BodyShape,
|
|
62
|
+
options: InflateOptions = {},
|
|
63
|
+
): ResolvedInflate {
|
|
64
|
+
const puffiness = clamp01(options.puffiness ?? shape.puffiness ?? 0.6);
|
|
65
|
+
const edgeProfile = options.edgeProfile ?? shape.edgeProfile ?? 'dome';
|
|
66
|
+
const bumps = (options.bumps ?? shape.bumps ?? []).slice(0, MAX_BUMPS);
|
|
67
|
+
const innerRadius = inscribedRadius(shape);
|
|
68
|
+
// Domes and pillows rise over the whole body, so narrow parts stay thinner.
|
|
69
|
+
// A gummy keeps a flat face and only rounds a band along its edge.
|
|
70
|
+
const edgeRadius =
|
|
71
|
+
edgeProfile === 'gummy'
|
|
72
|
+
? Math.min(innerRadius, lerp(4, 16, puffiness))
|
|
73
|
+
: innerRadius;
|
|
74
|
+
const amplitude =
|
|
75
|
+
edgeProfile === 'gummy'
|
|
76
|
+
? edgeRadius * lerp(0.6, 1, puffiness)
|
|
77
|
+
: innerRadius *
|
|
78
|
+
(edgeProfile === 'dome'
|
|
79
|
+
? lerp(0.2, 1.1, puffiness)
|
|
80
|
+
: lerp(0.15, 0.8, puffiness));
|
|
81
|
+
return { puffiness, edgeProfile, bumps, innerRadius, amplitude, edgeRadius };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Height over the distance from the edge, as the shader computes it. */
|
|
85
|
+
export function profileHeight(inflate: ResolvedInflate, distance: number) {
|
|
86
|
+
const t = clamp01(distance / inflate.edgeRadius);
|
|
87
|
+
const shape =
|
|
88
|
+
inflate.edgeProfile === 'pillow'
|
|
89
|
+
? 1 - (1 - t) ** 3
|
|
90
|
+
: Math.sqrt(1 - (1 - t) ** 2);
|
|
91
|
+
return inflate.amplitude * shape;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const innerRadii = new WeakMap<BodyShape, number>();
|
|
95
|
+
|
|
96
|
+
// The largest inscribed circle normalizes the profile, so every form reaches
|
|
97
|
+
// the same roundness regardless of its size.
|
|
98
|
+
export function inscribedRadius(shape: BodyShape) {
|
|
99
|
+
const cached = innerRadii.get(shape);
|
|
100
|
+
if (cached !== undefined) return cached;
|
|
101
|
+
const contour = bodyContour(shape);
|
|
102
|
+
let minX = Infinity;
|
|
103
|
+
let maxX = -Infinity;
|
|
104
|
+
let minY = Infinity;
|
|
105
|
+
let maxY = -Infinity;
|
|
106
|
+
for (const [x = 0, y = 0] of contour) {
|
|
107
|
+
minX = Math.min(minX, x);
|
|
108
|
+
maxX = Math.max(maxX, x);
|
|
109
|
+
minY = Math.min(minY, y);
|
|
110
|
+
maxY = Math.max(maxY, y);
|
|
111
|
+
}
|
|
112
|
+
let best = 0;
|
|
113
|
+
const measure = (px: number, py: number) => {
|
|
114
|
+
let inside = false;
|
|
115
|
+
let nearest = Infinity;
|
|
116
|
+
for (let i = 0; i < contour.length; i++) {
|
|
117
|
+
const [ax = 0, ay = 0] = contour[i] ?? [];
|
|
118
|
+
const [bx = ax, by = ay] = contour[(i + 1) % contour.length] ?? [];
|
|
119
|
+
if (ay > py !== by > py && px < ax + ((py - ay) / (by - ay)) * (bx - ax))
|
|
120
|
+
inside = !inside;
|
|
121
|
+
const dx = bx - ax;
|
|
122
|
+
const dy = by - ay;
|
|
123
|
+
const t = clamp01(
|
|
124
|
+
((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy || 1),
|
|
125
|
+
);
|
|
126
|
+
nearest = Math.min(
|
|
127
|
+
nearest,
|
|
128
|
+
Math.hypot(px - ax - t * dx, py - ay - t * dy),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
return inside ? nearest : 0;
|
|
132
|
+
};
|
|
133
|
+
// Coarse grid, then refine around the best candidate.
|
|
134
|
+
let bestX = (minX + maxX) / 2;
|
|
135
|
+
let bestY = (minY + maxY) / 2;
|
|
136
|
+
for (let y = minY; y <= maxY; y += 3) {
|
|
137
|
+
for (let x = minX; x <= maxX; x += 3) {
|
|
138
|
+
const radius = measure(x, y);
|
|
139
|
+
if (radius > best) {
|
|
140
|
+
best = radius;
|
|
141
|
+
bestX = x;
|
|
142
|
+
bestY = y;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (let y = bestY - 3; y <= bestY + 3; y += 0.5) {
|
|
147
|
+
for (let x = bestX - 3; x <= bestX + 3; x += 0.5) {
|
|
148
|
+
best = Math.max(best, measure(x, y));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const radius = Math.max(1, best);
|
|
152
|
+
innerRadii.set(shape, radius);
|
|
153
|
+
return radius;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export type InflateFace = { x: number; y: number; angle: number };
|
|
157
|
+
|
|
158
|
+
/** The body turns about the center of its current bounds, at depth zero. */
|
|
159
|
+
export function turnPivot(points: readonly (readonly [number, number])[]) {
|
|
160
|
+
let minX = Infinity;
|
|
161
|
+
let maxX = -Infinity;
|
|
162
|
+
let minY = Infinity;
|
|
163
|
+
let maxY = -Infinity;
|
|
164
|
+
for (const [x, y] of points) {
|
|
165
|
+
minX = Math.min(minX, x);
|
|
166
|
+
maxX = Math.max(maxX, x);
|
|
167
|
+
minY = Math.min(minY, y);
|
|
168
|
+
maxY = Math.max(maxY, y);
|
|
169
|
+
}
|
|
170
|
+
return points.length
|
|
171
|
+
? ([(minX + maxX) / 2, (minY + maxY) / 2] as const)
|
|
172
|
+
: ([80, 90] as const);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Rotate a body-space point (rig units, y down) into view space: yaw about
|
|
177
|
+
* the vertical axis, then pitch about the view's horizontal axis.
|
|
178
|
+
*/
|
|
179
|
+
export function turnPoint(
|
|
180
|
+
[x, y, z]: readonly [number, number, number],
|
|
181
|
+
[pivotX, pivotY]: readonly [number, number],
|
|
182
|
+
yaw: number,
|
|
183
|
+
pitch: number,
|
|
184
|
+
) {
|
|
185
|
+
const dx = x - pivotX;
|
|
186
|
+
const dy = y - pivotY;
|
|
187
|
+
const yawedX = dx * Math.cos(yaw) + z * Math.sin(yaw);
|
|
188
|
+
const yawedZ = -dx * Math.sin(yaw) + z * Math.cos(yaw);
|
|
189
|
+
return [
|
|
190
|
+
pivotX + yawedX,
|
|
191
|
+
pivotY + dy * Math.cos(pitch) - yawedZ * Math.sin(pitch),
|
|
192
|
+
dy * Math.sin(pitch) + yawedZ * Math.cos(pitch),
|
|
193
|
+
] as const;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Move bumps from the rest outline onto the current frame. Face bumps follow
|
|
198
|
+
* the eyes; body bumps keep their relative place in the silhouette's bounds.
|
|
199
|
+
*/
|
|
200
|
+
export function placeBumps(
|
|
201
|
+
shape: BodyShape,
|
|
202
|
+
bumps: readonly InflateBump[],
|
|
203
|
+
points: readonly (readonly [number, number])[],
|
|
204
|
+
face: InflateFace,
|
|
205
|
+
facing: number,
|
|
206
|
+
out: Float32Array,
|
|
207
|
+
offset: number,
|
|
208
|
+
) {
|
|
209
|
+
let restMinX = Infinity;
|
|
210
|
+
let restMaxX = -Infinity;
|
|
211
|
+
let restMinY = Infinity;
|
|
212
|
+
for (const [x, y] of shape.outline) {
|
|
213
|
+
restMinX = Math.min(restMinX, x);
|
|
214
|
+
restMaxX = Math.max(restMaxX, x);
|
|
215
|
+
restMinY = Math.min(restMinY, y);
|
|
216
|
+
}
|
|
217
|
+
let minX = Infinity;
|
|
218
|
+
let maxX = -Infinity;
|
|
219
|
+
let minY = Infinity;
|
|
220
|
+
let maxY = -Infinity;
|
|
221
|
+
for (const [x, y] of points) {
|
|
222
|
+
minX = Math.min(minX, x);
|
|
223
|
+
maxX = Math.max(maxX, x);
|
|
224
|
+
minY = Math.min(minY, y);
|
|
225
|
+
maxY = Math.max(maxY, y);
|
|
226
|
+
}
|
|
227
|
+
const blend = (facing + 1) / 2;
|
|
228
|
+
const cos = Math.cos((face.angle * Math.PI) / 180);
|
|
229
|
+
const sin = Math.sin((face.angle * Math.PI) / 180);
|
|
230
|
+
for (let i = 0; i < MAX_BUMPS; i++) {
|
|
231
|
+
const bump = bumps[i];
|
|
232
|
+
const base = offset + i * 4;
|
|
233
|
+
if (!bump) {
|
|
234
|
+
out.fill(0, base, base + 4);
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
let x: number;
|
|
238
|
+
let y: number;
|
|
239
|
+
if (bump.anchor === 'face') {
|
|
240
|
+
const dx = (bump.x - (80 + shape.eyeX)) * (blend * 2 - 1);
|
|
241
|
+
const dy = bump.y - shape.eyeY;
|
|
242
|
+
x = face.x + dx * cos - dy * sin;
|
|
243
|
+
y = face.y + dx * sin + dy * cos;
|
|
244
|
+
} else {
|
|
245
|
+
const u = (bump.x - restMinX) / (restMaxX - restMinX || 1);
|
|
246
|
+
const v = (bump.y - restMinY) / (128 - restMinY || 1);
|
|
247
|
+
x = minX + (u * blend + (1 - u) * (1 - blend)) * (maxX - minX);
|
|
248
|
+
y = minY + v * (maxY - minY);
|
|
249
|
+
}
|
|
250
|
+
out[base] = x;
|
|
251
|
+
out[base + 1] = y;
|
|
252
|
+
out[base + 2] = bump.radius;
|
|
253
|
+
out[base + 3] = bump.height;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Sample the periodic B-spline the SVG body draws, as a flat vertex list. */
|
|
258
|
+
export function tessellate(
|
|
259
|
+
points: readonly (readonly [number, number])[],
|
|
260
|
+
out: Float32Array,
|
|
261
|
+
offset: number,
|
|
262
|
+
samples = segmentSamples(points.length),
|
|
263
|
+
) {
|
|
264
|
+
const count = points.length;
|
|
265
|
+
let index = offset;
|
|
266
|
+
for (let i = 0; i < count; i++) {
|
|
267
|
+
const p0 = points[(i + count - 1) % count];
|
|
268
|
+
const p1 = points[i];
|
|
269
|
+
const p2 = points[(i + 1) % count];
|
|
270
|
+
const p3 = points[(i + 2) % count];
|
|
271
|
+
if (!p0 || !p1 || !p2 || !p3) continue;
|
|
272
|
+
for (let s = 0; s < samples; s++) {
|
|
273
|
+
const t = s / samples;
|
|
274
|
+
const t2 = t * t;
|
|
275
|
+
const t3 = t2 * t;
|
|
276
|
+
const w0 = (1 - t) ** 3 / 6;
|
|
277
|
+
const w1 = (3 * t3 - 6 * t2 + 4) / 6;
|
|
278
|
+
const w2 = (-3 * t3 + 3 * t2 + 3 * t + 1) / 6;
|
|
279
|
+
const w3 = t3 / 6;
|
|
280
|
+
out[index++] = p0[0] * w0 + p1[0] * w1 + p2[0] * w2 + p3[0] * w3;
|
|
281
|
+
out[index++] = p0[1] * w0 + p1[1] * w1 + p2[1] * w2 + p3[1] * w3;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return (index - offset) / 2;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Dense outlines need fewer samples per span for the same smoothness. */
|
|
288
|
+
export function segmentSamples(pointCount: number) {
|
|
289
|
+
return Math.min(8, Math.max(2, Math.ceil(128 / Math.max(1, pointCount))));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function hexToLinear(color: string): [number, number, number] {
|
|
293
|
+
const match = /^#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(color.trim());
|
|
294
|
+
const channels = match
|
|
295
|
+
? [match[1], match[2], match[3]].map(
|
|
296
|
+
(hex) => Number.parseInt(hex ?? 'ff', 16) / 255,
|
|
297
|
+
)
|
|
298
|
+
: [1, 1, 1];
|
|
299
|
+
return channels.map((value) =>
|
|
300
|
+
value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4,
|
|
301
|
+
) as [number, number, number];
|
|
302
|
+
}
|