@kubohiroya/turbowarp-aframe 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +18 -8
- package/README.md +86 -9
- package/THIRD_PARTY_NOTICES.md +17 -0
- package/dist/extension-manifest.json +88 -0
- package/dist/turbowarp-aframe.js +6469 -44
- package/docs/architecture.ja.md +5 -3
- package/docs/architecture.md +5 -3
- package/package.json +6 -2
- package/src/aframe-loader.ts +74 -0
- package/src/block-definitions.json +62 -1
- package/src/extension.ts +101 -6
- package/src/globals.d.ts +10 -0
- package/src/runtime-capability.ts +69 -17
- package/src/vrm.ts +168 -0
package/src/vrm.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {createThreeVrm} from 'virtual:three-vrm-factory';
|
|
2
|
+
|
|
3
|
+
/** The subset of Three.js, as exposed by `AFRAME.THREE`, that VRM avatars use. */
|
|
4
|
+
export interface VrmThreeApi {
|
|
5
|
+
GLTFLoader: new () => GltfLoaderLike;
|
|
6
|
+
MathUtils: {degToRad(degrees: number): number};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface GltfLoaderLike {
|
|
10
|
+
register(callback: (parser: unknown) => unknown): unknown;
|
|
11
|
+
loadAsync(url: string): Promise<{userData: {vrm?: VrmLike}}>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface BoneLike {
|
|
15
|
+
rotation: {set(x: number, y: number, z: number): unknown};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface VrmLike {
|
|
19
|
+
scene: object;
|
|
20
|
+
meta?: {metaVersion?: string};
|
|
21
|
+
humanoid: {
|
|
22
|
+
humanBones: Record<string, unknown>;
|
|
23
|
+
getNormalizedBoneNode(name: string): BoneLike | null;
|
|
24
|
+
};
|
|
25
|
+
expressionManager?: {
|
|
26
|
+
expressions: ReadonlyArray<{expressionName: string}>;
|
|
27
|
+
getExpression(name: string): unknown;
|
|
28
|
+
setValue(name: string, weight: number): void;
|
|
29
|
+
};
|
|
30
|
+
update(deltaSeconds: number): void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type VrmState = 'none' | 'loading' | 'ready' | 'error';
|
|
34
|
+
|
|
35
|
+
interface VrmEntry {
|
|
36
|
+
state: VrmState;
|
|
37
|
+
error: string;
|
|
38
|
+
vrm: VrmLike | null;
|
|
39
|
+
generation: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Loads VRM avatars onto A-Frame nodes and turns their humanoid bones.
|
|
44
|
+
*
|
|
45
|
+
* Rotations are written to the normalized humanoid bones, whose rest pose is the same
|
|
46
|
+
* T-pose for every model, and `update` copies them to the model's own bones. That is
|
|
47
|
+
* what makes one rotation mean the same thing on any VRM.
|
|
48
|
+
*/
|
|
49
|
+
export class VrmAvatars {
|
|
50
|
+
private readonly entries = new Map<string, VrmEntry>();
|
|
51
|
+
private vrmApi: ReturnType<typeof createThreeVrm> | null = null;
|
|
52
|
+
private vrmApiThree: VrmThreeApi | null = null;
|
|
53
|
+
private generation = 0;
|
|
54
|
+
|
|
55
|
+
public constructor(private readonly getThree: () => VrmThreeApi | null) {}
|
|
56
|
+
|
|
57
|
+
public async load(nodeId: string, url: string, attach: (scene: object) => void): Promise<void> {
|
|
58
|
+
const generation = ++this.generation;
|
|
59
|
+
this.remove(nodeId);
|
|
60
|
+
const entry: VrmEntry = {state: 'loading', error: '', vrm: null, generation};
|
|
61
|
+
this.entries.set(nodeId, entry);
|
|
62
|
+
try {
|
|
63
|
+
const THREE = this.requireThree();
|
|
64
|
+
const {VRMLoaderPlugin, VRMUtils} = this.api(THREE);
|
|
65
|
+
const loader = new THREE.GLTFLoader();
|
|
66
|
+
loader.register((parser) => new VRMLoaderPlugin(parser as never));
|
|
67
|
+
const gltf = await loader.loadAsync(url);
|
|
68
|
+
const vrm = gltf.userData.vrm;
|
|
69
|
+
if (vrm === undefined) throw new Error(`Not a VRM file: ${url}`);
|
|
70
|
+
if (this.entries.get(nodeId)?.generation !== generation) {
|
|
71
|
+
VRMUtils.deepDispose(vrm.scene as never);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
VRMUtils.rotateVRM0(vrm as never);
|
|
75
|
+
attach(vrm.scene);
|
|
76
|
+
entry.vrm = vrm;
|
|
77
|
+
entry.state = 'ready';
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (this.entries.get(nodeId)?.generation !== generation) return;
|
|
80
|
+
entry.state = 'error';
|
|
81
|
+
entry.error = error instanceof Error ? error.message : String(error);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public setBoneRotation(nodeId: string, bone: string, degrees: {x: number; y: number; z: number}): void {
|
|
87
|
+
const vrm = this.requireVrm(nodeId);
|
|
88
|
+
const node = vrm.humanoid.getNormalizedBoneNode(bone);
|
|
89
|
+
if (node === null) throw new Error(`VRM ${nodeId} has no humanoid bone: ${bone}`);
|
|
90
|
+
const {degToRad} = this.requireThree().MathUtils;
|
|
91
|
+
node.rotation.set(degToRad(degrees.x), degToRad(degrees.y), degToRad(degrees.z));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Sets an expression's weight. The weight is clamped to 0 through 1, as three-vrm does, and
|
|
96
|
+
* the scene tick applies it with the other expressions.
|
|
97
|
+
*/
|
|
98
|
+
public setExpression(nodeId: string, name: string, weight: number): void {
|
|
99
|
+
if (!Number.isFinite(weight)) throw new Error(`VRM expression weight must be a number: ${weight}`);
|
|
100
|
+
const vrm = this.requireVrm(nodeId);
|
|
101
|
+
const expressions = vrm.expressionManager;
|
|
102
|
+
if (expressions === undefined) throw new Error(`VRM ${nodeId} has no expressions.`);
|
|
103
|
+
if (expressions.getExpression(name) == null) {
|
|
104
|
+
throw new Error(`VRM ${nodeId} has no expression: ${name}`);
|
|
105
|
+
}
|
|
106
|
+
expressions.setValue(name, Math.min(Math.max(weight, 0), 1));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public expressionNames(nodeId: string): string[] {
|
|
110
|
+
const expressions = this.entries.get(nodeId)?.vrm?.expressionManager?.expressions ?? [];
|
|
111
|
+
return expressions.map((expression) => expression.expressionName);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public boneNames(nodeId: string): string[] {
|
|
115
|
+
const vrm = this.entries.get(nodeId)?.vrm;
|
|
116
|
+
if (vrm === null || vrm === undefined) return [];
|
|
117
|
+
return Object.keys(vrm.humanoid.humanBones).filter(
|
|
118
|
+
(name) => vrm.humanoid.getNormalizedBoneNode(name) !== null
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public state(nodeId: string): VrmState {
|
|
123
|
+
return this.entries.get(nodeId)?.state ?? 'none';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public error(nodeId: string): string {
|
|
127
|
+
return this.entries.get(nodeId)?.error ?? '';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public update(deltaSeconds: number): void {
|
|
131
|
+
for (const entry of this.entries.values()) entry.vrm?.update(deltaSeconds);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public remove(nodeId: string): void {
|
|
135
|
+
const entry = this.entries.get(nodeId);
|
|
136
|
+
if (entry === undefined) return;
|
|
137
|
+
this.entries.delete(nodeId);
|
|
138
|
+
if (entry.vrm !== null && this.vrmApi !== null) {
|
|
139
|
+
this.vrmApi.VRMUtils.deepDispose(entry.vrm.scene as never);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public clear(): void {
|
|
144
|
+
for (const nodeId of [...this.entries.keys()]) this.remove(nodeId);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private requireVrm(nodeId: string): VrmLike {
|
|
148
|
+
const entry = this.entries.get(nodeId);
|
|
149
|
+
if (entry?.vrm === null || entry?.vrm === undefined) {
|
|
150
|
+
throw new Error(`VRM ${nodeId} is ${entry?.state ?? 'not loaded'}.`);
|
|
151
|
+
}
|
|
152
|
+
return entry.vrm;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private requireThree(): VrmThreeApi {
|
|
156
|
+
const THREE = this.getThree();
|
|
157
|
+
if (THREE === null) throw new Error('VRM avatars need A-Frame to be loaded first.');
|
|
158
|
+
return THREE;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private api(THREE: VrmThreeApi): ReturnType<typeof createThreeVrm> {
|
|
162
|
+
if (this.vrmApi === null || this.vrmApiThree !== THREE) {
|
|
163
|
+
this.vrmApi = createThreeVrm(THREE);
|
|
164
|
+
this.vrmApiThree = THREE;
|
|
165
|
+
}
|
|
166
|
+
return this.vrmApi;
|
|
167
|
+
}
|
|
168
|
+
}
|