@mochiya/avatar-composition 0.1.2 → 0.3.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/dist/index.js CHANGED
@@ -1,7 +1,12 @@
1
+ // src/api.ts
2
+ import {
3
+ MathUtils
4
+ } from "three";
5
+
1
6
  // src/schema.ts
2
7
  import { z } from "zod";
3
8
  var EXTENSION_NAME = "MOCHIYA_avatar_composition";
4
- var SPEC_VERSION = "0.1";
9
+ var SPEC_VERSION = "0.2";
5
10
  var ASSET_KINDS = ["avatar", "attachment"];
6
11
  var CAPABILITIES = [
7
12
  "mergeArmature",
@@ -174,6 +179,8 @@ var CompositionError = class extends Error {
174
179
  code;
175
180
  };
176
181
  function parseManifest(input) {
182
+ if (typeof input === "object" && input !== null && !Array.isArray(input) && "specVersion" in input && input.specVersion === "0.1")
183
+ input = { ...input, specVersion: SPEC_VERSION };
177
184
  const result = manifestSchema.safeParse(input);
178
185
  if (!result.success)
179
186
  throw new CompositionError(
@@ -203,17 +210,6 @@ function menuItems(manifest) {
203
210
  }
204
211
 
205
212
  // src/asset.ts
206
- var MochiyaAvatarCompositionLoaderPlugin = class {
207
- constructor(parser) {
208
- this.parser = parser;
209
- }
210
- parser;
211
- name = EXTENSION_NAME;
212
- async afterRoot(gltf) {
213
- const value = this.parser.json.extensions?.[EXTENSION_NAME];
214
- if (value) gltf.userData.mochiyaAvatarComposition = parseManifest(value);
215
- }
216
- };
217
213
  var imageOwners = /* @__PURE__ */ new WeakMap();
218
214
  var AvatarAsset = class {
219
215
  constructor(scene, options = {}) {
@@ -269,6 +265,10 @@ var AvatarAsset = class {
269
265
  this.authoredActive.set(node, entry.active);
270
266
  node.visible = entry.active;
271
267
  }
268
+ for (const [node, active] of this.authoredActive) {
269
+ this.localActive.set(node, active);
270
+ this.evaluatedActive.set(node, active);
271
+ }
272
272
  for (const image of this.imageBitmaps)
273
273
  imageOwners.set(image, (imageOwners.get(image) ?? 0) + 1);
274
274
  }
@@ -284,7 +284,11 @@ var AvatarAsset = class {
284
284
  primitiveSlots = /* @__PURE__ */ new Map();
285
285
  materials;
286
286
  restWorld = /* @__PURE__ */ new Map();
287
+ activationOverrides = /* @__PURE__ */ new Map();
288
+ localActive = /* @__PURE__ */ new Map();
289
+ evaluatedActive = /* @__PURE__ */ new Map();
287
290
  authoredActive = /* @__PURE__ */ new Map();
291
+ sourceNames = /* @__PURE__ */ new Map();
288
292
  authoredParents = /* @__PURE__ */ new Map();
289
293
  rootRestWorld;
290
294
  manifest;
@@ -292,6 +296,7 @@ var AvatarAsset = class {
292
296
  nodeMeshes(node) {
293
297
  if (node.isMesh) return [node];
294
298
  return this.meshes.filter((mesh) => {
299
+ if (this.indices.has(mesh)) return false;
295
300
  for (let p = mesh.parent; p; p = p.parent) {
296
301
  if (p === node) return true;
297
302
  if (this.indices.has(p)) return false;
@@ -304,16 +309,23 @@ var AvatarAsset = class {
304
309
  return this.scene.matrixWorld.clone().multiply(this.rootRestWorld.clone().invert()).multiply(this.restWorld.get(node) ?? node.matrixWorld);
305
310
  }
306
311
  };
307
- async function prepareAvatarAsset(gltf) {
312
+ async function resolveAvatarDependencies(parser) {
313
+ const [nodes, materials] = await Promise.all([
314
+ parser.getDependencies("node"),
315
+ parser.getDependencies("material")
316
+ ]);
317
+ return { nodes, materials };
318
+ }
319
+ function prepareAvatarAsset(gltf, dependencies) {
308
320
  const parser = gltf.parser;
309
321
  const nodes = /* @__PURE__ */ new Map();
310
322
  const aliases = /* @__PURE__ */ new Map();
311
- const loaded = await parser.getDependencies("node");
323
+ const loaded = dependencies.nodes;
312
324
  loaded.forEach((node, index2) => {
313
325
  nodes.set(index2, node);
314
326
  aliases.set(node, [parser.json.nodes?.[index2]?.name ?? ""]);
315
327
  });
316
- const materials = await parser.getDependencies("material");
328
+ const materials = dependencies.materials;
317
329
  const textures = /* @__PURE__ */ new Set();
318
330
  for (const resource of parser.associations.keys())
319
331
  if (resource.isTexture) textures.add(resource);
@@ -359,6 +371,8 @@ async function prepareAvatarAsset(gltf) {
359
371
  if (association?.primitives !== void 0)
360
372
  asset.primitiveSlots.set(mesh, association.primitives);
361
373
  }
374
+ for (const [node, names] of aliases)
375
+ asset.sourceNames.set(node, names[0] || node.name);
362
376
  return asset;
363
377
  }
364
378
  function disposeAvatarAsset(asset) {
@@ -396,6 +410,11 @@ function disposeAvatarAsset(asset) {
396
410
  throw new AggregateError(errors, "Asset resource cleanup failed");
397
411
  }
398
412
 
413
+ // src/session.ts
414
+ import {
415
+ AnimationMixer
416
+ } from "three";
417
+
399
418
  // src/matching.ts
400
419
  function tokens(value) {
401
420
  return value.normalize("NFKC").replace(/([a-z\d])([A-Z])/g, "$1 $2").toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean);
@@ -592,15 +611,12 @@ var NameResolver = class {
592
611
  }
593
612
  };
594
613
 
595
- // src/session.ts
596
- import {
597
- AnimationMixer
598
- } from "three";
599
-
600
614
  // src/operations.ts
601
615
  function operations(component, sourceOrder) {
602
616
  const common2 = {
603
617
  componentId: component.id,
618
+ sourceNode: component.sourceNode,
619
+ automaticMenu: component.type === "menuItem",
604
620
  sourceOrder,
605
621
  condition: "condition" in component ? component.condition : void 0
606
622
  };
@@ -662,9 +678,9 @@ import {
662
678
  var ShapeDeletion = class {
663
679
  states = /* @__PURE__ */ new Map();
664
680
  apply(requests, registered) {
665
- const live = new Set(registered.map((r) => r.mesh));
681
+ const live2 = new Set(registered.map((r) => r.mesh));
666
682
  for (const [mesh, state] of this.states)
667
- if (!live.has(mesh)) {
683
+ if (!live2.has(mesh)) {
668
684
  this.release(mesh, state);
669
685
  this.states.delete(mesh);
670
686
  }
@@ -780,17 +796,17 @@ function createIndexView(source) {
780
796
  if (previous) return previous;
781
797
  let result;
782
798
  if ("data" in attribute) {
783
- let data = interleaved.get(attribute.data);
784
- if (!data) {
785
- data = new InterleavedBuffer(
799
+ let data2 = interleaved.get(attribute.data);
800
+ if (!data2) {
801
+ data2 = new InterleavedBuffer(
786
802
  attribute.data.array,
787
803
  attribute.data.stride
788
804
  );
789
- data.setUsage(attribute.data.usage);
790
- interleaved.set(attribute.data, data);
805
+ data2.setUsage(attribute.data.usage);
806
+ interleaved.set(attribute.data, data2);
791
807
  }
792
808
  result = new InterleavedBufferAttribute(
793
- data,
809
+ data2,
794
810
  attribute.itemSize,
795
811
  attribute.offset,
796
812
  attribute.normalized
@@ -1244,7 +1260,7 @@ var AvatarCompositionSession = class {
1244
1260
  item.warnings.push({
1245
1261
  code: "OVERLAPPING_WRITERS",
1246
1262
  operation: action.action.id,
1247
- message: `This action overlaps ${other.id}; layer order and instance ID determine the final value.`
1263
+ message: `This action overlaps ${other.id}; layer order and insertion order determine the final value.`
1248
1264
  });
1249
1265
  this.bindRig(item);
1250
1266
  item.status = item.requestedBones > 0 && item.matchedBones === 0 ? "unbound" : item.warnings.length ? "partial" : "attached";
@@ -1266,7 +1282,7 @@ var AvatarCompositionSession = class {
1266
1282
  }
1267
1283
  setItemState(id, patch) {
1268
1284
  this.requireLive();
1269
- const item = this.items.get(id);
1285
+ const item = id === "base" ? this.baseItem : this.items.get(id);
1270
1286
  if (!item)
1271
1287
  throw new CompositionError("MISSING_INSTANCE", `Unknown item ${id}`);
1272
1288
  this.restoreLayers();
@@ -1364,7 +1380,8 @@ var AvatarCompositionSession = class {
1364
1380
  afterVrmUpdate(delta) {
1365
1381
  this.requireLive();
1366
1382
  for (const item of this.items.values()) {
1367
- if (item.state.visible === false) continue;
1383
+ if (item.state.visible === false || item.asset.localActive.get(item.asset.scene) === false)
1384
+ continue;
1368
1385
  const vrm = item.asset.vrm;
1369
1386
  vrm?.expressionManager?.update();
1370
1387
  vrm?.nodeConstraintManager?.update();
@@ -1422,7 +1439,7 @@ var AvatarCompositionSession = class {
1422
1439
  return [
1423
1440
  this.baseItem,
1424
1441
  ...[...this.items.values()].sort(
1425
- (a, b) => (a.state.layerOrder ?? 0) - (b.state.layerOrder ?? 0) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)
1442
+ (a, b) => (a.state.layerOrder ?? 0) - (b.state.layerOrder ?? 0)
1426
1443
  )
1427
1444
  ];
1428
1445
  }
@@ -1498,16 +1515,16 @@ var AvatarCompositionSession = class {
1498
1515
  logicalActive(item, node, asset) {
1499
1516
  if (asset === item.asset && item.state.visible === false) return false;
1500
1517
  for (let current = node; current && asset.authoredActive.has(current); current = asset.authoredParents.get(current) ?? null)
1501
- if (!current.visible) return false;
1518
+ if (!(asset.evaluatedActive.get(current) ?? true)) return false;
1502
1519
  return true;
1503
1520
  }
1504
1521
  condition(item, condition) {
1505
1522
  if (!condition) return () => true;
1506
1523
  if (condition.type === "control") {
1507
- const controls = menuItems(item.asset.manifest).filter(
1524
+ const controls2 = menuItems(item.asset.manifest).filter(
1508
1525
  (c) => (c.parameter ?? c.id) === condition.control
1509
1526
  );
1510
- const control = controls.find((c) => Number(c.defaultValue) !== 0) ?? controls[0];
1527
+ const control = controls2.find((c) => Number(c.defaultValue) !== 0) ?? controls2[0];
1511
1528
  if (!control)
1512
1529
  throw new CompositionError(
1513
1530
  "INVALID_CONTROL",
@@ -1557,21 +1574,49 @@ var AvatarCompositionSession = class {
1557
1574
  }
1558
1575
  };
1559
1576
  }
1560
- visibleBinding(node) {
1577
+ visibleBinding(node, owner) {
1578
+ const asset = owner ?? [this.baseItem, ...this.items.values()].find(
1579
+ (i) => i.asset.authoredActive.has(node)
1580
+ )?.asset;
1561
1581
  return {
1562
1582
  key: `${node.uuid}:active`,
1583
+ get: () => asset?.evaluatedActive.get(node) ?? node.visible,
1584
+ set: (value) => {
1585
+ if (asset)
1586
+ asset.evaluatedActive.set(
1587
+ node,
1588
+ Boolean(value) && asset.activationOverrides.get(node) !== false
1589
+ );
1590
+ else node.visible = Boolean(value);
1591
+ }
1592
+ };
1593
+ }
1594
+ renderBinding(node) {
1595
+ return {
1596
+ key: `${node.uuid}:render`,
1563
1597
  get: () => node.visible,
1564
- set: (v) => {
1565
- node.visible = Boolean(v);
1598
+ set: (value) => {
1599
+ node.visible = Boolean(value);
1566
1600
  }
1567
1601
  };
1568
1602
  }
1603
+ refresh() {
1604
+ this.requireLive();
1605
+ this.restoreLayers();
1606
+ this.evaluate();
1607
+ }
1569
1608
  compile(item, action) {
1570
1609
  const condition = this.condition(item, action.condition);
1571
1610
  if (!condition) return;
1572
1611
  const result = {
1573
1612
  action,
1574
- condition,
1613
+ condition: () => {
1614
+ if (item.state.visible === false || item.asset.localActive.get(item.asset.scene) === false)
1615
+ return false;
1616
+ const source = item.asset.nodes.get(action.sourceNode);
1617
+ const inverted = action.condition?.inverse && action.type !== "morph.sync";
1618
+ return (action.automaticMenu || inverted || !source || this.logicalActive(item, source, item.asset)) && condition();
1619
+ },
1575
1620
  execute: () => {
1576
1621
  },
1577
1622
  reads: [],
@@ -1579,6 +1624,10 @@ var AvatarCompositionSession = class {
1579
1624
  conditionReads: [],
1580
1625
  targets: []
1581
1626
  };
1627
+ if (!action.automaticMenu && !action.condition?.inverse) {
1628
+ for (let node = item.asset.nodes.get(action.sourceNode); node; node = item.asset.authoredParents.get(node) ?? void 0)
1629
+ result.conditionReads.push(this.visibleBinding(node, item.asset).key);
1630
+ }
1582
1631
  if (action.condition?.type === "nodeActive") {
1583
1632
  const selector = {
1584
1633
  asset: action.condition.asset ?? "self",
@@ -1659,7 +1708,10 @@ var AvatarCompositionSession = class {
1659
1708
  action.id
1660
1709
  );
1661
1710
  if (!target) return;
1662
- const binding = this.visibleBinding(target);
1711
+ const binding = this.visibleBinding(
1712
+ target,
1713
+ item.resolver.asset(action.target)
1714
+ );
1663
1715
  result.targets = [target.name];
1664
1716
  result.writes = [binding.key];
1665
1717
  result.execute = () => this.write(binding, action.value);
@@ -1762,16 +1814,19 @@ var AvatarCompositionSession = class {
1762
1814
  const items = this.orderedItems();
1763
1815
  if (phase !== "properties") {
1764
1816
  for (const item of items) {
1765
- if (item !== this.baseItem)
1766
- this.write(
1767
- this.visibleBinding(item.asset.scene),
1768
- item.state.visible !== false
1769
- );
1817
+ for (const [node, active] of item.asset.localActive)
1818
+ item.asset.evaluatedActive.set(node, active);
1819
+ if (item.state.visible === false)
1820
+ item.asset.evaluatedActive.set(item.asset.scene, false);
1770
1821
  for (const [index2, active] of Object.entries(
1771
1822
  item.state.nodeVisibility ?? {}
1772
1823
  )) {
1773
1824
  const node = item.asset.nodes.get(Number(index2));
1774
- if (node) this.write(this.visibleBinding(node), active);
1825
+ if (node) {
1826
+ item.asset.localActive.set(node, active);
1827
+ item.asset.activationOverrides.set(node, active);
1828
+ item.asset.evaluatedActive.set(node, active);
1829
+ }
1775
1830
  }
1776
1831
  }
1777
1832
  const pending = items.flatMap(
@@ -1791,9 +1846,11 @@ var AvatarCompositionSession = class {
1791
1846
  };
1792
1847
  pending.forEach(execute);
1793
1848
  for (const item of items)
1794
- for (const mesh of item.asset.meshes)
1795
- if (!this.logicalActive(item, mesh, item.asset))
1796
- this.write(this.visibleBinding(mesh), false);
1849
+ for (const node of item.asset.authoredActive.keys())
1850
+ this.write(
1851
+ this.renderBinding(node),
1852
+ this.logicalActive(item, node, item.asset) && (item === this.baseItem || this.base.localActive.get(this.base.scene) !== false)
1853
+ );
1797
1854
  for (const type of ["material.swap"])
1798
1855
  for (const item of items)
1799
1856
  for (const compiled of [...item.actions].sort(
@@ -1853,26 +1910,313 @@ var AvatarCompositionSession = class {
1853
1910
  }
1854
1911
  }
1855
1912
  };
1913
+
1914
+ // src/api.ts
1915
+ function createAvatarAsset(scene, input = {}) {
1916
+ const asset = new AvatarAsset(scene, input);
1917
+ for (const [mesh, slot] of input.primitiveSlots ?? [])
1918
+ asset.primitiveSlots.set(mesh, slot);
1919
+ for (const [object, name] of input.sourceNames ?? [])
1920
+ asset.sourceNames.set(object, name);
1921
+ return new Asset(asset);
1922
+ }
1923
+ var data = /* @__PURE__ */ new WeakMap();
1924
+ var sessions = /* @__PURE__ */ new WeakMap();
1925
+ var controls = /* @__PURE__ */ new WeakMap();
1926
+ function live(asset) {
1927
+ if (asset.disposed)
1928
+ throw new CompositionError(
1929
+ "DISPOSED_ASSET",
1930
+ "This avatar asset has been disposed."
1931
+ );
1932
+ }
1933
+ var Item = class {
1934
+ constructor(asset, object, parent) {
1935
+ this.asset = asset;
1936
+ this.object = object;
1937
+ this.parent = parent;
1938
+ this.name = asset.manifest?.nodes.find(
1939
+ (node) => node.node === asset.indices.get(object)
1940
+ )?.aliases[0] || asset.sourceNames.get(object) || object.name;
1941
+ this.source = {
1942
+ node: asset.indices.get(object),
1943
+ path: parent ? [...parent.source.path, asset.sourceNames.get(object) || object.name] : []
1944
+ };
1945
+ this.three = { object3D: object, renderMeshes: asset.nodeMeshes(object) };
1946
+ }
1947
+ asset;
1948
+ object;
1949
+ parent;
1950
+ id = MathUtils.generateUUID();
1951
+ name;
1952
+ children = [];
1953
+ source;
1954
+ three;
1955
+ get activeSelf() {
1956
+ return this.asset.localActive.get(this.object) !== false;
1957
+ }
1958
+ get activeInHierarchy() {
1959
+ return this.asset.evaluatedActive.get(this.object) !== false && (this.parent?.activeInHierarchy ?? true);
1960
+ }
1961
+ setActive(active) {
1962
+ live(this.asset);
1963
+ this.asset.localActive.set(this.object, active);
1964
+ this.asset.activationOverrides.set(this.object, active);
1965
+ const owner = sessions.get(this.asset);
1966
+ if (owner) owner.session.refresh();
1967
+ else this.refreshDetached();
1968
+ }
1969
+ refreshDetached() {
1970
+ for (const [node, active] of this.asset.localActive)
1971
+ this.asset.evaluatedActive.set(node, active);
1972
+ for (const node of this.asset.authoredActive.keys()) {
1973
+ let visible = true;
1974
+ for (let p = node; p && this.asset.localActive.has(p); p = this.asset.authoredParents.get(p) ?? null)
1975
+ visible &&= this.asset.localActive.get(p) !== false;
1976
+ node.visible = visible;
1977
+ }
1978
+ }
1979
+ get controls() {
1980
+ const menus = menuItems(this.asset.manifest);
1981
+ return menus.filter((c) => this.asset.nodes.get(c.sourceNode) === this.object).map((c) => {
1982
+ const key = c.parameter ?? c.id;
1983
+ return {
1984
+ id: key,
1985
+ label: c.label,
1986
+ type: c.controlType,
1987
+ activeValue: c.value,
1988
+ value: controls.get(this.asset)?.[key] ?? menus.find(
1989
+ (m) => (m.parameter ?? m.id) === key && Number(m.defaultValue) !== 0
1990
+ )?.defaultValue ?? c.defaultValue
1991
+ };
1992
+ });
1993
+ }
1994
+ setControl(id, value) {
1995
+ live(this.asset);
1996
+ if (!this.controls.some((c) => c.id === id))
1997
+ throw new CompositionError(
1998
+ "INVALID_CONTROL",
1999
+ `Unknown item control: ${id}`
2000
+ );
2001
+ if (typeof value !== "boolean" && !Number.isFinite(value))
2002
+ throw new CompositionError(
2003
+ "INVALID_VALUE",
2004
+ "Control value must be finite."
2005
+ );
2006
+ const values = controls.get(this.asset) ?? {};
2007
+ values[id] = value;
2008
+ controls.set(this.asset, values);
2009
+ const owner = sessions.get(this.asset);
2010
+ owner?.session.setItemState(owner.key, { controls: { [id]: value } });
2011
+ }
2012
+ targets(id) {
2013
+ return this.three.renderMeshes.flatMap((mesh) => {
2014
+ const index2 = mesh.morphTargetDictionary?.[id];
2015
+ return index2 === void 0 || mesh.morphTargetInfluences?.[index2] === void 0 ? [] : [{ mesh, index: index2 }];
2016
+ });
2017
+ }
2018
+ get morphs() {
2019
+ const names = new Set(
2020
+ this.three.renderMeshes.flatMap(
2021
+ (m) => Object.keys(m.morphTargetDictionary ?? {})
2022
+ )
2023
+ );
2024
+ const session = sessions.get(this.asset)?.session;
2025
+ return [...names].map((name) => {
2026
+ const targets = this.targets(name), values = targets.map((t) => t.mesh.morphTargetInfluences[t.index]);
2027
+ return {
2028
+ id: name,
2029
+ name,
2030
+ value: values.every((v) => v === values[0]) ? values[0] : null,
2031
+ controlled: targets.some(
2032
+ (t) => session?.isMorphControlled(t.mesh, t.index)
2033
+ )
2034
+ };
2035
+ });
2036
+ }
2037
+ setMorph(id, value, options) {
2038
+ live(this.asset);
2039
+ if (!Number.isFinite(value))
2040
+ throw new CompositionError(
2041
+ "INVALID_VALUE",
2042
+ "Morph value must be finite."
2043
+ );
2044
+ const targets = this.targets(id), session = sessions.get(this.asset)?.session;
2045
+ if (!targets.length) return { applied: false, reason: "missing" };
2046
+ if (targets.some((t) => session?.isMorphControlled(t.mesh, t.index)))
2047
+ return { applied: false, reason: "controlled" };
2048
+ for (const { mesh, index: index2 } of targets) {
2049
+ if (session)
2050
+ session.setMorph(mesh, index2, value, { deferEvaluation: true });
2051
+ else mesh.morphTargetInfluences[index2] = value;
2052
+ }
2053
+ if (session && !options?.deferEvaluation) session.refresh();
2054
+ return { applied: true };
2055
+ }
2056
+ traverse(visitor) {
2057
+ visitor(this);
2058
+ this.children.forEach((child) => child.traverse(visitor));
2059
+ }
2060
+ };
2061
+ var Asset = class extends Item {
2062
+ constructor(asset) {
2063
+ super(asset, asset.scene, null);
2064
+ data.set(this, asset);
2065
+ const items = /* @__PURE__ */ new Map([[asset.scene, this]]);
2066
+ const create = (object) => {
2067
+ const existing = items.get(object);
2068
+ if (existing) return existing;
2069
+ let parent = asset.authoredParents.get(object);
2070
+ while (parent && parent !== asset.scene && !asset.indices.has(parent))
2071
+ parent = asset.authoredParents.get(parent);
2072
+ const owner = parent ? create(parent) : this;
2073
+ const item = new Item(asset, object, owner);
2074
+ items.set(object, item);
2075
+ owner.children.push(item);
2076
+ return item;
2077
+ };
2078
+ for (const object of asset.nodes.values()) create(object);
2079
+ }
2080
+ get scene() {
2081
+ return this.asset.scene;
2082
+ }
2083
+ get vrm() {
2084
+ return this.asset.vrm;
2085
+ }
2086
+ get manifest() {
2087
+ return this.asset.manifest;
2088
+ }
2089
+ get resources() {
2090
+ return this.asset.resources;
2091
+ }
2092
+ dispose() {
2093
+ if (sessions.has(this.asset))
2094
+ throw new CompositionError(
2095
+ "ASSET_IN_USE",
2096
+ "Remove this asset or dispose its composition before disposing the asset."
2097
+ );
2098
+ disposeAvatarAsset(this.asset);
2099
+ }
2100
+ };
2101
+ var AvatarCompositionLoaderPlugin = class {
2102
+ constructor(parser) {
2103
+ this.parser = parser;
2104
+ }
2105
+ parser;
2106
+ name = EXTENSION_NAME;
2107
+ async afterRoot(gltf) {
2108
+ const manifest = this.parser.json.extensions?.[EXTENSION_NAME];
2109
+ if (manifest !== void 0)
2110
+ gltf.userData.mochiyaAvatarComposition = parseManifest(manifest);
2111
+ const dependencies = await resolveAvatarDependencies(this.parser);
2112
+ let asset;
2113
+ Object.defineProperty(gltf.userData, "avatar", {
2114
+ enumerable: true,
2115
+ configurable: true,
2116
+ get: () => asset ??= new Asset(prepareAvatarAsset(gltf, dependencies))
2117
+ });
2118
+ }
2119
+ };
2120
+ var AvatarComposition = class {
2121
+ #session;
2122
+ #items = [];
2123
+ #disposed = false;
2124
+ base;
2125
+ constructor(base) {
2126
+ const asset = data.get(base);
2127
+ if (!asset)
2128
+ throw new CompositionError(
2129
+ "INVALID_ASSET",
2130
+ "Register AvatarCompositionLoaderPlugin and read userData.avatar after loading."
2131
+ );
2132
+ live(asset);
2133
+ this.base = base;
2134
+ this.#session = new AvatarCompositionSession({ base: asset });
2135
+ sessions.set(asset, { session: this.#session, key: "base" });
2136
+ this.#session.setItemState("base", { controls: controls.get(asset) });
2137
+ }
2138
+ get items() {
2139
+ return this.#items;
2140
+ }
2141
+ get issues() {
2142
+ return [
2143
+ ...this.#session.warnings,
2144
+ ...this.#session.attachments.flatMap((a) => a.warnings)
2145
+ ];
2146
+ }
2147
+ add(asset, options = {}) {
2148
+ const core = data.get(asset);
2149
+ if (!core)
2150
+ throw new CompositionError(
2151
+ "INVALID_ASSET",
2152
+ "Register AvatarCompositionLoaderPlugin and read userData.avatar after loading."
2153
+ );
2154
+ live(core);
2155
+ this.#session.attach(core, {
2156
+ id: asset.id,
2157
+ layerOrder: options.order,
2158
+ controls: controls.get(core)
2159
+ });
2160
+ sessions.set(core, { session: this.#session, key: asset.id });
2161
+ this.#items.push(asset);
2162
+ return asset;
2163
+ }
2164
+ remove(asset) {
2165
+ if (!this.#items.includes(asset)) return;
2166
+ this.#session.detach(asset.id);
2167
+ sessions.delete(data.get(asset));
2168
+ this.#items = this.#items.filter((a) => a !== asset);
2169
+ asset.refreshDetached();
2170
+ }
2171
+ setOrder(asset, order) {
2172
+ this.#session.setItemState(asset.id, { layerOrder: order });
2173
+ }
2174
+ inspect(asset) {
2175
+ if (asset === this.base)
2176
+ return {
2177
+ status: this.#session.warnings.length ? "partial" : "attached",
2178
+ warnings: this.#session.warnings,
2179
+ resolved: this.#session.resolved,
2180
+ matchedBones: 0,
2181
+ requestedBones: 0,
2182
+ appliedActions: this.#session.resolved.length
2183
+ };
2184
+ const result = this.#session.attachments.find((a) => a.id === asset.id);
2185
+ if (!result)
2186
+ throw new CompositionError(
2187
+ "MISSING_INSTANCE",
2188
+ "This asset is not in the composition."
2189
+ );
2190
+ return result;
2191
+ }
2192
+ setAnimation(clip, time = 0) {
2193
+ this.#session.setAnimation(clip, time);
2194
+ }
2195
+ get animationTime() {
2196
+ return this.#session.animationTime;
2197
+ }
2198
+ update(delta, paused = false) {
2199
+ this.#session.update(delta, paused);
2200
+ }
2201
+ dispose() {
2202
+ if (this.#disposed) return;
2203
+ this.#session.dispose();
2204
+ for (const asset of [this.base, ...this.#items]) {
2205
+ sessions.delete(data.get(asset));
2206
+ asset.refreshDetached();
2207
+ }
2208
+ this.#items = [];
2209
+ this.#disposed = true;
2210
+ }
2211
+ };
1856
2212
  export {
1857
2213
  ASSET_KINDS,
1858
- AvatarAsset,
1859
- AvatarCompositionSession,
2214
+ AvatarComposition,
2215
+ AvatarCompositionLoaderPlugin,
1860
2216
  CAPABILITIES,
1861
2217
  CompositionError,
1862
2218
  EXTENSION_NAME,
1863
- MochiyaAvatarCompositionLoaderPlugin,
1864
- NameResolver,
1865
2219
  SPEC_VERSION,
1866
- componentSchema,
1867
- conditionSchema,
1868
- curveSchema,
1869
- disposeAvatarAsset,
1870
- evaluateCurve,
1871
- keywordScore,
1872
- manifestSchema,
1873
- menuItems,
1874
- parseManifest,
1875
- prepareAvatarAsset,
1876
- selectorSchema,
1877
- tokens
2220
+ createAvatarAsset,
2221
+ parseManifest
1878
2222
  };
@@ -6,7 +6,7 @@
6
6
  "properties": {
7
7
  "specVersion": {
8
8
  "type": "string",
9
- "const": "0.1"
9
+ "const": "0.2"
10
10
  },
11
11
  "assetKind": {
12
12
  "type": "string",