@kubohiroya/turbowarp-aframe 0.4.0 → 0.6.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 +8 -2
- package/README.md +30 -2
- package/dist/extension-manifest.json +28 -0
- package/dist/turbowarp-aframe.js +115 -4
- package/docs/architecture.ja.md +5 -4
- package/docs/architecture.md +5 -4
- package/package.json +1 -1
- package/src/block-definitions.json +20 -0
- package/src/extension.ts +59 -5
- package/src/runtime-capability.ts +23 -0
- package/src/vrm.ts +25 -0
package/README.ja.md
CHANGED
|
@@ -38,6 +38,8 @@ interface AFrameRuntimeCapabilityV2 {
|
|
|
38
38
|
createFromTemplate(template: string, instance: string, parent: string): void;
|
|
39
39
|
setPosition(selector: string, x: number, y: number, z: number): void;
|
|
40
40
|
setRotation(selector: string, x: number, y: number, z: number): void;
|
|
41
|
+
setAttribute(selector: string, name: string, value: string): void;
|
|
42
|
+
setData(selector: string, key: string, value: string): void;
|
|
41
43
|
emitEvent(type: string, selector: string, data: string): void;
|
|
42
44
|
deleteSelector(selector: string): void;
|
|
43
45
|
countSelector(selector: string): number;
|
|
@@ -45,10 +47,14 @@ interface AFrameRuntimeCapabilityV2 {
|
|
|
45
47
|
setVrmBoneRotation(selector: string, bone: string, x: number, y: number, z: number): void;
|
|
46
48
|
vrmBoneNames(selector: string): string[];
|
|
47
49
|
vrmStatus(selector: string): {state: 'none' | 'loading' | 'ready' | 'error'; error: string};
|
|
50
|
+
setVrmExpression(selector: string, name: string, weight: number): void;
|
|
51
|
+
vrmExpressionNames(selector: string): string[];
|
|
48
52
|
}
|
|
49
53
|
```
|
|
50
54
|
|
|
51
|
-
|
|
55
|
+
`setAttribute`と`setData`は、`visible`などのA-Frameコンポーネント属性と`data-<key>`属性を、selectorに一致するすべてのnodeへ書きます。属性を書く必要のある連携機能拡張—たとえばtarget poseとvisibilityを書く`turbowarp-ar`—が、機能拡張の背後でDOMを直接触らずこのportを通り、機能拡張が持つscene stateと矛盾しないようにするために存在します。
|
|
56
|
+
|
|
57
|
+
version 2は、scene操作に加えてVRMアバターを扱います。`loadVrm`は、A-Frameが読み込んだThree.jsの上でthree-vrmを動かし、selectorに最初に一致したnodeへモデルを読み込んで、準備ができると解決します。`setVrmBoneRotation`は、`leftUpperArm`などの正規化されたヒューマノイドのボーンへ、Tポーズからの回転を度のEuler角で設定します。どのVRMでも同じ回転が同じ意味になり、sceneのtickがモデル本来のボーンへ反映します。VRMの準備ができていないnodeは飛ばし、存在しないボーン名は例外になります。`setVrmExpression`は、`happy`、`blink`、`aa`などのプリセットまたはカスタムの表情の重みを、three-vrmと同じく0〜1に丸めて設定し、sceneのtickが反映します。`vrmExpressionNames`はモデルが持つ表情名を返し、存在しない名前は例外になります。
|
|
52
58
|
|
|
53
59
|
このcapabilityが公開するのは宣言的template、selector、ヒューマノイドのボーン名だけです。private DOM node、A-Frame object、Three.js object、glTF内部実装は公開しません。未対応versionはfail closedし、機能拡張のdispose後に保持されたcapabilityを呼び出した場合も、すべて明示的に拒否します。
|
|
54
60
|
|
|
@@ -70,7 +76,7 @@ TurboWarp では `dist/turbowarp-aframe.js` を unsandboxed custom extension と
|
|
|
70
76
|
package として参照する場合は version を固定します。
|
|
71
77
|
|
|
72
78
|
```bash
|
|
73
|
-
pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.
|
|
79
|
+
pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.6.0
|
|
74
80
|
```
|
|
75
81
|
|
|
76
82
|
## ブロック概要
|
package/README.md
CHANGED
|
@@ -38,6 +38,8 @@ interface AFrameRuntimeCapabilityV2 {
|
|
|
38
38
|
createFromTemplate(template: string, instance: string, parent: string): void;
|
|
39
39
|
setPosition(selector: string, x: number, y: number, z: number): void;
|
|
40
40
|
setRotation(selector: string, x: number, y: number, z: number): void;
|
|
41
|
+
setAttribute(selector: string, name: string, value: string): void;
|
|
42
|
+
setData(selector: string, key: string, value: string): void;
|
|
41
43
|
emitEvent(type: string, selector: string, data: string): void;
|
|
42
44
|
deleteSelector(selector: string): void;
|
|
43
45
|
countSelector(selector: string): number;
|
|
@@ -45,10 +47,14 @@ interface AFrameRuntimeCapabilityV2 {
|
|
|
45
47
|
setVrmBoneRotation(selector: string, bone: string, x: number, y: number, z: number): void;
|
|
46
48
|
vrmBoneNames(selector: string): string[];
|
|
47
49
|
vrmStatus(selector: string): {state: 'none' | 'loading' | 'ready' | 'error'; error: string};
|
|
50
|
+
setVrmExpression(selector: string, name: string, weight: number): void;
|
|
51
|
+
vrmExpressionNames(selector: string): string[];
|
|
48
52
|
}
|
|
49
53
|
```
|
|
50
54
|
|
|
51
|
-
|
|
55
|
+
`setAttribute` and `setData` write an A-Frame component attribute such as `visible`, and a `data-<key>` attribute, on every matching node. They exist so that a companion extension which has to write attributes, such as `turbowarp-ar` writing target pose and visibility, goes through this port and keeps the extension's scene state in sync, instead of reaching into the DOM behind its back.
|
|
56
|
+
|
|
57
|
+
Besides the scene operations, version 2 carries VRM avatars. `loadVrm` loads the model onto the first node matching the selector with three-vrm, running on the Three.js that A-Frame loaded, and resolves when it is ready. `setVrmBoneRotation` sets Euler degrees on a normalized humanoid bone such as `leftUpperArm`, relative to the T-pose, so the same rotation means the same thing on every VRM; the scene tick applies it to the model's own bones. Nodes whose VRM is not ready are skipped, and an unknown bone name throws. `setVrmExpression` sets the weight of a preset expression such as `happy`, `blink`, or `aa`, or of a custom one, clamped to 0 through 1 as three-vrm does, and the scene tick applies it; `vrmExpressionNames` lists the names the model has, and an unknown name throws.
|
|
52
58
|
|
|
53
59
|
The capability deliberately exposes declarative templates, selectors, and humanoid bone names, not private DOM nodes, A-Frame objects, Three.js objects, or glTF internals. Unsupported versions fail closed. A retained capability reference also rejects every operation after the extension is disposed.
|
|
54
60
|
|
|
@@ -70,7 +76,7 @@ Load `dist/turbowarp-aframe.js` as an unsandboxed custom extension in TurboWarp.
|
|
|
70
76
|
For package-based reuse, pin the version:
|
|
71
77
|
|
|
72
78
|
```bash
|
|
73
|
-
pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.
|
|
79
|
+
pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.6.0
|
|
74
80
|
```
|
|
75
81
|
|
|
76
82
|
## Block reference
|
|
@@ -465,6 +471,28 @@ Returns the humanoid bone names of the first matching node's VRM as a JSON array
|
|
|
465
471
|
| Opcode | `vrmBoneNames` |
|
|
466
472
|
| `SELECTOR` | String, default: `#avatar` |
|
|
467
473
|
|
|
474
|
+
### `set VRM [SELECTOR] expression [NAME] to [WEIGHT]`
|
|
475
|
+
|
|
476
|
+
Sets the weight, clamped to 0 through 1, of a preset or custom VRM expression such as happy or blink on every matching node whose VRM is ready.
|
|
477
|
+
|
|
478
|
+
| Property | Value |
|
|
479
|
+
|---|---|
|
|
480
|
+
| Type | Command |
|
|
481
|
+
| Opcode | `setVrmExpression` |
|
|
482
|
+
| `SELECTOR` | String, default: `#avatar` |
|
|
483
|
+
| `NAME` | String, default: `happy` |
|
|
484
|
+
| `WEIGHT` | Number, default: `1` |
|
|
485
|
+
|
|
486
|
+
### `VRM [SELECTOR] expressions`
|
|
487
|
+
|
|
488
|
+
Returns the preset and custom expression names of the first matching node's VRM as a JSON array.
|
|
489
|
+
|
|
490
|
+
| Property | Value |
|
|
491
|
+
|---|---|
|
|
492
|
+
| Type | Reporter |
|
|
493
|
+
| Opcode | `vrmExpressionNames` |
|
|
494
|
+
| `SELECTOR` | String, default: `#avatar` |
|
|
495
|
+
|
|
468
496
|
### `VRM [SELECTOR] state`
|
|
469
497
|
|
|
470
498
|
Returns none, loading, ready, or error with its reason for the first matching node.
|
|
@@ -519,6 +519,24 @@
|
|
|
519
519
|
}
|
|
520
520
|
]
|
|
521
521
|
},
|
|
522
|
+
{
|
|
523
|
+
"opcode": "setVrmExpression",
|
|
524
|
+
"blockType": "COMMAND",
|
|
525
|
+
"arguments": [
|
|
526
|
+
{
|
|
527
|
+
"id": "NAME",
|
|
528
|
+
"type": "STRING"
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
"id": "SELECTOR",
|
|
532
|
+
"type": "STRING"
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
"id": "WEIGHT",
|
|
536
|
+
"type": "NUMBER"
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
},
|
|
522
540
|
{
|
|
523
541
|
"opcode": "stopAnimationClip",
|
|
524
542
|
"blockType": "COMMAND",
|
|
@@ -543,6 +561,16 @@
|
|
|
543
561
|
}
|
|
544
562
|
]
|
|
545
563
|
},
|
|
564
|
+
{
|
|
565
|
+
"opcode": "vrmExpressionNames",
|
|
566
|
+
"blockType": "REPORTER",
|
|
567
|
+
"arguments": [
|
|
568
|
+
{
|
|
569
|
+
"id": "SELECTOR",
|
|
570
|
+
"type": "STRING"
|
|
571
|
+
}
|
|
572
|
+
]
|
|
573
|
+
},
|
|
546
574
|
{
|
|
547
575
|
"opcode": "vrmState",
|
|
548
576
|
"blockType": "REPORTER",
|
package/dist/turbowarp-aframe.js
CHANGED
|
@@ -640,6 +640,36 @@
|
|
|
640
640
|
"defaultValue": "#avatar"
|
|
641
641
|
} }
|
|
642
642
|
},
|
|
643
|
+
{
|
|
644
|
+
"opcode": "setVrmExpression",
|
|
645
|
+
"blockType": "COMMAND",
|
|
646
|
+
"text": "set VRM [SELECTOR] expression [NAME] to [WEIGHT]",
|
|
647
|
+
"description": "Sets the weight, clamped to 0 through 1, of a preset or custom VRM expression such as happy or blink on every matching node whose VRM is ready.",
|
|
648
|
+
"arguments": {
|
|
649
|
+
"SELECTOR": {
|
|
650
|
+
"type": "STRING",
|
|
651
|
+
"defaultValue": "#avatar"
|
|
652
|
+
},
|
|
653
|
+
"NAME": {
|
|
654
|
+
"type": "STRING",
|
|
655
|
+
"defaultValue": "happy"
|
|
656
|
+
},
|
|
657
|
+
"WEIGHT": {
|
|
658
|
+
"type": "NUMBER",
|
|
659
|
+
"defaultValue": 1
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
{
|
|
664
|
+
"opcode": "vrmExpressionNames",
|
|
665
|
+
"blockType": "REPORTER",
|
|
666
|
+
"text": "VRM [SELECTOR] expressions",
|
|
667
|
+
"description": "Returns the preset and custom expression names of the first matching node's VRM as a JSON array.",
|
|
668
|
+
"arguments": { "SELECTOR": {
|
|
669
|
+
"type": "STRING",
|
|
670
|
+
"defaultValue": "#avatar"
|
|
671
|
+
} }
|
|
672
|
+
},
|
|
643
673
|
{
|
|
644
674
|
"opcode": "vrmState",
|
|
645
675
|
"blockType": "REPORTER",
|
|
@@ -671,6 +701,14 @@
|
|
|
671
701
|
assertActive();
|
|
672
702
|
scene.setRotation(selector, x, y, z);
|
|
673
703
|
},
|
|
704
|
+
setAttribute(selector, name, value) {
|
|
705
|
+
assertActive();
|
|
706
|
+
scene.setAttribute(selector, name, value);
|
|
707
|
+
},
|
|
708
|
+
setData(selector, key, value) {
|
|
709
|
+
assertActive();
|
|
710
|
+
scene.setData(selector, key, value);
|
|
711
|
+
},
|
|
674
712
|
emitEvent(type, selector, data) {
|
|
675
713
|
assertActive();
|
|
676
714
|
scene.emitEvent(type, selector, data);
|
|
@@ -700,6 +738,14 @@
|
|
|
700
738
|
vrmStatus(selector) {
|
|
701
739
|
assertActive();
|
|
702
740
|
return scene.vrmStatus(selector);
|
|
741
|
+
},
|
|
742
|
+
setVrmExpression(selector, name, weight) {
|
|
743
|
+
assertActive();
|
|
744
|
+
scene.setVrmExpression(selector, name, weight);
|
|
745
|
+
},
|
|
746
|
+
vrmExpressionNames(selector) {
|
|
747
|
+
assertActive();
|
|
748
|
+
return scene.vrmExpressionNames(selector);
|
|
703
749
|
}
|
|
704
750
|
};
|
|
705
751
|
const capability = Object.freeze({
|
|
@@ -6875,6 +6921,20 @@
|
|
|
6875
6921
|
const { degToRad } = this.requireThree().MathUtils;
|
|
6876
6922
|
node.rotation.set(degToRad(degrees.x), degToRad(degrees.y), degToRad(degrees.z));
|
|
6877
6923
|
}
|
|
6924
|
+
/**
|
|
6925
|
+
* Sets an expression's weight. The weight is clamped to 0 through 1, as three-vrm does, and
|
|
6926
|
+
* the scene tick applies it with the other expressions.
|
|
6927
|
+
*/
|
|
6928
|
+
setExpression(nodeId, name, weight) {
|
|
6929
|
+
if (!Number.isFinite(weight)) throw new Error(`VRM expression weight must be a number: ${weight}`);
|
|
6930
|
+
const expressions = this.requireVrm(nodeId).expressionManager;
|
|
6931
|
+
if (expressions === void 0) throw new Error(`VRM ${nodeId} has no expressions.`);
|
|
6932
|
+
if (expressions.getExpression(name) == null) throw new Error(`VRM ${nodeId} has no expression: ${name}`);
|
|
6933
|
+
expressions.setValue(name, Math.min(Math.max(weight, 0), 1));
|
|
6934
|
+
}
|
|
6935
|
+
expressionNames(nodeId) {
|
|
6936
|
+
return (this.entries.get(nodeId)?.vrm?.expressionManager?.expressions ?? []).map((expression) => expression.expressionName);
|
|
6937
|
+
}
|
|
6878
6938
|
boneNames(nodeId) {
|
|
6879
6939
|
const vrm = this.entries.get(nodeId)?.vrm;
|
|
6880
6940
|
if (vrm === null || vrm === void 0) return [];
|
|
@@ -6918,6 +6978,18 @@
|
|
|
6918
6978
|
};
|
|
6919
6979
|
//#endregion
|
|
6920
6980
|
//#region src/extension.ts
|
|
6981
|
+
var SCENE_LAYERS = Object.freeze(["above-stage", "below-stage"]);
|
|
6982
|
+
var DEFAULT_SCENE_LAYER = "above-stage";
|
|
6983
|
+
/**
|
|
6984
|
+
* Stacking positions for the two hosts that can share the stage.
|
|
6985
|
+
*
|
|
6986
|
+
* `turbowarp-ar` puts its camera background one step below the `above-stage` value here, so a 3D
|
|
6987
|
+
* scene renders over the camera image. Keep the two in step when either extension changes.
|
|
6988
|
+
*/
|
|
6989
|
+
var SCENE_HOST_Z_INDEX = {
|
|
6990
|
+
"above-stage": "10",
|
|
6991
|
+
"below-stage": "0"
|
|
6992
|
+
};
|
|
6921
6993
|
var blockDefinitions = block_definitions_default.blocks;
|
|
6922
6994
|
var ROOT_ID = "scene";
|
|
6923
6995
|
var DOM_EVENT_TYPES = [
|
|
@@ -6945,7 +7017,7 @@
|
|
|
6945
7017
|
this.domEventTypes = new Set(DOM_EVENT_TYPES);
|
|
6946
7018
|
this.runtimeId = `twaframe-${Math.random().toString(36).slice(2)}`;
|
|
6947
7019
|
this.sceneOptions = {
|
|
6948
|
-
layer:
|
|
7020
|
+
layer: DEFAULT_SCENE_LAYER,
|
|
6949
7021
|
mode: "3d"
|
|
6950
7022
|
};
|
|
6951
7023
|
this.rootElement = null;
|
|
@@ -6976,6 +7048,16 @@
|
|
|
6976
7048
|
Y: y,
|
|
6977
7049
|
Z: z
|
|
6978
7050
|
}),
|
|
7051
|
+
setAttribute: (selector, name, value) => this.setAttribute({
|
|
7052
|
+
SELECTOR: selector,
|
|
7053
|
+
NAME: name,
|
|
7054
|
+
VALUE: value
|
|
7055
|
+
}),
|
|
7056
|
+
setData: (selector, key, value) => this.setData({
|
|
7057
|
+
SELECTOR: selector,
|
|
7058
|
+
KEY: key,
|
|
7059
|
+
VALUE: value
|
|
7060
|
+
}),
|
|
6979
7061
|
emitEvent: (type, selector, data) => this.emitEvent({
|
|
6980
7062
|
TYPE: type,
|
|
6981
7063
|
SELECTOR: selector,
|
|
@@ -6995,7 +7077,13 @@
|
|
|
6995
7077
|
Z: z
|
|
6996
7078
|
}),
|
|
6997
7079
|
vrmBoneNames: (selector) => this.vrmBoneNamesFor(Scratch.Cast.toString(selector)),
|
|
6998
|
-
vrmStatus: (selector) => this.vrmStatusFor(Scratch.Cast.toString(selector))
|
|
7080
|
+
vrmStatus: (selector) => this.vrmStatusFor(Scratch.Cast.toString(selector)),
|
|
7081
|
+
setVrmExpression: (selector, name, weight) => this.setVrmExpression({
|
|
7082
|
+
SELECTOR: selector,
|
|
7083
|
+
NAME: name,
|
|
7084
|
+
WEIGHT: weight
|
|
7085
|
+
}),
|
|
7086
|
+
vrmExpressionNames: (selector) => this.vrmExpressionNamesFor(Scratch.Cast.toString(selector))
|
|
6999
7087
|
}, () => this.assertActive());
|
|
7000
7088
|
const runtime = Scratch.vm?.runtime;
|
|
7001
7089
|
if (runtime) runtime[runtimeCapabilityKey] = this.runtimeCapability;
|
|
@@ -7012,7 +7100,7 @@
|
|
|
7012
7100
|
async createScene(args) {
|
|
7013
7101
|
await ensureAFrame();
|
|
7014
7102
|
this.sceneOptions = {
|
|
7015
|
-
layer: Scratch.Cast.toString(args.LAYER)
|
|
7103
|
+
layer: this.normalizeLayer(Scratch.Cast.toString(args.LAYER)),
|
|
7016
7104
|
mode: Scratch.Cast.toString(args.MODE) || "3d"
|
|
7017
7105
|
};
|
|
7018
7106
|
this.resetGraph();
|
|
@@ -7270,6 +7358,17 @@
|
|
|
7270
7358
|
this.vrms.setBoneRotation(node.id, bone, rotation);
|
|
7271
7359
|
}
|
|
7272
7360
|
}
|
|
7361
|
+
setVrmExpression(args) {
|
|
7362
|
+
const name = Scratch.Cast.toString(args.NAME).trim();
|
|
7363
|
+
const weight = Scratch.Cast.toNumber(args.WEIGHT);
|
|
7364
|
+
for (const node of this.matches(Scratch.Cast.toString(args.SELECTOR))) {
|
|
7365
|
+
if (this.vrms.state(node.id) !== "ready") continue;
|
|
7366
|
+
this.vrms.setExpression(node.id, name, weight);
|
|
7367
|
+
}
|
|
7368
|
+
}
|
|
7369
|
+
vrmExpressionNames(args) {
|
|
7370
|
+
return JSON.stringify(this.vrmExpressionNamesFor(Scratch.Cast.toString(args.SELECTOR)));
|
|
7371
|
+
}
|
|
7273
7372
|
vrmBoneNames(args) {
|
|
7274
7373
|
return JSON.stringify(this.vrmBoneNamesFor(Scratch.Cast.toString(args.SELECTOR)));
|
|
7275
7374
|
}
|
|
@@ -7429,7 +7528,7 @@
|
|
|
7429
7528
|
host.style.position = "absolute";
|
|
7430
7529
|
host.style.inset = "0";
|
|
7431
7530
|
host.style.pointerEvents = "auto";
|
|
7432
|
-
host.style.zIndex = this.sceneOptions.layer
|
|
7531
|
+
host.style.zIndex = SCENE_HOST_Z_INDEX[this.sceneOptions.layer];
|
|
7433
7532
|
const scene = document.createElement("a-scene");
|
|
7434
7533
|
host.append(scene);
|
|
7435
7534
|
this.findStageMount().append(host);
|
|
@@ -7662,6 +7761,10 @@
|
|
|
7662
7761
|
const node = this.firstMatch(selector);
|
|
7663
7762
|
return node === void 0 ? [] : this.vrms.boneNames(node.id);
|
|
7664
7763
|
}
|
|
7764
|
+
vrmExpressionNamesFor(selector) {
|
|
7765
|
+
const node = this.firstMatch(selector);
|
|
7766
|
+
return node === void 0 ? [] : this.vrms.expressionNames(node.id);
|
|
7767
|
+
}
|
|
7665
7768
|
vrmStatusFor(selector) {
|
|
7666
7769
|
const node = this.firstMatch(selector);
|
|
7667
7770
|
if (node === void 0) return {
|
|
@@ -7799,6 +7902,14 @@
|
|
|
7799
7902
|
normalizeId(value) {
|
|
7800
7903
|
return this.normalizeToken(value) || "node";
|
|
7801
7904
|
}
|
|
7905
|
+
/**
|
|
7906
|
+
* Blocks take whatever string a project hands them, so an unknown layer falls back to the default
|
|
7907
|
+
* rather than stopping the script, and never reaches the host as a value it cannot honor.
|
|
7908
|
+
*/
|
|
7909
|
+
normalizeLayer(value) {
|
|
7910
|
+
const layer = value.trim();
|
|
7911
|
+
return SCENE_LAYERS.includes(layer) ? layer : DEFAULT_SCENE_LAYER;
|
|
7912
|
+
}
|
|
7802
7913
|
normalizeToken(value) {
|
|
7803
7914
|
return value.trim().replace(/[^a-zA-Z0-9_-]/g, "-");
|
|
7804
7915
|
}
|
package/docs/architecture.ja.md
CHANGED
|
@@ -24,7 +24,7 @@ src/config.ts + src/block-definitions.json
|
|
|
24
24
|
|
|
25
25
|
## version付きruntime capability
|
|
26
26
|
|
|
27
|
-
機能拡張は`Scratch.vm.runtime.turbowarpAFrameCapability`を公開します。version 2は、`loadTemplate`、`createFromTemplate`、`setPosition`、`setRotation`、`emitEvent`、`deleteSelector`、`countSelector`に、`loadVrm`、`setVrmBoneRotation`、`vrmBoneNames`、`vrmStatus`を加えた、freezeされた狭い型付きportです。scene操作だけを持っていたversion 1は、version 2と並べて残さず0.4.0で撤去しました。正しく保つ契約を1
|
|
27
|
+
機能拡張は`Scratch.vm.runtime.turbowarpAFrameCapability`を公開します。version 2は、`loadTemplate`、`createFromTemplate`、`setPosition`、`setRotation`、`setAttribute`、`setData`、`emitEvent`、`deleteSelector`、`countSelector`に、`loadVrm`、`setVrmBoneRotation`、`vrmBoneNames`、`vrmStatus`、`setVrmExpression`、`vrmExpressionNames`を加えた、freezeされた狭い型付きportです。scene操作だけを持っていたversion 1は、version 2と並べて残さず0.4.0で撤去しました。正しく保つ契約を1つにするためです。表情の操作と、後から加えた`setAttribute`・`setData`は、methodを加えるだけでconsumerは使うmethodの有無を確かめるため、versionを変えずにversion 2へ加えました。`setAttribute`と`setData`は、A-Frame属性を書く必要のある連携機能拡張—たとえばtarget poseとvisibilityを書く`turbowarp-ar`—が、機能拡張の背後でDOMを直接触らずこのportを通るようにするために存在します。
|
|
28
28
|
|
|
29
29
|
各port methodは対応するblock handlerへ委譲します。そのため、block呼び出しと複合機能拡張からの呼び出しは、cast、validation、selector matching、event queue、機能拡張所有のscene stateを共有します。portはDOM element、A-Frame/Three.js object、glTF内部実装を公開しません。
|
|
30
30
|
|
|
@@ -36,13 +36,14 @@ consumerは利用前に`requireVersion(2)`を呼ぶ必要があります。1を
|
|
|
36
36
|
|
|
37
37
|
`createScene` は root の `#scene` ノードを作り、ブラウザ DOM がある場合は `#tw-aframe-root > a-scene` を作成します。host は最初に検出できた TurboWarp stage wrapper または canvas の親へ mount し、stage らしい要素が見つからない場合だけ `document.body` に fallback します。
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
layer の語彙は閉じており、`turbowarp-ar` もカメラ背景に同じ2つの値を使います。
|
|
40
40
|
|
|
41
41
|
- `above-stage`: A-Frame を TurboWarp stage の上に重ねます。
|
|
42
42
|
- `below-stage`: host CSS が許す場合に A-Frame を stage の背面へ置きます。
|
|
43
|
-
- `camera-under-3d`: camera/video を 3D layer の下に置く想定です。
|
|
44
43
|
|
|
45
|
-
|
|
44
|
+
block の引数は文字列のままなので作品は任意の値を渡せますが、認識できない値は `above-stage` へフォールバックします。host が honor できない値を layer として保持することはありません。`above-stage` のとき、host は `turbowarp-ar` のカメラ背景より1つ上の重なり位置に置かれ、3D scene がカメラ映像の上に描かれます。`camera-under-3d` はかつてここに記載されていましたが実装されたことはありません。それが表していた順序は構造であって、呼び出し側が指定できる値ではありません。
|
|
45
|
+
|
|
46
|
+
一方 mode は、意図を述べるだけの開いた文字列です。
|
|
46
47
|
|
|
47
48
|
- `3d`: 通常の埋め込み 3D シーンです。
|
|
48
49
|
- `camera`: camera 合成を想定します。
|
package/docs/architecture.md
CHANGED
|
@@ -24,7 +24,7 @@ The generated JavaScript bundle is an unsandboxed TurboWarp extension. The manif
|
|
|
24
24
|
|
|
25
25
|
## Versioned Runtime Capability
|
|
26
26
|
|
|
27
|
-
The extension publishes `Scratch.vm.runtime.turbowarpAFrameCapability`. Version 2 is a narrow typed, frozen port containing `loadTemplate`, `createFromTemplate`, `setPosition`, `setRotation`, `emitEvent`, `deleteSelector`, and `countSelector`, plus `loadVrm`, `setVrmBoneRotation`, `vrmBoneNames`, and `
|
|
27
|
+
The extension publishes `Scratch.vm.runtime.turbowarpAFrameCapability`. Version 2 is a narrow typed, frozen port containing `loadTemplate`, `createFromTemplate`, `setPosition`, `setRotation`, `setAttribute`, `setData`, `emitEvent`, `deleteSelector`, and `countSelector`, plus `loadVrm`, `setVrmBoneRotation`, `vrmBoneNames`, `vrmStatus`, `setVrmExpression`, and `vrmExpressionNames`. The expression operations, and later `setAttribute` and `setData`, were added to version 2 without changing its version, because they only add methods and a consumer checks for the methods it uses. `setAttribute` and `setData` exist so that a companion extension which needs to write A-Frame attributes, such as `turbowarp-ar` writing target pose and visibility, goes through this port instead of reaching into the DOM behind the extension's back. Version 1, which had only the scene operations, was removed in 0.4.0 rather than kept beside version 2, so there is a single contract to keep correct.
|
|
28
28
|
|
|
29
29
|
Each port method delegates to the corresponding block handler. Consequently, block calls and composite-extension calls share casting, validation, selector matching, event queuing, and the extension-owned scene state. The port never exposes DOM elements, A-Frame/Three.js objects, or glTF internals.
|
|
30
30
|
|
|
@@ -36,13 +36,14 @@ Before building the scene, `createScene` makes sure A-Frame 1.8.0 is on the page
|
|
|
36
36
|
|
|
37
37
|
`createScene` creates a root `#scene` node and, when a browser DOM exists, a `#tw-aframe-root > a-scene` host. The host is mounted inside the first detected TurboWarp stage wrapper or canvas parent, falling back to `document.body` only when no stage-like element is found.
|
|
38
38
|
|
|
39
|
-
The
|
|
39
|
+
The layer vocabulary is closed, and `turbowarp-ar` uses the same two values for its camera background:
|
|
40
40
|
|
|
41
41
|
- `above-stage`: A-Frame overlays the TurboWarp stage.
|
|
42
42
|
- `below-stage`: A-Frame stays behind the stage when the host CSS allows it.
|
|
43
|
-
- `camera-under-3d`: camera/video should sit below the 3D layer.
|
|
44
43
|
|
|
45
|
-
The
|
|
44
|
+
The block argument stays a plain string, so a project can pass anything; an unrecognized value falls back to `above-stage` instead of being stored as a layer the host cannot honor. On `above-stage` the host is given a stacking position one step above `turbowarp-ar`'s camera background, so a 3D scene renders over the camera image. `camera-under-3d` was listed here once and never implemented — the ordering it described is structural, not a value a caller can ask for.
|
|
45
|
+
|
|
46
|
+
The mode values, by contrast, are open strings that only describe intent:
|
|
46
47
|
|
|
47
48
|
- `3d`: ordinary embedded 3D scene.
|
|
48
49
|
- `camera`: camera-backed composition.
|
package/package.json
CHANGED
|
@@ -357,6 +357,26 @@
|
|
|
357
357
|
"SELECTOR": {"type": "STRING", "defaultValue": "#avatar"}
|
|
358
358
|
}
|
|
359
359
|
},
|
|
360
|
+
{
|
|
361
|
+
"opcode": "setVrmExpression",
|
|
362
|
+
"blockType": "COMMAND",
|
|
363
|
+
"text": "set VRM [SELECTOR] expression [NAME] to [WEIGHT]",
|
|
364
|
+
"description": "Sets the weight, clamped to 0 through 1, of a preset or custom VRM expression such as happy or blink on every matching node whose VRM is ready.",
|
|
365
|
+
"arguments": {
|
|
366
|
+
"SELECTOR": {"type": "STRING", "defaultValue": "#avatar"},
|
|
367
|
+
"NAME": {"type": "STRING", "defaultValue": "happy"},
|
|
368
|
+
"WEIGHT": {"type": "NUMBER", "defaultValue": 1}
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
"opcode": "vrmExpressionNames",
|
|
373
|
+
"blockType": "REPORTER",
|
|
374
|
+
"text": "VRM [SELECTOR] expressions",
|
|
375
|
+
"description": "Returns the preset and custom expression names of the first matching node's VRM as a JSON array.",
|
|
376
|
+
"arguments": {
|
|
377
|
+
"SELECTOR": {"type": "STRING", "defaultValue": "#avatar"}
|
|
378
|
+
}
|
|
379
|
+
},
|
|
360
380
|
{
|
|
361
381
|
"opcode": "vrmState",
|
|
362
382
|
"blockType": "REPORTER",
|
package/src/extension.ts
CHANGED
|
@@ -53,8 +53,28 @@ interface SceneEvent {
|
|
|
53
53
|
data: string;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Where the scene host sits relative to the TurboWarp stage.
|
|
58
|
+
*
|
|
59
|
+
* This is the whole vocabulary, and `turbowarp-ar` uses the same two values for its camera
|
|
60
|
+
* background. `mode` stays an open string because it only describes intent; `layer` decides where
|
|
61
|
+
* the host is drawn, so a value the host cannot honor does not belong in it.
|
|
62
|
+
*/
|
|
63
|
+
type SceneLayer = 'above-stage' | 'below-stage';
|
|
64
|
+
|
|
65
|
+
const SCENE_LAYERS: readonly SceneLayer[] = Object.freeze(['above-stage', 'below-stage']);
|
|
66
|
+
const DEFAULT_SCENE_LAYER: SceneLayer = 'above-stage';
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Stacking positions for the two hosts that can share the stage.
|
|
70
|
+
*
|
|
71
|
+
* `turbowarp-ar` puts its camera background one step below the `above-stage` value here, so a 3D
|
|
72
|
+
* scene renders over the camera image. Keep the two in step when either extension changes.
|
|
73
|
+
*/
|
|
74
|
+
const SCENE_HOST_Z_INDEX: Record<SceneLayer, string> = {'above-stage': '10', 'below-stage': '0'};
|
|
75
|
+
|
|
56
76
|
interface SceneOptions {
|
|
57
|
-
layer:
|
|
77
|
+
layer: SceneLayer;
|
|
58
78
|
mode: string;
|
|
59
79
|
}
|
|
60
80
|
|
|
@@ -139,7 +159,7 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
139
159
|
private readonly eventQueue: SceneEvent[] = [];
|
|
140
160
|
private readonly domEventTypes = new Set<string>(DOM_EVENT_TYPES);
|
|
141
161
|
private readonly runtimeId = `twaframe-${Math.random().toString(36).slice(2)}`;
|
|
142
|
-
private sceneOptions: SceneOptions = {layer:
|
|
162
|
+
private sceneOptions: SceneOptions = {layer: DEFAULT_SCENE_LAYER, mode: '3d'};
|
|
143
163
|
private rootElement: Element | null = null;
|
|
144
164
|
private sceneReadyPending = false;
|
|
145
165
|
private lastEvent: SceneEvent | null = null;
|
|
@@ -158,6 +178,10 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
158
178
|
this.setPosition({SELECTOR: selector, X: x, Y: y, Z: z}),
|
|
159
179
|
setRotation: (selector, x, y, z) =>
|
|
160
180
|
this.setRotation({SELECTOR: selector, X: x, Y: y, Z: z}),
|
|
181
|
+
setAttribute: (selector, name, value) =>
|
|
182
|
+
this.setAttribute({SELECTOR: selector, NAME: name, VALUE: value}),
|
|
183
|
+
setData: (selector, key, value) =>
|
|
184
|
+
this.setData({SELECTOR: selector, KEY: key, VALUE: value}),
|
|
161
185
|
emitEvent: (type, selector, data) =>
|
|
162
186
|
this.emitEvent({TYPE: type, SELECTOR: selector, DATA: data}),
|
|
163
187
|
deleteSelector: (selector) => this.deleteSelector({SELECTOR: selector}),
|
|
@@ -166,7 +190,10 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
166
190
|
setVrmBoneRotation: (selector, bone, x, y, z) =>
|
|
167
191
|
this.setVrmBoneRotation({SELECTOR: selector, BONE: bone, X: x, Y: y, Z: z}),
|
|
168
192
|
vrmBoneNames: (selector) => this.vrmBoneNamesFor(Scratch.Cast.toString(selector)),
|
|
169
|
-
vrmStatus: (selector) => this.vrmStatusFor(Scratch.Cast.toString(selector))
|
|
193
|
+
vrmStatus: (selector) => this.vrmStatusFor(Scratch.Cast.toString(selector)),
|
|
194
|
+
setVrmExpression: (selector, name, weight) =>
|
|
195
|
+
this.setVrmExpression({SELECTOR: selector, NAME: name, WEIGHT: weight}),
|
|
196
|
+
vrmExpressionNames: (selector) => this.vrmExpressionNamesFor(Scratch.Cast.toString(selector))
|
|
170
197
|
},
|
|
171
198
|
() => this.assertActive()
|
|
172
199
|
);
|
|
@@ -187,7 +214,7 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
187
214
|
public async createScene(args: {LAYER: unknown; MODE: unknown}): Promise<void> {
|
|
188
215
|
await ensureAFrame();
|
|
189
216
|
this.sceneOptions = {
|
|
190
|
-
layer: Scratch.Cast.toString(args.LAYER)
|
|
217
|
+
layer: this.normalizeLayer(Scratch.Cast.toString(args.LAYER)),
|
|
191
218
|
mode: Scratch.Cast.toString(args.MODE) || '3d'
|
|
192
219
|
};
|
|
193
220
|
this.resetGraph();
|
|
@@ -546,6 +573,19 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
546
573
|
}
|
|
547
574
|
}
|
|
548
575
|
|
|
576
|
+
public setVrmExpression(args: {SELECTOR: unknown; NAME: unknown; WEIGHT: unknown}): void {
|
|
577
|
+
const name = Scratch.Cast.toString(args.NAME).trim();
|
|
578
|
+
const weight = Scratch.Cast.toNumber(args.WEIGHT);
|
|
579
|
+
for (const node of this.matches(Scratch.Cast.toString(args.SELECTOR))) {
|
|
580
|
+
if (this.vrms.state(node.id) !== 'ready') continue;
|
|
581
|
+
this.vrms.setExpression(node.id, name, weight);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
public vrmExpressionNames(args: {SELECTOR: unknown}): string {
|
|
586
|
+
return JSON.stringify(this.vrmExpressionNamesFor(Scratch.Cast.toString(args.SELECTOR)));
|
|
587
|
+
}
|
|
588
|
+
|
|
549
589
|
public vrmBoneNames(args: {SELECTOR: unknown}): string {
|
|
550
590
|
return JSON.stringify(this.vrmBoneNamesFor(Scratch.Cast.toString(args.SELECTOR)));
|
|
551
591
|
}
|
|
@@ -745,7 +785,7 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
745
785
|
host.style.position = 'absolute';
|
|
746
786
|
host.style.inset = '0';
|
|
747
787
|
host.style.pointerEvents = 'auto';
|
|
748
|
-
host.style.zIndex = this.sceneOptions.layer
|
|
788
|
+
host.style.zIndex = SCENE_HOST_Z_INDEX[this.sceneOptions.layer];
|
|
749
789
|
|
|
750
790
|
const scene = document.createElement('a-scene');
|
|
751
791
|
host.append(scene);
|
|
@@ -1097,6 +1137,11 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
1097
1137
|
return node === undefined ? [] : this.vrms.boneNames(node.id);
|
|
1098
1138
|
}
|
|
1099
1139
|
|
|
1140
|
+
private vrmExpressionNamesFor(selector: string): string[] {
|
|
1141
|
+
const node = this.firstMatch(selector);
|
|
1142
|
+
return node === undefined ? [] : this.vrms.expressionNames(node.id);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1100
1145
|
private vrmStatusFor(selector: string): AFrameVrmStatus {
|
|
1101
1146
|
const node = this.firstMatch(selector);
|
|
1102
1147
|
if (node === undefined) return {state: 'none', error: ''};
|
|
@@ -1276,6 +1321,15 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
|
|
|
1276
1321
|
return this.normalizeToken(value) || 'node';
|
|
1277
1322
|
}
|
|
1278
1323
|
|
|
1324
|
+
/**
|
|
1325
|
+
* Blocks take whatever string a project hands them, so an unknown layer falls back to the default
|
|
1326
|
+
* rather than stopping the script, and never reaches the host as a value it cannot honor.
|
|
1327
|
+
*/
|
|
1328
|
+
private normalizeLayer(value: string): SceneLayer {
|
|
1329
|
+
const layer = value.trim() as SceneLayer;
|
|
1330
|
+
return SCENE_LAYERS.includes(layer) ? layer : DEFAULT_SCENE_LAYER;
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1279
1333
|
private normalizeToken(value: string): string {
|
|
1280
1334
|
return value.trim().replace(/[^a-zA-Z0-9_-]/g, '-');
|
|
1281
1335
|
}
|
|
@@ -7,6 +7,10 @@ export interface AFrameScenePort {
|
|
|
7
7
|
createFromTemplate(template: string, instance: string, parent: string): void;
|
|
8
8
|
setPosition(selector: string, x: number, y: number, z: number): void;
|
|
9
9
|
setRotation(selector: string, x: number, y: number, z: number): void;
|
|
10
|
+
/** Sets an A-Frame component attribute, such as `visible`, on every matching node. */
|
|
11
|
+
setAttribute(selector: string, name: string, value: string): void;
|
|
12
|
+
/** Sets a `data-<key>` attribute on every matching node. */
|
|
13
|
+
setData(selector: string, key: string, value: string): void;
|
|
10
14
|
emitEvent(type: string, selector: string, data: string): void;
|
|
11
15
|
deleteSelector(selector: string): void;
|
|
12
16
|
countSelector(selector: string): number;
|
|
@@ -26,6 +30,9 @@ export interface AFrameVrmPort {
|
|
|
26
30
|
setVrmBoneRotation(selector: string, bone: string, x: number, y: number, z: number): void;
|
|
27
31
|
vrmBoneNames(selector: string): string[];
|
|
28
32
|
vrmStatus(selector: string): AFrameVrmStatus;
|
|
33
|
+
/** Weight from 0 through 1, clamped, of a preset or custom VRM expression. */
|
|
34
|
+
setVrmExpression(selector: string, name: string, weight: number): void;
|
|
35
|
+
vrmExpressionNames(selector: string): string[];
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
export interface AFrameRuntimeCapabilityV2 extends AFrameScenePort, AFrameVrmPort {
|
|
@@ -54,6 +61,14 @@ export function createRuntimeCapability(
|
|
|
54
61
|
assertActive();
|
|
55
62
|
scene.setRotation(selector, x, y, z);
|
|
56
63
|
},
|
|
64
|
+
setAttribute(selector, name, value) {
|
|
65
|
+
assertActive();
|
|
66
|
+
scene.setAttribute(selector, name, value);
|
|
67
|
+
},
|
|
68
|
+
setData(selector, key, value) {
|
|
69
|
+
assertActive();
|
|
70
|
+
scene.setData(selector, key, value);
|
|
71
|
+
},
|
|
57
72
|
emitEvent(type, selector, data) {
|
|
58
73
|
assertActive();
|
|
59
74
|
scene.emitEvent(type, selector, data);
|
|
@@ -84,6 +99,14 @@ export function createRuntimeCapability(
|
|
|
84
99
|
vrmStatus(selector) {
|
|
85
100
|
assertActive();
|
|
86
101
|
return scene.vrmStatus(selector);
|
|
102
|
+
},
|
|
103
|
+
setVrmExpression(selector, name, weight) {
|
|
104
|
+
assertActive();
|
|
105
|
+
scene.setVrmExpression(selector, name, weight);
|
|
106
|
+
},
|
|
107
|
+
vrmExpressionNames(selector) {
|
|
108
|
+
assertActive();
|
|
109
|
+
return scene.vrmExpressionNames(selector);
|
|
87
110
|
}
|
|
88
111
|
};
|
|
89
112
|
|
package/src/vrm.ts
CHANGED
|
@@ -22,6 +22,11 @@ export interface VrmLike {
|
|
|
22
22
|
humanBones: Record<string, unknown>;
|
|
23
23
|
getNormalizedBoneNode(name: string): BoneLike | null;
|
|
24
24
|
};
|
|
25
|
+
expressionManager?: {
|
|
26
|
+
expressions: ReadonlyArray<{expressionName: string}>;
|
|
27
|
+
getExpression(name: string): unknown;
|
|
28
|
+
setValue(name: string, weight: number): void;
|
|
29
|
+
};
|
|
25
30
|
update(deltaSeconds: number): void;
|
|
26
31
|
}
|
|
27
32
|
|
|
@@ -86,6 +91,26 @@ export class VrmAvatars {
|
|
|
86
91
|
node.rotation.set(degToRad(degrees.x), degToRad(degrees.y), degToRad(degrees.z));
|
|
87
92
|
}
|
|
88
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
|
+
|
|
89
114
|
public boneNames(nodeId: string): string[] {
|
|
90
115
|
const vrm = this.entries.get(nodeId)?.vrm;
|
|
91
116
|
if (vrm === null || vrm === undefined) return [];
|