@naanouff/w3dts-avatar 0.0.0-stage → 0.0.1-preview.sha35b5c8a
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/LICENSE +10 -0
- package/README.md +55 -2
- package/dist/AvatarHandle.d.ts +44 -0
- package/dist/AvatarHandle.d.ts.map +1 -0
- package/dist/collectAvatarEntities.d.ts +17 -0
- package/dist/collectAvatarEntities.d.ts.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/loadAvatarAsset.d.ts +18 -0
- package/dist/loadAvatarAsset.d.ts.map +1 -0
- package/dist/parseAvatarDescriptor.d.ts +10 -0
- package/dist/parseAvatarDescriptor.d.ts.map +1 -0
- package/dist/spawnAvatar.d.ts +16 -0
- package/dist/spawnAvatar.d.ts.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/w3dts-avatar.es.js +278 -0
- package/dist/w3dts-avatar.umd.js +1 -0
- package/package.json +44 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Licence propriétaire — W3DTS
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cyril TARRIET. Tous droits réservés.
|
|
4
|
+
|
|
5
|
+
Le logiciel W3DTS et ses extensions sont propriétaires.
|
|
6
|
+
|
|
7
|
+
Il est interdit de modifier ce logiciel, en tout ou en partie.
|
|
8
|
+
Il est interdit de le copier, le publier, le distribuer, le sous-licencier ou de le rendre disponible, en tout ou en partie, sous quelque forme que ce soit.
|
|
9
|
+
|
|
10
|
+
L'installation du paquet non modifié comme dépendance d'une application, via le registre désigné par l'auteur, ne transfère aucun autre droit.
|
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @naanouff/w3dts-avatar
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Runtime module for **game-ready avatars**: load an `AvatarAsset` (`avatar.json` + `model.glb`), spawn it into a W3DTS world, then drive morphs and locomotion clips.
|
|
4
|
+
|
|
5
|
+
Authoring (Adobe Fuse / Mixamo decode) lives in the private product repo
|
|
6
|
+
[`naanouff/w3dts-avatar-editor`](https://github.com/naanouff/w3dts-avatar-editor) — not in this package.
|
|
7
|
+
See [AVATAR_RUNTIME_PLAN.md](../../documentation/domains/avatar/AVATAR_RUNTIME_PLAN.md).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# Prefer the preview tag from develop until a main/latest cut
|
|
13
|
+
pnpm add @naanouff/w3dts-avatar@preview @naanouff/w3dts-core @naanouff/w3dts-animation @naanouff/w3dts-loader-glb
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Pass your engine `modelLoader` (typically GLB / composite) into `spawnAvatar` — this package does not depend on `loader-glb` directly.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { loadAvatarAsset, spawnAvatar } from '@naanouff/w3dts-avatar';
|
|
23
|
+
|
|
24
|
+
const asset = await loadAvatarAsset({
|
|
25
|
+
url: '/avatars/hero/avatar.json',
|
|
26
|
+
});
|
|
27
|
+
const handle = await spawnAvatar({
|
|
28
|
+
asset,
|
|
29
|
+
world: engine.ecsManager.world, // or your World
|
|
30
|
+
modelLoader: engine.modelLoader,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
handle.playClip('walk');
|
|
34
|
+
handle.setMorph('noseWide', 0.4);
|
|
35
|
+
handle.setRootTransform({ position: [0, 0, 0] });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### `avatar.json` (schemaVersion 1)
|
|
39
|
+
|
|
40
|
+
| Field | Role |
|
|
41
|
+
|-------|------|
|
|
42
|
+
| `rig` | `mixamo` \| `custom` |
|
|
43
|
+
| `model` | Relative GLB path when loading from URL |
|
|
44
|
+
| `morphs[]` | `{ id, targetIndex?, defaultWeight? }` |
|
|
45
|
+
| `locomotion` | `{ idle?, walk?, run? }` → glTF clip names |
|
|
46
|
+
| `eyeHeight` / `bounds` | Camera / motor helpers |
|
|
47
|
+
|
|
48
|
+
Fixture example: [`fixtures/avatar.json`](./fixtures/avatar.json).
|
|
49
|
+
|
|
50
|
+
## Composition with `@naanouff/w3dts-character`
|
|
51
|
+
|
|
52
|
+
Keep packages separate. Each frame (or on motor update), copy the motor feet transform onto `handle.setRootTransform`, or parent the avatar root under the motor entity. Do **not** merge motor logic into this module.
|
|
53
|
+
|
|
54
|
+
## Systems
|
|
55
|
+
|
|
56
|
+
Ensure the host engine runs `createGltfAnimationApplySystem` (and skin systems) so `playClip` advances on the GPU/CPU path already used for glTF.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Entity, SceneNode, World } from '@naanouff/w3dts-core';
|
|
2
|
+
import { quat, vec3 } from 'gl-matrix';
|
|
3
|
+
import { AvatarAsset } from './types';
|
|
4
|
+
export declare class AvatarHandleError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Controls a spawned avatar instance in the ECS world.
|
|
9
|
+
*
|
|
10
|
+
* Optional composition with `@naanouff/w3dts-character`: each frame copy the motor
|
|
11
|
+
* entity feet transform onto {@link setRootTransform} (or parent the avatar root
|
|
12
|
+
* under the motor entity). Packages stay separate — no shared god module.
|
|
13
|
+
*/
|
|
14
|
+
export declare class AvatarHandle {
|
|
15
|
+
readonly asset: AvatarAsset;
|
|
16
|
+
readonly root: SceneNode;
|
|
17
|
+
private readonly world;
|
|
18
|
+
private readonly morphEntities;
|
|
19
|
+
private readonly playerEntity;
|
|
20
|
+
private readonly morphIdToIndex;
|
|
21
|
+
constructor(asset: AvatarAsset, root: SceneNode, world: World, morphEntities: readonly Entity[], playerEntity: Entity | null, morphIdToIndex: ReadonlyMap<string, number>);
|
|
22
|
+
/** Root ECS entity of the loaded model. */
|
|
23
|
+
get rootEntity(): Entity;
|
|
24
|
+
/**
|
|
25
|
+
* Sets a named morph weight (0…1) on all morph-bearing entities under the root.
|
|
26
|
+
* @throws {AvatarHandleError} when the morph id is unknown
|
|
27
|
+
*/
|
|
28
|
+
setMorph(id: string, weight: number): void;
|
|
29
|
+
/**
|
|
30
|
+
* Plays a clip by locomotion alias (`idle` / `walk` / `run`) or exact glTF clip name.
|
|
31
|
+
* @throws {AvatarHandleError} when no player or clip is found
|
|
32
|
+
*/
|
|
33
|
+
playClip(name: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* Writes TRS on the root {@link TransformComponent}.
|
|
36
|
+
* Omitted axes keep their current values.
|
|
37
|
+
*/
|
|
38
|
+
setRootTransform(opts: {
|
|
39
|
+
position?: vec3 | [number, number, number];
|
|
40
|
+
rotation?: quat | [number, number, number, number];
|
|
41
|
+
scale?: vec3 | [number, number, number];
|
|
42
|
+
}): void;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=AvatarHandle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AvatarHandle.d.ts","sourceRoot":"","sources":["../src/AvatarHandle.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAIL,KAAK,MAAM,EACX,KAAK,SAAS,EACd,KAAK,KAAK,EACX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,OAAO,EAAE,MAAM;CAI5B;AAQD;;;;;;GAMG;AACH,qBAAa,YAAY;aAEL,KAAK,EAAE,WAAW;aAClB,IAAI,EAAE,SAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,cAAc;gBALf,KAAK,EAAE,WAAW,EAClB,IAAI,EAAE,SAAS,EACd,KAAK,EAAE,KAAK,EACZ,aAAa,EAAE,SAAS,MAAM,EAAE,EAChC,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,cAAc,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC;IAG9D,2CAA2C;IAC3C,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;;OAGG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAc1C;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAyB5B;;;OAGG;IACH,gBAAgB,CAAC,IAAI,EAAE;QACrB,QAAQ,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3C,QAAQ,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QACnD,KAAK,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;KACzC,GAAG,IAAI;CAgBT"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Entity, SceneNode, World } from '@naanouff/w3dts-core';
|
|
2
|
+
export interface AvatarEntityBundle {
|
|
3
|
+
morphEntities: Entity[];
|
|
4
|
+
playerEntity: Entity | null;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Collects morph-bearing entities and the first {@link GltfAnimationPlayerComponent} under `root`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function collectAvatarEntities(world: World, root: SceneNode): AvatarEntityBundle;
|
|
10
|
+
/**
|
|
11
|
+
* Builds morph id → target index map from the descriptor (array order fallback).
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildMorphIdToIndex(morphs: {
|
|
14
|
+
id: string;
|
|
15
|
+
targetIndex?: number;
|
|
16
|
+
}[] | undefined): Map<string, number>;
|
|
17
|
+
//# sourceMappingURL=collectAvatarEntities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collectAvatarEntities.d.ts","sourceRoot":"","sources":["../src/collectAvatarEntities.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAGL,KAAK,MAAM,EACX,KAAK,SAAS,EACd,KAAK,KAAK,EACX,MAAM,sBAAsB,CAAC;AAE9B,MAAM,WAAW,kBAAkB;IACjC,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AASD;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,GAAG,kBAAkB,CAevF;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,GAAG,SAAS,GACzD,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAQrB"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file index.ts
|
|
3
|
+
* @package @naanouff/w3dts-avatar
|
|
4
|
+
* @description Public exports for the avatar runtime module.
|
|
5
|
+
*/
|
|
6
|
+
export { AVATAR_SCHEMA_VERSION, type AvatarAsset, type AvatarDescriptor, type AvatarLocomotionClips, type AvatarMorphDef, type AvatarRigKind, type AvatarSlotDef, } from './types';
|
|
7
|
+
export { parseAvatarDescriptor, AvatarDescriptorError } from './parseAvatarDescriptor';
|
|
8
|
+
export { loadAvatarAsset, type LoadAvatarAssetInput } from './loadAvatarAsset';
|
|
9
|
+
export { spawnAvatar, type SpawnAvatarOptions } from './spawnAvatar';
|
|
10
|
+
export { AvatarHandle, AvatarHandleError } from './AvatarHandle';
|
|
11
|
+
export { collectAvatarEntities, buildMorphIdToIndex } from './collectAvatarEntities';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,qBAAqB,EACrB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,aAAa,GACnB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AACvF,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AvatarAsset, AvatarDescriptor } from './types';
|
|
2
|
+
export type LoadAvatarAssetInput = {
|
|
3
|
+
/** Already-parsed or raw descriptor object / JSON string. */
|
|
4
|
+
descriptor: AvatarDescriptor | string | Record<string, unknown>;
|
|
5
|
+
/** Model URL, Blob, or ArrayBuffer (wrapped as Blob). */
|
|
6
|
+
model: string | Blob | ArrayBuffer;
|
|
7
|
+
} | {
|
|
8
|
+
/** Absolute or relative URL to `avatar.json`. */
|
|
9
|
+
url: string;
|
|
10
|
+
/** Optional override for the model (otherwise resolved from descriptor.model + url). */
|
|
11
|
+
model?: string | Blob | ArrayBuffer;
|
|
12
|
+
fetchImpl?: typeof fetch;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Loads and validates an avatar asset for {@link spawnAvatar}.
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadAvatarAsset(input: LoadAvatarAssetInput): Promise<AvatarAsset>;
|
|
18
|
+
//# sourceMappingURL=loadAvatarAsset.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadAvatarAsset.d.ts","sourceRoot":"","sources":["../src/loadAvatarAsset.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE7D,MAAM,MAAM,oBAAoB,GAC5B;IACE,6DAA6D;IAC7D,UAAU,EAAE,gBAAgB,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChE,yDAAyD;IACzD,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,WAAW,CAAC;CACpC,GACD;IACE,iDAAiD;IACjD,GAAG,EAAE,MAAM,CAAC;IACZ,wFAAwF;IACxF,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,WAAW,CAAC;IACpC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAC1B,CAAC;AAgCN;;GAEG;AACH,wBAAsB,eAAe,CAAC,KAAK,EAAE,oBAAoB,GAAG,OAAO,CAAC,WAAW,CAAC,CA8BvF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AvatarDescriptor } from './types';
|
|
2
|
+
export declare class AvatarDescriptorError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Parses and validates a raw JSON value into {@link AvatarDescriptor}.
|
|
7
|
+
* @throws {AvatarDescriptorError} when the schema is invalid
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseAvatarDescriptor(raw: unknown): AvatarDescriptor;
|
|
10
|
+
//# sourceMappingURL=parseAvatarDescriptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseAvatarDescriptor.d.ts","sourceRoot":"","sources":["../src/parseAvatarDescriptor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAEL,KAAK,gBAAgB,EAKtB,MAAM,SAAS,CAAC;AAEjB,qBAAa,qBAAsB,SAAQ,KAAK;gBAClC,OAAO,EAAE,MAAM;CAI5B;AA2FD;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,GAAG,gBAAgB,CAqDpE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IModelLoaderService, World } from '@naanouff/w3dts-core';
|
|
2
|
+
import { AvatarHandle } from './AvatarHandle';
|
|
3
|
+
import { AvatarAsset } from './types';
|
|
4
|
+
export interface SpawnAvatarOptions {
|
|
5
|
+
asset: AvatarAsset;
|
|
6
|
+
world: World;
|
|
7
|
+
modelLoader: IModelLoaderService;
|
|
8
|
+
/** When true (default), apply descriptor morph defaultWeight after load. */
|
|
9
|
+
applyDefaultMorphs?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Loads the avatar GLB into `world` and returns a control handle.
|
|
13
|
+
* @throws {AvatarHandleError} when the model loader returns null
|
|
14
|
+
*/
|
|
15
|
+
export declare function spawnAvatar(options: SpawnAvatarOptions): Promise<AvatarHandle>;
|
|
16
|
+
//# sourceMappingURL=spawnAvatar.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spawnAvatar.d.ts","sourceRoot":"","sources":["../src/spawnAvatar.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAEvE,OAAO,EAAE,YAAY,EAAqB,MAAM,gBAAgB,CAAC;AAEjE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,WAAW,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC;IACb,WAAW,EAAE,mBAAmB,CAAC;IACjC,4EAA4E;IAC5E,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,YAAY,CAAC,CA+BpF"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file types.ts
|
|
3
|
+
* @package @naanouff/w3dts-avatar
|
|
4
|
+
* @description AvatarAsset interchange types (GLB + avatar.json). Independent of Adobe Fuse.
|
|
5
|
+
*/
|
|
6
|
+
/** Current `avatar.json` schema version accepted by this package. */
|
|
7
|
+
export declare const AVATAR_SCHEMA_VERSION: 1;
|
|
8
|
+
export type AvatarRigKind = 'mixamo' | 'custom';
|
|
9
|
+
/** Named morph / blend shape exposed to gameplay and netcode. */
|
|
10
|
+
export interface AvatarMorphDef {
|
|
11
|
+
/** Stable id (not a display label). */
|
|
12
|
+
id: string;
|
|
13
|
+
/**
|
|
14
|
+
* Index into glTF morph targets / {@link MorphWeightsComponent.weights}.
|
|
15
|
+
* Defaults to the morph's order in {@link AvatarDescriptor.morphs}.
|
|
16
|
+
*/
|
|
17
|
+
targetIndex?: number;
|
|
18
|
+
/** Weight applied after spawn when set (0…1). */
|
|
19
|
+
defaultWeight?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Optional wardrobe slot metadata (V1: meshes already baked into the GLB). */
|
|
22
|
+
export interface AvatarSlotDef {
|
|
23
|
+
id: string;
|
|
24
|
+
meshName?: string;
|
|
25
|
+
}
|
|
26
|
+
/** Clip names for locomotion aliases (`playClip('idle')` resolves via this map). */
|
|
27
|
+
export interface AvatarLocomotionClips {
|
|
28
|
+
idle?: string;
|
|
29
|
+
walk?: string;
|
|
30
|
+
run?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Versioned descriptor stored as `avatar.json` next to `model.glb`.
|
|
34
|
+
*/
|
|
35
|
+
export interface AvatarDescriptor {
|
|
36
|
+
schemaVersion: number;
|
|
37
|
+
rig: AvatarRigKind;
|
|
38
|
+
/** Relative or absolute model URL when the descriptor is loaded from a URL. */
|
|
39
|
+
model?: string;
|
|
40
|
+
hipsBone?: string;
|
|
41
|
+
boneMap?: Record<string, string>;
|
|
42
|
+
morphs?: AvatarMorphDef[];
|
|
43
|
+
slots?: AvatarSlotDef[];
|
|
44
|
+
locomotion?: AvatarLocomotionClips;
|
|
45
|
+
bounds?: {
|
|
46
|
+
min: [number, number, number];
|
|
47
|
+
max: [number, number, number];
|
|
48
|
+
};
|
|
49
|
+
/** Eye height in metres for camera / motor alignment. */
|
|
50
|
+
eyeHeight?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Loaded runtime asset: validated descriptor + model payload for {@link IModelLoaderService.load}.
|
|
54
|
+
*/
|
|
55
|
+
export interface AvatarAsset {
|
|
56
|
+
descriptor: AvatarDescriptor;
|
|
57
|
+
model: string | Blob;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,qEAAqE;AACrE,eAAO,MAAM,qBAAqB,EAAG,CAAU,CAAC;AAEhD,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEhD,iEAAiE;AACjE,MAAM,WAAW,cAAc;IAC7B,uCAAuC;IACvC,EAAE,EAAE,MAAM,CAAC;IACX;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iDAAiD;IACjD,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,+EAA+E;AAC/E,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,oFAAoF;AACpF,MAAM,WAAW,qBAAqB;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,GAAG,EAAE,aAAa,CAAC;IACnB,+EAA+E;IAC/E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,cAAc,EAAE,CAAC;IAC1B,KAAK,CAAC,EAAE,aAAa,EAAE,CAAC;IACxB,UAAU,CAAC,EAAE,qBAAqB,CAAC;IACnC,MAAM,CAAC,EAAE;QACP,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAC9B,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;KAC/B,CAAC;IACF,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,gBAAgB,CAAC;IAC7B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB"}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { MorphWeightsComponent as d, GltfAnimationPlayerComponent as v, TransformComponent as N } from "@naanouff/w3dts-core";
|
|
2
|
+
import { AnimationMixer as $ } from "@naanouff/w3dts-animation";
|
|
3
|
+
import { vec3 as u, quat as C } from "gl-matrix";
|
|
4
|
+
const g = 1;
|
|
5
|
+
class i extends Error {
|
|
6
|
+
constructor(e) {
|
|
7
|
+
super(e), this.name = "AvatarDescriptorError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function c(t) {
|
|
11
|
+
return typeof t == "object" && t !== null && !Array.isArray(t);
|
|
12
|
+
}
|
|
13
|
+
function M(t) {
|
|
14
|
+
if (t === "mixamo" || t === "custom") return t;
|
|
15
|
+
throw new i(`Invalid rig: expected "mixamo" | "custom", got ${String(t)}`);
|
|
16
|
+
}
|
|
17
|
+
function T(t) {
|
|
18
|
+
if (t === void 0) return;
|
|
19
|
+
if (!Array.isArray(t)) throw new i("morphs must be an array");
|
|
20
|
+
const e = [];
|
|
21
|
+
for (let o = 0; o < t.length; o++) {
|
|
22
|
+
const n = t[o];
|
|
23
|
+
if (!c(n) || typeof n.id != "string" || n.id.length === 0)
|
|
24
|
+
throw new i(`morphs[${o}].id must be a non-empty string`);
|
|
25
|
+
const r = { id: n.id };
|
|
26
|
+
if (n.targetIndex !== void 0) {
|
|
27
|
+
if (typeof n.targetIndex != "number" || !Number.isInteger(n.targetIndex) || n.targetIndex < 0)
|
|
28
|
+
throw new i(`morphs[${o}].targetIndex must be a non-negative integer`);
|
|
29
|
+
r.targetIndex = n.targetIndex;
|
|
30
|
+
}
|
|
31
|
+
if (n.defaultWeight !== void 0) {
|
|
32
|
+
if (typeof n.defaultWeight != "number" || Number.isNaN(n.defaultWeight))
|
|
33
|
+
throw new i(`morphs[${o}].defaultWeight must be a number`);
|
|
34
|
+
r.defaultWeight = n.defaultWeight;
|
|
35
|
+
}
|
|
36
|
+
e.push(r);
|
|
37
|
+
}
|
|
38
|
+
return e;
|
|
39
|
+
}
|
|
40
|
+
function V(t) {
|
|
41
|
+
if (t === void 0) return;
|
|
42
|
+
if (!Array.isArray(t)) throw new i("slots must be an array");
|
|
43
|
+
const e = [];
|
|
44
|
+
for (let o = 0; o < t.length; o++) {
|
|
45
|
+
const n = t[o];
|
|
46
|
+
if (!c(n) || typeof n.id != "string" || n.id.length === 0)
|
|
47
|
+
throw new i(`slots[${o}].id must be a non-empty string`);
|
|
48
|
+
const r = { id: n.id };
|
|
49
|
+
if (n.meshName !== void 0) {
|
|
50
|
+
if (typeof n.meshName != "string")
|
|
51
|
+
throw new i(`slots[${o}].meshName must be a string`);
|
|
52
|
+
r.meshName = n.meshName;
|
|
53
|
+
}
|
|
54
|
+
e.push(r);
|
|
55
|
+
}
|
|
56
|
+
return e;
|
|
57
|
+
}
|
|
58
|
+
function H(t) {
|
|
59
|
+
if (t === void 0) return;
|
|
60
|
+
if (!c(t)) throw new i("locomotion must be an object");
|
|
61
|
+
const e = {};
|
|
62
|
+
for (const o of ["idle", "walk", "run"]) {
|
|
63
|
+
const n = t[o];
|
|
64
|
+
if (n !== void 0) {
|
|
65
|
+
if (typeof n != "string" || n.length === 0)
|
|
66
|
+
throw new i(`locomotion.${o} must be a non-empty string`);
|
|
67
|
+
e[o] = n;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return e;
|
|
71
|
+
}
|
|
72
|
+
function R(t) {
|
|
73
|
+
if (t === void 0) return;
|
|
74
|
+
if (!c(t)) throw new i("bounds must be an object");
|
|
75
|
+
const e = (o) => {
|
|
76
|
+
const n = t[o];
|
|
77
|
+
if (!Array.isArray(n) || n.length !== 3 || !n.every((r) => typeof r == "number"))
|
|
78
|
+
throw new i(`bounds.${o} must be [number, number, number]`);
|
|
79
|
+
return [n[0], n[1], n[2]];
|
|
80
|
+
};
|
|
81
|
+
return { min: e("min"), max: e("max") };
|
|
82
|
+
}
|
|
83
|
+
function y(t) {
|
|
84
|
+
if (!c(t))
|
|
85
|
+
throw new i("avatar descriptor must be a JSON object");
|
|
86
|
+
if (typeof t.schemaVersion != "number" || !Number.isInteger(t.schemaVersion))
|
|
87
|
+
throw new i("schemaVersion must be an integer");
|
|
88
|
+
if (t.schemaVersion !== g)
|
|
89
|
+
throw new i(
|
|
90
|
+
`Unsupported schemaVersion ${t.schemaVersion}; expected ${g}`
|
|
91
|
+
);
|
|
92
|
+
const e = {
|
|
93
|
+
schemaVersion: t.schemaVersion,
|
|
94
|
+
rig: M(t.rig)
|
|
95
|
+
};
|
|
96
|
+
if (t.model !== void 0) {
|
|
97
|
+
if (typeof t.model != "string" || t.model.length === 0)
|
|
98
|
+
throw new i("model must be a non-empty string when present");
|
|
99
|
+
e.model = t.model;
|
|
100
|
+
}
|
|
101
|
+
if (t.hipsBone !== void 0) {
|
|
102
|
+
if (typeof t.hipsBone != "string")
|
|
103
|
+
throw new i("hipsBone must be a string");
|
|
104
|
+
e.hipsBone = t.hipsBone;
|
|
105
|
+
}
|
|
106
|
+
if (t.boneMap !== void 0) {
|
|
107
|
+
if (!c(t.boneMap)) throw new i("boneMap must be an object");
|
|
108
|
+
const o = {};
|
|
109
|
+
for (const [n, r] of Object.entries(t.boneMap)) {
|
|
110
|
+
if (typeof r != "string")
|
|
111
|
+
throw new i(`boneMap.${n} must be a string`);
|
|
112
|
+
o[n] = r;
|
|
113
|
+
}
|
|
114
|
+
e.boneMap = o;
|
|
115
|
+
}
|
|
116
|
+
if (e.morphs = T(t.morphs), e.slots = V(t.slots), e.locomotion = H(t.locomotion), e.bounds = R(t.bounds), t.eyeHeight !== void 0) {
|
|
117
|
+
if (typeof t.eyeHeight != "number" || Number.isNaN(t.eyeHeight))
|
|
118
|
+
throw new i("eyeHeight must be a number");
|
|
119
|
+
e.eyeHeight = t.eyeHeight;
|
|
120
|
+
}
|
|
121
|
+
return e;
|
|
122
|
+
}
|
|
123
|
+
function w(t) {
|
|
124
|
+
return typeof t == "string" || t instanceof Blob ? t : new Blob([t], { type: "model/gltf-binary" });
|
|
125
|
+
}
|
|
126
|
+
function W(t, e) {
|
|
127
|
+
try {
|
|
128
|
+
return new URL(e, t).href;
|
|
129
|
+
} catch {
|
|
130
|
+
throw new i(`Cannot resolve model path "${e}" against "${t}"`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function b(t) {
|
|
134
|
+
if (typeof t == "string") {
|
|
135
|
+
let e;
|
|
136
|
+
try {
|
|
137
|
+
e = JSON.parse(t);
|
|
138
|
+
} catch (o) {
|
|
139
|
+
throw new i(
|
|
140
|
+
`Invalid avatar JSON: ${o instanceof Error ? o.message : String(o)}`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
return y(e);
|
|
144
|
+
}
|
|
145
|
+
return y(t);
|
|
146
|
+
}
|
|
147
|
+
async function U(t) {
|
|
148
|
+
if ("url" in t) {
|
|
149
|
+
const e = t.fetchImpl ?? globalThis.fetch;
|
|
150
|
+
if (typeof e != "function")
|
|
151
|
+
throw new i(
|
|
152
|
+
"fetch is not available; pass fetchImpl or provide descriptor+model"
|
|
153
|
+
);
|
|
154
|
+
const o = await e(t.url);
|
|
155
|
+
if (!o.ok)
|
|
156
|
+
throw new i(`Failed to fetch avatar.json (${o.status}): ${t.url}`);
|
|
157
|
+
const n = await o.text(), r = b(n);
|
|
158
|
+
if (t.model !== void 0)
|
|
159
|
+
return { descriptor: r, model: w(t.model) };
|
|
160
|
+
if (!r.model)
|
|
161
|
+
throw new i('avatar.json loaded from URL must declare a "model" field');
|
|
162
|
+
return {
|
|
163
|
+
descriptor: r,
|
|
164
|
+
model: W(t.url, r.model)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
descriptor: b(t.descriptor),
|
|
169
|
+
model: w(t.model)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
class f extends Error {
|
|
173
|
+
constructor(e) {
|
|
174
|
+
super(e), this.name = "AvatarHandleError";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function j(t) {
|
|
178
|
+
return t < 0 ? 0 : t > 1 ? 1 : t;
|
|
179
|
+
}
|
|
180
|
+
class B {
|
|
181
|
+
constructor(e, o, n, r, s, a) {
|
|
182
|
+
this.asset = e, this.root = o, this.world = n, this.morphEntities = r, this.playerEntity = s, this.morphIdToIndex = a;
|
|
183
|
+
}
|
|
184
|
+
/** Root ECS entity of the loaded model. */
|
|
185
|
+
get rootEntity() {
|
|
186
|
+
return this.root.entityId;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Sets a named morph weight (0…1) on all morph-bearing entities under the root.
|
|
190
|
+
* @throws {AvatarHandleError} when the morph id is unknown
|
|
191
|
+
*/
|
|
192
|
+
setMorph(e, o) {
|
|
193
|
+
const n = this.morphIdToIndex.get(e);
|
|
194
|
+
if (n === void 0)
|
|
195
|
+
throw new f(`Unknown morph id "${e}"`);
|
|
196
|
+
const r = j(o);
|
|
197
|
+
for (const s of this.morphEntities) {
|
|
198
|
+
const a = this.world.getComponent(s, d);
|
|
199
|
+
a && (n >= a.morphTargetCount || n >= a.weights.length || (a.weights[n] = r));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Plays a clip by locomotion alias (`idle` / `walk` / `run`) or exact glTF clip name.
|
|
204
|
+
* @throws {AvatarHandleError} when no player or clip is found
|
|
205
|
+
*/
|
|
206
|
+
playClip(e) {
|
|
207
|
+
if (this.playerEntity === null)
|
|
208
|
+
throw new f("Avatar has no GltfAnimationPlayerComponent");
|
|
209
|
+
const o = this.world.getComponent(this.playerEntity, v);
|
|
210
|
+
if (!o || o.clips.length === 0)
|
|
211
|
+
throw new f("Avatar animation player has no clips");
|
|
212
|
+
const n = this.asset.descriptor.locomotion, r = e === "idle" || e === "walk" || e === "run" ? (n == null ? void 0 : n[e]) ?? e : e;
|
|
213
|
+
let s = o.clips.findIndex((a) => a.name === r);
|
|
214
|
+
if (s < 0 && (s = o.clips.findIndex((a) => a.name === e)), s < 0)
|
|
215
|
+
throw new f(`Clip not found: "${e}" (resolved "${r}")`);
|
|
216
|
+
o.activeClipIndex = s, o.mixer = new $(o.clips[s], { loop: "repeat" });
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Writes TRS on the root {@link TransformComponent}.
|
|
220
|
+
* Omitted axes keep their current values.
|
|
221
|
+
*/
|
|
222
|
+
setRootTransform(e) {
|
|
223
|
+
const o = this.world.getComponent(this.root.entityId, N);
|
|
224
|
+
if (!o)
|
|
225
|
+
throw new f("Root entity has no TransformComponent");
|
|
226
|
+
e.position && u.copy(o.position, e.position), e.rotation && C.copy(o.rotation, e.rotation), e.scale && u.copy(o.scale, e.scale), o.updateLocalTransform();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function x(t, e) {
|
|
230
|
+
e(t);
|
|
231
|
+
for (const o of t.children)
|
|
232
|
+
x(o, e);
|
|
233
|
+
}
|
|
234
|
+
function S(t, e) {
|
|
235
|
+
const o = [];
|
|
236
|
+
let n = null;
|
|
237
|
+
return x(e, (r) => {
|
|
238
|
+
const s = r.entityId;
|
|
239
|
+
t.getComponent(s, d) && o.push(s), !n && t.getComponent(s, v) && (n = s);
|
|
240
|
+
}), { morphEntities: o, playerEntity: n };
|
|
241
|
+
}
|
|
242
|
+
function k(t) {
|
|
243
|
+
const e = /* @__PURE__ */ new Map();
|
|
244
|
+
if (!t) return e;
|
|
245
|
+
for (let o = 0; o < t.length; o++) {
|
|
246
|
+
const n = t[o];
|
|
247
|
+
e.set(n.id, n.targetIndex ?? o);
|
|
248
|
+
}
|
|
249
|
+
return e;
|
|
250
|
+
}
|
|
251
|
+
async function F(t) {
|
|
252
|
+
const { asset: e, world: o, modelLoader: n } = t, r = t.applyDefaultMorphs !== !1, s = await n.load(e.model, o);
|
|
253
|
+
if (!s)
|
|
254
|
+
throw new f("Model loader returned null for avatar model");
|
|
255
|
+
const { morphEntities: a, playerEntity: I } = S(o, s), p = k(e.descriptor.morphs), A = new B(e, s, o, a, I, p);
|
|
256
|
+
if (r && e.descriptor.morphs)
|
|
257
|
+
for (const h of e.descriptor.morphs) {
|
|
258
|
+
if (h.defaultWeight === void 0 || a.length === 0) continue;
|
|
259
|
+
const l = p.get(h.id);
|
|
260
|
+
if (l !== void 0)
|
|
261
|
+
for (const E of a) {
|
|
262
|
+
const m = o.getComponent(E, d);
|
|
263
|
+
m && (l >= m.morphTargetCount || l >= m.weights.length || (m.weights[l] = h.defaultWeight));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return A;
|
|
267
|
+
}
|
|
268
|
+
export {
|
|
269
|
+
g as AVATAR_SCHEMA_VERSION,
|
|
270
|
+
i as AvatarDescriptorError,
|
|
271
|
+
B as AvatarHandle,
|
|
272
|
+
f as AvatarHandleError,
|
|
273
|
+
k as buildMorphIdToIndex,
|
|
274
|
+
S as collectAvatarEntities,
|
|
275
|
+
U as loadAvatarAsset,
|
|
276
|
+
y as parseAvatarDescriptor,
|
|
277
|
+
F as spawnAvatar
|
|
278
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(a,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@naanouff/w3dts-core"),require("@naanouff/w3dts-animation"),require("gl-matrix")):typeof define=="function"&&define.amd?define(["exports","@naanouff/w3dts-core","@naanouff/w3dts-animation","gl-matrix"],d):(a=typeof globalThis<"u"?globalThis:a||self,d(a.W3dtsAvatar={},a.W3dtsCore,a.W3dtsAnimation,a.glMatrix))})(this,function(a,d,x,u){"use strict";class i extends Error{constructor(e){super(e),this.name="AvatarDescriptorError"}}function l(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)}function M(t){if(t==="mixamo"||t==="custom")return t;throw new i(`Invalid rig: expected "mixamo" | "custom", got ${String(t)}`)}function N(t){if(t===void 0)return;if(!Array.isArray(t))throw new i("morphs must be an array");const e=[];for(let o=0;o<t.length;o++){const n=t[o];if(!l(n)||typeof n.id!="string"||n.id.length===0)throw new i(`morphs[${o}].id must be a non-empty string`);const r={id:n.id};if(n.targetIndex!==void 0){if(typeof n.targetIndex!="number"||!Number.isInteger(n.targetIndex)||n.targetIndex<0)throw new i(`morphs[${o}].targetIndex must be a non-negative integer`);r.targetIndex=n.targetIndex}if(n.defaultWeight!==void 0){if(typeof n.defaultWeight!="number"||Number.isNaN(n.defaultWeight))throw new i(`morphs[${o}].defaultWeight must be a number`);r.defaultWeight=n.defaultWeight}e.push(r)}return e}function C(t){if(t===void 0)return;if(!Array.isArray(t))throw new i("slots must be an array");const e=[];for(let o=0;o<t.length;o++){const n=t[o];if(!l(n)||typeof n.id!="string"||n.id.length===0)throw new i(`slots[${o}].id must be a non-empty string`);const r={id:n.id};if(n.meshName!==void 0){if(typeof n.meshName!="string")throw new i(`slots[${o}].meshName must be a string`);r.meshName=n.meshName}e.push(r)}return e}function T(t){if(t===void 0)return;if(!l(t))throw new i("locomotion must be an object");const e={};for(const o of["idle","walk","run"]){const n=t[o];if(n!==void 0){if(typeof n!="string"||n.length===0)throw new i(`locomotion.${o} must be a non-empty string`);e[o]=n}}return e}function V(t){if(t===void 0)return;if(!l(t))throw new i("bounds must be an object");const e=o=>{const n=t[o];if(!Array.isArray(n)||n.length!==3||!n.every(r=>typeof r=="number"))throw new i(`bounds.${o} must be [number, number, number]`);return[n[0],n[1],n[2]]};return{min:e("min"),max:e("max")}}function p(t){if(!l(t))throw new i("avatar descriptor must be a JSON object");if(typeof t.schemaVersion!="number"||!Number.isInteger(t.schemaVersion))throw new i("schemaVersion must be an integer");if(t.schemaVersion!==1)throw new i(`Unsupported schemaVersion ${t.schemaVersion}; expected 1`);const e={schemaVersion:t.schemaVersion,rig:M(t.rig)};if(t.model!==void 0){if(typeof t.model!="string"||t.model.length===0)throw new i("model must be a non-empty string when present");e.model=t.model}if(t.hipsBone!==void 0){if(typeof t.hipsBone!="string")throw new i("hipsBone must be a string");e.hipsBone=t.hipsBone}if(t.boneMap!==void 0){if(!l(t.boneMap))throw new i("boneMap must be an object");const o={};for(const[n,r]of Object.entries(t.boneMap)){if(typeof r!="string")throw new i(`boneMap.${n} must be a string`);o[n]=r}e.boneMap=o}if(e.morphs=N(t.morphs),e.slots=C(t.slots),e.locomotion=T(t.locomotion),e.bounds=V(t.bounds),t.eyeHeight!==void 0){if(typeof t.eyeHeight!="number"||Number.isNaN(t.eyeHeight))throw new i("eyeHeight must be a number");e.eyeHeight=t.eyeHeight}return e}function y(t){return typeof t=="string"||t instanceof Blob?t:new Blob([t],{type:"model/gltf-binary"})}function $(t,e){try{return new URL(e,t).href}catch{throw new i(`Cannot resolve model path "${e}" against "${t}"`)}}function w(t){if(typeof t=="string"){let e;try{e=JSON.parse(t)}catch(o){throw new i(`Invalid avatar JSON: ${o instanceof Error?o.message:String(o)}`)}return p(e)}return p(t)}async function S(t){if("url"in t){const e=t.fetchImpl??globalThis.fetch;if(typeof e!="function")throw new i("fetch is not available; pass fetchImpl or provide descriptor+model");const o=await e(t.url);if(!o.ok)throw new i(`Failed to fetch avatar.json (${o.status}): ${t.url}`);const n=await o.text(),r=w(n);if(t.model!==void 0)return{descriptor:r,model:y(t.model)};if(!r.model)throw new i('avatar.json loaded from URL must declare a "model" field');return{descriptor:r,model:$(t.url,r.model)}}return{descriptor:w(t.descriptor),model:y(t.model)}}class c extends Error{constructor(e){super(e),this.name="AvatarHandleError"}}function R(t){return t<0?0:t>1?1:t}class b{constructor(e,o,n,r,s,f){this.asset=e,this.root=o,this.world=n,this.morphEntities=r,this.playerEntity=s,this.morphIdToIndex=f}get rootEntity(){return this.root.entityId}setMorph(e,o){const n=this.morphIdToIndex.get(e);if(n===void 0)throw new c(`Unknown morph id "${e}"`);const r=R(o);for(const s of this.morphEntities){const f=this.world.getComponent(s,d.MorphWeightsComponent);f&&(n>=f.morphTargetCount||n>=f.weights.length||(f.weights[n]=r))}}playClip(e){if(this.playerEntity===null)throw new c("Avatar has no GltfAnimationPlayerComponent");const o=this.world.getComponent(this.playerEntity,d.GltfAnimationPlayerComponent);if(!o||o.clips.length===0)throw new c("Avatar animation player has no clips");const n=this.asset.descriptor.locomotion,r=e==="idle"||e==="walk"||e==="run"?(n==null?void 0:n[e])??e:e;let s=o.clips.findIndex(f=>f.name===r);if(s<0&&(s=o.clips.findIndex(f=>f.name===e)),s<0)throw new c(`Clip not found: "${e}" (resolved "${r}")`);o.activeClipIndex=s,o.mixer=new x.AnimationMixer(o.clips[s],{loop:"repeat"})}setRootTransform(e){const o=this.world.getComponent(this.root.entityId,d.TransformComponent);if(!o)throw new c("Root entity has no TransformComponent");e.position&&u.vec3.copy(o.position,e.position),e.rotation&&u.quat.copy(o.rotation,e.rotation),e.scale&&u.vec3.copy(o.scale,e.scale),o.updateLocalTransform()}}function A(t,e){e(t);for(const o of t.children)A(o,e)}function v(t,e){const o=[];let n=null;return A(e,r=>{const s=r.entityId;t.getComponent(s,d.MorphWeightsComponent)&&o.push(s),!n&&t.getComponent(s,d.GltfAnimationPlayerComponent)&&(n=s)}),{morphEntities:o,playerEntity:n}}function I(t){const e=new Map;if(!t)return e;for(let o=0;o<t.length;o++){const n=t[o];e.set(n.id,n.targetIndex??o)}return e}async function H(t){const{asset:e,world:o,modelLoader:n}=t,r=t.applyDefaultMorphs!==!1,s=await n.load(e.model,o);if(!s)throw new c("Model loader returned null for avatar model");const{morphEntities:f,playerEntity:W}=v(o,s),E=I(e.descriptor.morphs),j=new b(e,s,o,f,W,E);if(r&&e.descriptor.morphs)for(const g of e.descriptor.morphs){if(g.defaultWeight===void 0||f.length===0)continue;const h=E.get(g.id);if(h!==void 0)for(const O of f){const m=o.getComponent(O,d.MorphWeightsComponent);m&&(h>=m.morphTargetCount||h>=m.weights.length||(m.weights[h]=g.defaultWeight))}}return j}a.AVATAR_SCHEMA_VERSION=1,a.AvatarDescriptorError=i,a.AvatarHandle=b,a.AvatarHandleError=c,a.buildMorphIdToIndex=I,a.collectAvatarEntities=v,a.loadAvatarAsset=S,a.parseAvatarDescriptor=p,a.spawnAvatar=H,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naanouff/w3dts-avatar",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1-preview.sha35b5c8a",
|
|
4
|
+
"description": "Runtime avatar loader/spawner (AvatarAsset GLB + descriptor) for W3DTS games",
|
|
5
|
+
"main": "dist/w3dts-avatar.umd.js",
|
|
6
|
+
"module": "dist/w3dts-avatar.es.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public",
|
|
11
|
+
"registry": "https://registry.npmjs.org"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/naanouff/w3dts.git"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"dev": "vite build --watch",
|
|
22
|
+
"build": "vite build",
|
|
23
|
+
"test": "vitest --run --config ../../vitest.config.ts",
|
|
24
|
+
"check": "tsc --noEmit -p ./tsconfig.json"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@naanouff/w3dts-animation": "0.0.15",
|
|
28
|
+
"@naanouff/w3dts-core": "0.0.38",
|
|
29
|
+
"@naanouff/w3dts-logger": "0.0.23",
|
|
30
|
+
"gl-matrix": "^3.4.3"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "^5.5.4",
|
|
34
|
+
"vite": "^5.4.21",
|
|
35
|
+
"vite-plugin-dts": "^4.0.3",
|
|
36
|
+
"vitest": "^3.2.7"
|
|
37
|
+
},
|
|
38
|
+
"license": "UNLICENSED",
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"import": "./dist/w3dts-avatar.es.js",
|
|
43
|
+
"require": "./dist/w3dts-avatar.umd.js"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|