@doki-land/live2d-renderer 0.0.17 → 0.0.19

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.d.ts CHANGED
@@ -34,7 +34,7 @@ interface TextureData {
34
34
  }
35
35
  interface ModelDrawPass {
36
36
  setTextures(textures: TextureData[]): void;
37
- draw(drawables: DrawableMesh[], modelMatrix: Float32Array): void;
37
+ draw(drawables: readonly DrawableMesh[], modelMatrix: Float32Array): void;
38
38
  destroy(): void;
39
39
  }
40
40
  /** Top-level renderer bound to a canvas. */
@@ -138,123 +138,6 @@ declare class WebGpuRendererImpl implements WebGpuRenderer {
138
138
  declare function createWebGpuRenderer(options?: WebGpuRendererOptions): WebGpuRenderer;
139
139
  declare function isWebGpuAvailable(): boolean;
140
140
 
141
- /**
142
- * Map Live2D blend modes to WebGL / Canvas compositing.
143
- */
144
-
145
- /** Apply premultiplied-friendly Live2D blend factors on a WebGL2 context. */
146
- declare function applyWebGl2BlendMode(gl: WebGL2RenderingContext, mode: BlendMode): void;
147
- /** Canvas2D globalCompositeOperation for Live2D blend modes. */
148
- declare function canvasCompositeForBlendMode(mode: BlendMode): GlobalCompositeOperation;
149
- /** WebGPU blend state for Live2D modes (straight alpha draw path). */
150
- declare function webGpuBlendState(mode: BlendMode): GPUBlendState;
151
-
152
- /**
153
- * Clipping-mask context grouping + mask atlas layout.
154
- *
155
- * Two packing modes:
156
- * - `uv-grid`: √N UV cells, alpha channel only (Canvas2D / simple path)
157
- * - `rgba`: Cubism-style channel × UV packing (≤4 → full UV on R/G/B/A)
158
- */
159
- interface ClippingDrawableRef {
160
- readonly index: number;
161
- readonly maskIndices: readonly number[];
162
- readonly invertedMask: boolean;
163
- }
164
- interface ClippingContext {
165
- /** Stable key: invert flag + sorted mask drawable indices. */
166
- readonly key: string;
167
- readonly maskIndices: readonly number[];
168
- readonly clippedIndices: readonly number[];
169
- readonly invertedMask: boolean;
170
- }
171
- /** UV-space rectangle inside the shared mask atlas (0..1). */
172
- interface MaskLayoutRect {
173
- readonly x: number;
174
- readonly y: number;
175
- readonly width: number;
176
- readonly height: number;
177
- }
178
- /** R/G/B/A write/sample selector (Cubism channelFlag). */
179
- type MaskChannelFlag = readonly [number, number, number, number];
180
- declare const MASK_CHANNEL_FLAGS: readonly MaskChannelFlag[];
181
- type MaskAtlasMode = "uv-grid" | "rgba";
182
- interface MaskAtlasOptions {
183
- /** Packing strategy. Default `rgba` (Cubism density). */
184
- readonly mode?: MaskAtlasMode;
185
- /** Cell inset for `uv-grid` only (reduces neighbour bleed). */
186
- readonly inset?: number;
187
- /** Render-texture count for `rgba` (default 1 → max 36). */
188
- readonly renderTextureCount?: number;
189
- }
190
- interface LaidOutClippingContext extends ClippingContext {
191
- readonly layout: MaskLayoutRect;
192
- /** 0=R … 3=A. `uv-grid` always uses A (3). */
193
- readonly channelIndex: number;
194
- readonly channelFlag: MaskChannelFlag;
195
- /** Cubism multi-RT buffer index; currently always 0. */
196
- readonly bufferIndex: number;
197
- /**
198
- * Expanded AABB of clipped drawables in vertex/NDC space.
199
- * Mask write/sample map this rect into `layout` (Cubism bounds-fit).
200
- * Default before `fitClippingContexts`: full NDC (-1..1)².
201
- */
202
- readonly modelBounds: MaskLayoutRect;
203
- }
204
- /** Full clip-space quad used when no valid clipped bounds exist. */
205
- declare const FULL_NDC_BOUNDS: MaskLayoutRect;
206
- interface ClippingPartition {
207
- readonly contexts: readonly LaidOutClippingContext[];
208
- /** Drawables that consume a mask (drawn after mask pass). */
209
- readonly clipped: ReadonlySet<number>;
210
- /** Drawables used as masks (mask buffer only, not color target). */
211
- readonly maskOnly: ReadonlySet<number>;
212
- }
213
- /**
214
- * Group drawables that consume clipping masks into shared contexts.
215
- * Drawables with empty maskIndices are omitted.
216
- */
217
- declare function buildClippingContexts(drawables: readonly ClippingDrawableRef[]): ClippingContext[];
218
- /**
219
- * Pack clipping contexts into a square-ish UV grid on one atlas (alpha only).
220
- */
221
- declare function layoutMaskAtlasUvGrid(contexts: readonly ClippingContext[], options?: {
222
- inset?: number;
223
- }): LaidOutClippingContext[];
224
- /**
225
- * Cubism `setupLayoutBounds`: pack across R/G/B/A then UV cells.
226
- * Single RT → max 36 (4×9); multi-RT → 32 per sheet.
227
- */
228
- declare function layoutMaskAtlasRgba(contexts: readonly ClippingContext[], options?: {
229
- renderTextureCount?: number;
230
- }): LaidOutClippingContext[];
231
- /** Pack clipping contexts (default: Cubism RGBA density). */
232
- declare function layoutMaskAtlas(contexts: readonly ClippingContext[], options?: MaskAtlasOptions): LaidOutClippingContext[];
233
- /** Partition drawables and assign atlas layouts. */
234
- declare function partitionForClipping(drawables: readonly ClippingDrawableRef[], options?: MaskAtlasOptions): ClippingPartition;
235
- /** Axis-aligned bounds of interleaved xy vertex positions. */
236
- declare function calcVertexBounds(positions: Float32Array): MaskLayoutRect | null;
237
- /** Expand bounds by a relative margin on each axis (Cubism uses 0.05). */
238
- declare function expandBounds(bounds: MaskLayoutRect, margin?: number): MaskLayoutRect;
239
- interface MaskBoundsMeshRef {
240
- readonly index: number;
241
- readonly vertexPositions: Float32Array;
242
- }
243
- /**
244
- * Union AABB of clipped drawables, expanded by margin (Cubism
245
- * `calcClippedDrawableTotalBounds` + 0.05 expand).
246
- */
247
- declare function calcClippedDrawableBounds(clippedIndices: readonly number[], byIndex: ReadonlyMap<number, MaskBoundsMeshRef>, margin?: number): MaskLayoutRect;
248
- /**
249
- * Attach per-context `modelBounds` so mask write/sample fit the clipped
250
- * drawable AABB into the atlas cell (higher mask texel density).
251
- */
252
- declare function fitClippingContexts(contexts: readonly LaidOutClippingContext[], byIndex: ReadonlyMap<number, MaskBoundsMeshRef>, margin?: number): LaidOutClippingContext[];
253
- /** Flatten layout to `[x, y, w, h]` for GPU uniforms. */
254
- declare function maskLayoutVec4(layout: MaskLayoutRect): Float32Array;
255
- /** Flatten channel flag to `[r, g, b, a]` for GPU uniforms. */
256
- declare function maskChannelVec4(flag: MaskChannelFlag): Float32Array;
257
-
258
141
  declare const CPU_PROGRAM_KIND: "cpu-program";
259
142
  interface CpuProgramFile {
260
143
  readonly kind: typeof CPU_PROGRAM_KIND;
@@ -294,27 +177,11 @@ declare function parseCpuProgram(bytes: ArrayBuffer): ModelProgram;
294
177
  /** Create a runtime instance with default parameter values. */
295
178
  declare function createModelInstance(program: ModelProgram): ModelInstance;
296
179
  declare function setParameterValue(instance: ModelInstance, parameterId: string, value: number): void;
297
- /** CPU deform 鈫?FrameSnapshot. */
180
+ /** CPU deform FrameSnapshot. */
298
181
  declare function evaluateFrame(instance: ModelInstance): FrameSnapshot;
299
182
  /** Stable fingerprint for golden tests (positions + topology). */
300
183
  declare function fingerprintSnapshot(snapshot: FrameSnapshot): string;
301
184
 
302
- interface CreateRendererOptions {
303
- /**
304
- * Backend try order at initialize time.
305
- * Default: `["webgpu", "webgl2", "canvas2d"]`.
306
- */
307
- prefer?: RendererKind[];
308
- webgpu?: WebGpuRendererOptions;
309
- webgl2?: WebGl2RendererOptions;
310
- canvas2d?: Canvas2DRendererOptions;
311
- }
312
- /**
313
- * Create a renderer provider. Actual backend is chosen on `initialize`
314
- * (WebGPU → WebGL2 → Canvas2D by default).
315
- */
316
- declare function createRenderer(options?: CreateRendererOptions): Renderer;
317
-
318
185
  /**
319
186
  * moc binary format peek. Settings JSON detection lives in live2d-core
320
187
  * (`detectModelSettingsFormat`) so loader and renderer share one helper.
@@ -412,6 +279,11 @@ interface ModelBackend {
412
279
  setParameter?(model: InternalModel, id: string, value: number): void;
413
280
  /** Optional PartOpacity override (moc3 parts / pose / motion). */
414
281
  setPartOpacity?(model: InternalModel, id: string, value: number): void;
282
+ /**
283
+ * Resolve parameter id → index (O(1) after load).
284
+ * Used by motion blend / hosts instead of `listParameters().find`.
285
+ */
286
+ resolveParameter?(model: InternalModel, id: string): number | undefined;
415
287
  listParameters?(model: InternalModel): readonly ParameterBinding[];
416
288
  }
417
289
  declare function selectModelBackend(backends: ModelBackend[], json: unknown): ModelBackend;
@@ -425,9 +297,12 @@ declare class Moc2Backend implements ModelBackend {
425
297
  getDrawables(model: InternalModel): DrawableMesh[];
426
298
  captureFrame(model: InternalModel): FrameSnapshot | null;
427
299
  setParameter(model: InternalModel, id: string, value: number): void;
300
+ resolveParameter(model: InternalModel, id: string): number | undefined;
428
301
  listParameters(model: InternalModel): readonly ParameterBinding[];
429
302
  hitTest(_model: InternalModel, _x: number, _y: number): string | null;
430
303
  destroyModel(model: InternalModel): void;
304
+ /** Test/harness: stable program + instance refs. */
305
+ getResidentInstance(model: InternalModel): ModelInstance | null;
431
306
  }
432
307
  declare function createMoc2Backend(): Moc2Backend;
433
308
 
@@ -441,9 +316,11 @@ declare class Moc3Backend implements ModelBackend {
441
316
  captureFrame(model: InternalModel): FrameSnapshot | null;
442
317
  setParameter(model: InternalModel, id: string, value: number): void;
443
318
  setPartOpacity(model: InternalModel, id: string, value: number): void;
319
+ resolveParameter(model: InternalModel, id: string): number | undefined;
444
320
  listParameters(model: InternalModel): readonly ParameterBinding[];
445
321
  hitTest(_model: InternalModel, _x: number, _y: number): string | null;
446
322
  destroyModel(model: InternalModel): void;
323
+ getResidentInstance(model: InternalModel): ModelInstance | null;
447
324
  }
448
325
  declare function createMoc3Backend(): Moc3Backend;
449
326
 
@@ -522,6 +399,123 @@ declare function applyMoc3Glues(positionsByMesh: Map<number, Float32Array>, glue
522
399
  /** Mean Euclidean distance between glued vertex pairs (for tests). */
523
400
  declare function meanGlueSeamDistance(positionsByMesh: ReadonlyMap<number, Float32Array>, glue: Moc3GlueDef): number;
524
401
 
402
+ /**
403
+ * Map Live2D blend modes to WebGL / Canvas compositing.
404
+ */
405
+
406
+ /** Apply premultiplied-friendly Live2D blend factors on a WebGL2 context. */
407
+ declare function applyWebGl2BlendMode(gl: WebGL2RenderingContext, mode: BlendMode): void;
408
+ /** Canvas2D globalCompositeOperation for Live2D blend modes. */
409
+ declare function canvasCompositeForBlendMode(mode: BlendMode): GlobalCompositeOperation;
410
+ /** WebGPU blend state for Live2D modes (straight alpha draw path). */
411
+ declare function webGpuBlendState(mode: BlendMode): GPUBlendState;
412
+
413
+ /**
414
+ * Clipping-mask context grouping + mask atlas layout.
415
+ *
416
+ * Two packing modes:
417
+ * - `uv-grid`: √N UV cells, alpha channel only (Canvas2D / simple path)
418
+ * - `rgba`: Cubism-style channel × UV packing (≤4 → full UV on R/G/B/A)
419
+ */
420
+ interface ClippingDrawableRef {
421
+ readonly index: number;
422
+ readonly maskIndices: readonly number[];
423
+ readonly invertedMask: boolean;
424
+ }
425
+ interface ClippingContext {
426
+ /** Stable key: invert flag + sorted mask drawable indices. */
427
+ readonly key: string;
428
+ readonly maskIndices: readonly number[];
429
+ readonly clippedIndices: readonly number[];
430
+ readonly invertedMask: boolean;
431
+ }
432
+ /** UV-space rectangle inside the shared mask atlas (0..1). */
433
+ interface MaskLayoutRect {
434
+ readonly x: number;
435
+ readonly y: number;
436
+ readonly width: number;
437
+ readonly height: number;
438
+ }
439
+ /** R/G/B/A write/sample selector (Cubism channelFlag). */
440
+ type MaskChannelFlag = readonly [number, number, number, number];
441
+ declare const MASK_CHANNEL_FLAGS: readonly MaskChannelFlag[];
442
+ type MaskAtlasMode = "uv-grid" | "rgba";
443
+ interface MaskAtlasOptions {
444
+ /** Packing strategy. Default `rgba` (Cubism density). */
445
+ readonly mode?: MaskAtlasMode;
446
+ /** Cell inset for `uv-grid` only (reduces neighbour bleed). */
447
+ readonly inset?: number;
448
+ /** Render-texture count for `rgba` (default 1 → max 36). */
449
+ readonly renderTextureCount?: number;
450
+ }
451
+ interface LaidOutClippingContext extends ClippingContext {
452
+ readonly layout: MaskLayoutRect;
453
+ /** 0=R … 3=A. `uv-grid` always uses A (3). */
454
+ readonly channelIndex: number;
455
+ readonly channelFlag: MaskChannelFlag;
456
+ /** Cubism multi-RT buffer index; currently always 0. */
457
+ readonly bufferIndex: number;
458
+ /**
459
+ * Expanded AABB of clipped drawables in vertex/NDC space.
460
+ * Mask write/sample map this rect into `layout` (Cubism bounds-fit).
461
+ * Default before `fitClippingContexts`: full NDC (-1..1)².
462
+ */
463
+ readonly modelBounds: MaskLayoutRect;
464
+ }
465
+ /** Full clip-space quad used when no valid clipped bounds exist. */
466
+ declare const FULL_NDC_BOUNDS: MaskLayoutRect;
467
+ interface ClippingPartition {
468
+ readonly contexts: readonly LaidOutClippingContext[];
469
+ /** Drawables that consume a mask (drawn after mask pass). */
470
+ readonly clipped: ReadonlySet<number>;
471
+ /** Drawables used as masks (mask buffer only, not color target). */
472
+ readonly maskOnly: ReadonlySet<number>;
473
+ }
474
+ /**
475
+ * Group drawables that consume clipping masks into shared contexts.
476
+ * Drawables with empty maskIndices are omitted.
477
+ */
478
+ declare function buildClippingContexts(drawables: readonly ClippingDrawableRef[]): ClippingContext[];
479
+ /**
480
+ * Pack clipping contexts into a square-ish UV grid on one atlas (alpha only).
481
+ */
482
+ declare function layoutMaskAtlasUvGrid(contexts: readonly ClippingContext[], options?: {
483
+ inset?: number;
484
+ }): LaidOutClippingContext[];
485
+ /**
486
+ * Cubism `setupLayoutBounds`: pack across R/G/B/A then UV cells.
487
+ * Single RT → max 36 (4×9); multi-RT → 32 per sheet.
488
+ */
489
+ declare function layoutMaskAtlasRgba(contexts: readonly ClippingContext[], options?: {
490
+ renderTextureCount?: number;
491
+ }): LaidOutClippingContext[];
492
+ /** Pack clipping contexts (default: Cubism RGBA density). */
493
+ declare function layoutMaskAtlas(contexts: readonly ClippingContext[], options?: MaskAtlasOptions): LaidOutClippingContext[];
494
+ /** Partition drawables and assign atlas layouts. */
495
+ declare function partitionForClipping(drawables: readonly ClippingDrawableRef[], options?: MaskAtlasOptions): ClippingPartition;
496
+ /** Axis-aligned bounds of interleaved xy vertex positions. */
497
+ declare function calcVertexBounds(positions: Float32Array): MaskLayoutRect | null;
498
+ /** Expand bounds by a relative margin on each axis (Cubism uses 0.05). */
499
+ declare function expandBounds(bounds: MaskLayoutRect, margin?: number): MaskLayoutRect;
500
+ interface MaskBoundsMeshRef {
501
+ readonly index: number;
502
+ readonly vertexPositions: Float32Array;
503
+ }
504
+ /**
505
+ * Union AABB of clipped drawables, expanded by margin (Cubism
506
+ * `calcClippedDrawableTotalBounds` + 0.05 expand).
507
+ */
508
+ declare function calcClippedDrawableBounds(clippedIndices: readonly number[], byIndex: ReadonlyMap<number, MaskBoundsMeshRef>, margin?: number): MaskLayoutRect;
509
+ /**
510
+ * Attach per-context `modelBounds` so mask write/sample fit the clipped
511
+ * drawable AABB into the atlas cell (higher mask texel density).
512
+ */
513
+ declare function fitClippingContexts(contexts: readonly LaidOutClippingContext[], byIndex: ReadonlyMap<number, MaskBoundsMeshRef>, margin?: number): LaidOutClippingContext[];
514
+ /** Flatten layout to `[x, y, w, h]` for GPU uniforms. */
515
+ declare function maskLayoutVec4(layout: MaskLayoutRect): Float32Array;
516
+ /** Flatten channel flag to `[r, g, b, a]` for GPU uniforms. */
517
+ declare function maskChannelVec4(flag: MaskChannelFlag): Float32Array;
518
+
525
519
  /** Shared untextured preview colors (Canvas2D / WebGL2 / WebGPU parity). */
526
520
  declare const PREVIEW_FILL: {
527
521
  readonly r: number;
@@ -538,6 +532,22 @@ declare const PREVIEW_STROKE: {
538
532
  /** Expand triangle indices into a line-list (each edge twice-wound). */
539
533
  declare function triangleEdgesToLineList(indices: Uint16Array): Uint16Array;
540
534
 
535
+ interface CreateRendererOptions {
536
+ /**
537
+ * Backend try order at initialize time.
538
+ * Default: `["webgpu", "webgl2", "canvas2d"]`.
539
+ */
540
+ prefer?: RendererKind[];
541
+ webgpu?: WebGpuRendererOptions;
542
+ webgl2?: WebGl2RendererOptions;
543
+ canvas2d?: Canvas2DRendererOptions;
544
+ }
545
+ /**
546
+ * Create a renderer provider. Actual backend is chosen on `initialize`
547
+ * (WebGPU → WebGL2 → Canvas2D by default).
548
+ */
549
+ declare function createRenderer(options?: CreateRendererOptions): Renderer;
550
+
541
551
  /** One-time decode of moc bytes for stage-level {@link ModelAsset} sharing. */
542
552
  declare function compileSharedModelCompile(settings: ModelSettings, mocBytes: ArrayBuffer): SharedModelCompile;
543
553