@npcify/web 0.1.2 → 0.1.4

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/dist/index.js CHANGED
@@ -1,250 +1,3 @@
1
- // ../ts-shared/src/body-occlusion.ts
2
- var BODY_TRIANGLE_MASK_MAX_BYTES = 16 * 1024 * 1024;
3
-
4
- // ../ts-shared/src/avatar-editor.ts
5
- var AVATAR_BODY_MORPHS = [
6
- // Torso
7
- {
8
- name: "breastSize",
9
- label: "Breast Size",
10
- category: "Torso",
11
- min: 0,
12
- max: 1,
13
- default: 0
14
- },
15
- {
16
- name: "waistWidth",
17
- label: "Waist Width",
18
- category: "Torso",
19
- min: 0,
20
- max: 1,
21
- default: 0
22
- },
23
- {
24
- name: "hipWidth",
25
- label: "Hip Width",
26
- category: "Torso",
27
- min: 0,
28
- max: 1,
29
- default: 0
30
- },
31
- {
32
- name: "bellySize",
33
- label: "Belly Size",
34
- category: "Torso",
35
- min: 0,
36
- max: 1,
37
- default: 0
38
- },
39
- {
40
- name: "buttSize",
41
- label: "Butt Size",
42
- category: "Torso",
43
- min: 0,
44
- max: 1,
45
- default: 0
46
- },
47
- {
48
- name: "chestWidth",
49
- label: "Chest Width",
50
- category: "Torso",
51
- min: 0,
52
- max: 1,
53
- default: 0
54
- },
55
- {
56
- name: "torsoLength",
57
- label: "Torso Length",
58
- category: "Torso",
59
- min: 0,
60
- max: 1,
61
- default: 0
62
- },
63
- // Upper Body
64
- {
65
- name: "shoulderBreadth",
66
- label: "Shoulder Breadth",
67
- category: "Upper Body",
68
- min: 0,
69
- max: 1,
70
- default: 0
71
- },
72
- {
73
- name: "neckThickness",
74
- label: "Neck Thickness",
75
- category: "Upper Body",
76
- min: 0,
77
- max: 1,
78
- default: 0
79
- },
80
- // Limbs
81
- {
82
- name: "armThickness",
83
- label: "Arm Thickness",
84
- category: "Limbs",
85
- min: 0,
86
- max: 1,
87
- default: 0
88
- },
89
- {
90
- name: "forearmThickness",
91
- label: "Forearm Thickness",
92
- category: "Limbs",
93
- min: 0,
94
- max: 1,
95
- default: 0
96
- },
97
- {
98
- name: "handSize",
99
- label: "Hand Size",
100
- category: "Limbs",
101
- min: 0,
102
- max: 1,
103
- default: 0
104
- },
105
- // Legs
106
- {
107
- name: "upperThighFat",
108
- label: "Upper Thigh",
109
- category: "Legs",
110
- min: 0,
111
- max: 1,
112
- default: 0
113
- },
114
- {
115
- name: "lowerThighFat",
116
- label: "Lower Thigh",
117
- category: "Legs",
118
- min: 0,
119
- max: 1,
120
- default: 0
121
- },
122
- {
123
- name: "calfSize",
124
- label: "Calf Size",
125
- category: "Legs",
126
- min: 0,
127
- max: 1,
128
- default: 0
129
- },
130
- {
131
- name: "footSize",
132
- label: "Foot Size",
133
- category: "Legs",
134
- min: 0,
135
- max: 1,
136
- default: 0
137
- }
138
- ];
139
- var AVATAR_DEFAULT_SKIN_COLOR = "#FFE0BD";
140
- var AVATAR_FACE_MORPHS = [
141
- // Face
142
- {
143
- name: "faceWidth",
144
- label: "Face Width",
145
- category: "Face",
146
- min: 0,
147
- max: 1,
148
- default: 0
149
- },
150
- {
151
- name: "jawWidth",
152
- label: "Jaw Width",
153
- category: "Face",
154
- min: 0,
155
- max: 1,
156
- default: 0
157
- },
158
- {
159
- name: "chinLength",
160
- label: "Chin Length",
161
- category: "Face",
162
- min: 0,
163
- max: 1,
164
- default: 0
165
- },
166
- {
167
- name: "cheekFullness",
168
- label: "Cheek Fullness",
169
- category: "Face",
170
- min: 0,
171
- max: 1,
172
- default: 0
173
- },
174
- // Eyes
175
- {
176
- name: "eyeSize",
177
- label: "Eye Size",
178
- category: "Eyes",
179
- min: 0,
180
- max: 1,
181
- default: 0
182
- },
183
- {
184
- name: "eyeSpacing",
185
- label: "Eye Spacing",
186
- category: "Eyes",
187
- min: 0,
188
- max: 1,
189
- default: 0
190
- },
191
- {
192
- name: "eyeAngle",
193
- label: "Eye Angle",
194
- category: "Eyes",
195
- min: 0,
196
- max: 1,
197
- default: 0
198
- },
199
- // Nose
200
- {
201
- name: "noseSize",
202
- label: "Nose Size",
203
- category: "Nose",
204
- min: 0,
205
- max: 1,
206
- default: 0
207
- },
208
- {
209
- name: "noseHeight",
210
- label: "Nose Height",
211
- category: "Nose",
212
- min: 0,
213
- max: 1,
214
- default: 0
215
- },
216
- // Mouth
217
- {
218
- name: "mouthSize",
219
- label: "Mouth Size",
220
- category: "Mouth",
221
- min: 0,
222
- max: 1,
223
- default: 0
224
- },
225
- {
226
- name: "lipThickness",
227
- label: "Lip Thickness",
228
- category: "Mouth",
229
- min: 0,
230
- max: 1,
231
- default: 0
232
- },
233
- // Ears
234
- {
235
- name: "earSize",
236
- label: "Ear Size",
237
- category: "Ears",
238
- min: 0,
239
- max: 1,
240
- default: 0
241
- }
242
- ];
243
- var AVATAR_ALL_MORPHS = [
244
- ...AVATAR_BODY_MORPHS,
245
- ...AVATAR_FACE_MORPHS
246
- ];
247
-
248
1
  // ../ts-shared/src/llm.ts
249
2
  var STAGING_PROTOCOL_VERSION = 1;
250
3
 
@@ -256,22 +9,8 @@ var MAXIMUM_PLAYBACK_DURATION_SECONDS = 10 * 60;
256
9
  var TTS_PROVIDERS = ["11labs"];
257
10
  var DEFAULT_TTS_PROVIDER = TTS_PROVIDERS[0];
258
11
 
259
- // ../ts-shared/src/avatar-generation-options.ts
260
- var DEFAULT_AVATAR_BASE_MODEL_ID = "npcify-female";
261
-
262
- // ../ts-shared/src/avatar-generation-catalog.ts
263
- var LEGACY_AVATAR_GENERATION_BASE = {
264
- id: DEFAULT_AVATAR_BASE_MODEL_ID,
265
- label: "Female",
266
- gender: "female",
267
- baseModelVersion: "legacy",
268
- catalogVersion: "legacy",
269
- catalogFingerprint: `sha256:${"0".repeat(64)}`,
270
- manifestKey: "avatar-generation/manifest.json",
271
- previewObjectKey: "avatar-generation/base/female.vrm",
272
- morphs: [...AVATAR_ALL_MORPHS],
273
- skinColor: { default: AVATAR_DEFAULT_SKIN_COLOR, colorizable: true }
274
- };
12
+ // ../ts-shared/src/body-occlusion.ts
13
+ var BODY_TRIANGLE_MASK_MAX_BYTES = 16 * 1024 * 1024;
275
14
 
276
15
  // ../ts-shared/src/baked-conversation.ts
277
16
  var BAKED_CONVERSATION_VERSION = 1;
@@ -3456,360 +3195,287 @@ var LazyAnimations = class {
3456
3195
  };
3457
3196
 
3458
3197
  // ../../clients/web-client/src/api/info.ts
