@klex/mascot 0.4.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 +1 -1
- package/src/emotes.ts +3 -1
- 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 +123 -33
- package/src/presets.ts +35 -0
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
// Every pass after the mask draws one instanced quad per dirty atlas tile.
|
|
2
|
+
// Per-tile data rides along as instance attributes, so a whole scene costs a
|
|
3
|
+
// fixed number of draw calls regardless of how many characters it shows.
|
|
4
|
+
|
|
5
|
+
export const TILE_VERTEX = `#version 300 es
|
|
6
|
+
layout(location = 0) in vec2 aCorner;
|
|
7
|
+
layout(location = 1) in vec4 aTile; // column, row, lift, facing
|
|
8
|
+
layout(location = 2) in vec4 aLook; // linear rgb, material
|
|
9
|
+
layout(location = 3) in vec4 aProfile; // amplitude, edge radius, profile, inner radius
|
|
10
|
+
layout(location = 4) in vec4 aBump0;
|
|
11
|
+
layout(location = 5) in vec4 aBump1;
|
|
12
|
+
layout(location = 6) in vec4 aBump2;
|
|
13
|
+
layout(location = 7) in vec4 aBump3;
|
|
14
|
+
layout(location = 8) in vec4 aTurn; // yaw, pitch, pivot x, pivot y
|
|
15
|
+
layout(location = 9) in vec4 aHead; // yaw, pitch, neck y, pivot x
|
|
16
|
+
uniform vec2 uTiles;
|
|
17
|
+
flat out vec4 vTile;
|
|
18
|
+
flat out vec4 vLook;
|
|
19
|
+
flat out vec4 vProfile;
|
|
20
|
+
flat out vec4 vBump0;
|
|
21
|
+
flat out vec4 vBump1;
|
|
22
|
+
flat out vec4 vBump2;
|
|
23
|
+
flat out vec4 vBump3;
|
|
24
|
+
flat out vec4 vTurn;
|
|
25
|
+
flat out vec4 vHead;
|
|
26
|
+
void main() {
|
|
27
|
+
vTile = aTile;
|
|
28
|
+
vLook = aLook;
|
|
29
|
+
vProfile = aProfile;
|
|
30
|
+
vBump0 = aBump0;
|
|
31
|
+
vBump1 = aBump1;
|
|
32
|
+
vBump2 = aBump2;
|
|
33
|
+
vBump3 = aBump3;
|
|
34
|
+
vTurn = aTurn;
|
|
35
|
+
vHead = aHead;
|
|
36
|
+
gl_Position = vec4((aTile.xy + aCorner) / uTiles * 2.0 - 1.0, 0.0, 1.0);
|
|
37
|
+
}`;
|
|
38
|
+
|
|
39
|
+
export const MASK_VERTEX = `#version 300 es
|
|
40
|
+
layout(location = 0) in vec2 aPosition;
|
|
41
|
+
uniform vec4 uRegion;
|
|
42
|
+
void main() {
|
|
43
|
+
vec2 n = (aPosition - uRegion.xy) / uRegion.zw;
|
|
44
|
+
gl_Position = vec4(n.x * 2.0 - 1.0, 1.0 - n.y * 2.0, 0.0, 1.0);
|
|
45
|
+
}`;
|
|
46
|
+
|
|
47
|
+
// Paired with ONE_MINUS_DST_COLOR blending, each covering fan triangle flips
|
|
48
|
+
// the pixel, which fills any simple polygon without a stencil buffer.
|
|
49
|
+
export const MASK_FRAGMENT = `#version 300 es
|
|
50
|
+
precision mediump float;
|
|
51
|
+
out vec4 outColor;
|
|
52
|
+
void main() { outColor = vec4(1.0); }`;
|
|
53
|
+
|
|
54
|
+
// Seeds sit on the antialiased edge with subpixel precision: the coverage
|
|
55
|
+
// gradient gives the edge normal, the coverage itself its offset.
|
|
56
|
+
export const SEED_FRAGMENT = `#version 300 es
|
|
57
|
+
precision highp float;
|
|
58
|
+
uniform sampler2D uMask;
|
|
59
|
+
uniform vec2 uSize;
|
|
60
|
+
uniform vec2 uTileSize;
|
|
61
|
+
flat in vec4 vTile;
|
|
62
|
+
out vec2 outSeed;
|
|
63
|
+
float coverage(vec2 p) { return texture(uMask, p / uSize).r; }
|
|
64
|
+
void main() {
|
|
65
|
+
vec2 p = gl_FragCoord.xy;
|
|
66
|
+
float c = coverage(p);
|
|
67
|
+
float l = coverage(p - vec2(1.0, 0.0));
|
|
68
|
+
float r = coverage(p + vec2(1.0, 0.0));
|
|
69
|
+
float d = coverage(p - vec2(0.0, 1.0));
|
|
70
|
+
float u = coverage(p + vec2(0.0, 1.0));
|
|
71
|
+
float lowest = min(min(l, r), min(d, u));
|
|
72
|
+
if (c <= 0.01 || (c >= 0.99 && lowest >= 0.01)) {
|
|
73
|
+
outSeed = vec2(-1.0);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
vec2 g = vec2(r - l, u - d);
|
|
77
|
+
float len = length(g);
|
|
78
|
+
vec2 n = len > 1e-4 ? g / len : vec2(0.0);
|
|
79
|
+
outSeed = p - vTile.xy * uTileSize - n * (c - 0.5);
|
|
80
|
+
}`;
|
|
81
|
+
|
|
82
|
+
export const JFA_FRAGMENT = `#version 300 es
|
|
83
|
+
precision highp float;
|
|
84
|
+
uniform highp sampler2D uSeeds;
|
|
85
|
+
uniform vec2 uTileSize;
|
|
86
|
+
uniform int uStep;
|
|
87
|
+
flat in vec4 vTile;
|
|
88
|
+
out vec2 outSeed;
|
|
89
|
+
void main() {
|
|
90
|
+
ivec2 p = ivec2(gl_FragCoord.xy);
|
|
91
|
+
ivec2 lo = ivec2(vTile.xy * uTileSize);
|
|
92
|
+
ivec2 hi = lo + ivec2(uTileSize) - 1;
|
|
93
|
+
vec2 local = gl_FragCoord.xy - vec2(lo);
|
|
94
|
+
vec2 best = vec2(-1.0);
|
|
95
|
+
float bestDistance = 1e20;
|
|
96
|
+
for (int y = -1; y <= 1; y++) {
|
|
97
|
+
for (int x = -1; x <= 1; x++) {
|
|
98
|
+
ivec2 q = p + ivec2(x, y) * uStep;
|
|
99
|
+
if (any(lessThan(q, lo)) || any(greaterThan(q, hi))) continue;
|
|
100
|
+
vec2 seed = texelFetch(uSeeds, q, 0).xy;
|
|
101
|
+
if (seed.x < 0.0) continue;
|
|
102
|
+
vec2 delta = seed - local;
|
|
103
|
+
float distance = dot(delta, delta);
|
|
104
|
+
if (distance < bestDistance) {
|
|
105
|
+
bestDistance = distance;
|
|
106
|
+
best = seed;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
outSeed = best;
|
|
111
|
+
}`;
|
|
112
|
+
|
|
113
|
+
export const HEIGHT_FRAGMENT = `#version 300 es
|
|
114
|
+
precision highp float;
|
|
115
|
+
uniform highp sampler2D uSeeds;
|
|
116
|
+
uniform sampler2D uMask;
|
|
117
|
+
uniform vec2 uSize;
|
|
118
|
+
uniform vec2 uTileSize;
|
|
119
|
+
uniform vec4 uRegion;
|
|
120
|
+
flat in vec4 vProfile;
|
|
121
|
+
flat in vec4 vTile;
|
|
122
|
+
flat in vec4 vBump0;
|
|
123
|
+
flat in vec4 vBump1;
|
|
124
|
+
flat in vec4 vBump2;
|
|
125
|
+
flat in vec4 vBump3;
|
|
126
|
+
out vec2 outHeight;
|
|
127
|
+
float bump(vec4 b, vec2 point, float inside) {
|
|
128
|
+
if (b.z <= 0.0) return 0.0;
|
|
129
|
+
vec2 q = (point - b.xy) / b.z;
|
|
130
|
+
return b.w * exp(-2.0 * dot(q, q)) * smoothstep(0.0, b.z * 0.6, inside);
|
|
131
|
+
}
|
|
132
|
+
void main() {
|
|
133
|
+
vec2 origin = vTile.xy * uTileSize;
|
|
134
|
+
vec2 local = gl_FragCoord.xy - origin;
|
|
135
|
+
float c = texture(uMask, gl_FragCoord.xy / uSize).r;
|
|
136
|
+
vec2 seed = texelFetch(uSeeds, ivec2(gl_FragCoord.xy), 0).xy;
|
|
137
|
+
float unitsPerPixel = uRegion.z / uTileSize.x;
|
|
138
|
+
// Inside pixels the flood never reached lie beyond the plateau.
|
|
139
|
+
float distance = seed.x < 0.0 ? 1e4 : length(seed - local) * unitsPerPixel;
|
|
140
|
+
float signedDistance = c >= 0.5 ? distance : -distance;
|
|
141
|
+
float t = clamp(signedDistance / vProfile.y, 0.0, 1.0);
|
|
142
|
+
float h = vProfile.z > 0.5 && vProfile.z < 1.5
|
|
143
|
+
? 1.0 - pow(1.0 - t, 3.0)
|
|
144
|
+
: sqrt(max(0.0, 1.0 - (1.0 - t) * (1.0 - t)));
|
|
145
|
+
h *= vProfile.x;
|
|
146
|
+
vec2 point = vec2(
|
|
147
|
+
uRegion.x + local.x / uTileSize.x * uRegion.z,
|
|
148
|
+
uRegion.y + (1.0 - local.y / uTileSize.y) * uRegion.w
|
|
149
|
+
);
|
|
150
|
+
float inside = max(signedDistance, 0.0);
|
|
151
|
+
h += bump(vBump0, point, inside) + bump(vBump1, point, inside)
|
|
152
|
+
+ bump(vBump2, point, inside) + bump(vBump3, point, inside);
|
|
153
|
+
outHeight = vec2(max(h, 0.0), clamp(signedDistance, -64.0, 64.0));
|
|
154
|
+
}`;
|
|
155
|
+
|
|
156
|
+
// Separable blur rounds off the medial-axis crease of the distance field.
|
|
157
|
+
export const BLUR_FRAGMENT = `#version 300 es
|
|
158
|
+
precision highp float;
|
|
159
|
+
uniform highp sampler2D uSource;
|
|
160
|
+
uniform vec2 uTileSize;
|
|
161
|
+
uniform vec2 uDirection;
|
|
162
|
+
uniform float uSigma;
|
|
163
|
+
flat in vec4 vTile;
|
|
164
|
+
out vec2 outHeight;
|
|
165
|
+
void main() {
|
|
166
|
+
vec2 lo = vTile.xy * uTileSize;
|
|
167
|
+
vec2 hi = lo + uTileSize - 1.0;
|
|
168
|
+
vec2 p = floor(gl_FragCoord.xy);
|
|
169
|
+
vec2 sum = vec2(0.0);
|
|
170
|
+
float total = 0.0;
|
|
171
|
+
for (int i = -6; i <= 6; i++) {
|
|
172
|
+
float w = exp(-float(i * i) / (2.0 * uSigma * uSigma));
|
|
173
|
+
vec2 q = clamp(p + uDirection * float(i), lo, hi);
|
|
174
|
+
sum += texelFetch(uSource, ivec2(q), 0).xy * w;
|
|
175
|
+
total += w;
|
|
176
|
+
}
|
|
177
|
+
outHeight = sum / total;
|
|
178
|
+
}`;
|
|
179
|
+
|
|
180
|
+
export const SHADE_FRAGMENT = `#version 300 es
|
|
181
|
+
precision highp float;
|
|
182
|
+
uniform sampler2D uMask;
|
|
183
|
+
uniform sampler2D uHeight;
|
|
184
|
+
uniform vec2 uSize;
|
|
185
|
+
uniform vec2 uTileSize;
|
|
186
|
+
uniform vec4 uRegion;
|
|
187
|
+
flat in vec4 vTile;
|
|
188
|
+
flat in vec4 vLook;
|
|
189
|
+
flat in vec4 vProfile;
|
|
190
|
+
flat in vec4 vTurn;
|
|
191
|
+
flat in vec4 vHead;
|
|
192
|
+
out vec4 outColor;
|
|
193
|
+
|
|
194
|
+
float hash(vec2 p) {
|
|
195
|
+
p = fract(p * vec2(123.34, 456.21));
|
|
196
|
+
p += dot(p, p + 45.32);
|
|
197
|
+
return fract(p.x * p.y);
|
|
198
|
+
}
|
|
199
|
+
float noise(vec2 p) {
|
|
200
|
+
vec2 i = floor(p);
|
|
201
|
+
vec2 f = fract(p);
|
|
202
|
+
vec2 u = f * f * (3.0 - 2.0 * f);
|
|
203
|
+
return mix(
|
|
204
|
+
mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x),
|
|
205
|
+
mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x),
|
|
206
|
+
u.y
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
float ggx(float nh, float roughness) {
|
|
210
|
+
float a = roughness * roughness;
|
|
211
|
+
float a2 = a * a;
|
|
212
|
+
float d = nh * nh * (a2 - 1.0) + 1.0;
|
|
213
|
+
return a2 / (3.14159 * d * d);
|
|
214
|
+
}
|
|
215
|
+
vec3 shoulder(vec3 c) {
|
|
216
|
+
return mix(c, 0.8 + 0.2 * (1.0 - exp(-(c - 0.8) / 0.2)), step(0.8, c));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const float FUR = 2.6;
|
|
220
|
+
// Both sides bulge alike, so a character turned round shows a full face.
|
|
221
|
+
const float BACK = 1.0;
|
|
222
|
+
const int STEPS = 96;
|
|
223
|
+
|
|
224
|
+
// Rig units (y down) to this tile's texture coordinates.
|
|
225
|
+
vec2 rigToUv(vec2 q) {
|
|
226
|
+
vec2 local = vec2(
|
|
227
|
+
(q.x - uRegion.x) / uRegion.z,
|
|
228
|
+
1.0 - (q.y - uRegion.y) / uRegion.w
|
|
229
|
+
) * uTileSize;
|
|
230
|
+
local = clamp(local, vec2(1.0), uTileSize - 1.0);
|
|
231
|
+
return (vTile.xy * uTileSize + local) / uSize;
|
|
232
|
+
}
|
|
233
|
+
// Body to view: yaw about the vertical axis, then pitch about the view's x
|
|
234
|
+
// axis, so looking up tilts up whichever way the body faces.
|
|
235
|
+
vec3 toView(vec3 v, vec4 r) {
|
|
236
|
+
vec3 a = vec3(v.x * r.y + v.z * r.x, v.y, -v.x * r.x + v.z * r.y);
|
|
237
|
+
return vec3(a.x, a.y * r.w - a.z * r.z, a.y * r.z + a.z * r.w);
|
|
238
|
+
}
|
|
239
|
+
vec3 toBody(vec3 v, vec4 r) {
|
|
240
|
+
vec3 a = vec3(v.x, v.y * r.w + v.z * r.z, -v.y * r.z + v.z * r.w);
|
|
241
|
+
return vec3(a.x * r.y - a.z * r.x, a.y, a.x * r.x + a.z * r.y);
|
|
242
|
+
}
|
|
243
|
+
// How solidly a body-space point sits inside the stuffed volume.
|
|
244
|
+
float occupancy(vec3 p) {
|
|
245
|
+
vec2 q = rigToUv(p.xy);
|
|
246
|
+
float h = texture(uHeight, q).x;
|
|
247
|
+
return texture(uMask, q).r
|
|
248
|
+
* smoothstep(-0.6, 0.6, h - p.z)
|
|
249
|
+
* smoothstep(-0.6, 0.6, p.z + h * BACK);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
void main() {
|
|
253
|
+
vec2 uv = gl_FragCoord.xy / uSize;
|
|
254
|
+
float coverage = texture(uMask, uv).r;
|
|
255
|
+
float material = vLook.w;
|
|
256
|
+
vec2 local = gl_FragCoord.xy - vTile.xy * uTileSize;
|
|
257
|
+
vec2 point = vec2(
|
|
258
|
+
uRegion.x + local.x / uTileSize.x * uRegion.z,
|
|
259
|
+
uRegion.y + (1.0 - local.y / uTileSize.y) * uRegion.w
|
|
260
|
+
);
|
|
261
|
+
float lift = vTile.z;
|
|
262
|
+
// The head's turn fades from the eyes down to the sole, so neckless bodies
|
|
263
|
+
// twist like jelly. A turn about the vertical axis keeps every row in
|
|
264
|
+
// place, so each row can twist by its own angle.
|
|
265
|
+
// The head turns about its own pivot first, then the whole body about the
|
|
266
|
+
// body's center, so a body turned round keeps its head on its shoulders.
|
|
267
|
+
float head = 1.0 - smoothstep(vHead.z, 126.0, point.y);
|
|
268
|
+
vec2 headAngles = vHead.xy * head;
|
|
269
|
+
// sin and cos of yaw, then of pitch.
|
|
270
|
+
vec4 rotation = vec4(sin(vTurn.x), cos(vTurn.x), sin(vTurn.y), cos(vTurn.y));
|
|
271
|
+
vec4 headRotation = vec4(
|
|
272
|
+
sin(headAngles.x), cos(headAngles.x), sin(headAngles.y), cos(headAngles.y)
|
|
273
|
+
);
|
|
274
|
+
bool turned =
|
|
275
|
+
abs(vTurn.x) + abs(vTurn.y) + abs(headAngles.x) + abs(headAngles.y) > 1e-3;
|
|
276
|
+
vec3 pivot = vec3(vTurn.zw, 0.0);
|
|
277
|
+
vec3 headPivot = vec3(vHead.w, vTurn.w, 0.0);
|
|
278
|
+
vec3 hit = vec3(point, 0.0);
|
|
279
|
+
// Signed distance to the silhouette that the fur reaches across.
|
|
280
|
+
float edge = 0.0;
|
|
281
|
+
if (turned) {
|
|
282
|
+
// March a view ray through the turned body: front at +h, back at -h·BACK.
|
|
283
|
+
float depth = vProfile.x * 1.15 + 8.0;
|
|
284
|
+
vec3 origin = toBody(vec3(point, depth) - pivot, rotation) + pivot;
|
|
285
|
+
origin = toBody(origin - headPivot, headRotation) + headPivot;
|
|
286
|
+
vec3 direction = toBody(toBody(vec3(0.0, 0.0, -1.0), rotation), headRotation);
|
|
287
|
+
float far = 2.0 * depth;
|
|
288
|
+
float best = 0.0;
|
|
289
|
+
float t = 0.0;
|
|
290
|
+
float previous = 0.0;
|
|
291
|
+
bool found = false;
|
|
292
|
+
// The closest miss, for fur that reaches past the turned silhouette.
|
|
293
|
+
float gap = 1e4;
|
|
294
|
+
vec3 closest = origin;
|
|
295
|
+
for (int i = 0; i < STEPS; i++) {
|
|
296
|
+
vec3 p = origin + direction * t;
|
|
297
|
+
vec2 q = rigToUv(p.xy);
|
|
298
|
+
vec2 field = texture(uHeight, q).xy;
|
|
299
|
+
float inside = texture(uMask, q).r
|
|
300
|
+
* smoothstep(-0.6, 0.6, field.x - p.z)
|
|
301
|
+
* smoothstep(-0.6, 0.6, p.z + field.x * BACK);
|
|
302
|
+
best = max(best, inside);
|
|
303
|
+
float outside = max(max(-field.y, p.z - field.x), -p.z - field.x * BACK);
|
|
304
|
+
if (outside < gap) {
|
|
305
|
+
gap = outside;
|
|
306
|
+
closest = p;
|
|
307
|
+
}
|
|
308
|
+
if (inside > 0.5) {
|
|
309
|
+
found = true;
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
// Step by the distance to the volume, so thin parts like a tail seen
|
|
313
|
+
// straight on are not skipped between two samples.
|
|
314
|
+
previous = t;
|
|
315
|
+
t += clamp(outside * 0.8, 0.3, 2.0);
|
|
316
|
+
if (t > far) break;
|
|
317
|
+
}
|
|
318
|
+
if (!found && best <= 0.004 && (material >= 0.5 || gap > FUR)) {
|
|
319
|
+
outColor = vec4(0.0);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (found) {
|
|
323
|
+
// Bisect the crossing for a stable surface point.
|
|
324
|
+
float lo = previous;
|
|
325
|
+
float hi = t;
|
|
326
|
+
for (int i = 0; i < 6; i++) {
|
|
327
|
+
float mid = (lo + hi) * 0.5;
|
|
328
|
+
if (occupancy(origin + direction * mid) > 0.5) hi = mid;
|
|
329
|
+
else lo = mid;
|
|
330
|
+
}
|
|
331
|
+
t = hi;
|
|
332
|
+
}
|
|
333
|
+
hit = found ? origin + direction * t : closest;
|
|
334
|
+
edge = found ? FUR : -gap;
|
|
335
|
+
coverage = clamp(best * 2.0, 0.0, 1.0);
|
|
336
|
+
uv = rigToUv(hit.xy);
|
|
337
|
+
}
|
|
338
|
+
// x: height, y: signed distance from the edge, positive inside.
|
|
339
|
+
vec2 here = texture(uHeight, uv).xy;
|
|
340
|
+
if (!turned) edge = here.y;
|
|
341
|
+
// Plush hairs reach past the silhouette.
|
|
342
|
+
if (!turned && coverage <= 0.002 && (material >= 0.5 || here.y < -FUR)) {
|
|
343
|
+
outColor = vec4(0.0);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
// Fur and grain stick to the body, not to the screen.
|
|
347
|
+
vec2 surface = turned ? hit.xy : point;
|
|
348
|
+
|
|
349
|
+
// The height texture has half the resolution; step one of its texels.
|
|
350
|
+
vec2 texel = 2.0 / uSize;
|
|
351
|
+
float unitsPerTexel = 2.0 * uRegion.z / uTileSize.x;
|
|
352
|
+
vec2 hl = texture(uHeight, uv - vec2(texel.x, 0.0)).xy;
|
|
353
|
+
vec2 hr = texture(uHeight, uv + vec2(texel.x, 0.0)).xy;
|
|
354
|
+
vec2 hd = texture(uHeight, uv - vec2(0.0, texel.y)).xy;
|
|
355
|
+
vec2 hu = texture(uHeight, uv + vec2(0.0, texel.y)).xy;
|
|
356
|
+
vec2 gradient = vec2(hr.x - hl.x, hu.x - hd.x) / (2.0 * unitsPerTexel);
|
|
357
|
+
vec2 inward = vec2(hr.y - hl.y, hu.y - hd.y);
|
|
358
|
+
vec2 outward = -inward / max(length(inward), 1e-4);
|
|
359
|
+
|
|
360
|
+
float fiber = 0.0;
|
|
361
|
+
float hairs = 0.0;
|
|
362
|
+
if (material < 0.5) {
|
|
363
|
+
// Fibers are combed away from the nearest edge, like a plush toy.
|
|
364
|
+
vec2 p = vec2(surface.x, -surface.y);
|
|
365
|
+
vec2 across = vec2(-outward.y, outward.x);
|
|
366
|
+
float side = dot(p, across);
|
|
367
|
+
// Depth breaks the strands up where the side of a turned body shows.
|
|
368
|
+
vec2 q = vec2(side * 3.2, (dot(p, outward) + hit.z * 1.3) * 0.55);
|
|
369
|
+
fiber = noise(q) * 0.55 + noise(q * vec2(2.1, 1.7) + 9.1) * 0.3
|
|
370
|
+
+ noise(p * 2.4) * 0.15;
|
|
371
|
+
gradient += across * (noise(q + vec2(0.5, 0.0)) - noise(q - vec2(0.5, 0.0))) * 1.4;
|
|
372
|
+
// Each strand has its own length; tips thin out.
|
|
373
|
+
float strand = FUR * (0.3 + 0.7 * noise(vec2(side * 2.2, 3.7)));
|
|
374
|
+
float along = clamp((strand + edge) / strand, 0.0, 1.0);
|
|
375
|
+
hairs = smoothstep(0.3, 0.62, fiber) * smoothstep(0.0, 0.45, along);
|
|
376
|
+
} else if (material < 1.5) {
|
|
377
|
+
vec2 q = surface * 3.0;
|
|
378
|
+
gradient += (vec2(noise(q), noise(q + 17.0)) - 0.5) * 0.06;
|
|
379
|
+
}
|
|
380
|
+
vec3 n = normalize(vec3(-gradient, 1.0));
|
|
381
|
+
if (turned) {
|
|
382
|
+
// Body normals use y down; the front faces +z, the back -z.
|
|
383
|
+
// Blend front and back across the seam so the rim stays round.
|
|
384
|
+
vec3 front = vec3(-gradient.x, gradient.y, 1.0);
|
|
385
|
+
vec3 back = vec3(-gradient.x * BACK, gradient.y * BACK, -1.0);
|
|
386
|
+
float seam = max(1.0, here.x * 0.35);
|
|
387
|
+
vec3 body = mix(back, front, smoothstep(-seam, seam, hit.z));
|
|
388
|
+
vec3 view = normalize(toView(toView(body, headRotation), rotation));
|
|
389
|
+
n = vec3(view.x, -view.y, view.z);
|
|
390
|
+
}
|
|
391
|
+
vec3 v = vec3(0.0, 0.0, 1.0);
|
|
392
|
+
vec3 key = normalize(vec3(-0.45, 0.62, 0.64));
|
|
393
|
+
vec3 fill = normalize(vec3(0.7, -0.1, 0.7));
|
|
394
|
+
vec3 base = vLook.rgb;
|
|
395
|
+
float nv = max(n.z, 0.0);
|
|
396
|
+
float thickness = clamp(here.x / max(vProfile.x, 1e-3), 0.0, 1.0);
|
|
397
|
+
|
|
398
|
+
// Sky above, the floor's bounce below.
|
|
399
|
+
vec3 ambient = mix(vec3(0.36, 0.34, 0.33), vec3(0.62, 0.65, 0.7), n.y * 0.5 + 0.5);
|
|
400
|
+
float groundDistance = max(0.0, 128.0 - point.y + lift);
|
|
401
|
+
float contact = (1.0 - smoothstep(0.0, 18.0, groundDistance)) * exp(-lift / 24.0);
|
|
402
|
+
float occlusion = 1.0 - 0.45 * contact * (0.55 + 0.45 * max(0.0, -n.y));
|
|
403
|
+
|
|
404
|
+
vec3 color;
|
|
405
|
+
float alpha = coverage;
|
|
406
|
+
if (material < 0.5) {
|
|
407
|
+
vec3 albedo = base * (0.78 + 0.44 * fiber);
|
|
408
|
+
float diffuse = max((dot(n, key) + 0.6) / 1.6, 0.0);
|
|
409
|
+
// Fur packs denser and darker towards the silhouette.
|
|
410
|
+
float depth = mix(0.58, 1.0, smoothstep(-1.0, 7.0, here.y));
|
|
411
|
+
color = albedo * depth * (ambient * 0.72 + vec3(1.0, 0.97, 0.92) * 0.8 * diffuse
|
|
412
|
+
+ 0.1 * max(dot(n, fill), 0.0));
|
|
413
|
+
// Fiber tips catch the key light along the rim.
|
|
414
|
+
vec2 keySide = normalize(key.xy);
|
|
415
|
+
float tips = pow(1.0 - nv, 1.6) * max(dot(normalize(n.xy + 1e-4), keySide), 0.0);
|
|
416
|
+
color += mix(base, vec3(1.0), 0.6) * tips * 0.3 * (0.4 + fiber);
|
|
417
|
+
alpha = turned
|
|
418
|
+
? max(coverage, hairs)
|
|
419
|
+
: max(coverage * smoothstep(-0.4, 0.9, here.y + (fiber - 0.5) * 1.6), hairs);
|
|
420
|
+
} else if (material < 1.5) {
|
|
421
|
+
// Soft-touch rubber: Lambert with a broad, low sheen.
|
|
422
|
+
float diffuse = max(dot(n, key), 0.0);
|
|
423
|
+
vec3 h = normalize(key + v);
|
|
424
|
+
float fresnel = 0.04 + 0.96 * pow(1.0 - nv, 5.0);
|
|
425
|
+
float specular = ggx(max(dot(n, h), 0.0), 0.48) * 0.16 * diffuse;
|
|
426
|
+
color = base * (ambient * 0.82 + vec3(1.0, 0.97, 0.93) * 0.8 * diffuse + 0.14 * max(dot(n, fill), 0.0));
|
|
427
|
+
color += vec3(specular) + fresnel * 0.12 * vec3(0.8, 0.85, 0.95);
|
|
428
|
+
} else {
|
|
429
|
+
// Jelly: thin parts glow brighter, thick parts deepen the color.
|
|
430
|
+
vec3 deep = pow(base, vec3(1.7)) * 0.9;
|
|
431
|
+
vec3 bright = mix(base, vec3(1.0), 0.25);
|
|
432
|
+
vec3 body = mix(bright, deep, smoothstep(0.0, 1.0, thickness));
|
|
433
|
+
float diffuse = max(dot(n, key), 0.0);
|
|
434
|
+
vec3 h = normalize(key + v);
|
|
435
|
+
float fresnel = 0.05 + 0.95 * pow(1.0 - nv, 5.0);
|
|
436
|
+
float specular = ggx(max(dot(n, h), 0.0), 0.14) * 0.22;
|
|
437
|
+
vec3 r = reflect(-v, n);
|
|
438
|
+
float softbox = smoothstep(0.55, 0.75, r.y) * smoothstep(-0.65, -0.2, r.x) * smoothstep(0.25, -0.05, r.x);
|
|
439
|
+
color = body * (ambient * 0.75 + 0.55 * diffuse + 0.25);
|
|
440
|
+
color += base * 0.35 * pow(1.0 - thickness, 2.0);
|
|
441
|
+
color += vec3(specular + softbox * 0.55) + fresnel * 0.35 * vec3(0.85, 0.9, 1.0);
|
|
442
|
+
alpha *= mix(0.8, 0.97, thickness);
|
|
443
|
+
}
|
|
444
|
+
color *= occlusion;
|
|
445
|
+
color = pow(shoulder(max(color, 0.0)), vec3(1.0 / 2.2));
|
|
446
|
+
outColor = vec4(color * alpha, alpha);
|
|
447
|
+
}`;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One instance for every user on the page, created on first use and disposed
|
|
3
|
+
* after the last release. Browsers cap live WebGL contexts at about 16, so all
|
|
4
|
+
* inflated characters draw through a single renderer.
|
|
5
|
+
*/
|
|
6
|
+
export function createShared<T>(
|
|
7
|
+
create: () => T | null,
|
|
8
|
+
dispose: (value: T) => void,
|
|
9
|
+
) {
|
|
10
|
+
let value: T | null = null;
|
|
11
|
+
let users = 0;
|
|
12
|
+
return {
|
|
13
|
+
acquire(): T | null {
|
|
14
|
+
value ??= create();
|
|
15
|
+
if (value) users++;
|
|
16
|
+
return value;
|
|
17
|
+
},
|
|
18
|
+
release(released: T) {
|
|
19
|
+
if (released !== value) return;
|
|
20
|
+
users = Math.max(0, users - 1);
|
|
21
|
+
if (users) return;
|
|
22
|
+
value = null;
|
|
23
|
+
dispose(released);
|
|
24
|
+
},
|
|
25
|
+
/** Drop a broken instance, e.g. after context loss, without disposing users. */
|
|
26
|
+
invalidate(broken: T) {
|
|
27
|
+
if (broken !== value) return;
|
|
28
|
+
value = null;
|
|
29
|
+
users = 0;
|
|
30
|
+
dispose(broken);
|
|
31
|
+
},
|
|
32
|
+
get users() {
|
|
33
|
+
return users;
|
|
34
|
+
},
|
|
35
|
+
get current() {
|
|
36
|
+
return value;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type KlexAppearance = 'flat' | 'inflated';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The flat SVG stays the default and the fallback. The inflated body needs
|
|
5
|
+
* WebGL2 with float render targets, an HTML layout to host its canvas, and
|
|
6
|
+
* motion: a frozen frame would still cost a GPU context per page.
|
|
7
|
+
*/
|
|
8
|
+
export function chooseAppearance({
|
|
9
|
+
requested,
|
|
10
|
+
reducedMotion,
|
|
11
|
+
layout,
|
|
12
|
+
supported,
|
|
13
|
+
}: {
|
|
14
|
+
requested: KlexAppearance;
|
|
15
|
+
reducedMotion: boolean;
|
|
16
|
+
layout: 'track' | 'avatar' | 'svg';
|
|
17
|
+
supported: boolean;
|
|
18
|
+
}): KlexAppearance {
|
|
19
|
+
if (requested !== 'inflated') return 'flat';
|
|
20
|
+
if (reducedMotion || layout === 'svg' || !supported) return 'flat';
|
|
21
|
+
return 'inflated';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type WebGLProbe = () => WebGL2RenderingContext | null;
|
|
25
|
+
|
|
26
|
+
let cachedSupport: boolean | undefined;
|
|
27
|
+
|
|
28
|
+
export function detectInflateSupport(probe: WebGLProbe = defaultProbe) {
|
|
29
|
+
if (cachedSupport !== undefined) return cachedSupport;
|
|
30
|
+
let supported = false;
|
|
31
|
+
try {
|
|
32
|
+
const gl = probe();
|
|
33
|
+
supported = Boolean(gl?.getExtension('EXT_color_buffer_float'));
|
|
34
|
+
gl?.getExtension('WEBGL_lose_context')?.loseContext();
|
|
35
|
+
} catch {
|
|
36
|
+
supported = false;
|
|
37
|
+
}
|
|
38
|
+
cachedSupport = supported;
|
|
39
|
+
return supported;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Forget the cached probe result. Tests and a lost context use this. */
|
|
43
|
+
export function resetInflateSupport(next?: boolean) {
|
|
44
|
+
cachedSupport = next;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function defaultProbe() {
|
|
48
|
+
if (
|
|
49
|
+
typeof document === 'undefined' ||
|
|
50
|
+
typeof WebGL2RenderingContext === 'undefined'
|
|
51
|
+
)
|
|
52
|
+
return null;
|
|
53
|
+
return document.createElement('canvas').getContext('webgl2');
|
|
54
|
+
}
|