@klex/mascot 0.4.0 → 0.6.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 +20 -5
- package/package.json +1 -1
- package/src/activity.ts +109 -28
- package/src/book.tsx +94 -0
- package/src/emotes.ts +3 -1
- package/src/extras.css +55 -1
- package/src/extras.tsx +54 -0
- package/src/geometry.ts +27 -0
- package/src/glide.ts +187 -36
- 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 +147 -34
- package/src/learning.ts +126 -0
- package/src/presets.ts +35 -0
- package/src/recalling.ts +111 -0
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
import type { BodyShape } from '../presets';
|
|
2
|
+
import {
|
|
3
|
+
type AtlasLayout,
|
|
4
|
+
atlasLayout,
|
|
5
|
+
createTileSlots,
|
|
6
|
+
REGION,
|
|
7
|
+
tileDensity,
|
|
8
|
+
} from './atlas';
|
|
9
|
+
import {
|
|
10
|
+
hexToLinear,
|
|
11
|
+
type InflateFace,
|
|
12
|
+
type KlexMaterial,
|
|
13
|
+
MAX_BUMPS,
|
|
14
|
+
placeBumps,
|
|
15
|
+
profileId,
|
|
16
|
+
type ResolvedInflate,
|
|
17
|
+
segmentSamples,
|
|
18
|
+
tessellate,
|
|
19
|
+
turnPivot,
|
|
20
|
+
} from './params';
|
|
21
|
+
import {
|
|
22
|
+
BLUR_FRAGMENT,
|
|
23
|
+
HEIGHT_FRAGMENT,
|
|
24
|
+
JFA_FRAGMENT,
|
|
25
|
+
MASK_FRAGMENT,
|
|
26
|
+
MASK_VERTEX,
|
|
27
|
+
SEED_FRAGMENT,
|
|
28
|
+
SHADE_FRAGMENT,
|
|
29
|
+
TILE_VERTEX,
|
|
30
|
+
} from './shaders';
|
|
31
|
+
import { createShared } from './shared';
|
|
32
|
+
import { headFrame } from './turn';
|
|
33
|
+
|
|
34
|
+
export type InflateFrame = {
|
|
35
|
+
points: readonly (readonly [number, number])[];
|
|
36
|
+
face: InflateFace;
|
|
37
|
+
facing: number;
|
|
38
|
+
lift: number;
|
|
39
|
+
/** Radians. Positive yaw turns the face to the viewer's right, pitch up. */
|
|
40
|
+
yaw: number;
|
|
41
|
+
pitch: number;
|
|
42
|
+
/** Added above the neck, so only the head turns. */
|
|
43
|
+
headYaw: number;
|
|
44
|
+
headPitch: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type InflateLook = {
|
|
48
|
+
shape: BodyShape;
|
|
49
|
+
color: string;
|
|
50
|
+
material: KlexMaterial;
|
|
51
|
+
inflate: ResolvedInflate;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type InflateBot = {
|
|
55
|
+
/** Called by the motion engine after each rig update. */
|
|
56
|
+
draw(frame: InflateFrame): void;
|
|
57
|
+
setLook(look: InflateLook): void;
|
|
58
|
+
/** The 2D canvas that receives this character's tile, and its CSS width. */
|
|
59
|
+
setTarget(canvas: HTMLCanvasElement | null, size: number): void;
|
|
60
|
+
release(): void;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type InflateRenderer = {
|
|
64
|
+
register(): InflateBot;
|
|
65
|
+
/** Fires once if the GPU drops the context; characters fall back to SVG. */
|
|
66
|
+
onLost(listener: () => void): () => void;
|
|
67
|
+
readonly stats: { frames: number; tiles: number; lastFlushMs: number };
|
|
68
|
+
destroy(): void;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type Bot = {
|
|
72
|
+
slot: number;
|
|
73
|
+
frame: InflateFrame | null;
|
|
74
|
+
look: InflateLook | null;
|
|
75
|
+
canvas: HTMLCanvasElement | null;
|
|
76
|
+
context: CanvasRenderingContext2D | null;
|
|
77
|
+
size: number;
|
|
78
|
+
density: number;
|
|
79
|
+
dirty: boolean;
|
|
80
|
+
/** Output pixels drawn last time, cleared before the next copy. */
|
|
81
|
+
drawn: [number, number, number, number] | null;
|
|
82
|
+
drawnAt: number;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const INSTANCE_FLOATS = 4 * (5 + MAX_BUMPS);
|
|
86
|
+
/**
|
|
87
|
+
* Tiles reshaded per frame. Busy scenes refresh the rest round-robin: travel
|
|
88
|
+
* stays smooth through the DOM transform, only deformation updates less often.
|
|
89
|
+
*/
|
|
90
|
+
export const TILE_BUDGET = 16;
|
|
91
|
+
const MATERIAL_IDS: Record<KlexMaterial, number> = {
|
|
92
|
+
plush: 0,
|
|
93
|
+
rubber: 1,
|
|
94
|
+
jelly: 2,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
function compile(gl: WebGL2RenderingContext, vertex: string, fragment: string) {
|
|
98
|
+
const program = gl.createProgram();
|
|
99
|
+
for (const [type, source] of [
|
|
100
|
+
[gl.VERTEX_SHADER, vertex],
|
|
101
|
+
[gl.FRAGMENT_SHADER, fragment],
|
|
102
|
+
] as const) {
|
|
103
|
+
const shader = gl.createShader(type);
|
|
104
|
+
if (!shader) throw new Error('Could not create shader');
|
|
105
|
+
gl.shaderSource(shader, source);
|
|
106
|
+
gl.compileShader(shader);
|
|
107
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
|
|
108
|
+
throw new Error(gl.getShaderInfoLog(shader) ?? 'Shader failed');
|
|
109
|
+
gl.attachShader(program, shader);
|
|
110
|
+
gl.deleteShader(shader);
|
|
111
|
+
}
|
|
112
|
+
gl.linkProgram(program);
|
|
113
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
|
|
114
|
+
throw new Error(gl.getProgramInfoLog(program) ?? 'Program failed');
|
|
115
|
+
const uniforms = new Map<string, WebGLUniformLocation | null>();
|
|
116
|
+
return {
|
|
117
|
+
program,
|
|
118
|
+
uniform(name: string) {
|
|
119
|
+
if (!uniforms.has(name))
|
|
120
|
+
uniforms.set(name, gl.getUniformLocation(program, name));
|
|
121
|
+
return uniforms.get(name) ?? null;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
type Target = { texture: WebGLTexture; framebuffer: WebGLFramebuffer };
|
|
127
|
+
|
|
128
|
+
function atlasCanvas(): HTMLCanvasElement | OffscreenCanvas {
|
|
129
|
+
return typeof OffscreenCanvas === 'undefined'
|
|
130
|
+
? document.createElement('canvas')
|
|
131
|
+
: new OffscreenCanvas(1, 1);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function createInflateRenderer(
|
|
135
|
+
canvas: HTMLCanvasElement | OffscreenCanvas = atlasCanvas(),
|
|
136
|
+
): InflateRenderer | null {
|
|
137
|
+
const context = canvas.getContext('webgl2', {
|
|
138
|
+
alpha: true,
|
|
139
|
+
premultipliedAlpha: true,
|
|
140
|
+
antialias: false,
|
|
141
|
+
depth: false,
|
|
142
|
+
stencil: false,
|
|
143
|
+
preserveDrawingBuffer: false,
|
|
144
|
+
});
|
|
145
|
+
if (!context?.getExtension('EXT_color_buffer_float')) return null;
|
|
146
|
+
const gl: WebGL2RenderingContext = context;
|
|
147
|
+
const selectProgram = (program: WebGLProgram) =>
|
|
148
|
+
// biome-ignore lint/correctness/useHookAtTopLevel: WebGL's useProgram is not a React hook.
|
|
149
|
+
gl.useProgram(program);
|
|
150
|
+
|
|
151
|
+
const maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE) as number;
|
|
152
|
+
const samples = Math.min(4, gl.getParameter(gl.MAX_SAMPLES) as number);
|
|
153
|
+
const programs = {
|
|
154
|
+
mask: compile(gl, MASK_VERTEX, MASK_FRAGMENT),
|
|
155
|
+
seed: compile(gl, TILE_VERTEX, SEED_FRAGMENT),
|
|
156
|
+
jfa: compile(gl, TILE_VERTEX, JFA_FRAGMENT),
|
|
157
|
+
height: compile(gl, TILE_VERTEX, HEIGHT_FRAGMENT),
|
|
158
|
+
blur: compile(gl, TILE_VERTEX, BLUR_FRAGMENT),
|
|
159
|
+
shade: compile(gl, TILE_VERTEX, SHADE_FRAGMENT),
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const quadBuffer = gl.createBuffer();
|
|
163
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
164
|
+
gl.bufferData(
|
|
165
|
+
gl.ARRAY_BUFFER,
|
|
166
|
+
new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]),
|
|
167
|
+
gl.STATIC_DRAW,
|
|
168
|
+
);
|
|
169
|
+
const instanceBuffer = gl.createBuffer();
|
|
170
|
+
const tileVao = gl.createVertexArray();
|
|
171
|
+
gl.bindVertexArray(tileVao);
|
|
172
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
173
|
+
gl.enableVertexAttribArray(0);
|
|
174
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
175
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, instanceBuffer);
|
|
176
|
+
for (let i = 0; i < INSTANCE_FLOATS / 4; i++) {
|
|
177
|
+
gl.enableVertexAttribArray(1 + i);
|
|
178
|
+
gl.vertexAttribPointer(
|
|
179
|
+
1 + i,
|
|
180
|
+
4,
|
|
181
|
+
gl.FLOAT,
|
|
182
|
+
false,
|
|
183
|
+
INSTANCE_FLOATS * 4,
|
|
184
|
+
i * 16,
|
|
185
|
+
);
|
|
186
|
+
gl.vertexAttribDivisor(1 + i, 1);
|
|
187
|
+
}
|
|
188
|
+
const contourBuffer = gl.createBuffer();
|
|
189
|
+
const maskVao = gl.createVertexArray();
|
|
190
|
+
gl.bindVertexArray(maskVao);
|
|
191
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, contourBuffer);
|
|
192
|
+
gl.enableVertexAttribArray(0);
|
|
193
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
194
|
+
gl.bindVertexArray(null);
|
|
195
|
+
|
|
196
|
+
const bots = new Set<Bot>();
|
|
197
|
+
const slots = createTileSlots();
|
|
198
|
+
const lostListeners = new Set<() => void>();
|
|
199
|
+
const stats = { frames: 0, tiles: 0, lastFlushMs: 0 };
|
|
200
|
+
let layout: AtlasLayout | null = null;
|
|
201
|
+
let textures: {
|
|
202
|
+
maskSamples: WebGLRenderbuffer;
|
|
203
|
+
maskSamplesFramebuffer: WebGLFramebuffer;
|
|
204
|
+
mask: Target;
|
|
205
|
+
seeds: [Target, Target];
|
|
206
|
+
heights: [Target, Target];
|
|
207
|
+
} | null = null;
|
|
208
|
+
let contour = new Float32Array(4096);
|
|
209
|
+
let instances = new Float32Array(INSTANCE_FLOATS * 16);
|
|
210
|
+
let scheduled = false;
|
|
211
|
+
let lost = false;
|
|
212
|
+
|
|
213
|
+
canvas.addEventListener('webglcontextlost', (event: Event) => {
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
lost = true;
|
|
216
|
+
for (const listener of lostListeners) listener();
|
|
217
|
+
lostListeners.clear();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
function target(
|
|
221
|
+
width: number,
|
|
222
|
+
height: number,
|
|
223
|
+
format: number,
|
|
224
|
+
filter: number,
|
|
225
|
+
): Target {
|
|
226
|
+
const texture = gl.createTexture();
|
|
227
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
228
|
+
gl.texStorage2D(gl.TEXTURE_2D, 1, format, width, height);
|
|
229
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
|
|
230
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
|
|
231
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
232
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
233
|
+
const framebuffer = gl.createFramebuffer();
|
|
234
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
235
|
+
gl.framebufferTexture2D(
|
|
236
|
+
gl.FRAMEBUFFER,
|
|
237
|
+
gl.COLOR_ATTACHMENT0,
|
|
238
|
+
gl.TEXTURE_2D,
|
|
239
|
+
texture,
|
|
240
|
+
0,
|
|
241
|
+
);
|
|
242
|
+
return { texture, framebuffer };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function freeTextures() {
|
|
246
|
+
if (!textures) return;
|
|
247
|
+
gl.deleteRenderbuffer(textures.maskSamples);
|
|
248
|
+
gl.deleteFramebuffer(textures.maskSamplesFramebuffer);
|
|
249
|
+
for (const item of [
|
|
250
|
+
textures.mask,
|
|
251
|
+
...textures.seeds,
|
|
252
|
+
...textures.heights,
|
|
253
|
+
]) {
|
|
254
|
+
gl.deleteTexture(item.texture);
|
|
255
|
+
gl.deleteFramebuffer(item.framebuffer);
|
|
256
|
+
}
|
|
257
|
+
textures = null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Grow the atlas when characters join or a larger one needs more pixels.
|
|
261
|
+
function ensureLayout() {
|
|
262
|
+
let density = 0.4;
|
|
263
|
+
for (const bot of bots) density = Math.max(density, bot.density);
|
|
264
|
+
const tiles = Math.max(1, slots.highest);
|
|
265
|
+
if (
|
|
266
|
+
layout &&
|
|
267
|
+
textures &&
|
|
268
|
+
layout.density === density &&
|
|
269
|
+
layout.capacity >= tiles
|
|
270
|
+
)
|
|
271
|
+
return layout;
|
|
272
|
+
const next = atlasLayout(
|
|
273
|
+
density,
|
|
274
|
+
Math.max(tiles, layout?.capacity ?? 1),
|
|
275
|
+
maxSize,
|
|
276
|
+
);
|
|
277
|
+
freeTextures();
|
|
278
|
+
layout = next;
|
|
279
|
+
canvas.width = next.width;
|
|
280
|
+
canvas.height = next.height;
|
|
281
|
+
const halfWidth = next.width / 2;
|
|
282
|
+
const halfHeight = next.height / 2;
|
|
283
|
+
const maskSamples = gl.createRenderbuffer();
|
|
284
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, maskSamples);
|
|
285
|
+
gl.renderbufferStorageMultisample(
|
|
286
|
+
gl.RENDERBUFFER,
|
|
287
|
+
samples,
|
|
288
|
+
gl.R8,
|
|
289
|
+
next.width,
|
|
290
|
+
next.height,
|
|
291
|
+
);
|
|
292
|
+
const maskSamplesFramebuffer = gl.createFramebuffer();
|
|
293
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, maskSamplesFramebuffer);
|
|
294
|
+
gl.framebufferRenderbuffer(
|
|
295
|
+
gl.FRAMEBUFFER,
|
|
296
|
+
gl.COLOR_ATTACHMENT0,
|
|
297
|
+
gl.RENDERBUFFER,
|
|
298
|
+
maskSamples,
|
|
299
|
+
);
|
|
300
|
+
textures = {
|
|
301
|
+
maskSamples,
|
|
302
|
+
maskSamplesFramebuffer,
|
|
303
|
+
mask: target(next.width, next.height, gl.R8, gl.LINEAR),
|
|
304
|
+
seeds: [
|
|
305
|
+
target(halfWidth, halfHeight, gl.RG16F, gl.NEAREST),
|
|
306
|
+
target(halfWidth, halfHeight, gl.RG16F, gl.NEAREST),
|
|
307
|
+
],
|
|
308
|
+
heights: [
|
|
309
|
+
target(halfWidth, halfHeight, gl.RG16F, gl.LINEAR),
|
|
310
|
+
target(halfWidth, halfHeight, gl.RG16F, gl.LINEAR),
|
|
311
|
+
],
|
|
312
|
+
};
|
|
313
|
+
// Tiles moved or were wiped: redraw everyone from their last frame.
|
|
314
|
+
for (const bot of bots) bot.dirty = Boolean(bot.frame);
|
|
315
|
+
return next;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function schedule() {
|
|
319
|
+
if (scheduled || lost) return;
|
|
320
|
+
scheduled = true;
|
|
321
|
+
// GSAP runs every character's tick inside one animation frame callback.
|
|
322
|
+
// The microtask runs after all of them, so one flush draws the scene.
|
|
323
|
+
queueMicrotask(flush);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function flush() {
|
|
327
|
+
scheduled = false;
|
|
328
|
+
if (lost) return;
|
|
329
|
+
const started = performance.now();
|
|
330
|
+
const waiting: Bot[] = [];
|
|
331
|
+
for (const bot of bots) {
|
|
332
|
+
if (bot.dirty && bot.frame && bot.look && bot.context) waiting.push(bot);
|
|
333
|
+
}
|
|
334
|
+
if (!waiting.length) return;
|
|
335
|
+
const dirty = pickTiles(waiting, TILE_BUDGET);
|
|
336
|
+
if (dirty.length < waiting.length) requestAnimationFrame(schedule);
|
|
337
|
+
const atlas = ensureLayout();
|
|
338
|
+
const maps = textures;
|
|
339
|
+
if (!maps) return;
|
|
340
|
+
const halfWidth = atlas.width / 2;
|
|
341
|
+
const halfHeight = atlas.height / 2;
|
|
342
|
+
|
|
343
|
+
// Contours for the mask pass.
|
|
344
|
+
let vertexCount = 0;
|
|
345
|
+
const spans: [number, number][] = [];
|
|
346
|
+
for (const bot of dirty) {
|
|
347
|
+
const points = bot.frame?.points ?? [];
|
|
348
|
+
const needed = (vertexCount + points.length * 8) * 2;
|
|
349
|
+
if (needed > contour.length) {
|
|
350
|
+
const grown = new Float32Array(needed * 2);
|
|
351
|
+
grown.set(contour);
|
|
352
|
+
contour = grown;
|
|
353
|
+
}
|
|
354
|
+
const count = tessellate(
|
|
355
|
+
points,
|
|
356
|
+
contour,
|
|
357
|
+
vertexCount * 2,
|
|
358
|
+
segmentSamples(points.length),
|
|
359
|
+
);
|
|
360
|
+
spans.push([vertexCount, count]);
|
|
361
|
+
vertexCount += count;
|
|
362
|
+
}
|
|
363
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, contourBuffer);
|
|
364
|
+
gl.bufferData(
|
|
365
|
+
gl.ARRAY_BUFFER,
|
|
366
|
+
contour.subarray(0, vertexCount * 2),
|
|
367
|
+
gl.DYNAMIC_DRAW,
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
// Per-tile instance data.
|
|
371
|
+
if (instances.length < dirty.length * INSTANCE_FLOATS)
|
|
372
|
+
instances = new Float32Array(dirty.length * INSTANCE_FLOATS * 2);
|
|
373
|
+
let maxEdge = 1;
|
|
374
|
+
for (const [index, bot] of dirty.entries()) {
|
|
375
|
+
const frame = bot.frame;
|
|
376
|
+
const look = bot.look;
|
|
377
|
+
if (!frame || !look) continue;
|
|
378
|
+
const base = index * INSTANCE_FLOATS;
|
|
379
|
+
const [red, green, blue] = hexToLinear(look.color);
|
|
380
|
+
instances[base] = bot.slot % atlas.columns;
|
|
381
|
+
instances[base + 1] = Math.floor(bot.slot / atlas.columns);
|
|
382
|
+
instances[base + 2] = frame.lift;
|
|
383
|
+
instances[base + 3] = frame.facing;
|
|
384
|
+
instances[base + 4] = red;
|
|
385
|
+
instances[base + 5] = green;
|
|
386
|
+
instances[base + 6] = blue;
|
|
387
|
+
instances[base + 7] = MATERIAL_IDS[look.material];
|
|
388
|
+
instances[base + 8] = look.inflate.amplitude;
|
|
389
|
+
instances[base + 9] = look.inflate.edgeRadius;
|
|
390
|
+
instances[base + 10] = profileId(look.inflate.edgeProfile);
|
|
391
|
+
instances[base + 11] = look.inflate.innerRadius;
|
|
392
|
+
placeBumps(
|
|
393
|
+
look.shape,
|
|
394
|
+
look.inflate.bumps,
|
|
395
|
+
frame.points,
|
|
396
|
+
frame.face,
|
|
397
|
+
frame.facing,
|
|
398
|
+
instances,
|
|
399
|
+
base + 12,
|
|
400
|
+
);
|
|
401
|
+
const [pivotX, pivotY] = turnPivot(frame.points);
|
|
402
|
+
instances[base + 12 + 4 * MAX_BUMPS] = frame.yaw;
|
|
403
|
+
instances[base + 13 + 4 * MAX_BUMPS] = frame.pitch;
|
|
404
|
+
instances[base + 14 + 4 * MAX_BUMPS] = pivotX;
|
|
405
|
+
instances[base + 15 + 4 * MAX_BUMPS] = pivotY;
|
|
406
|
+
const head = headFrame(frame.points, frame.face);
|
|
407
|
+
instances[base + 16 + 4 * MAX_BUMPS] = frame.headYaw;
|
|
408
|
+
instances[base + 17 + 4 * MAX_BUMPS] = frame.headPitch;
|
|
409
|
+
instances[base + 18 + 4 * MAX_BUMPS] = head.neckY;
|
|
410
|
+
instances[base + 19 + 4 * MAX_BUMPS] = head.pivotX;
|
|
411
|
+
maxEdge = Math.max(maxEdge, look.inflate.edgeRadius);
|
|
412
|
+
}
|
|
413
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, instanceBuffer);
|
|
414
|
+
gl.bufferData(
|
|
415
|
+
gl.ARRAY_BUFFER,
|
|
416
|
+
instances.subarray(0, dirty.length * INSTANCE_FLOATS),
|
|
417
|
+
gl.DYNAMIC_DRAW,
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
gl.disable(gl.DEPTH_TEST);
|
|
421
|
+
// 1. Mask: XOR-filled fans into a multisampled buffer, then resolve.
|
|
422
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, maps.maskSamplesFramebuffer);
|
|
423
|
+
gl.viewport(0, 0, atlas.width, atlas.height);
|
|
424
|
+
gl.clearColor(0, 0, 0, 0);
|
|
425
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
426
|
+
selectProgram(programs.mask.program);
|
|
427
|
+
gl.uniform4f(
|
|
428
|
+
programs.mask.uniform('uRegion'),
|
|
429
|
+
REGION.x,
|
|
430
|
+
REGION.y,
|
|
431
|
+
REGION.width,
|
|
432
|
+
REGION.height,
|
|
433
|
+
);
|
|
434
|
+
gl.enable(gl.BLEND);
|
|
435
|
+
gl.blendFunc(gl.ONE_MINUS_DST_COLOR, gl.ZERO);
|
|
436
|
+
gl.bindVertexArray(maskVao);
|
|
437
|
+
for (const [index, bot] of dirty.entries()) {
|
|
438
|
+
const [first, count] = spans[index] ?? [0, 0];
|
|
439
|
+
gl.viewport(
|
|
440
|
+
(bot.slot % atlas.columns) * atlas.tileWidth,
|
|
441
|
+
Math.floor(bot.slot / atlas.columns) * atlas.tileHeight,
|
|
442
|
+
atlas.tileWidth,
|
|
443
|
+
atlas.tileHeight,
|
|
444
|
+
);
|
|
445
|
+
gl.drawArrays(gl.TRIANGLE_FAN, first, count);
|
|
446
|
+
}
|
|
447
|
+
gl.disable(gl.BLEND);
|
|
448
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, maps.maskSamplesFramebuffer);
|
|
449
|
+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, maps.mask.framebuffer);
|
|
450
|
+
gl.blitFramebuffer(
|
|
451
|
+
0,
|
|
452
|
+
0,
|
|
453
|
+
atlas.width,
|
|
454
|
+
atlas.height,
|
|
455
|
+
0,
|
|
456
|
+
0,
|
|
457
|
+
atlas.width,
|
|
458
|
+
atlas.height,
|
|
459
|
+
gl.COLOR_BUFFER_BIT,
|
|
460
|
+
gl.NEAREST,
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
gl.bindVertexArray(tileVao);
|
|
464
|
+
const tiles = dirty.length;
|
|
465
|
+
const pass = (
|
|
466
|
+
program: ReturnType<typeof compile>,
|
|
467
|
+
output: Target | null,
|
|
468
|
+
width: number,
|
|
469
|
+
height: number,
|
|
470
|
+
tileWidth: number,
|
|
471
|
+
tileHeight: number,
|
|
472
|
+
bind: () => void,
|
|
473
|
+
) => {
|
|
474
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, output?.framebuffer ?? null);
|
|
475
|
+
gl.viewport(0, 0, width, height);
|
|
476
|
+
selectProgram(program.program);
|
|
477
|
+
gl.uniform2f(program.uniform('uTiles'), atlas.columns, atlas.rows);
|
|
478
|
+
gl.uniform2f(program.uniform('uSize'), width, height);
|
|
479
|
+
gl.uniform2f(program.uniform('uTileSize'), tileWidth, tileHeight);
|
|
480
|
+
gl.uniform4f(
|
|
481
|
+
program.uniform('uRegion'),
|
|
482
|
+
REGION.x,
|
|
483
|
+
REGION.y,
|
|
484
|
+
REGION.width,
|
|
485
|
+
REGION.height,
|
|
486
|
+
);
|
|
487
|
+
bind();
|
|
488
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, tiles);
|
|
489
|
+
};
|
|
490
|
+
const texture = (
|
|
491
|
+
program: ReturnType<typeof compile>,
|
|
492
|
+
name: string,
|
|
493
|
+
unit: number,
|
|
494
|
+
source: WebGLTexture,
|
|
495
|
+
) => {
|
|
496
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
497
|
+
gl.bindTexture(gl.TEXTURE_2D, source);
|
|
498
|
+
gl.uniform1i(program.uniform(name), unit);
|
|
499
|
+
};
|
|
500
|
+
const halfTileWidth = atlas.tileWidth / 2;
|
|
501
|
+
const halfTileHeight = atlas.tileHeight / 2;
|
|
502
|
+
|
|
503
|
+
// 2. Jump flooding: seeds on the edge, nearest one for every pixel.
|
|
504
|
+
pass(
|
|
505
|
+
programs.seed,
|
|
506
|
+
maps.seeds[0],
|
|
507
|
+
halfWidth,
|
|
508
|
+
halfHeight,
|
|
509
|
+
halfTileWidth,
|
|
510
|
+
halfTileHeight,
|
|
511
|
+
() => texture(programs.seed, 'uMask', 0, maps.mask.texture),
|
|
512
|
+
);
|
|
513
|
+
// Only distances up to the widest plateau matter; deeper pixels are flat.
|
|
514
|
+
const reach = Math.min(
|
|
515
|
+
Math.max(halfTileWidth, halfTileHeight),
|
|
516
|
+
(maxEdge * halfTileWidth) / REGION.width + 2,
|
|
517
|
+
);
|
|
518
|
+
let step = 1;
|
|
519
|
+
while (step * 2 < reach) step *= 2;
|
|
520
|
+
let current = 0;
|
|
521
|
+
for (const size of [...steps(step), 1]) {
|
|
522
|
+
const source = maps.seeds[current];
|
|
523
|
+
const output = maps.seeds[1 - current];
|
|
524
|
+
if (!source || !output) break;
|
|
525
|
+
pass(
|
|
526
|
+
programs.jfa,
|
|
527
|
+
output,
|
|
528
|
+
halfWidth,
|
|
529
|
+
halfHeight,
|
|
530
|
+
halfTileWidth,
|
|
531
|
+
halfTileHeight,
|
|
532
|
+
() => {
|
|
533
|
+
texture(programs.jfa, 'uSeeds', 0, source.texture);
|
|
534
|
+
gl.uniform1i(programs.jfa.uniform('uStep'), size);
|
|
535
|
+
},
|
|
536
|
+
);
|
|
537
|
+
current = 1 - current;
|
|
538
|
+
}
|
|
539
|
+
const seeds = maps.seeds[current];
|
|
540
|
+
const [heightA, heightB] = maps.heights;
|
|
541
|
+
if (!seeds) return;
|
|
542
|
+
|
|
543
|
+
// 3. Height from the edge distance, then blur away the ridge crease.
|
|
544
|
+
pass(
|
|
545
|
+
programs.height,
|
|
546
|
+
heightA,
|
|
547
|
+
halfWidth,
|
|
548
|
+
halfHeight,
|
|
549
|
+
halfTileWidth,
|
|
550
|
+
halfTileHeight,
|
|
551
|
+
() => {
|
|
552
|
+
texture(programs.height, 'uSeeds', 0, seeds.texture);
|
|
553
|
+
texture(programs.height, 'uMask', 1, maps.mask.texture);
|
|
554
|
+
},
|
|
555
|
+
);
|
|
556
|
+
const sigma = Math.max(1, (1.8 * halfTileWidth) / REGION.width);
|
|
557
|
+
for (const [source, output, x, y] of [
|
|
558
|
+
[heightA, heightB, 1, 0],
|
|
559
|
+
[heightB, heightA, 0, 1],
|
|
560
|
+
] as const) {
|
|
561
|
+
pass(
|
|
562
|
+
programs.blur,
|
|
563
|
+
output,
|
|
564
|
+
halfWidth,
|
|
565
|
+
halfHeight,
|
|
566
|
+
halfTileWidth,
|
|
567
|
+
halfTileHeight,
|
|
568
|
+
() => {
|
|
569
|
+
texture(programs.blur, 'uSource', 0, source.texture);
|
|
570
|
+
gl.uniform2f(programs.blur.uniform('uDirection'), x, y);
|
|
571
|
+
gl.uniform1f(programs.blur.uniform('uSigma'), sigma);
|
|
572
|
+
},
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// 4. Shade at full resolution into the drawing buffer.
|
|
577
|
+
pass(
|
|
578
|
+
programs.shade,
|
|
579
|
+
null,
|
|
580
|
+
atlas.width,
|
|
581
|
+
atlas.height,
|
|
582
|
+
atlas.tileWidth,
|
|
583
|
+
atlas.tileHeight,
|
|
584
|
+
() => {
|
|
585
|
+
texture(programs.shade, 'uMask', 0, maps.mask.texture);
|
|
586
|
+
texture(programs.shade, 'uHeight', 1, heightA.texture);
|
|
587
|
+
},
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
// 5. Copy each tile into its character's own canvas, which sits in the
|
|
591
|
+
// DOM between the shadow and the eyes. Every drawImage from a WebGL
|
|
592
|
+
// canvas snapshots the whole drawing buffer; transferring it once as a
|
|
593
|
+
// bitmap keeps thirty copies as cheap as one.
|
|
594
|
+
const source =
|
|
595
|
+
'transferToImageBitmap' in canvas
|
|
596
|
+
? canvas.transferToImageBitmap()
|
|
597
|
+
: canvas;
|
|
598
|
+
for (const bot of dirty) {
|
|
599
|
+
const context = bot.context;
|
|
600
|
+
const output = bot.canvas;
|
|
601
|
+
if (!context || !output) continue;
|
|
602
|
+
const column = bot.slot % atlas.columns;
|
|
603
|
+
const row = Math.floor(bot.slot / atlas.columns);
|
|
604
|
+
// Copy only the silhouette's bounds; most of the frame is empty.
|
|
605
|
+
let minX = Infinity;
|
|
606
|
+
let minY = Infinity;
|
|
607
|
+
let maxX = -Infinity;
|
|
608
|
+
let maxY = -Infinity;
|
|
609
|
+
for (const [x, y] of bot.frame?.points ?? []) {
|
|
610
|
+
minX = Math.min(minX, x);
|
|
611
|
+
minY = Math.min(minY, y);
|
|
612
|
+
maxX = Math.max(maxX, x);
|
|
613
|
+
maxY = Math.max(maxY, y);
|
|
614
|
+
}
|
|
615
|
+
// A turned body shows its sides, so copy the whole frame.
|
|
616
|
+
const frame = bot.frame;
|
|
617
|
+
if (
|
|
618
|
+
frame &&
|
|
619
|
+
(frame.yaw || frame.pitch || frame.headYaw || frame.headPitch)
|
|
620
|
+
) {
|
|
621
|
+
minX = minY = -Infinity;
|
|
622
|
+
maxX = maxY = Infinity;
|
|
623
|
+
}
|
|
624
|
+
const tileScale = atlas.tileWidth / REGION.width;
|
|
625
|
+
const outputScale = output.width / REGION.width;
|
|
626
|
+
const left = Math.max(0, Math.floor(minX - 4 - REGION.x));
|
|
627
|
+
const top = Math.max(0, Math.floor(minY - 4 - REGION.y));
|
|
628
|
+
const right = Math.min(REGION.width, Math.ceil(maxX + 4 - REGION.x));
|
|
629
|
+
const bottom = Math.min(REGION.height, Math.ceil(maxY + 4 - REGION.y));
|
|
630
|
+
if (bot.drawn) context.clearRect(...bot.drawn);
|
|
631
|
+
if (right <= left || bottom <= top) {
|
|
632
|
+
bot.drawn = null;
|
|
633
|
+
} else {
|
|
634
|
+
bot.drawn = [
|
|
635
|
+
left * outputScale,
|
|
636
|
+
top * outputScale,
|
|
637
|
+
(right - left) * outputScale,
|
|
638
|
+
(bottom - top) * outputScale,
|
|
639
|
+
];
|
|
640
|
+
context.drawImage(
|
|
641
|
+
source,
|
|
642
|
+
column * atlas.tileWidth + left * tileScale,
|
|
643
|
+
atlas.height - (row + 1) * atlas.tileHeight + top * tileScale,
|
|
644
|
+
(right - left) * tileScale,
|
|
645
|
+
(bottom - top) * tileScale,
|
|
646
|
+
...bot.drawn,
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
bot.dirty = false;
|
|
650
|
+
bot.drawnAt = stats.frames;
|
|
651
|
+
}
|
|
652
|
+
if ('close' in source) source.close();
|
|
653
|
+
stats.frames++;
|
|
654
|
+
stats.tiles = tiles;
|
|
655
|
+
stats.lastFlushMs = performance.now() - started;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
return {
|
|
659
|
+
stats,
|
|
660
|
+
destroy() {
|
|
661
|
+
freeTextures();
|
|
662
|
+
bots.clear();
|
|
663
|
+
lost = true;
|
|
664
|
+
gl.getExtension('WEBGL_lose_context')?.loseContext();
|
|
665
|
+
},
|
|
666
|
+
register() {
|
|
667
|
+
const bot: Bot = {
|
|
668
|
+
slot: slots.take(),
|
|
669
|
+
frame: null,
|
|
670
|
+
look: null,
|
|
671
|
+
canvas: null,
|
|
672
|
+
context: null,
|
|
673
|
+
size: 160,
|
|
674
|
+
density: 0.4,
|
|
675
|
+
dirty: false,
|
|
676
|
+
drawn: null,
|
|
677
|
+
drawnAt: -1,
|
|
678
|
+
};
|
|
679
|
+
bots.add(bot);
|
|
680
|
+
return {
|
|
681
|
+
draw(frame) {
|
|
682
|
+
bot.frame = frame;
|
|
683
|
+
bot.dirty = true;
|
|
684
|
+
schedule();
|
|
685
|
+
},
|
|
686
|
+
setLook(look) {
|
|
687
|
+
bot.look = look;
|
|
688
|
+
bot.dirty = Boolean(bot.frame);
|
|
689
|
+
schedule();
|
|
690
|
+
},
|
|
691
|
+
setTarget(output, size) {
|
|
692
|
+
const pixelRatio =
|
|
693
|
+
typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
694
|
+
bot.canvas = output;
|
|
695
|
+
bot.context = output?.getContext('2d') ?? null;
|
|
696
|
+
bot.size = size;
|
|
697
|
+
bot.density = tileDensity(size, pixelRatio);
|
|
698
|
+
if (output) {
|
|
699
|
+
const scale = (size / 160) * pixelRatio;
|
|
700
|
+
output.width = Math.round(REGION.width * scale);
|
|
701
|
+
output.height = Math.round(REGION.height * scale);
|
|
702
|
+
bot.drawn = null;
|
|
703
|
+
}
|
|
704
|
+
bot.dirty = Boolean(bot.frame);
|
|
705
|
+
schedule();
|
|
706
|
+
},
|
|
707
|
+
release() {
|
|
708
|
+
if (!bots.delete(bot)) return;
|
|
709
|
+
slots.give(bot.slot);
|
|
710
|
+
},
|
|
711
|
+
};
|
|
712
|
+
},
|
|
713
|
+
onLost(listener) {
|
|
714
|
+
if (lost) {
|
|
715
|
+
listener();
|
|
716
|
+
return () => {};
|
|
717
|
+
}
|
|
718
|
+
lostListeners.add(listener);
|
|
719
|
+
return () => lostListeners.delete(listener);
|
|
720
|
+
},
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
function steps(start: number) {
|
|
724
|
+
const list: number[] = [];
|
|
725
|
+
for (let size = start; size >= 1; size /= 2) list.push(size);
|
|
726
|
+
return list;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const shared = createShared<InflateRenderer>(
|
|
731
|
+
() => {
|
|
732
|
+
try {
|
|
733
|
+
return createInflateRenderer();
|
|
734
|
+
} catch (error) {
|
|
735
|
+
console.warn('Klex: inflated rendering unavailable', error);
|
|
736
|
+
return null;
|
|
737
|
+
}
|
|
738
|
+
},
|
|
739
|
+
(renderer) => renderer.destroy(),
|
|
740
|
+
);
|
|
741
|
+
|
|
742
|
+
/** The page-wide renderer, or null without WebGL2 float targets. */
|
|
743
|
+
export function acquireInflateRenderer() {
|
|
744
|
+
return shared.acquire();
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
export function releaseInflateRenderer(renderer: InflateRenderer) {
|
|
748
|
+
shared.release(renderer);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
export function invalidateInflateRenderer(renderer: InflateRenderer) {
|
|
752
|
+
shared.invalidate(renderer);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/** The longest-waiting tiles first, so every character keeps updating. */
|
|
756
|
+
export function pickTiles<T extends { drawnAt: number }>(
|
|
757
|
+
waiting: T[],
|
|
758
|
+
budget: number,
|
|
759
|
+
) {
|
|
760
|
+
if (waiting.length <= budget) return waiting;
|
|
761
|
+
return [...waiting].sort((a, b) => a.drawnAt - b.drawnAt).slice(0, budget);
|
|
762
|
+
}
|