3459
- import { VRMLoaderPlugin, VRMUtils } from "@pixiv/three-vrm";
3460
- import {
3461
- VRMAnimationLoaderPlugin
3462
- } from "@pixiv/three-vrm-animation";
3463
- import { LoadingManager } from "three";
3464
- import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
3465
- var GLB_HEADER_MAGIC = 1179937895;
3466
- var GLB_CHUNK_TYPE_JSON = 1313821514;
3467
- var textDecoder = new TextDecoder();
3468
- var isRecord2 = function(value) {
3469
- return typeof value === "object" && value !== null;
3470
- };
3471
- var resolveResourceUrl = function(url, baseUrl) {
3472
- try {
3473
- return new URL(url, baseUrl).toString();
3474
- } catch {
3475
- return url;
3198
+ import { VRMLoaderPlugin as VRMLoaderPlugin2, VRMUtils as VRMUtils4 } from "@pixiv/three-vrm";
3199
+
3200
+ // ../vrm-renderer/src/scene.ts
3201
+ import { Clock, PerspectiveCamera, Scene, WebGLRenderer } from "three";
3202
+ import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
3203
+
3204
+ // ../vrm-renderer/src/texture-cache.ts
3205
+ import { Source, Texture } from "three";
3206
+ var rendererReleases = /* @__PURE__ */ new WeakMap();
3207
+ var releaseRendererTextureCache = function(renderer) {
3208
+ for (const release of rendererReleases.get(renderer) ?? []) {
3209
+ release();
3476
3210
  }
3211
+ rendererReleases.delete(renderer);
3477
3212
  };
3478
- var parseContentLength = function(response) {
3479
- if (response.headers.get("content-encoding")) {
3480
- return null;
3481
- }
3482
- const header = response.headers.get("content-length");
3483
- if (!header) {
3484
- return null;
3213
+ var uploadOptions = function(texture) {
3214
+ return {
3215
+ wrapS: texture.wrapS,
3216
+ wrapT: texture.wrapT,
3217
+ magFilter: texture.magFilter,
3218
+ minFilter: texture.minFilter,
3219
+ anisotropy: texture.anisotropy,
3220
+ internalFormat: texture.internalFormat,
3221
+ format: texture.format,
3222
+ type: texture.type,
3223
+ generateMipmaps: texture.generateMipmaps,
3224
+ premultiplyAlpha: texture.premultiplyAlpha,
3225
+ flipY: texture.flipY,
3226
+ unpackAlignment: texture.unpackAlignment,
3227
+ colorSpace: texture.colorSpace
3228
+ };
3229
+ };
3230
+ var VrmTextureCache = class {
3231
+ entries = /* @__PURE__ */ new Map();
3232
+ bytes = 0;
3233
+ hits = 0;
3234
+ misses = 0;
3235
+ maxBytes;
3236
+ maxEntries;
3237
+ variants = /* @__PURE__ */ new WeakMap();
3238
+ constructor(options = {}) {
3239
+ if (Object.values(options).some(
3240
+ (value) => value !== void 0 && !Number.isFinite(value)
3241
+ )) {
3242
+ throw new Error("Texture cache limits must be finite");
3243
+ }
3244
+ this.maxBytes = Math.max(0, options.maxBytes ?? 64 * 1024 * 1024);
3245
+ this.maxEntries = Math.max(0, options.maxEntries ?? 64);
3485
3246
  }
3486
- const parsed = Number(header);
3487
- if (!Number.isFinite(parsed) || parsed <= 0) {
3488
- return null;
3247
+ getStats() {
3248
+ let idleBytes = 0;
3249
+ for (const entry of this.entries.values()) {
3250
+ if (entry.refs === 0) {
3251
+ idleBytes += this.retainedBytes(entry);
3252
+ }
3253
+ }
3254
+ return {
3255
+ entries: this.entries.size,
3256
+ bytes: this.bytes,
3257
+ idleBytes,
3258
+ hits: this.hits,
3259
+ misses: this.misses
3260
+ };
3489
3261
  }
3490
- return parsed;
3491
- };
3492
- var parseGltfJson = function(data) {
3493
- if (data.byteLength < 12) {
3494
- return { json: null, jsonText: null, isBinary: false };
3262
+ clear() {
3263
+ for (const entry of this.entries.values()) {
3264
+ this.evict(entry);
3265
+ }
3495
3266
  }
3496
- const view = new DataView(data);
3497
- const magic = view.getUint32(0, true);
3498
- if (magic === GLB_HEADER_MAGIC) {
3499
- let offset = 12;
3500
- while (offset + 8 <= view.byteLength) {
3501
- const chunkLength = view.getUint32(offset, true);
3502
- const chunkType = view.getUint32(offset + 4, true);
3503
- offset += 8;
3504
- if (chunkType === GLB_CHUNK_TYPE_JSON) {
3505
- const chunkData = new Uint8Array(data, offset, chunkLength);
3506
- const jsonText2 = textDecoder.decode(chunkData);
3507
- try {
3508
- const json = JSON.parse(jsonText2);
3509
- return { json, jsonText: jsonText2, isBinary: true };
3510
- } catch {
3511
- return { json: null, jsonText: jsonText2, isBinary: true };
3267
+ async acquire(key, decode2) {
3268
+ let entry = this.entries.get(key);
3269
+ if (entry) {
3270
+ this.hits += 1;
3271
+ this.entries.delete(key);
3272
+ this.entries.set(key, entry);
3273
+ } else {
3274
+ this.misses += 1;
3275
+ entry = {
3276
+ key,
3277
+ source: new Source(null),
3278
+ refs: 0,
3279
+ retained: true,
3280
+ closed: false,
3281
+ bytes: 0,
3282
+ anchors: /* @__PURE__ */ new Map(),
3283
+ rendererReleases: /* @__PURE__ */ new Map(),
3284
+ ready: Promise.resolve().then(decode2)
3285
+ };
3286
+ this.entries.set(key, entry);
3287
+ const created = entry;
3288
+ entry.ready = entry.ready.then((image) => {
3289
+ created.source.data = image;
3290
+ created.source.needsUpdate = true;
3291
+ created.bytes = Math.max(1, image.width * image.height * 4);
3292
+ if (created.retained) {
3293
+ this.bytes += created.bytes;
3512
3294
  }
3513
- }
3514
- offset += chunkLength;
3295
+ this.closeUnused(created);
3296
+ return image;
3297
+ }).catch((error) => {
3298
+ this.evict(created);
3299
+ throw error;
3300
+ });
3301
+ }
3302
+ entry.refs += 1;
3303
+ const acquired = entry;
3304
+ try {
3305
+ const image = await acquired.ready;
3306
+ let released = false;
3307
+ return {
3308
+ image,
3309
+ source: acquired.source,
3310
+ release: () => {
3311
+ if (!released) {
3312
+ released = true;
3313
+ this.unref(acquired);
3314
+ }
3315
+ },
3316
+ adopt: (texture) => {
3317
+ acquired.refs += 1;
3318
+ texture.source = acquired.source;
3319
+ const dispose = () => {
3320
+ texture.removeEventListener("dispose", dispose);
3321
+ this.unref(acquired);
3322
+ };
3323
+ texture.addEventListener("dispose", dispose);
3324
+ },
3325
+ retainGpu: (renderer, texture) => this.retainGpu(acquired, renderer, texture)
3326
+ };
3327
+ } catch (error) {
3328
+ this.unref(acquired);
3329
+ throw error;
3515
3330
  }
3516
- return { json: null, jsonText: null, isBinary: true };
3517
3331
  }
3518
- const jsonText = textDecoder.decode(new Uint8Array(data));
3519
- try {
3520
- const json = JSON.parse(jsonText);
3521
- return { json, jsonText, isBinary: false };
3522
- } catch {
3523
- return { json: null, jsonText, isBinary: false };
3332
+ /**
3333
+ * Drops one use. An image that becomes idle counts as the most recently
3334
+ * used one, and the idle limits apply again.
3335
+ */
3336
+ unref(entry) {
3337
+ entry.refs -= 1;
3338
+ if (entry.refs === 0 && this.entries.get(entry.key) === entry) {
3339
+ this.entries.delete(entry.key);
3340
+ this.entries.set(entry.key, entry);
3341
+ this.trim();
3342
+ }
3343
+ this.closeUnused(entry);
3524
3344
  }
3525
- };
3526
- var collectExternalResourceUrls = function(gltfJson, baseUrl) {
3527
- const urls = /* @__PURE__ */ new Set();
3528
- const addUri = function(uri) {
3529
- if (!uri) {
3345
+ retainGpu(entry, renderer, texture) {
3346
+ if (!entry.retained || texture.source !== entry.source) {
3530
3347
  return;
3531
3348
  }
3532
- if (uri.startsWith("data:") || uri.startsWith("blob:")) {
3533
- return;
3349
+ let anchors = entry.anchors.get(renderer);
3350
+ if (!anchors) {
3351
+ anchors = /* @__PURE__ */ new Map();
3352
+ entry.anchors.set(renderer, anchors);
3353
+ const release = () => {
3354
+ const textures = entry.anchors.get(renderer);
3355
+ if (textures) {
3356
+ for (const anchor2 of textures.values()) {
3357
+ anchor2.dispose();
3358
+ if (entry.retained) {
3359
+ this.bytes -= this.gpuBytes(entry, anchor2);
3360
+ }
3361
+ }
3362
+ entry.anchors.delete(renderer);
3363
+ }
3364
+ rendererReleases.get(renderer)?.delete(release);
3365
+ entry.rendererReleases.delete(renderer);
3366
+ };
3367
+ const releases = rendererReleases.get(renderer) ?? /* @__PURE__ */ new Set();
3368
+ releases.add(release);
3369
+ rendererReleases.set(renderer, releases);
3370
+ entry.rendererReleases.set(renderer, release);
3371
+ }
3372
+ let variant = this.variants.get(texture);
3373
+ if (!variant || variant.version !== texture.version) {
3374
+ const options = uploadOptions(texture);
3375
+ variant = {
3376
+ version: texture.version,
3377
+ key: JSON.stringify(options),
3378
+ options
3379
+ };
3380
+ this.variants.set(texture, variant);
3534
3381
  }
3535
- urls.add(resolveResourceUrl(uri, baseUrl));
3536
- };
3537
- const collectFrom = function(entries) {
3538
- if (!Array.isArray(entries)) {
3382
+ const { key } = variant;
3383
+ if (anchors.has(key)) {
3539
3384
  return;
3540
3385
  }
3541
- entries.forEach((entry) => {
3542
- if (!isRecord2(entry)) {
3543
- return;
3386
+ const anchor = new Texture(texture.image);
3387
+ Object.assign(anchor, variant.options);
3388
+ anchor.needsUpdate = true;
3389
+ anchor.source = texture.source;
3390
+ anchors.set(key, anchor);
3391
+ this.bytes += this.gpuBytes(entry, anchor);
3392
+ try {
3393
+ renderer.initTexture(anchor);
3394
+ } catch (error) {
3395
+ anchors.delete(key);
3396
+ this.bytes -= this.gpuBytes(entry, anchor);
3397
+ anchor.dispose();
3398
+ throw error;
3399
+ }
3400
+ if (entry.refs === 0) {
3401
+ this.trim();
3402
+ }
3403
+ }
3404
+ gpuBytes(entry, texture) {
3405
+ return Math.ceil(entry.bytes * (texture.generateMipmaps ? 4 / 3 : 1));
3406
+ }
3407
+ /** Decoded bytes plus the GPU uploads the cache keeps for the image. */
3408
+ retainedBytes(entry) {
3409
+ let total = entry.bytes;
3410
+ for (const anchors of entry.anchors.values()) {
3411
+ for (const anchor of anchors.values()) {
3412
+ total += this.gpuBytes(entry, anchor);
3544
3413
  }
3545
- const uri = entry.uri;
3546
- if (typeof uri !== "string") {
3414
+ }
3415
+ return total;
3416
+ }
3417
+ /**
3418
+ * Evicts the least recently used idle images until the idle ones fit the
3419
+ * limits. Evicting an image in use would free nothing: its live textures
3420
+ * keep the pixels and the GPU upload.
3421
+ */
3422
+ trim() {
3423
+ let idleEntries = 0;
3424
+ let idleBytes = 0;
3425
+ for (const entry of this.entries.values()) {
3426
+ if (entry.refs === 0) {
3427
+ idleEntries += 1;
3428
+ idleBytes += this.retainedBytes(entry);
3429
+ }
3430
+ }
3431
+ for (const entry of [...this.entries.values()]) {
3432
+ if (idleEntries <= this.maxEntries && idleBytes <= this.maxBytes) {
3547
3433
  return;
3548
3434
  }
3549
- addUri(uri);
3550
- });
3551
- };
3552
- collectFrom(gltfJson.buffers);
3553
- collectFrom(gltfJson.images);
3554
- return Array.from(urls);
3555
- };
3556
- var readResponseChunks = async function(response, resourceId, onProgress) {
3557
- const total = parseContentLength(response);
3558
- onProgress?.(resourceId, 0, total ?? 0);
3559
- if (!response.body) {
3560
- const buffer = await response.arrayBuffer();
3561
- const loaded2 = buffer.byteLength;
3562
- onProgress?.(resourceId, loaded2, total ?? loaded2);
3563
- return [new Uint8Array(buffer)];
3435
+ if (entry.refs === 0) {
3436
+ idleEntries -= 1;
3437
+ idleBytes -= this.retainedBytes(entry);
3438
+ this.evict(entry);
3439
+ }
3440
+ }
3564
3441
  }
3565
- const reader = response.body.getReader();
3566
- const chunks = [];
3567
- let loaded = 0;
3568
- while (true) {
3569
- const { done, value } = await reader.read();
3570
- if (done) {
3571
- break;
3442
+ evict(entry) {
3443
+ if (!entry.retained) {
3444
+ return;
3572
3445
  }
3573
- if (!value) {
3574
- continue;
3446
+ if (this.entries.get(entry.key) === entry) {
3447
+ this.entries.delete(entry.key);
3575
3448
  }
3576
- chunks.push(value);
3577
- loaded += value.length;
3578
- onProgress?.(resourceId, loaded, total ?? 0);
3579
- }
3580
- onProgress?.(resourceId, loaded, total ?? loaded);
3581
- return chunks;
3582
- };
3583
- var concatChunks = function(chunks) {
3584
- if (chunks.length === 0) {
3585
- return new Uint8Array();
3586
- }
3587
- if (chunks.length === 1) {
3588
- return chunks[0] ?? new Uint8Array();
3449
+ this.bytes -= entry.bytes;
3450
+ for (const release of entry.rendererReleases.values()) {
3451
+ release();
3452
+ }
3453
+ entry.anchors.clear();
3454
+ entry.retained = false;
3455
+ this.closeUnused(entry);
3589
3456
  }
3590
- const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
3591
- const combined = new Uint8Array(totalLength);
3592
- let offset = 0;
3593
- for (const chunk of chunks) {
3594
- combined.set(chunk, offset);
3595
- offset += chunk.length;
3457
+ closeUnused(entry) {
3458
+ if (!entry.retained && entry.refs === 0 && entry.source.data && !entry.closed) {
3459
+ entry.closed = true;
3460
+ const image = entry.source.data;
3461
+ if ("close" in image) {
3462
+ image.close();
3463
+ }
3464
+ }
3596
3465
  }
3597
- return combined;
3598
3466
  };
3599
- var toArrayBuffer = function(data) {
3600
- const buffer = new ArrayBuffer(data.byteLength);
3601
- new Uint8Array(buffer).set(data);
3602
- return buffer;
3603
- };
3604
- var fetchChunksWithProgress = async function(url, onProgress) {
3605
- const response = await fetch(url);
3606
- if (!response.ok) {
3607
- throw new Error(await response.text());
3608
- }
3609
- return await readResponseChunks(response, url, onProgress);
3610
- };
3611
- var fetchArrayBufferWithProgress = async function(url, onProgress) {
3612
- const chunks = await fetchChunksWithProgress(url, onProgress);
3613
- const combined = concatChunks(chunks);
3614
- return toArrayBuffer(combined);
3615
- };
3616
- var fetchBlobWithProgress = async function(url, onProgress) {
3617
- const chunks = await fetchChunksWithProgress(url, onProgress);
3618
- const combined = concatChunks(chunks);
3619
- return new Blob([toArrayBuffer(combined)]);
3620
- };
3621
- var loadGltfWithProgress = async function({
3622
- url,
3623
- onProgress,
3624
- configureLoader
3625
- }) {
3626
- const baseUrl = new URL(".", url).toString();
3627
- const buffer = await fetchArrayBufferWithProgress(url, onProgress);
3628
- const { json, jsonText, isBinary } = parseGltfJson(buffer);
3629
- const resourceUrls = json ? collectExternalResourceUrls(json, baseUrl) : [];
3630
- const resourceMap = /* @__PURE__ */ new Map();
3631
- if (resourceUrls.length > 0) {
3632
- const blobs = await Promise.all(
3633
- resourceUrls.map(async (resourceUrl) => {
3634
- const blob = await fetchBlobWithProgress(resourceUrl, onProgress);
3635
- return { resourceUrl, blob };
3636
- })
3637
- );
3638
- blobs.forEach(({ resourceUrl, blob }) => {
3639
- resourceMap.set(resourceUrl, URL.createObjectURL(blob));
3640
- });
3641
- }
3642
- const manager = new LoadingManager();
3643
- manager.setURLModifier((requestedUrl) => {
3644
- if (requestedUrl.startsWith("blob:") || requestedUrl.startsWith("data:")) {
3645
- return requestedUrl;
3646
- }
3647
- const resolved = resolveResourceUrl(requestedUrl, baseUrl);
3648
- return resourceMap.get(resolved) ?? resourceMap.get(requestedUrl) ?? requestedUrl;
3649
- });
3650
- const loader = new GLTFLoader(manager);
3651
- configureLoader?.(loader);
3652
- const parseInput = isBinary ? buffer : jsonText;
3653
- if (!parseInput) {
3654
- throw new Error("Failed to parse GLTF payload.");
3655
- }
3656
- try {
3657
- return await new Promise((resolve, reject) => {
3658
- loader.parse(parseInput, baseUrl, resolve, reject);
3659
- });
3660
- } finally {
3661
- resourceMap.forEach((blobUrl) => {
3662
- URL.revokeObjectURL(blobUrl);
3663
- });
3664
- }
3665
- };
3666
- var loadVrm = async ({
3667
- baseUrl,
3668
- characterId,
3669
- apiToken,
3670
- onProgress,
3671
- assetProfile
3672
- }) => {
3673
- const url = new URL(`/avatars/${characterId}/download-url`, baseUrl);
3674
- if (assetProfile && assetProfile !== "original") {
3675
- url.searchParams.set("assetProfile", assetProfile);
3676
- }
3677
- const res = await fetch(url.toString(), {
3678
- headers: buildAuthHeaders(apiToken, { allowEmpty: true }),
3679
- credentials: apiToken ? "omit" : "include"
3680
- });
3681
- if (!res.ok) {
3682
- throw new Error(await res.text());
3683
- }
3684
- const { url: vrmUrl } = await res.json();
3685
- const gltf = await loadGltfWithProgress({
3686
- url: vrmUrl,
3687
- onProgress,
3688
- configureLoader: (loader) => {
3689
- loader.register((parser) => new VRMLoaderPlugin(parser));
3690
- }
3691
- });
3692
- VRMUtils.removeUnnecessaryVertices(gltf.scene);
3693
- VRMUtils.combineSkeletons(gltf.scene);
3694
- return gltf.userData.vrm;
3695
- };
3696
- var loadVrma = async ({
3697
- baseUrl,
3698
- characterId,
3699
- animationId,
3700
- apiToken,
3701
- onProgress,
3702
- assetProfile
3703
- }) => {
3704
- const url = new URL(
3705
- `/avatars/${characterId}/animations/${animationId}/download-url`,
3706
- baseUrl
3707
- );
3708
- if (assetProfile && assetProfile !== "original") {
3709
- url.searchParams.set("assetProfile", assetProfile);
3710
- }
3711
- const res = await fetch(url.toString(), {
3712
- headers: buildAuthHeaders(apiToken, { allowEmpty: true }),
3713
- credentials: apiToken ? "omit" : "include"
3714
- });
3715
- if (!res.ok) {
3716
- throw new Error(await res.text());
3717
- }
3718
- const { url: vrmaUrl } = await res.json();
3719
- const gltf = await loadGltfWithProgress({
3720
- url: vrmaUrl,
3721
- onProgress,
3722
- configureLoader: (loader) => {
3723
- loader.register(
3724
- (parser) => new VRMAnimationLoaderPlugin(parser)
3725
- );
3726
- }
3727
- });
3728
- const animations = gltf.userData.vrmAnimations ?? gltf.userData.animations;
3729
- const animationList = Array.isArray(animations) ? animations : [];
3730
- if (animationList.length === 0) {
3731
- throw new Error(
3732
- `No animations found for character ${characterId} and animation ${animationId}`
3733
- );
3734
- }
3735
- const firstAnimation = animationList[0];
3736
- if (!firstAnimation) {
3737
- throw new Error(
3738
- `No animations found for character ${characterId} and animation ${animationId}`
3739
- );
3740
- }
3741
- return firstAnimation;
3742
- };
3743
- var getInfo = async ({
3744
- baseUrl,
3745
- characterId,
3746
- apiToken
3747
- }) => {
3748
- const url = new URL(`/avatars/${characterId}/info`, baseUrl);
3749
- const res = await fetch(url.toString(), {
3750
- headers: buildAuthHeaders(apiToken, { allowEmpty: true }),
3751
- credentials: apiToken ? "omit" : "include"
3752
- });
3753
- if (!res.ok) {
3754
- throw new Error(await res.text());
3755
- }
3756
- return await res.json();
3757
- };
3758
-
3759
- // ../../clients/web-client/src/asset-load-progress.ts
3760
- var createAssetProgressReporter = function(callback) {
3761
- if (!callback) {
3762
- const noop = () => {
3763
- };
3764
- return {
3765
- updateResource: noop
3766
- };
3767
- }
3768
- const resources = /* @__PURE__ */ new Map();
3769
- let lastProgress = -1;
3770
- let lastTotal = -1;
3771
- const updateResource = (resourceId, loaded, total) => {
3772
- const safeLoaded = Math.max(0, loaded);
3773
- const safeTotal = Math.max(0, total);
3774
- resources.set(resourceId, { loaded: safeLoaded, total: safeTotal });
3775
- let summedLoaded = 0;
3776
- let summedTotal = 0;
3777
- for (const resource of resources.values()) {
3778
- if (resource.total <= 0) {
3779
- continue;
3780
- }
3781
- summedLoaded += Math.min(resource.loaded, resource.total);
3782
- summedTotal += resource.total;
3783
- }
3784
- if (summedLoaded === lastProgress && summedTotal === lastTotal) {
3785
- return;
3786
- }
3787
- lastProgress = summedLoaded;
3788
- lastTotal = summedTotal;
3789
- callback({ progress: summedLoaded, total: summedTotal });
3790
- };
3791
- return { updateResource };
3792
- };
3793
-
3794
- // ../vrm-renderer/src/scene.ts
3795
- import {
3796
- Clock,
3797
- PerspectiveCamera,
3798
- Scene,
3799
- WebGLRenderer
3800
- } from "three";
3801
- import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
3802
-
3803
- // ../vrm-renderer/src/core/lighting.ts
3804
- import {
3805
- AmbientLight,
3806
- DirectionalLight,
3807
- NoToneMapping,
3808
- SRGBColorSpace
3809
- } from "three";
3810
- var AVATAR_COLOR_SETTINGS = {
3811
- toneMapping: NoToneMapping,
3812
- outputColorSpace: SRGBColorSpace
3467
+ var defaultVrmTextureCache = new VrmTextureCache();
3468
+
3469
+ // ../vrm-renderer/src/core/lighting.ts
3470
+ import {
3471
+ AmbientLight,
3472
+ DirectionalLight,
3473
+ NoToneMapping,
3474
+ SRGBColorSpace
3475
+ } from "three";
3476
+ var AVATAR_COLOR_SETTINGS = {
3477
+ toneMapping: NoToneMapping,
3478
+ outputColorSpace: SRGBColorSpace
3813
3479
  };
3814
3480
  var addAvatarLights = function(scene, {
3815
3481
  ambientIntensity = 0.7,
@@ -3838,8 +3504,8 @@ var CAMERA_PRESETS = {
3838
3504
  lookAt: [0, 1.4, 0]
3839
3505
  }
3840
3506
  };
3841
- var cameraCollisionRadius = function(near, verticalFovDegrees, aspect) {
3842
- const verticalHalf = Math.max(near, 1e-3) * Math.tan(MathUtils.degToRad(verticalFovDegrees) / 2);
3507
+ var cameraCollisionRadius = function(near2, verticalFovDegrees, aspect) {
3508
+ const verticalHalf = Math.max(near2, 1e-3) * Math.tan(MathUtils.degToRad(verticalFovDegrees) / 2);
3843
3509
  const horizontalHalf = verticalHalf * Math.max(aspect, 1e-3);
3844
3510
  return Math.max(0.06, Math.hypot(horizontalHalf, verticalHalf));
3845
3511
  };
@@ -4134,114 +3800,638 @@ var resolveSceneCamera = function(ctx, deltaSeconds) {
4134
3800
  desiredPosition: ctx.intentCamera.position,
4135
3801
  target: ctx.controls.target
4136
3802
  });
4137
- ctx.camera.position.copy(result.position);
4138
- ctx.camera.quaternion.copy(ctx.intentCamera.quaternion);
4139
- ctx.camera.scale.copy(ctx.intentCamera.scale);
4140
- ctx.camera.lookAt(ctx.controls.target);
4141
- ctx.camera.updateMatrixWorld();
4142
- };
4143
- var disposeSceneContext = function(ctx) {
4144
- ctx.controls.dispose();
4145
- ctx.renderer.dispose();
4146
- ctx.renderer.forceContextLoss();
4147
- };
4148
-
4149
- // ../vrm-renderer/src/preview-controller.ts
4150
- import { VRMUtils as VRMUtils4 } from "@pixiv/three-vrm";
4151
- import { Box3, GridHelper, Vector3 as Vector33 } from "three";
4152
-
4153
- // ../vrm-renderer/src/loaders.ts
4154
- import { VRMLoaderPlugin as VRMLoaderPlugin2, VRMUtils as VRMUtils2 } from "@pixiv/three-vrm";
4155
- import { VRMAnimationLoaderPlugin as VRMAnimationLoaderPlugin2 } from "@pixiv/three-vrm-animation";
4156
- import { GLTFLoader as GLTFLoader2 } from "three/examples/jsm/loaders/GLTFLoader.js";
4157
-
4158
- // ../vrm-renderer/src/session.ts
4159
- import { VRMUtils as VRMUtils3 } from "@pixiv/three-vrm";
4160
- import { AnimationMixer, LoopOnce, LoopRepeat } from "three";
4161
-
4162
- // ../vrm-renderer/src/animation.ts
4163
- import {
4164
- createVRMAnimationClip as createPixivVRMAnimationClip
4165
- } from "@pixiv/three-vrm-animation";
4166
- var NPCIFY_VRM_NORMALIZATION_ROOT_NAME = "NPCifyNormalizationRoot";
4167
- var canonicalAnimationTargets = /* @__PURE__ */ new WeakMap();
4168
- var isNpcifyNormalizedVrm = function(vrm) {
4169
- return vrm.scene.children.some(
4170
- (child) => child.name === NPCIFY_VRM_NORMALIZATION_ROOT_NAME
4171
- );
3803
+ ctx.camera.position.copy(result.position);
3804
+ ctx.camera.quaternion.copy(ctx.intentCamera.quaternion);
3805
+ ctx.camera.scale.copy(ctx.intentCamera.scale);
3806
+ ctx.camera.lookAt(ctx.controls.target);
3807
+ ctx.camera.updateMatrixWorld();
3808
+ };
3809
+ var disposeSceneContext = function(ctx) {
3810
+ ctx.controls.dispose();
3811
+ releaseRendererTextureCache(ctx.renderer);
3812
+ ctx.renderer.dispose();
3813
+ ctx.renderer.forceContextLoss();
3814
+ };
3815
+
3816
+ // ../vrm-renderer/src/preview-controller.ts
3817
+ import { VRMUtils as VRMUtils3 } from "@pixiv/three-vrm";
3818
+ import { Box3, GridHelper, Vector3 as Vector33 } from "three";
3819
+
3820
+ // ../vrm-renderer/src/loaders.ts
3821
+ import { VRMLoaderPlugin, VRMUtils } from "@pixiv/three-vrm";
3822
+ import { VRMAnimationLoaderPlugin } from "@pixiv/three-vrm-animation";
3823
+ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
3824
+
3825
+ // ../vrm-renderer/src/texture-cache-loader.ts
3826
+ import {
3827
+ Cache,
3828
+ ImageBitmapLoader,
3829
+ Texture as Texture2
3830
+ } from "three";
3831
+
3832
+ // ../vrm-renderer/src/texture-expressions.ts
3833
+ import {
3834
+ MToonMaterial,
3835
+ VRMExpressionTextureTransformBind
3836
+ } from "@pixiv/three-vrm";
3837
+
3838
+ // ../vrm-renderer/src/held-mouth-timeline.ts
3839
+ var MinimumExposure = 0.1;
3840
+ var MaximumLinkedGap = 0.3;
3841
+ var UnlinkedExposure = 0.2;
3842
+ var Tolerance = 1e-6;
3843
+ var HeldMouthTimeline = class _HeldMouthTimeline {
3844
+ cues = [];
3845
+ next = 0;
3846
+ shown = null;
3847
+ shownAt = -Infinity;
3848
+ previousTime = -Infinity;
3849
+ static endTime(cue, next, speechEnd = Infinity) {
3850
+ const nextTime = next?.timestamp ?? speechEnd;
3851
+ return Math.min(
3852
+ speechEnd,
3853
+ nextTime - cue.timestamp <= MaximumLinkedGap + Tolerance ? nextTime : cue.timestamp + UnlinkedExposure
3854
+ );
3855
+ }
3856
+ setCues(cues) {
3857
+ this.cues = [...cues].sort((a, b) => a.timestamp - b.timestamp);
3858
+ this.reset();
3859
+ }
3860
+ reset() {
3861
+ this.next = 0;
3862
+ this.shown = null;
3863
+ this.shownAt = -Infinity;
3864
+ this.previousTime = -Infinity;
3865
+ }
3866
+ sample(time, speechEnd = Infinity) {
3867
+ if (!Number.isFinite(time) || time < 0) {
3868
+ this.reset();
3869
+ return null;
3870
+ }
3871
+ if (time < this.previousTime) {
3872
+ this.reset();
3873
+ }
3874
+ this.previousTime = time;
3875
+ while (this.cues[this.next] && this.cues[this.next].timestamp <= time + Tolerance) {
3876
+ this.next += 1;
3877
+ }
3878
+ const cue = this.cues[this.next - 1];
3879
+ const end = cue ? _HeldMouthTimeline.endTime(cue, this.cues[this.next], speechEnd) : 0;
3880
+ const weight = cue?.weight ?? 1;
3881
+ if (!cue || time + Tolerance >= end || !Number.isFinite(weight) || weight <= 0) {
3882
+ this.shown = null;
3883
+ return null;
3884
+ }
3885
+ if (!this.shown || cue.name !== this.shown.name && time - this.shownAt + Tolerance >= MinimumExposure) {
3886
+ this.shown = cue;
3887
+ this.shownAt = time;
3888
+ } else if (cue.name === this.shown.name) {
3889
+ this.shown = cue;
3890
+ }
3891
+ return this.shown;
3892
+ }
3893
+ };
3894
+
3895
+ // ../vrm-renderer/src/texture-expressions.ts
3896
+ var MOUTHS = ["aa", "ih", "ou", "ee", "oh"];
3897
+ var installed = /* @__PURE__ */ new WeakSet();
3898
+ var near = function(a, b) {
3899
+ return Math.abs(a - b) < 1e-5;
3900
+ };
3901
+ var findAtlasBindings = function(vrm) {
3902
+ const bindings = [];
3903
+ for (const [index, name] of MOUTHS.entries()) {
3904
+ const expression = vrm.expressionManager?.getExpression(name);
3905
+ if (!expression?.isBinary || !expression.binds.length) {
3906
+ return [];
3907
+ }
3908
+ for (const bind of expression.binds) {
3909
+ if (!(bind instanceof VRMExpressionTextureTransformBind) || !(bind.material instanceof MToonMaterial) || !near(bind.offset.x, (index + 1) / 6) || !near(bind.offset.y, 0) || !near(bind.scale.x, 1) || !near(bind.scale.y, 1)) {
3910
+ return [];
3911
+ }
3912
+ bindings.push({ expression, bind, column: index + 1 });
3913
+ }
3914
+ }
3915
+ const materials = new Set(bindings.map(({ bind }) => bind.material));
3916
+ for (const material of materials) {
3917
+ if (new Set(
3918
+ bindings.filter(({ bind }) => bind.material === material).map(({ column }) => column)
3919
+ ).size !== 5) {
3920
+ return [];
3921
+ }
3922
+ const face = material;
3923
+ if (!face.map || face.shadeMultiplyTexture && face.shadeMultiplyTexture.image !== face.map.image) {
3924
+ return [];
3925
+ }
3926
+ for (const texture of [face.map, face.shadeMultiplyTexture]) {
3927
+ if (texture && (!near(texture.offset.x, 0) || !near(texture.offset.y, 0) || !near(texture.repeat.x, 1) || !near(texture.repeat.y, 1) || !near(texture.rotation, 0))) {
3928
+ return [];
3929
+ }
3930
+ }
3931
+ let hasBlinkRow = false;
3932
+ let rowDirection = 0;
3933
+ for (const expression of vrm.expressionManager.expressions) {
3934
+ if (MOUTHS.some((name) => name === expression.expressionName)) {
3935
+ continue;
3936
+ }
3937
+ for (const bind of expression.binds) {
3938
+ if (!(bind instanceof VRMExpressionTextureTransformBind) || bind.material !== material) {
3939
+ continue;
3940
+ }
3941
+ const row = Math.round(bind.offset.y * 7);
3942
+ if (!expression.isBinary || !near(bind.offset.x, 0) || !near(bind.offset.y, row / 7) || Math.abs(row) > 6 || row !== 0 && rowDirection !== 0 && Math.sign(row) !== rowDirection || !near(bind.scale.x, 1) || !near(bind.scale.y, 1)) {
3943
+ return [];
3944
+ }
3945
+ if (row !== 0) {
3946
+ rowDirection = Math.sign(row);
3947
+ }
3948
+ hasBlinkRow ||= expression.expressionName === "blink" && Math.abs(row) === 1;
3949
+ }
3950
+ }
3951
+ if (!hasBlinkRow) {
3952
+ return [];
3953
+ }
3954
+ }
3955
+ return bindings;
3956
+ };
3957
+ var hasTextureMouthExpressions = function(manager) {
3958
+ return !!manager && installed.has(manager);
3959
+ };
3960
+ var enableTextureExpressionPlayback = function(vrm) {
3961
+ const manager = vrm.expressionManager;
3962
+ if (!manager || installed.has(manager)) {
3963
+ return;
3964
+ }
3965
+ const bindings = findAtlasBindings(vrm);
3966
+ if (!bindings.length) {
3967
+ return;
3968
+ }
3969
+ installed.add(manager);
3970
+ const materials = new Set(
3971
+ bindings.map(({ bind }) => bind.material)
3972
+ );
3973
+ for (const material of materials) {
3974
+ const mouths = bindings.filter(({ bind }) => bind.material === material);
3975
+ const targets = new Float32Array(6);
3976
+ let selected = 0;
3977
+ for (const { expression, bind, column } of mouths) {
3978
+ expression.isBinary = false;
3979
+ expression.deleteBind(bind);
3980
+ expression.addBind({
3981
+ clearAppliedWeight: () => {
3982
+ targets[column] = 0;
3983
+ bind.clearAppliedWeight();
3984
+ },
3985
+ applyWeight: (weight) => {
3986
+ targets[column] = Number.isFinite(weight) ? Math.max(0, weight) : 0;
3987
+ }
3988
+ });
3989
+ }
3990
+ const originalUpdate = material.update;
3991
+ material.update = function(delta) {
3992
+ let winner = selected;
3993
+ let weight = targets[winner];
3994
+ for (let column = 1; column < 6; column += 1) {
3995
+ if (targets[column] > weight) {
3996
+ winner = column;
3997
+ weight = targets[column];
3998
+ }
3999
+ }
4000
+ selected = weight > 0 ? winner : 0;
4001
+ mouths.find(({ column }) => column === selected)?.bind.applyWeight(1);
4002
+ originalUpdate.call(material, delta);
4003
+ };
4004
+ const dispose = function() {
4005
+ material.removeEventListener("dispose", dispose);
4006
+ material.update = originalUpdate;
4007
+ };
4008
+ material.addEventListener("dispose", dispose);
4009
+ }
4010
+ };
4011
+
4012
+ // ../vrm-renderer/src/session.ts
4013
+ import { VRMUtils as VRMUtils2 } from "@pixiv/three-vrm";
4014
+ import { AnimationMixer, LoopOnce, LoopRepeat } from "three";
4015
+
4016
+ // ../vrm-renderer/src/animation.ts
4017
+ import {
4018
+ createVRMAnimationClip as createPixivVRMAnimationClip
4019
+ } from "@pixiv/three-vrm-animation";
4020
+ var NPCIFY_VRM_NORMALIZATION_ROOT_NAME = "NPCifyNormalizationRoot";
4021
+ var canonicalAnimationTargets = /* @__PURE__ */ new WeakMap();
4022
+ var isNpcifyNormalizedVrm = function(vrm) {
4023
+ return vrm.scene.children.some(
4024
+ (child) => child.name === NPCIFY_VRM_NORMALIZATION_ROOT_NAME
4025
+ );
4026
+ };
4027
+ var canonicalAnimationTarget = function(vrm) {
4028
+ if (vrm.meta.metaVersion !== "0" || !isNpcifyNormalizedVrm(vrm)) {
4029
+ return vrm;
4030
+ }
4031
+ const cached = canonicalAnimationTargets.get(vrm);
4032
+ if (cached) {
4033
+ return cached;
4034
+ }
4035
+ const target = new Proxy(vrm, {
4036
+ get(source, property, receiver) {
4037
+ if (property === "meta") {
4038
+ return { ...source.meta, metaVersion: "1" };
4039
+ }
4040
+ return Reflect.get(source, property, receiver);
4041
+ }
4042
+ });
4043
+ canonicalAnimationTargets.set(vrm, target);
4044
+ return target;
4045
+ };
4046
+ var createVRMAnimationClip = function(animation, vrm) {
4047
+ return createPixivVRMAnimationClip(animation, canonicalAnimationTarget(vrm));
4048
+ };
4049
+
4050
+ // ../vrm-renderer/src/session.ts
4051
+ var attachVrm = function(ctx, vrm) {
4052
+ ctx.scene.add(vrm.scene);
4053
+ setCameraCollisionBackend(
4054
+ ctx,
4055
+ new HumanoidCapsuleCollisionBackend(
4056
+ (name) => vrm.humanoid.getRawBoneNode(name)
4057
+ )
4058
+ );
4059
+ const mixer = new AnimationMixer(vrm.scene);
4060
+ return { vrm, mixer, activeAction: null, animationFrameId: null };
4061
+ };
4062
+ var detachVrm = function(ctx, session, options = {}) {
4063
+ stopRenderLoop(session);
4064
+ session.mixer.stopAllAction();
4065
+ if (session.vrm.scene.parent) {
4066
+ session.vrm.scene.parent.remove(session.vrm.scene);
4067
+ }
4068
+ setCameraCollisionBackend(ctx, null);
4069
+ session.mixer.uncacheRoot(session.vrm.scene);
4070
+ if (options.dispose) {
4071
+ disposeVrm(session.vrm);
4072
+ }
4073
+ };
4074
+ var disposeVrm = function(vrm) {
4075
+ disposeObject3D(vrm.scene);
4076
+ };
4077
+ var disposeObject3D = function(object) {
4078
+ VRMUtils2.deepDispose(object);
4079
+ };
4080
+ var stopRenderLoop = function(session) {
4081
+ if (session.animationFrameId === null) {
4082
+ return;
4083
+ }
4084
+ cancelAnimationFrame(session.animationFrameId);
4085
+ session.animationFrameId = null;
4086
+ };
4087
+ var setCameraView = function(ctx, position, lookAt) {
4088
+ ctx.camera.position.set(...position);
4089
+ ctx.camera.lookAt(...lookAt);
4090
+ ctx.intentCamera.position.set(...position);
4091
+ ctx.intentCamera.lookAt(...lookAt);
4092
+ ctx.controls.target.set(...lookAt);
4093
+ ctx.controls.update();
4094
+ resolveSceneCamera(ctx, 0);
4095
+ };
4096
+ var applyCameraPreset = function(ctx, preset) {
4097
+ const config = CAMERA_PRESETS[preset];
4098
+ setCameraView(ctx, config.position, config.lookAt);
4099
+ };
4100
+
4101
+ // ../../clients/web-client/src/api/info.ts
4102
+ import {
4103
+ VRMAnimationLoaderPlugin as VRMAnimationLoaderPlugin2
4104
+ } from "@pixiv/three-vrm-animation";
4105
+ import { LoadingManager } from "three";
4106
+ import { GLTFLoader as GLTFLoader2 } from "three/examples/jsm/loaders/GLTFLoader.js";
4107
+ var GLB_HEADER_MAGIC = 1179937895;
4108
+ var GLB_CHUNK_TYPE_JSON = 1313821514;
4109
+ var textDecoder = new TextDecoder();
4110
+ var isRecord2 = function(value) {
4111
+ return typeof value === "object" && value !== null;
4112
+ };
4113
+ var resolveResourceUrl = function(url, baseUrl) {
4114
+ try {
4115
+ return new URL(url, baseUrl).toString();
4116
+ } catch {
4117
+ return url;
4118
+ }
4119
+ };
4120
+ var parseContentLength = function(response) {
4121
+ if (response.headers.get("content-encoding")) {
4122
+ return null;
4123
+ }
4124
+ const header = response.headers.get("content-length");
4125
+ if (!header) {
4126
+ return null;
4127
+ }
4128
+ const parsed = Number(header);
4129
+ if (!Number.isFinite(parsed) || parsed <= 0) {
4130
+ return null;
4131
+ }
4132
+ return parsed;
4133
+ };
4134
+ var parseGltfJson = function(data) {
4135
+ if (data.byteLength < 12) {
4136
+ return { json: null, jsonText: null, isBinary: false };
4137
+ }
4138
+ const view = new DataView(data);
4139
+ const magic = view.getUint32(0, true);
4140
+ if (magic === GLB_HEADER_MAGIC) {
4141
+ let offset = 12;
4142
+ while (offset + 8 <= view.byteLength) {
4143
+ const chunkLength = view.getUint32(offset, true);
4144
+ const chunkType = view.getUint32(offset + 4, true);
4145
+ offset += 8;
4146
+ if (chunkType === GLB_CHUNK_TYPE_JSON) {
4147
+ const chunkData = new Uint8Array(data, offset, chunkLength);
4148
+ const jsonText2 = textDecoder.decode(chunkData);
4149
+ try {
4150
+ const json = JSON.parse(jsonText2);
4151
+ return { json, jsonText: jsonText2, isBinary: true };
4152
+ } catch {
4153
+ return { json: null, jsonText: jsonText2, isBinary: true };
4154
+ }
4155
+ }
4156
+ offset += chunkLength;
4157
+ }
4158
+ return { json: null, jsonText: null, isBinary: true };
4159
+ }
4160
+ const jsonText = textDecoder.decode(new Uint8Array(data));
4161
+ try {
4162
+ const json = JSON.parse(jsonText);
4163
+ return { json, jsonText, isBinary: false };
4164
+ } catch {
4165
+ return { json: null, jsonText, isBinary: false };
4166
+ }
4167
+ };
4168
+ var collectExternalResourceUrls = function(gltfJson, baseUrl) {
4169
+ const urls = /* @__PURE__ */ new Set();
4170
+ const addUri = function(uri) {
4171
+ if (!uri) {
4172
+ return;
4173
+ }
4174
+ if (uri.startsWith("data:") || uri.startsWith("blob:")) {
4175
+ return;
4176
+ }
4177
+ urls.add(resolveResourceUrl(uri, baseUrl));
4178
+ };
4179
+ const collectFrom = function(entries) {
4180
+ if (!Array.isArray(entries)) {
4181
+ return;
4182
+ }
4183
+ entries.forEach((entry) => {
4184
+ if (!isRecord2(entry)) {
4185
+ return;
4186
+ }
4187
+ const uri = entry.uri;
4188
+ if (typeof uri !== "string") {
4189
+ return;
4190
+ }
4191
+ addUri(uri);
4192
+ });
4193
+ };
4194
+ collectFrom(gltfJson.buffers);
4195
+ collectFrom(gltfJson.images);
4196
+ return Array.from(urls);
4197
+ };
4198
+ var readResponseChunks = async function(response, resourceId, onProgress) {
4199
+ const total = parseContentLength(response);
4200
+ onProgress?.(resourceId, 0, total ?? 0);
4201
+ if (!response.body) {
4202
+ const buffer = await response.arrayBuffer();
4203
+ const loaded2 = buffer.byteLength;
4204
+ onProgress?.(resourceId, loaded2, total ?? loaded2);
4205
+ return [new Uint8Array(buffer)];
4206
+ }
4207
+ const reader = response.body.getReader();
4208
+ const chunks = [];
4209
+ let loaded = 0;
4210
+ while (true) {
4211
+ const { done, value } = await reader.read();
4212
+ if (done) {
4213
+ break;
4214
+ }
4215
+ if (!value) {
4216
+ continue;
4217
+ }
4218
+ chunks.push(value);
4219
+ loaded += value.length;
4220
+ onProgress?.(resourceId, loaded, total ?? 0);
4221
+ }
4222
+ onProgress?.(resourceId, loaded, total ?? loaded);
4223
+ return chunks;
4224
+ };
4225
+ var concatChunks = function(chunks) {
4226
+ if (chunks.length === 0) {
4227
+ return new Uint8Array();
4228
+ }
4229
+ if (chunks.length === 1) {
4230
+ return chunks[0] ?? new Uint8Array();
4231
+ }
4232
+ const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
4233
+ const combined = new Uint8Array(totalLength);
4234
+ let offset = 0;
4235
+ for (const chunk of chunks) {
4236
+ combined.set(chunk, offset);
4237
+ offset += chunk.length;
4238
+ }
4239
+ return combined;
4240
+ };
4241
+ var toArrayBuffer = function(data) {
4242
+ const buffer = new ArrayBuffer(data.byteLength);
4243
+ new Uint8Array(buffer).set(data);
4244
+ return buffer;
4245
+ };
4246
+ var fetchChunksWithProgress = async function(url, onProgress) {
4247
+ const response = await fetch(url);
4248
+ if (!response.ok) {
4249
+ throw new Error(await response.text());
4250
+ }
4251
+ return await readResponseChunks(response, url, onProgress);
4252
+ };
4253
+ var fetchArrayBufferWithProgress = async function(url, onProgress) {
4254
+ const chunks = await fetchChunksWithProgress(url, onProgress);
4255
+ const combined = concatChunks(chunks);
4256
+ return toArrayBuffer(combined);
4257
+ };
4258
+ var fetchBlobWithProgress = async function(url, onProgress) {
4259
+ const chunks = await fetchChunksWithProgress(url, onProgress);
4260
+ const combined = concatChunks(chunks);
4261
+ return new Blob([toArrayBuffer(combined)]);
4262
+ };
4263
+ var loadGltfWithProgress = async function({
4264
+ url,
4265
+ onProgress,
4266
+ configureLoader
4267
+ }) {
4268
+ const baseUrl = new URL(".", url).toString();
4269
+ const buffer = await fetchArrayBufferWithProgress(url, onProgress);
4270
+ const { json, jsonText, isBinary } = parseGltfJson(buffer);
4271
+ const resourceUrls = json ? collectExternalResourceUrls(json, baseUrl) : [];
4272
+ const resourceMap = /* @__PURE__ */ new Map();
4273
+ if (resourceUrls.length > 0) {
4274
+ const blobs = await Promise.all(
4275
+ resourceUrls.map(async (resourceUrl) => {
4276
+ const blob = await fetchBlobWithProgress(resourceUrl, onProgress);
4277
+ return { resourceUrl, blob };
4278
+ })
4279
+ );
4280
+ blobs.forEach(({ resourceUrl, blob }) => {
4281
+ resourceMap.set(resourceUrl, URL.createObjectURL(blob));
4282
+ });
4283
+ }
4284
+ const manager = new LoadingManager();
4285
+ manager.setURLModifier((requestedUrl) => {
4286
+ if (requestedUrl.startsWith("blob:") || requestedUrl.startsWith("data:")) {
4287
+ return requestedUrl;
4288
+ }
4289
+ const resolved = resolveResourceUrl(requestedUrl, baseUrl);
4290
+ return resourceMap.get(resolved) ?? resourceMap.get(requestedUrl) ?? requestedUrl;
4291
+ });
4292
+ const loader = new GLTFLoader2(manager);
4293
+ configureLoader?.(loader);
4294
+ const parseInput = isBinary ? buffer : jsonText;
4295
+ if (!parseInput) {
4296
+ throw new Error("Failed to parse GLTF payload.");
4297
+ }
4298
+ try {
4299
+ return await new Promise((resolve, reject) => {
4300
+ loader.parse(parseInput, baseUrl, resolve, reject);
4301
+ });
4302
+ } finally {
4303
+ resourceMap.forEach((blobUrl) => {
4304
+ URL.revokeObjectURL(blobUrl);
4305
+ });
4306
+ }
4172
4307
  };
4173
- var canonicalAnimationTarget = function(vrm) {
4174
- if (vrm.meta.metaVersion !== "0" || !isNpcifyNormalizedVrm(vrm)) {
4175
- return vrm;
4308
+ var loadVrm = async ({
4309
+ baseUrl,
4310
+ characterId,
4311
+ apiToken,
4312
+ onProgress,
4313
+ assetProfile
4314
+ }) => {
4315
+ const url = new URL(`/avatars/${characterId}/download-url`, baseUrl);
4316
+ if (assetProfile && assetProfile !== "original") {
4317
+ url.searchParams.set("assetProfile", assetProfile);
4176
4318
  }
4177
- const cached = canonicalAnimationTargets.get(vrm);
4178
- if (cached) {
4179
- return cached;
4319
+ const res = await fetch(url.toString(), {
4320
+ headers: buildAuthHeaders(apiToken, { allowEmpty: true }),
4321
+ credentials: apiToken ? "omit" : "include"
4322
+ });
4323
+ if (!res.ok) {
4324
+ throw new Error(await res.text());
4180
4325
  }
4181
- const target = new Proxy(vrm, {
4182
- get(source, property, receiver) {
4183
- if (property === "meta") {
4184
- return { ...source.meta, metaVersion: "1" };
4185
- }
4186
- return Reflect.get(source, property, receiver);
4326
+ const { url: vrmUrl } = await res.json();
4327
+ const gltf = await loadGltfWithProgress({
4328
+ url: vrmUrl,
4329
+ onProgress,
4330
+ configureLoader: (loader) => {
4331
+ loader.register((parser) => new VRMLoaderPlugin2(parser));
4187
4332
  }
4188
4333
  });
4189
- canonicalAnimationTargets.set(vrm, target);
4190
- return target;
4191
- };
4192
- var createVRMAnimationClip = function(animation, vrm) {
4193
- return createPixivVRMAnimationClip(animation, canonicalAnimationTarget(vrm));
4334
+ VRMUtils4.removeUnnecessaryVertices(gltf.scene);
4335
+ VRMUtils4.combineSkeletons(gltf.scene);
4336
+ enableTextureExpressionPlayback(gltf.userData.vrm);
4337
+ return gltf.userData.vrm;
4194
4338
  };
4195
-
4196
- // ../vrm-renderer/src/session.ts
4197
- var attachVrm = function(ctx, vrm) {
4198
- ctx.scene.add(vrm.scene);
4199
- setCameraCollisionBackend(
4200
- ctx,
4201
- new HumanoidCapsuleCollisionBackend(
4202
- (name) => vrm.humanoid.getRawBoneNode(name)
4203
- )
4339
+ var loadVrma = async ({
4340
+ baseUrl,
4341
+ characterId,
4342
+ animationId,
4343
+ apiToken,
4344
+ onProgress,
4345
+ assetProfile
4346
+ }) => {
4347
+ const url = new URL(
4348
+ `/avatars/${characterId}/animations/${animationId}/download-url`,
4349
+ baseUrl
4204
4350
  );
4205
- const mixer = new AnimationMixer(vrm.scene);
4206
- return { vrm, mixer, activeAction: null, animationFrameId: null };
4207
- };
4208
- var detachVrm = function(ctx, session, options = {}) {
4209
- stopRenderLoop(session);
4210
- session.mixer.stopAllAction();
4211
- if (session.vrm.scene.parent) {
4212
- session.vrm.scene.parent.remove(session.vrm.scene);
4351
+ if (assetProfile && assetProfile !== "original") {
4352
+ url.searchParams.set("assetProfile", assetProfile);
4213
4353
  }
4214
- setCameraCollisionBackend(ctx, null);
4215
- session.mixer.uncacheRoot(session.vrm.scene);
4216
- if (options.dispose) {
4217
- disposeVrm(session.vrm);
4354
+ const res = await fetch(url.toString(), {
4355
+ headers: buildAuthHeaders(apiToken, { allowEmpty: true }),
4356
+ credentials: apiToken ? "omit" : "include"
4357
+ });
4358
+ if (!res.ok) {
4359
+ throw new Error(await res.text());
4218
4360
  }
4219
- };
4220
- var disposeVrm = function(vrm) {
4221
- disposeObject3D(vrm.scene);
4222
- };
4223
- var disposeObject3D = function(object) {
4224
- VRMUtils3.deepDispose(object);
4225
- };
4226
- var stopRenderLoop = function(session) {
4227
- if (session.animationFrameId === null) {
4228
- return;
4361
+ const { url: vrmaUrl } = await res.json();
4362
+ const gltf = await loadGltfWithProgress({
4363
+ url: vrmaUrl,
4364
+ onProgress,
4365
+ configureLoader: (loader) => {
4366
+ loader.register(
4367
+ (parser) => new VRMAnimationLoaderPlugin2(parser)
4368
+ );
4369
+ }
4370
+ });
4371
+ const animations = gltf.userData.vrmAnimations ?? gltf.userData.animations;
4372
+ const animationList = Array.isArray(animations) ? animations : [];
4373
+ if (animationList.length === 0) {
4374
+ throw new Error(
4375
+ `No animations found for character ${characterId} and animation ${animationId}`
4376
+ );
4229
4377
  }
4230
- cancelAnimationFrame(session.animationFrameId);
4231
- session.animationFrameId = null;
4378
+ const firstAnimation = animationList[0];
4379
+ if (!firstAnimation) {
4380
+ throw new Error(
4381
+ `No animations found for character ${characterId} and animation ${animationId}`
4382
+ );
4383
+ }
4384
+ return firstAnimation;
4232
4385
  };
4233
- var setCameraView = function(ctx, position, lookAt) {
4234
- ctx.camera.position.set(...position);
4235
- ctx.camera.lookAt(...lookAt);
4236
- ctx.intentCamera.position.set(...position);
4237
- ctx.intentCamera.lookAt(...lookAt);
4238
- ctx.controls.target.set(...lookAt);
4239
- ctx.controls.update();
4240
- resolveSceneCamera(ctx, 0);
4386
+ var getInfo = async ({
4387
+ baseUrl,
4388
+ characterId,
4389
+ apiToken
4390
+ }) => {
4391
+ const url = new URL(`/avatars/${characterId}/info`, baseUrl);
4392
+ const res = await fetch(url.toString(), {
4393
+ headers: buildAuthHeaders(apiToken, { allowEmpty: true }),
4394
+ credentials: apiToken ? "omit" : "include"
4395
+ });
4396
+ if (!res.ok) {
4397
+ throw new Error(await res.text());
4398
+ }
4399
+ return await res.json();
4241
4400
  };
4242
- var applyCameraPreset = function(ctx, preset) {
4243
- const config = CAMERA_PRESETS[preset];
4244
- setCameraView(ctx, config.position, config.lookAt);
4401
+
4402
+ // ../../clients/web-client/src/asset-load-progress.ts
4403
+ var createAssetProgressReporter = function(callback) {
4404
+ if (!callback) {
4405
+ const noop = () => {
4406
+ };
4407
+ return {
4408
+ updateResource: noop
4409
+ };
4410
+ }
4411
+ const resources = /* @__PURE__ */ new Map();
4412
+ let lastProgress = -1;
4413
+ let lastTotal = -1;
4414
+ const updateResource = (resourceId, loaded, total) => {
4415
+ const safeLoaded = Math.max(0, loaded);
4416
+ const safeTotal = Math.max(0, total);
4417
+ resources.set(resourceId, { loaded: safeLoaded, total: safeTotal });
4418
+ let summedLoaded = 0;
4419
+ let summedTotal = 0;
4420
+ for (const resource of resources.values()) {
4421
+ if (resource.total <= 0) {
4422
+ continue;
4423
+ }
4424
+ summedLoaded += Math.min(resource.loaded, resource.total);
4425
+ summedTotal += resource.total;
4426
+ }
4427
+ if (summedLoaded === lastProgress && summedTotal === lastTotal) {
4428
+ return;
4429
+ }
4430
+ lastProgress = summedLoaded;
4431
+ lastTotal = summedTotal;
4432
+ callback({ progress: summedLoaded, total: summedTotal });
4433
+ };
4434
+ return { updateResource };
4245
4435
  };
4246
4436
 
4247
4437
  // ../../clients/web-client/src/character/animation.ts
@@ -4275,6 +4465,8 @@ var AnimationController = class {
4275
4465
  mixer;
4276
4466
  clips = /* @__PURE__ */ new Map();
4277
4467
  configs = /* @__PURE__ */ new Map();
4468
+ previewMixer = null;
4469
+ restorePreview = null;
4278
4470
  activeAction = null;
4279
4471
  idleAnimationId;
4280
4472
  pendingReturnToIdleAction = null;
@@ -4289,11 +4481,35 @@ var AnimationController = class {
4289
4481
  weight: config?.weight ?? defaultAnimationConfig.weight
4290
4482
  });
4291
4483
  }
4484
+ previewAnimation(animation) {
4485
+ this.restorePreview?.();
4486
+ const clip = createVRMAnimationClip(animation, this.vrm);
4487
+ const previewMixer = new AnimationMixer2(this.vrm.scene);
4488
+ previewMixer.clipAction(clip).setLoop(LoopRepeat2, Infinity).reset().play();
4489
+ this.previewMixer = previewMixer;
4490
+ const restore = () => {
4491
+ if (this.restorePreview !== restore) {
4492
+ return;
4493
+ }
4494
+ this.restorePreview = null;
4495
+ previewMixer.stopAllAction();
4496
+ previewMixer.uncacheRoot(this.vrm.scene);
4497
+ this.previewMixer = null;
4498
+ if (!this.activeAction) {
4499
+ this.vrm.humanoid.resetNormalizedPose();
4500
+ }
4501
+ this.mixer.update(0);
4502
+ };
4503
+ this.restorePreview = restore;
4504
+ previewMixer.update(0);
4505
+ return restore;
4506
+ }
4292
4507
  setIdleAnimation(animationId) {
4293
4508
  this.idleAnimationId = animationId;
4294
4509
  this.playIdle();
4295
4510
  }
4296
4511
  clearAnimations() {
4512
+ this.restorePreview?.();
4297
4513
  this.mixer.stopAllAction();
4298
4514
  this.mixer.uncacheRoot(this.vrm.scene);
4299
4515
  this.clips.clear();
@@ -4304,13 +4520,15 @@ var AnimationController = class {
4304
4520
  this.vrm.humanoid.resetNormalizedPose();
4305
4521
  }
4306
4522
  playAnimation(animationId, fadeDuration = 0.25, loopOverride) {
4523
+ this.restorePreview?.();
4307
4524
  const clip = this.clips.get(animationId);
4308
4525
  if (!clip) {
4309
4526
  throw new Error(`Animation ${animationId} not found`);
4310
4527
  }
4311
4528
  const config = this.configs.get(animationId) ?? defaultAnimationConfig;
4312
4529
  const action = this.mixer.clipAction(clip);
4313
- const loop = loopOverride ?? config.loop;
4530
+ const isIdleAnimation = animationId === this.idleAnimationId;
4531
+ const loop = isIdleAnimation || (loopOverride ?? config.loop);
4314
4532
  if (loop) {
4315
4533
  action.setLoop(LoopRepeat2, Infinity);
4316
4534
  action.clampWhenFinished = false;
@@ -4329,14 +4547,14 @@ var AnimationController = class {
4329
4547
  action.reset().play();
4330
4548
  }
4331
4549
  this.activeAction = action;
4332
- const isIdleAnimation = animationId === this.idleAnimationId;
4333
- if (!config.loop && !isIdleAnimation && this.idleAnimationId) {
4550
+ if (!loop && this.idleAnimationId) {
4334
4551
  this.pendingReturnToIdleAction = action;
4335
4552
  } else {
4336
4553
  this.pendingReturnToIdleAction = null;
4337
4554
  }
4338
4555
  }
4339
4556
  playIdle(fadeDuration = 0.25) {
4557
+ this.restorePreview?.();
4340
4558
  if (!this.idleAnimationId || !this.clips.has(this.idleAnimationId)) {
4341
4559
  this.mixer.stopAllAction();
4342
4560
  this.activeAction = null;
@@ -4350,6 +4568,7 @@ var AnimationController = class {
4350
4568
  this.playAnimation(this.idleAnimationId, fadeDuration, true);
4351
4569
  }
4352
4570
  stopAnimation(fadeDuration = 0.25) {
4571
+ this.restorePreview?.();
4353
4572
  if (!this.activeAction) {
4354
4573
  return;
4355
4574
  }
@@ -4358,7 +4577,8 @@ var AnimationController = class {
4358
4577
  this.pendingReturnToIdleAction = null;
4359
4578
  }
4360
4579
  update(delta) {
4361
- this.mixer.update(delta);
4580
+ ;
4581
+ (this.previewMixer ?? this.mixer).update(delta);
4362
4582
  }
4363
4583
  handleActionFinished = (event) => {
4364
4584
  if (!this.idleAnimationId || !this.pendingReturnToIdleAction || event.action !== this.pendingReturnToIdleAction || !this.clips.has(this.idleAnimationId)) {
@@ -4661,9 +4881,17 @@ var AudioPlayer = class {
4661
4881
  cleanupListeners();
4662
4882
  resolve();
4663
4883
  };
4664
- const onError = () => {
4884
+ const failPlayback = (error) => {
4665
4885
  cleanupListeners();
4666
- reject(new Error("Audio playback failed"));
4886
+ if (!options?.signal?.aborted) {
4887
+ element.pause();
4888
+ element.removeAttribute("src");
4889
+ element.load();
4890
+ }
4891
+ reject(error);
4892
+ };
4893
+ const onError = () => {
4894
+ failPlayback(new Error("Audio playback failed"));
4667
4895
  };
4668
4896
  const onAbort = () => {
4669
4897
  element.pause();
@@ -4674,10 +4902,7 @@ var AudioPlayer = class {
4674
4902
  element.addEventListener("ended", onEnded);
4675
4903
  element.addEventListener("error", onError);
4676
4904
  options?.signal?.addEventListener("abort", onAbort, { once: true });
4677
- void element.play().then(markStarted).catch((error) => {
4678
- cleanupListeners();
4679
- reject(error);
4680
- });
4905
+ void element.play().then(markStarted).catch(failPlayback);
4681
4906
  });
4682
4907
  } finally {
4683
4908
  options?.signal?.removeEventListener("abort", abortSource);
@@ -4765,7 +4990,7 @@ var AudioPlayer = class {
4765
4990
  return this.audioElement;
4766
4991
  }
4767
4992
  const element = document.createElement("audio");
4768
- element.autoplay = true;
4993
+ element.autoplay = false;
4769
4994
  element.setAttribute("playsinline", "true");
4770
4995
  element.preload = "auto";
4771
4996
  element.style.display = "none";
@@ -5005,6 +5230,7 @@ var PerformanceWeightMixer = class {
5005
5230
  ...definition,
5006
5231
  id,
5007
5232
  responseRate: normalizeResponseRate(definition.responseRate),
5233
+ transition: definition.transition ?? "smooth",
5008
5234
  weight
5009
5235
  },
5010
5236
  order: this.nextLayerOrder,
@@ -5081,12 +5307,13 @@ var PerformanceWeightMixer = class {
5081
5307
  const resolved = /* @__PURE__ */ new Map();
5082
5308
  this.orderedLayers.forEach((layer) => {
5083
5309
  const responseRate = layer.definition.responseRate;
5310
+ const frameDelta = layer.definition.transition === "step" ? 0 : delta;
5084
5311
  layer.value = clamp01(
5085
- smoothValue(layer.value, layer.target, responseRate, delta)
5312
+ smoothValue(layer.value, layer.target, responseRate, frameDelta)
5086
5313
  );
5087
5314
  layer.channels.forEach((channel, name) => {
5088
5315
  channel.value = clamp01(
5089
- smoothValue(channel.value, channel.target, responseRate, delta)
5316
+ smoothValue(channel.value, channel.target, responseRate, frameDelta)
5090
5317
  );
5091
5318
  const current = resolved.get(name) ?? 0;
5092
5319
  const contribution = channel.value * layer.value;
@@ -5212,6 +5439,9 @@ var FacialPerformanceMixer = class {
5212
5439
  getVisemeChannelNames() {
5213
5440
  return this.getLayerChannels(FacialPerformanceLayer.Viseme);
5214
5441
  }
5442
+ get usesDiscreteMouths() {
5443
+ return hasTextureMouthExpressions(this.vrm?.expressionManager);
5444
+ }
5215
5445
  playCue(channel, options) {
5216
5446
  if (!this.availableChannelSet.has(channel)) {
5217
5447
  return;
@@ -5372,7 +5602,8 @@ var FacialPerformanceMixer = class {
5372
5602
  this.weightMixer.registerLayer({
5373
5603
  id: FacialPerformanceLayer.Viseme,
5374
5604
  priority: 200,
5375
- blendMode: "override"
5605
+ blendMode: "override",
5606
+ transition: this.usesDiscreteMouths ? "step" : "smooth"
5376
5607
  });
5377
5608
  this.weightMixer.registerLayer({
5378
5609
  id: FacialPerformanceLayer.Blink,
@@ -6121,6 +6352,7 @@ var VisemePlayer = class {
6121
6352
  phase = "idle";
6122
6353
  running = false;
6123
6354
  timelineRevision = 0;
6355
+ heldMouths = new HeldMouthTimeline();
6124
6356
  attachAudioElement(element) {
6125
6357
  if (this.audioElement === element) {
6126
6358
  return;
@@ -6155,6 +6387,7 @@ var VisemePlayer = class {
6155
6387
  this.rawVisemes = visemes.map((viseme) => ({ ...viseme }));
6156
6388
  this.normalizedTimeline = normalizeVisemeTimeline(this.rawVisemes);
6157
6389
  this.visemes = this.normalizedTimeline.entries;
6390
+ this.heldMouths.setCues(this.visemes);
6158
6391
  this.timelineRevision += 1;
6159
6392
  this.resetPlaybackState();
6160
6393
  this.recordTimeline();
@@ -6216,6 +6449,10 @@ var VisemePlayer = class {
6216
6449
  if (currentTime === null) {
6217
6450
  return;
6218
6451
  }
6452
+ if (this.facialMixer.usesDiscreteMouths) {
6453
+ this.updateHeldMouth(currentTime);
6454
+ return;
6455
+ }
6219
6456
  const dueCueIndices = this.collectDueCueIndices(currentTime);
6220
6457
  const latestDueCueIndex = dueCueIndices.at(-1);
6221
6458
  dueCueIndices.slice(0, -1).forEach((cueIndex) => {
@@ -6246,6 +6483,34 @@ var VisemePlayer = class {
6246
6483
  state: "playing"
6247
6484
  });
6248
6485
  };
6486
+ updateHeldMouth(currentTime) {
6487
+ const duration = this.audioElement?.duration;
6488
+ const speechEnd = Number.isFinite(duration) ? duration : void 0;
6489
+ const cue = this.heldMouths.sample(currentTime, speechEnd);
6490
+ if (!cue) {
6491
+ if (this.activeCueIndex !== null) {
6492
+ this.releaseToNeutral("scheduled");
6493
+ }
6494
+ return;
6495
+ }
6496
+ if (cue === this.visemes[this.activeCueIndex ?? -1]) {
6497
+ return;
6498
+ }
6499
+ const index = this.visemes.indexOf(cue);
6500
+ this.facialMixer.playCue(cue.name, { weight: 1, durationMs: 0 });
6501
+ this.activeCueIndex = index;
6502
+ this.nextVisemeIndex = index + 1;
6503
+ this.phase = "open";
6504
+ this.recordCueDecision(index, currentTime, "played");
6505
+ }
6506
+ cueDuration(cue, next) {
6507
+ if (this.facialMixer.usesDiscreteMouths) {
6508
+ const duration = this.audioElement?.duration;
6509
+ const speechEnd = Number.isFinite(duration) ? duration : void 0;
6510
+ return (HeldMouthTimeline.endTime(cue, next, speechEnd) - cue.timestamp) * 1e3;
6511
+ }
6512
+ return getVisemeDurationMs(cue, next);
6513
+ }
6249
6514
  handleEnded = () => {
6250
6515
  this.recorder?.recordEvent({
6251
6516
  type: "audio.state",
@@ -6374,7 +6639,7 @@ var VisemePlayer = class {
6374
6639
  if (!cue) {
6375
6640
  return;
6376
6641
  }
6377
- const openDurationMs = getVisemeDurationMs(cue, this.visemes[cueIndex + 1]);
6642
+ const openDurationMs = this.cueDuration(cue, this.visemes[cueIndex + 1]);
6378
6643
  const elapsedMs = Math.max(currentTime - cue.timestamp, 0) * 1e3;
6379
6644
  this.recorder?.recordEvent({
6380
6645
  type: "viseme.cue",
@@ -6419,6 +6684,7 @@ var VisemePlayer = class {
6419
6684
  this.audioElement.removeEventListener("ended", this.handleEnded);
6420
6685
  }
6421
6686
  resetPlaybackState() {
6687
+ this.heldMouths.reset();
6422
6688
  this.running = false;
6423
6689
  this.nextVisemeIndex = 0;
6424
6690
  this.activeCueIndex = null;
@@ -7117,6 +7383,10 @@ var StreamPlaybackController = class {
7117
7383
  this.flushOptions(state);
7118
7384
  return true;
7119
7385
  } catch (error) {
7386
+ if (this.states.get(params.stagingId) !== state || this.activePlayback?.stagingId !== params.stagingId) {
7387
+ this.recordPlaybackEvent(params, "cancelled", this.errorReason(error));
7388
+ return false;
7389
+ }
7120
7390
  if (isPlaybackCancelledError(error)) {
7121
7391
  this.recordPlaybackEvent(params, "cancelled", this.errorReason(error));
7122
7392
  return false;