@kubohiroya/turbowarp-aframe 0.5.0 → 0.7.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 CHANGED
@@ -34,10 +34,15 @@ unsandboxed 拡張はページ DOM を操作できます。信頼できる生成
34
34
  interface AFrameRuntimeCapabilityV2 {
35
35
  readonly version: 2;
36
36
  requireVersion(version: number): AFrameRuntimeCapabilityV2;
37
+ createScene(layer: string, mode: string): Promise<void>;
38
+ createNode(type: string, id: string, parent: string): void;
39
+ addClass(className: string, selector: string): void;
37
40
  loadTemplate(id: string, source: string): void;
38
41
  createFromTemplate(template: string, instance: string, parent: string): void;
39
42
  setPosition(selector: string, x: number, y: number, z: number): void;
40
43
  setRotation(selector: string, x: number, y: number, z: number): void;
44
+ setAttribute(selector: string, name: string, value: string): void;
45
+ setData(selector: string, key: string, value: string): void;
41
46
  emitEvent(type: string, selector: string, data: string): void;
42
47
  deleteSelector(selector: string): void;
43
48
  countSelector(selector: string): number;
@@ -50,6 +55,10 @@ interface AFrameRuntimeCapabilityV2 {
50
55
  }
51
56
  ```
52
57
 
58
+ `createScene`、`createNode`、`addClass`はscene自体を構築します。scene記述をA-Frame nodeへ変換する機能拡張が、blockだけに頼らずこのportを通せるようにするためです。`createFromTemplate`は代用になりません。子のidにinstance idを前置し、`template`と`instance`のdata keyを書き込むため、自分でidを決めている呼び出し側は要求したidを得られません。
59
+
60
+ `setAttribute`と`setData`は、`visible`などのA-Frameコンポーネント属性と`data-<key>`属性を、selectorに一致するすべてのnodeへ書きます。属性を書く必要のある連携機能拡張—たとえばtarget poseとvisibilityを書く`turbowarp-ar`—が、機能拡張の背後でDOMを直接触らずこのportを通り、機能拡張が持つscene stateと矛盾しないようにするために存在します。
61
+
53
62
  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`はモデルが持つ表情名を返し、存在しない名前は例外になります。
54
63
 
55
64
  このcapabilityが公開するのは宣言的template、selector、ヒューマノイドのボーン名だけです。private DOM node、A-Frame object、Three.js object、glTF内部実装は公開しません。未対応versionはfail closedし、機能拡張のdispose後に保持されたcapabilityを呼び出した場合も、すべて明示的に拒否します。
@@ -72,7 +81,7 @@ TurboWarp では `dist/turbowarp-aframe.js` を unsandboxed custom extension と
72
81
  package として参照する場合は version を固定します。
73
82
 
74
83
  ```bash
75
- pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.5.0
84
+ pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.7.0
76
85
  ```
77
86
 
78
87
  ## ブロック概要
package/README.md CHANGED
@@ -34,10 +34,15 @@ Composite unsandboxed extensions can use the same scene operations as the blocks
34
34
  interface AFrameRuntimeCapabilityV2 {
35
35
  readonly version: 2;
36
36
  requireVersion(version: number): AFrameRuntimeCapabilityV2;
37
+ createScene(layer: string, mode: string): Promise<void>;
38
+ createNode(type: string, id: string, parent: string): void;
39
+ addClass(className: string, selector: string): void;
37
40
  loadTemplate(id: string, source: string): void;
38
41
  createFromTemplate(template: string, instance: string, parent: string): void;
39
42
  setPosition(selector: string, x: number, y: number, z: number): void;
40
43
  setRotation(selector: string, x: number, y: number, z: number): void;
44
+ setAttribute(selector: string, name: string, value: string): void;
45
+ setData(selector: string, key: string, value: string): void;
41
46
  emitEvent(type: string, selector: string, data: string): void;
42
47
  deleteSelector(selector: string): void;
43
48
  countSelector(selector: string): number;
@@ -50,6 +55,10 @@ interface AFrameRuntimeCapabilityV2 {
50
55
  }
51
56
  ```
52
57
 
58
+ `createScene`, `createNode`, and `addClass` build the scene itself, so an extension that turns a scene description into A-Frame nodes can do it through this port rather than only through blocks. `createFromTemplate` is not a substitute: it prefixes child ids with the instance id and writes `template` and `instance` data keys, so a caller that already owns its ids would not get the ids it asked for.
59
+
60
+ `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.
61
+
53
62
  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.
54
63
 
55
64
  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.
@@ -72,7 +81,7 @@ Load `dist/turbowarp-aframe.js` as an unsandboxed custom extension in TurboWarp.
72
81
  For package-based reuse, pin the version:
73
82
 
74
83
  ```bash
75
- pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.5.0
84
+ pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.7.0
76
85
  ```
77
86
 
78
87
  ## Block reference
@@ -685,6 +685,18 @@
685
685
  var runtimeCapabilityKey = "turbowarpAFrameCapability";
686
686
  function createRuntimeCapability(scene, assertActive) {
687
687
  const scenePort = {
688
+ async createScene(layer, mode) {
689
+ assertActive();
690
+ await scene.createScene(layer, mode);
691
+ },
692
+ createNode(type, id, parent) {
693
+ assertActive();
694
+ scene.createNode(type, id, parent);
695
+ },
696
+ addClass(className, selector) {
697
+ assertActive();
698
+ scene.addClass(className, selector);
699
+ },
688
700
  loadTemplate(id, source) {
689
701
  assertActive();
690
702
  scene.loadTemplate(id, source);
@@ -701,6 +713,14 @@
701
713
  assertActive();
702
714
  scene.setRotation(selector, x, y, z);
703
715
  },
716
+ setAttribute(selector, name, value) {
717
+ assertActive();
718
+ scene.setAttribute(selector, name, value);
719
+ },
720
+ setData(selector, key, value) {
721
+ assertActive();
722
+ scene.setData(selector, key, value);
723
+ },
704
724
  emitEvent(type, selector, data) {
705
725
  assertActive();
706
726
  scene.emitEvent(type, selector, data);
@@ -6970,6 +6990,18 @@
6970
6990
  };
6971
6991
  //#endregion
6972
6992
  //#region src/extension.ts
6993
+ var SCENE_LAYERS = Object.freeze(["above-stage", "below-stage"]);
6994
+ var DEFAULT_SCENE_LAYER = "above-stage";
6995
+ /**
6996
+ * Stacking positions for the two hosts that can share the stage.
6997
+ *
6998
+ * `turbowarp-ar` puts its camera background one step below the `above-stage` value here, so a 3D
6999
+ * scene renders over the camera image. Keep the two in step when either extension changes.
7000
+ */
7001
+ var SCENE_HOST_Z_INDEX = {
7002
+ "above-stage": "10",
7003
+ "below-stage": "0"
7004
+ };
6973
7005
  var blockDefinitions = block_definitions_default.blocks;
6974
7006
  var ROOT_ID = "scene";
6975
7007
  var DOM_EVENT_TYPES = [
@@ -6997,7 +7029,7 @@
6997
7029
  this.domEventTypes = new Set(DOM_EVENT_TYPES);
6998
7030
  this.runtimeId = `twaframe-${Math.random().toString(36).slice(2)}`;
6999
7031
  this.sceneOptions = {
7000
- layer: "above-stage",
7032
+ layer: DEFAULT_SCENE_LAYER,
7001
7033
  mode: "3d"
7002
7034
  };
7003
7035
  this.rootElement = null;
@@ -7007,6 +7039,19 @@
7007
7039
  this.vrms = new VrmAvatars(() => this.getThree());
7008
7040
  this.resetGraph();
7009
7041
  this.runtimeCapability = createRuntimeCapability({
7042
+ createScene: (layer, mode) => this.createScene({
7043
+ LAYER: layer,
7044
+ MODE: mode
7045
+ }),
7046
+ createNode: (type, id, parent) => this.createNode({
7047
+ TYPE: type,
7048
+ ID: id,
7049
+ PARENT: parent
7050
+ }),
7051
+ addClass: (className, selector) => this.addClass({
7052
+ CLASS: className,
7053
+ SELECTOR: selector
7054
+ }),
7010
7055
  loadTemplate: (id, source) => this.loadTemplate({
7011
7056
  ID: id,
7012
7057
  SOURCE: source
@@ -7028,6 +7073,16 @@
7028
7073
  Y: y,
7029
7074
  Z: z
7030
7075
  }),
7076
+ setAttribute: (selector, name, value) => this.setAttribute({
7077
+ SELECTOR: selector,
7078
+ NAME: name,
7079
+ VALUE: value
7080
+ }),
7081
+ setData: (selector, key, value) => this.setData({
7082
+ SELECTOR: selector,
7083
+ KEY: key,
7084
+ VALUE: value
7085
+ }),
7031
7086
  emitEvent: (type, selector, data) => this.emitEvent({
7032
7087
  TYPE: type,
7033
7088
  SELECTOR: selector,
@@ -7070,7 +7125,7 @@
7070
7125
  async createScene(args) {
7071
7126
  await ensureAFrame();
7072
7127
  this.sceneOptions = {
7073
- layer: Scratch.Cast.toString(args.LAYER) || "above-stage",
7128
+ layer: this.normalizeLayer(Scratch.Cast.toString(args.LAYER)),
7074
7129
  mode: Scratch.Cast.toString(args.MODE) || "3d"
7075
7130
  };
7076
7131
  this.resetGraph();
@@ -7498,7 +7553,7 @@
7498
7553
  host.style.position = "absolute";
7499
7554
  host.style.inset = "0";
7500
7555
  host.style.pointerEvents = "auto";
7501
- host.style.zIndex = this.sceneOptions.layer === "below-stage" ? "0" : "10";
7556
+ host.style.zIndex = SCENE_HOST_Z_INDEX[this.sceneOptions.layer];
7502
7557
  const scene = document.createElement("a-scene");
7503
7558
  host.append(scene);
7504
7559
  this.findStageMount().append(host);
@@ -7872,6 +7927,14 @@
7872
7927
  normalizeId(value) {
7873
7928
  return this.normalizeToken(value) || "node";
7874
7929
  }
7930
+ /**
7931
+ * Blocks take whatever string a project hands them, so an unknown layer falls back to the default
7932
+ * rather than stopping the script, and never reaches the host as a value it cannot honor.
7933
+ */
7934
+ normalizeLayer(value) {
7935
+ const layer = value.trim();
7936
+ return SCENE_LAYERS.includes(layer) ? layer : DEFAULT_SCENE_LAYER;
7937
+ }
7875
7938
  normalizeToken(value) {
7876
7939
  return value.trim().replace(/[^a-zA-Z0-9_-]/g, "-");
7877
7940
  }
@@ -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`、`setVrmExpression`、`vrmExpressionNames`を加えた、freezeされた狭い型付きportです。scene操作だけを持っていたversion 1は、version 2と並べて残さず0.4.0で撤去しました。正しく保つ契約を1つにするためです。表情の操作は、methodを加えるだけでconsumerは使うmethodの有無を確かめるため、versionを変えずにversion 2へ加えました。
27
+ 機能拡張は`Scratch.vm.runtime.turbowarpAFrameCapability`を公開します。version 2は、`createScene`、`createNode`、`addClass`、`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を通るようにするために存在します。`createScene`、`createNode`、`addClass`はその組を完成させ、記述からsceneを丸ごと構築する機能拡張がblockだけに頼らずportを通せるようにします。`createFromTemplate`では代用できません。子のidにinstance idを前置し、`template`と`instance`のdata keyを書き込むためで、自分でidを決める呼び出し側はそれを望みません。
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
- 初期 layer は文字列です。Book/API 側で語彙を調整しても block shape を変えずに済むようにしています。
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
- 初期 mode も文字列です。
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 合成を想定します。
@@ -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`, `vrmStatus`, `setVrmExpression`, and `vrmExpressionNames`. The expression operations were added to version 2 without changing its version, because they only add methods and a consumer checks for the methods it uses. 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.
27
+ The extension publishes `Scratch.vm.runtime.turbowarpAFrameCapability`. Version 2 is a narrow typed, frozen port containing `createScene`, `createNode`, `addClass`, `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. `createScene`, `createNode`, and `addClass` complete the set, so an extension that builds a whole scene from a description can do it through the port rather than only through blocks; `createFromTemplate` cannot stand in for them, because it prefixes child ids with the instance id and writes `template` and `instance` data keys, which a caller that owns its own ids does not want. 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 initial layer values are strings so the book/API can refine the vocabulary without changing the block shape:
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 initial mode values are also open strings:
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-aframe",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "A TurboWarp extension for declarative A-Frame scene graphs.",
5
5
  "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
6
6
  "license": "MPL-2.0",
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: string;
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: 'above-stage', mode: '3d'};
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;
@@ -151,6 +171,9 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
151
171
  this.resetGraph();
152
172
  this.runtimeCapability = createRuntimeCapability(
153
173
  {
174
+ createScene: (layer, mode) => this.createScene({LAYER: layer, MODE: mode}),
175
+ createNode: (type, id, parent) => this.createNode({TYPE: type, ID: id, PARENT: parent}),
176
+ addClass: (className, selector) => this.addClass({CLASS: className, SELECTOR: selector}),
154
177
  loadTemplate: (id, source) => this.loadTemplate({ID: id, SOURCE: source}),
155
178
  createFromTemplate: (template, instance, parent) =>
156
179
  this.createFromTemplate({TEMPLATE: template, INSTANCE: instance, PARENT: parent}),
@@ -158,6 +181,10 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
158
181
  this.setPosition({SELECTOR: selector, X: x, Y: y, Z: z}),
159
182
  setRotation: (selector, x, y, z) =>
160
183
  this.setRotation({SELECTOR: selector, X: x, Y: y, Z: z}),
184
+ setAttribute: (selector, name, value) =>
185
+ this.setAttribute({SELECTOR: selector, NAME: name, VALUE: value}),
186
+ setData: (selector, key, value) =>
187
+ this.setData({SELECTOR: selector, KEY: key, VALUE: value}),
161
188
  emitEvent: (type, selector, data) =>
162
189
  this.emitEvent({TYPE: type, SELECTOR: selector, DATA: data}),
163
190
  deleteSelector: (selector) => this.deleteSelector({SELECTOR: selector}),
@@ -190,7 +217,7 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
190
217
  public async createScene(args: {LAYER: unknown; MODE: unknown}): Promise<void> {
191
218
  await ensureAFrame();
192
219
  this.sceneOptions = {
193
- layer: Scratch.Cast.toString(args.LAYER) || 'above-stage',
220
+ layer: this.normalizeLayer(Scratch.Cast.toString(args.LAYER)),
194
221
  mode: Scratch.Cast.toString(args.MODE) || '3d'
195
222
  };
196
223
  this.resetGraph();
@@ -761,7 +788,7 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
761
788
  host.style.position = 'absolute';
762
789
  host.style.inset = '0';
763
790
  host.style.pointerEvents = 'auto';
764
- host.style.zIndex = this.sceneOptions.layer === 'below-stage' ? '0' : '10';
791
+ host.style.zIndex = SCENE_HOST_Z_INDEX[this.sceneOptions.layer];
765
792
 
766
793
  const scene = document.createElement('a-scene');
767
794
  host.append(scene);
@@ -1297,6 +1324,15 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
1297
1324
  return this.normalizeToken(value) || 'node';
1298
1325
  }
1299
1326
 
1327
+ /**
1328
+ * Blocks take whatever string a project hands them, so an unknown layer falls back to the default
1329
+ * rather than stopping the script, and never reaches the host as a value it cannot honor.
1330
+ */
1331
+ private normalizeLayer(value: string): SceneLayer {
1332
+ const layer = value.trim() as SceneLayer;
1333
+ return SCENE_LAYERS.includes(layer) ? layer : DEFAULT_SCENE_LAYER;
1334
+ }
1335
+
1300
1336
  private normalizeToken(value: string): string {
1301
1337
  return value.trim().replace(/[^a-zA-Z0-9_-]/g, '-');
1302
1338
  }
@@ -3,10 +3,19 @@ export const runtimeCapabilityVersion = 2 as const;
3
3
  export const runtimeCapabilityKey = 'turbowarpAFrameCapability';
4
4
 
5
5
  export interface AFrameScenePort {
6
+ /** Creates the scene host, loading A-Frame first when the page does not have it. */
7
+ createScene(layer: string, mode: string): Promise<void>;
8
+ /** Adds one node under the node the parent selector matches. */
9
+ createNode(type: string, id: string, parent: string): void;
10
+ addClass(className: string, selector: string): void;
6
11
  loadTemplate(id: string, source: string): void;
7
12
  createFromTemplate(template: string, instance: string, parent: string): void;
8
13
  setPosition(selector: string, x: number, y: number, z: number): void;
9
14
  setRotation(selector: string, x: number, y: number, z: number): void;
15
+ /** Sets an A-Frame component attribute, such as `visible`, on every matching node. */
16
+ setAttribute(selector: string, name: string, value: string): void;
17
+ /** Sets a `data-<key>` attribute on every matching node. */
18
+ setData(selector: string, key: string, value: string): void;
10
19
  emitEvent(type: string, selector: string, data: string): void;
11
20
  deleteSelector(selector: string): void;
12
21
  countSelector(selector: string): number;
@@ -41,6 +50,18 @@ export function createRuntimeCapability(
41
50
  assertActive: () => void
42
51
  ): AFrameRuntimeCapabilityV2 {
43
52
  const scenePort: AFrameScenePort = {
53
+ async createScene(layer, mode) {
54
+ assertActive();
55
+ await scene.createScene(layer, mode);
56
+ },
57
+ createNode(type, id, parent) {
58
+ assertActive();
59
+ scene.createNode(type, id, parent);
60
+ },
61
+ addClass(className, selector) {
62
+ assertActive();
63
+ scene.addClass(className, selector);
64
+ },
44
65
  loadTemplate(id, source) {
45
66
  assertActive();
46
67
  scene.loadTemplate(id, source);
@@ -57,6 +78,14 @@ export function createRuntimeCapability(
57
78
  assertActive();
58
79
  scene.setRotation(selector, x, y, z);
59
80
  },
81
+ setAttribute(selector, name, value) {
82
+ assertActive();
83
+ scene.setAttribute(selector, name, value);
84
+ },
85
+ setData(selector, key, value) {
86
+ assertActive();
87
+ scene.setData(selector, key, value);
88
+ },
60
89
  emitEvent(type, selector, data) {
61
90
  assertActive();
62
91
  scene.emitEvent(type, selector, data);