oomi-ai 0.2.50 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +203 -507
  2. package/agent_instructions.md +244 -253
  3. package/bin/oomi-ai.js +4026 -5795
  4. package/bin/sessionBridgeState.js +78 -78
  5. package/lib/openclawPaths.js +70 -71
  6. package/lib/openclawProfile.js +216 -216
  7. package/lib/personaApiClient.js +133 -303
  8. package/lib/spokenMetadata.js +137 -137
  9. package/openclaw.extension.js +341 -341
  10. package/openclaw.plugin.json +17 -17
  11. package/package.json +59 -59
  12. package/persona-app/README.md +27 -0
  13. package/persona-app/registry/v1.json +63 -0
  14. package/persona-app/schema/persona-app.v1.schema.json +90 -0
  15. package/skills/oomi/SKILL.md +165 -182
  16. package/skills/oomi/agent_instructions.md +99 -80
  17. package/lib/channelPluginClient.js +0 -119
  18. package/lib/openclawDevGateway.js +0 -384
  19. package/lib/personaJobExecutor.js +0 -139
  20. package/lib/personaJobPoller.js +0 -112
  21. package/lib/personaPortAllocator.js +0 -36
  22. package/lib/personaRuntimeManager.js +0 -496
  23. package/lib/personaRuntimeProcess.js +0 -924
  24. package/lib/personaRuntimeRegistry.js +0 -67
  25. package/lib/personaRuntimeSupervisor.js +0 -330
  26. package/lib/scaffold.js +0 -108
  27. package/lib/template.js +0 -45
  28. package/skills/oomi/config.json +0 -3
  29. package/skills/oomi/scripts/get_avatar_capabilities.py +0 -40
  30. package/skills/oomi/scripts/get_data.py +0 -49
  31. package/skills/oomi/scripts/install_agent_instructions.py +0 -78
  32. package/skills/oomi/scripts/send_goal.py +0 -53
  33. package/skills/oomi/scripts/sync.py +0 -46
  34. package/skills/oomi/setup.py +0 -41
  35. package/templates/persona-app/.env.example +0 -8
  36. package/templates/persona-app/README.md +0 -58
  37. package/templates/persona-app/eslint.config.js +0 -28
  38. package/templates/persona-app/index.html +0 -18
  39. package/templates/persona-app/oomi.runtime.json +0 -13
  40. package/templates/persona-app/package.json +0 -44
  41. package/templates/persona-app/persona/brief.md +0 -14
  42. package/templates/persona-app/persona.json +0 -14
  43. package/templates/persona-app/public/manifest.webmanifest +0 -8
  44. package/templates/persona-app/public/oomi.health.json +0 -6
  45. package/templates/persona-app/src/App.css +0 -379
  46. package/templates/persona-app/src/App.tsx +0 -17
  47. package/templates/persona-app/src/index.css +0 -53
  48. package/templates/persona-app/src/main.tsx +0 -23
  49. package/templates/persona-app/src/pages/HomePage.tsx +0 -127
  50. package/templates/persona-app/src/pages/ScenePage.tsx +0 -158
  51. package/templates/persona-app/src/persona/config.ts +0 -6
  52. package/templates/persona-app/src/persona/notes.ts +0 -10
  53. package/templates/persona-app/src/spatial.ts +0 -82
  54. package/templates/persona-app/src/vite-env.d.ts +0 -3
  55. package/templates/persona-app/template.json +0 -13
  56. package/templates/persona-app/tsconfig.app.json +0 -23
  57. package/templates/persona-app/tsconfig.json +0 -7
  58. package/templates/persona-app/tsconfig.node.json +0 -21
  59. package/templates/persona-app/vendor/webspatial/FORK.md +0 -6
  60. package/templates/persona-app/vendor/webspatial/core-sdk/LICENSE +0 -21
  61. package/templates/persona-app/vendor/webspatial/core-sdk/dist/iife/index.d.ts +0 -906
  62. package/templates/persona-app/vendor/webspatial/core-sdk/dist/iife/index.global.js +0 -75
  63. package/templates/persona-app/vendor/webspatial/core-sdk/dist/iife/index.global.js.map +0 -1
  64. package/templates/persona-app/vendor/webspatial/core-sdk/dist/index.d.ts +0 -906
  65. package/templates/persona-app/vendor/webspatial/core-sdk/dist/index.js +0 -3131
  66. package/templates/persona-app/vendor/webspatial/core-sdk/dist/index.js.map +0 -1
  67. package/templates/persona-app/vendor/webspatial/core-sdk/package.json +0 -45
  68. package/templates/persona-app/vendor/webspatial/react-sdk/LICENSE +0 -21
  69. package/templates/persona-app/vendor/webspatial/react-sdk/dist/default/index.d.ts +0 -365
  70. package/templates/persona-app/vendor/webspatial/react-sdk/dist/default/index.js +0 -4167
  71. package/templates/persona-app/vendor/webspatial/react-sdk/dist/default/index.js.map +0 -1
  72. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.d.ts +0 -82
  73. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.js +0 -66
  74. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.js.map +0 -1
  75. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.web.d.ts +0 -2
  76. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.web.js +0 -18
  77. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.web.js.map +0 -1
  78. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.d.ts +0 -5
  79. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.js +0 -66
  80. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.js.map +0 -1
  81. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.web.d.ts +0 -1
  82. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.web.js +0 -18
  83. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.web.js.map +0 -1
  84. package/templates/persona-app/vendor/webspatial/react-sdk/dist/web/index.d.ts +0 -365
  85. package/templates/persona-app/vendor/webspatial/react-sdk/dist/web/index.js +0 -4207
  86. package/templates/persona-app/vendor/webspatial/react-sdk/dist/web/index.js.map +0 -1
  87. package/templates/persona-app/vendor/webspatial/react-sdk/package.json +0 -94
  88. package/templates/persona-app/vite.config.ts +0 -31
@@ -1,3131 +0,0 @@
1
-
2
- (function(){
3
- if(typeof window === 'undefined') return;
4
- if(!window.__webspatialsdk__) window.__webspatialsdk__ = {}
5
- window.__webspatialsdk__['core-sdk-version'] = "1.2.1"
6
- })()
7
-
8
- var __defProp = Object.defineProperty;
9
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
- var __getOwnPropNames = Object.getOwnPropertyNames;
11
- var __hasOwnProp = Object.prototype.hasOwnProperty;
12
- var __esm = (fn, res) => function __init() {
13
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
14
- };
15
- var __export = (target, all) => {
16
- for (var name in all)
17
- __defProp(target, name, { get: all[name], enumerable: true });
18
- };
19
- var __copyProps = (to, from, except, desc) => {
20
- if (from && typeof from === "object" || typeof from === "function") {
21
- for (let key of __getOwnPropNames(from))
22
- if (!__hasOwnProp.call(to, key) && key !== except)
23
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
- }
25
- return to;
26
- };
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
-
29
- // src/platform-adapter/CommandResultUtils.ts
30
- function CommandResultSuccess(data) {
31
- return {
32
- success: true,
33
- data,
34
- errorCode: "",
35
- errorMessage: ""
36
- };
37
- }
38
- function CommandResultFailure(errorCode, errorMessage = "") {
39
- return {
40
- success: false,
41
- data: void 0,
42
- errorCode,
43
- errorMessage
44
- };
45
- }
46
- var init_CommandResultUtils = __esm({
47
- "src/platform-adapter/CommandResultUtils.ts"() {
48
- "use strict";
49
- }
50
- });
51
-
52
- // src/platform-adapter/puppeteer/PuppeteerPlatform.ts
53
- var PuppeteerPlatform_exports = {};
54
- __export(PuppeteerPlatform_exports, {
55
- PuppeteerPlatform: () => PuppeteerPlatform
56
- });
57
- var PuppeteerPlatform;
58
- var init_PuppeteerPlatform = __esm({
59
- "src/platform-adapter/puppeteer/PuppeteerPlatform.ts"() {
60
- "use strict";
61
- init_CommandResultUtils();
62
- console.log("PuppeteerPlatform");
63
- PuppeteerPlatform = class {
64
- // 存储iframe实例
65
- iframeRegistry = /* @__PURE__ */ new Map();
66
- constructor() {
67
- }
68
- callJSB(cmd, msg) {
69
- return new Promise((resolve) => {
70
- try {
71
- if (window.__handleJSBMessage) {
72
- try {
73
- console.log(` core-sdk Puppeteer Platform: callJSB: ${cmd}::${msg}`);
74
- const result = window.__handleJSBMessage(`${cmd}::${msg}`);
75
- console.log(
76
- ` core-sdk Puppeteer Platform callJSB result: ${result}`
77
- );
78
- resolve(CommandResultSuccess(result));
79
- } catch (err) {
80
- resolve(CommandResultFailure("500", "JSB execution error"));
81
- }
82
- } else {
83
- resolve(CommandResultSuccess("ok"));
84
- }
85
- } catch (error) {
86
- console.error(
87
- `PuppeteerPlatform cmd Error: ${cmd}, msg: ${msg} error: ${error}`
88
- );
89
- resolve(CommandResultFailure("500", "Internal error"));
90
- }
91
- });
92
- }
93
- /**
94
- * 同步创建Spatialized2DElement到Puppeteer Runner
95
- */
96
- createSpatializedElementSync(spatialId, webspatialUrl) {
97
- try {
98
- console.log(
99
- `[Puppeteer Platform] Creating spatialized element sync with id: ${spatialId}, url: ${webspatialUrl}`
100
- );
101
- const win = window;
102
- if (win.__handleJSBMessage) {
103
- const createCommand = {
104
- id: spatialId,
105
- url: webspatialUrl
106
- };
107
- win.__handleJSBMessage(
108
- `CreateSpatialized2DElement::${JSON.stringify(createCommand)}`
109
- );
110
- }
111
- } catch (error) {
112
- console.error("Error creating spatialized element sync:", error);
113
- }
114
- }
115
- callWebSpatialProtocol(command, query, target, features) {
116
- console.log(
117
- `PuppeteerPlatform: Calling webspatial protocol: webspatial://${command}${query ? `?${query}` : ""}`
118
- );
119
- return new Promise((resolve) => {
120
- try {
121
- const webspatialUrl = `webspatial://${command}${query ? `?${query}` : ""}`;
122
- const { spatialId, iframe, windowProxy } = this.createIframeWindow(
123
- webspatialUrl,
124
- target,
125
- features
126
- );
127
- if (command === "createSpatialized2DElement") {
128
- this.createSpatializedElementSync(spatialId, webspatialUrl);
129
- }
130
- console.log(
131
- `[Puppeteer Platform] iframe created with spatialId: ${spatialId}`
132
- );
133
- this.iframeRegistry.set(spatialId, iframe);
134
- resolve(CommandResultSuccess({ windowProxy, id: spatialId }));
135
- } catch (error) {
136
- console.error("Error calling webspatial protocol:", error);
137
- resolve(
138
- CommandResultFailure("500", "Failed to call webspatial protocol")
139
- );
140
- }
141
- });
142
- }
143
- callWebSpatialProtocolSync(command, query, target, features) {
144
- try {
145
- const webspatialUrl = `webspatial://${command}${query ? `?${query}` : ""}`;
146
- console.log(`Calling webspatial protocol sync: ${webspatialUrl}`);
147
- const { spatialId, iframe, windowProxy } = this.createIframeWindow(
148
- webspatialUrl,
149
- target,
150
- features
151
- );
152
- if (command === "createSpatialized2DElement") {
153
- this.createSpatializedElementSync(spatialId, webspatialUrl);
154
- }
155
- this.iframeRegistry.set(spatialId, iframe);
156
- return CommandResultSuccess({ windowProxy, id: spatialId });
157
- } catch (error) {
158
- console.error("Error calling webspatial protocol sync:", error);
159
- return CommandResultFailure(
160
- "500",
161
- "Failed to call webspatial protocol sync"
162
- );
163
- }
164
- }
165
- /**
166
- * 创建基于iframe的窗口
167
- */
168
- createIframeWindow(url, target, features) {
169
- const iframe = document.createElement("iframe");
170
- iframe.style.border = "none";
171
- iframe.style.display = "none";
172
- iframe.style.width = "100%";
173
- iframe.style.height = "100%";
174
- const spatialId = this.generateUUID();
175
- iframe.spatialId = spatialId;
176
- iframe.id = `spatial-iframe-${spatialId}`;
177
- const featuresObj = this.parseFeatures(features || "");
178
- if (featuresObj.width) {
179
- iframe.style.width = featuresObj.width;
180
- }
181
- if (featuresObj.height) {
182
- iframe.style.height = featuresObj.height;
183
- }
184
- if (featuresObj.left) {
185
- iframe.style.left = featuresObj.left;
186
- iframe.style.position = "absolute";
187
- }
188
- if (featuresObj.top) {
189
- iframe.style.top = featuresObj.top;
190
- iframe.style.position = "absolute";
191
- }
192
- document.body.appendChild(iframe);
193
- const windowProxy = this.createEnhancedWindowProxy(iframe, url, spatialId);
194
- iframe.src = "about:blank";
195
- console.log(
196
- `PuppeteerPlatform created iframe window with spatialId: ${spatialId}, URL: ${url}`
197
- );
198
- this.initializeIframeContent(iframe, url, spatialId);
199
- return { spatialId, iframe, windowProxy };
200
- }
201
- /**
202
- * 创建增强的windowProxy对象
203
- */
204
- createEnhancedWindowProxy(iframe, url, spatialId) {
205
- return {
206
- // 基本属性
207
- location: {
208
- href: url,
209
- toString: () => url,
210
- reload: () => {
211
- if (iframe.contentWindow) {
212
- iframe.contentWindow.location.reload();
213
- }
214
- }
215
- },
216
- navigator: {
217
- userAgent: `Mozilla/5.0 (WebKit) SpatialId/${spatialId}`
218
- },
219
- // 方法
220
- close: () => {
221
- console.log(`Closing iframe with spatialId: ${spatialId}`);
222
- iframe.remove();
223
- this.iframeRegistry.delete(spatialId);
224
- },
225
- // 文档访问
226
- document: iframe.contentDocument || {},
227
- contentWindow: iframe.contentWindow || {},
228
- // 添加消息通信方法
229
- postMessage: (message, targetOrigin) => {
230
- if (iframe.contentWindow) {
231
- iframe.contentWindow.postMessage(message, targetOrigin || "*");
232
- }
233
- },
234
- // 添加事件监听方法
235
- addEventListener: (type, listener) => {
236
- if (iframe.contentWindow) {
237
- iframe.contentWindow.addEventListener(type, listener);
238
- }
239
- },
240
- removeEventListener: (type, listener) => {
241
- if (iframe.contentWindow) {
242
- iframe.contentWindow.removeEventListener(type, listener);
243
- }
244
- },
245
- // 执行JavaScript
246
- executeScript: (code) => {
247
- if (iframe.contentWindow) {
248
- try {
249
- const win = iframe.contentWindow;
250
- return win.eval(code);
251
- } catch (error) {
252
- console.error(
253
- `Error executing script in iframe ${spatialId}:`,
254
- error
255
- );
256
- return null;
257
- }
258
- }
259
- return null;
260
- },
261
- // 获取iframe引用
262
- getIframe: () => iframe,
263
- // 获取spatialId
264
- getSpatialId: () => spatialId
265
- };
266
- }
267
- /**
268
- * 初始化iframe内容
269
- */
270
- initializeIframeContent(iframe, url, spatialId) {
271
- try {
272
- iframe.onload = () => {
273
- try {
274
- const iframeContent = `
275
- // \u6CE8\u5165\u901A\u4FE1\u811A\u672C
276
- window.webSpatialId = '${spatialId}';
277
- window.SpatialId = '${spatialId}';
278
-
279
- // \u91CD\u5199window.open\u4EE5\u652F\u6301webspatial\u534F\u8BAE
280
- const originalOpen = window.open;
281
- window.open = function(url, target, features) {
282
- if (url && url.startsWith('webspatial://')) {
283
- // \u901A\u8FC7windowProxy\u5904\u7406webspatial\u534F\u8BAE
284
- const windowProxy = new Proxy({}, {
285
- get: function(target, prop) {
286
- if (prop === 'toString') {
287
- return function() { return url; };
288
- }
289
- return undefined;
290
- }
291
- });
292
- return windowProxy;
293
- }
294
- return originalOpen.call(window, url, target, features);
295
- };
296
-
297
- // \u8BBE\u7F6Enavigator.userAgent\u4EE5\u8BC6\u522Bwebspatial\u73AF\u5883
298
- Object.defineProperty(navigator, 'userAgent', {
299
- value: 'WebSpatial/1.0 ' + navigator.userAgent,
300
- configurable: true
301
- });
302
-
303
- // \u53D1\u9001\u52A0\u8F7D\u5B8C\u6210\u6D88\u606F
304
- window.parent.postMessage({
305
- type: 'iframe_loaded',
306
- spatialId: '${spatialId}',
307
- url: '${url}'
308
- }, '${window.location.origin}');
309
-
310
- // \u8BBE\u7F6E\u6D88\u606F\u5904\u7406\u5668
311
- window.addEventListener('message', (event) => {
312
- if (event.origin !== window.parent.location.origin) return;
313
-
314
- const data = event.data;
315
- if (data && data.type === 'webspatial_command') {
316
- // \u5904\u7406\u6765\u81EA\u7236\u7A97\u53E3\u7684\u547D\u4EE4
317
- console.log('Received command in iframe from parent:', data.command);
318
- // \u8FD9\u91CC\u53EF\u4EE5\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u903B\u8F91
319
- }
320
- });
321
- `;
322
- const doc = iframe.contentDocument;
323
- if (doc) {
324
- doc.open();
325
- doc.write(`
326
- <!DOCTYPE html>
327
- <html>
328
- <head>
329
- <title>Spatial Iframe - ${spatialId}</title>
330
- <meta charset="UTF-8">
331
- <style>
332
- body {
333
- margin: 0;
334
- padding: 0;
335
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
336
- }
337
- </style>
338
- </head>
339
- <body>
340
- <script>${iframeContent}</script>
341
- </body>
342
- </html>
343
- `);
344
- doc.close();
345
- }
346
- } catch (error) {
347
- console.error("Error initializing iframe content:", error);
348
- }
349
- };
350
- } catch (error) {
351
- console.error("Error setting up iframe:", error);
352
- }
353
- }
354
- /**
355
- * 解析features字符串为对象
356
- */
357
- parseFeatures(features) {
358
- const result = {};
359
- const pairs = features.split(",");
360
- pairs.forEach((pair) => {
361
- const [key, value] = pair.split("=").map((s) => s.trim());
362
- if (key && value) {
363
- result[key] = value;
364
- }
365
- });
366
- return result;
367
- }
368
- /**
369
- * 发送消息到指定spatialId的iframe
370
- */
371
- sendMessageToIframe(spatialId, message) {
372
- const iframe = this.iframeRegistry.get(spatialId);
373
- if (iframe && iframe.contentWindow) {
374
- iframe.contentWindow.postMessage(message, window.location.origin);
375
- return true;
376
- }
377
- return false;
378
- }
379
- /**
380
- * 获取所有活跃的iframe
381
- */
382
- getAllActiveIframes() {
383
- const result = [];
384
- this.iframeRegistry.forEach((iframe, spatialId) => {
385
- result.push({ spatialId, iframe });
386
- });
387
- return result;
388
- }
389
- /**
390
- * 清理资源
391
- */
392
- dispose() {
393
- this.iframeRegistry.forEach((iframe, spatialId) => {
394
- console.log(`Disposing iframe with spatialId: ${spatialId}`);
395
- iframe.remove();
396
- });
397
- this.iframeRegistry.clear();
398
- }
399
- // 生成UUID函数
400
- generateUUID() {
401
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
402
- /[xy]/g,
403
- function(c) {
404
- const r = Math.random() * 16 | 0;
405
- const v = c === "x" ? r : r & 3 | 8;
406
- return v.toString(16).toUpperCase();
407
- }
408
- );
409
- }
410
- };
411
- }
412
- });
413
-
414
- // src/SpatialWebEvent.ts
415
- var SpatialWebEvent;
416
- var init_SpatialWebEvent = __esm({
417
- "src/SpatialWebEvent.ts"() {
418
- "use strict";
419
- SpatialWebEvent = class _SpatialWebEvent {
420
- static eventReceiver = {};
421
- static init() {
422
- window.__SpatialWebEvent = ({ id, data }) => {
423
- _SpatialWebEvent.eventReceiver[id]?.(data);
424
- };
425
- }
426
- static addEventReceiver(id, callback) {
427
- _SpatialWebEvent.eventReceiver[id] = callback;
428
- }
429
- static removeEventReceiver(id) {
430
- delete _SpatialWebEvent.eventReceiver[id];
431
- }
432
- };
433
- }
434
- });
435
-
436
- // src/platform-adapter/xr/XRPlatform.ts
437
- var XRPlatform_exports = {};
438
- __export(XRPlatform_exports, {
439
- XRPlatform: () => XRPlatform
440
- });
441
- function nextRequestId() {
442
- requestId = (requestId + 1) % MAX_ID;
443
- return `rId_${requestId}`;
444
- }
445
- var requestId, MAX_ID, XRPlatform;
446
- var init_XRPlatform = __esm({
447
- "src/platform-adapter/xr/XRPlatform.ts"() {
448
- "use strict";
449
- init_CommandResultUtils();
450
- init_SpatialWebEvent();
451
- requestId = 0;
452
- MAX_ID = 1e5;
453
- XRPlatform = class {
454
- async callJSB(cmd, msg) {
455
- return new Promise((resolve, reject) => {
456
- try {
457
- const rId = nextRequestId();
458
- SpatialWebEvent.addEventReceiver(rId, (result) => {
459
- SpatialWebEvent.removeEventReceiver(rId);
460
- if (result.success) {
461
- resolve(CommandResultSuccess(result.data));
462
- } else {
463
- const { code, message } = result.data;
464
- resolve(CommandResultFailure(code, message));
465
- }
466
- });
467
- const ans = window.webspatialBridge.postMessage(rId, cmd, msg);
468
- if (ans !== "") {
469
- SpatialWebEvent.removeEventReceiver(rId);
470
- const result = JSON.parse(ans);
471
- if (result.success) {
472
- resolve(CommandResultSuccess(result.data));
473
- } else {
474
- const { code, message } = result.data;
475
- resolve(CommandResultFailure(code, message));
476
- }
477
- }
478
- } catch (error) {
479
- console.error(`XRPlatform cmd: ${cmd}, msg: ${msg} error: ${error}`);
480
- const { code, message } = error;
481
- resolve(CommandResultFailure(code, message));
482
- }
483
- });
484
- }
485
- async callWebSpatialProtocol(command, query, target, features) {
486
- return new Promise((resolve, reject) => {
487
- const createdId = nextRequestId();
488
- try {
489
- let windowProxy = null;
490
- SpatialWebEvent.addEventReceiver(
491
- createdId,
492
- (result) => {
493
- console.log("createdId", createdId, result.spatialId);
494
- resolve(
495
- CommandResultSuccess({
496
- windowProxy,
497
- id: result.spatialId
498
- })
499
- );
500
- SpatialWebEvent.removeEventReceiver(createdId);
501
- }
502
- );
503
- windowProxy = this.openWindow(
504
- command,
505
- query,
506
- target,
507
- features
508
- ).windowProxy;
509
- windowProxy?.open(`about:blank?rid=${createdId}`, "_self");
510
- } catch (error) {
511
- console.error(`open window error: ${error}`);
512
- const { code, message } = error;
513
- SpatialWebEvent.removeEventReceiver(createdId);
514
- resolve(CommandResultFailure(code, message));
515
- }
516
- });
517
- }
518
- callWebSpatialProtocolSync(command, query, target, features) {
519
- const { spatialId: id = "", windowProxy } = this.openWindow(
520
- command,
521
- query,
522
- target,
523
- features
524
- );
525
- return CommandResultSuccess({ windowProxy, id });
526
- }
527
- openWindow(command, query, target, features) {
528
- const windowProxy = window.open(
529
- `webspatial://${command}?${query || ""}`,
530
- target,
531
- features
532
- );
533
- return { spatialId: "", windowProxy };
534
- }
535
- };
536
- }
537
- });
538
-
539
- // src/platform-adapter/android/AndroidPlatform.ts
540
- var AndroidPlatform_exports = {};
541
- __export(AndroidPlatform_exports, {
542
- AndroidPlatform: () => AndroidPlatform
543
- });
544
- function nextRequestId2() {
545
- requestId2 = (requestId2 + 1) % MAX_ID2;
546
- return `rId_${requestId2}`;
547
- }
548
- function uuid() {
549
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
550
- const r = Math.random() * 16 | 0;
551
- return (c === "x" ? r : r & 3 | 8).toString(16);
552
- });
553
- }
554
- function supportsLiveWindowProxyMode() {
555
- return window.__WebSpatialAndroidConfig?.renderMode === "live-window";
556
- }
557
- function appendQueryParam(query, key, value) {
558
- const prefix = query && query.length > 0 ? `${query}&` : "";
559
- return `${prefix}${key}=${encodeURIComponent(value)}`;
560
- }
561
- function isWindowDocumentReady(windowProxy) {
562
- const childWindow = windowProxy;
563
- const href = childWindow.location?.href;
564
- if (!href || href.startsWith("about:")) {
565
- return false;
566
- }
567
- const document2 = childWindow.document;
568
- if (!document2?.head || !document2.body) {
569
- return false;
570
- }
571
- if (document2.readyState !== "interactive" && document2.readyState !== "complete") {
572
- return false;
573
- }
574
- return childWindow.__WebSpatialChildReady === true;
575
- }
576
- function hasWindowDocumentStructure(windowProxy) {
577
- const href = windowProxy.location?.href;
578
- if (!href || href.startsWith("about:")) {
579
- return false;
580
- }
581
- const document2 = windowProxy.document;
582
- return Boolean(document2?.head && document2.body);
583
- }
584
- async function waitForWindowDocument(windowProxy, timeoutMs = 8e3) {
585
- const startedAt = Date.now();
586
- while (Date.now() - startedAt < timeoutMs) {
587
- try {
588
- if (isWindowDocumentReady(windowProxy)) {
589
- return true;
590
- }
591
- } catch {
592
- }
593
- await new Promise((resolve) => setTimeout(resolve, 16));
594
- }
595
- try {
596
- if (hasWindowDocumentStructure(windowProxy)) {
597
- console.warn(
598
- "[AndroidPlatform] Falling back to partially ready child window after timeout",
599
- windowProxy.location?.href
600
- );
601
- return true;
602
- }
603
- } catch {
604
- }
605
- return false;
606
- }
607
- function createFakeWindowProxy(elementId) {
608
- const createStyleProxy = () => {
609
- const styleObj = {
610
- cssText: "",
611
- backgroundColor: "",
612
- margin: "",
613
- display: "",
614
- minWidth: "",
615
- minHeight: "",
616
- maxWidth: "",
617
- background: "",
618
- visibility: "",
619
- position: "",
620
- top: "",
621
- left: "",
622
- width: "",
623
- height: "",
624
- overflow: "",
625
- transform: "",
626
- opacity: "",
627
- borderRadius: ""
628
- };
629
- return new Proxy(styleObj, {
630
- get(target, prop) {
631
- if (prop === "setProperty") {
632
- return (name, value) => {
633
- target[name] = value;
634
- };
635
- }
636
- if (prop === "getPropertyValue") {
637
- return (name) => target[name] || "";
638
- }
639
- if (prop === "removeProperty") {
640
- return (name) => {
641
- const oldValue = target[name];
642
- delete target[name];
643
- return oldValue || "";
644
- };
645
- }
646
- return target[prop] ?? "";
647
- },
648
- set(target, prop, value) {
649
- target[prop] = value;
650
- return true;
651
- }
652
- });
653
- };
654
- const createFakeElement = (tagName) => ({
655
- tagName: tagName.toUpperCase(),
656
- style: createStyleProxy(),
657
- setAttribute: () => {
658
- },
659
- getAttribute: () => null,
660
- appendChild: () => {
661
- },
662
- removeChild: () => {
663
- },
664
- innerHTML: "",
665
- textContent: "",
666
- className: "",
667
- id: "",
668
- name: "",
669
- content: ""
670
- });
671
- const fakeDocument = {
672
- documentElement: {
673
- style: createStyleProxy(),
674
- className: ""
675
- },
676
- head: {
677
- innerHTML: "",
678
- appendChild: () => {
679
- },
680
- removeChild: () => {
681
- },
682
- children: [],
683
- querySelectorAll: () => []
684
- },
685
- body: {
686
- innerHTML: "",
687
- style: createStyleProxy(),
688
- appendChild: () => {
689
- },
690
- removeChild: () => {
691
- },
692
- className: ""
693
- },
694
- title: "",
695
- onclick: null,
696
- createElement: (tagName) => createFakeElement(tagName),
697
- createTextNode: (text) => ({ textContent: text }),
698
- getElementById: () => null,
699
- querySelector: (selector) => {
700
- if (selector === 'meta[name="viewport"]') {
701
- return null;
702
- }
703
- return null;
704
- },
705
- querySelectorAll: () => [],
706
- write: () => {
707
- },
708
- close: () => {
709
- }
710
- };
711
- const fakeWindow = {
712
- __SpatialId: elementId,
713
- document: fakeDocument,
714
- location: {
715
- href: "about:blank"
716
- },
717
- navigator: {
718
- userAgent: window?.navigator?.userAgent ?? ""
719
- },
720
- addEventListener: () => {
721
- },
722
- removeEventListener: () => {
723
- },
724
- postMessage: () => {
725
- },
726
- close: () => {
727
- },
728
- focus: () => {
729
- },
730
- blur: () => {
731
- },
732
- open: () => fakeWindow,
733
- // For portal rendering (React needs these)
734
- parent: null,
735
- top: null,
736
- closed: false
737
- };
738
- fakeWindow.parent = fakeWindow;
739
- fakeWindow.top = fakeWindow;
740
- return fakeWindow;
741
- }
742
- var requestId2, MAX_ID2, AndroidPlatform;
743
- var init_AndroidPlatform = __esm({
744
- "src/platform-adapter/android/AndroidPlatform.ts"() {
745
- "use strict";
746
- init_CommandResultUtils();
747
- init_SpatialWebEvent();
748
- requestId2 = 0;
749
- MAX_ID2 = 1e5;
750
- AndroidPlatform = class {
751
- async callJSB(cmd, msg) {
752
- return new Promise((resolve, reject) => {
753
- try {
754
- const rId = nextRequestId2();
755
- SpatialWebEvent.addEventReceiver(rId, (result) => {
756
- SpatialWebEvent.removeEventReceiver(rId);
757
- if (result.success) {
758
- resolve(CommandResultSuccess(result.data));
759
- } else {
760
- const { code, message } = result.data;
761
- resolve(CommandResultFailure(code, message));
762
- }
763
- });
764
- const ans = window.webspatialBridge.postMessage(rId, cmd, msg);
765
- if (ans !== "") {
766
- SpatialWebEvent.removeEventReceiver(rId);
767
- const result = JSON.parse(ans);
768
- if (result.success) {
769
- resolve(CommandResultSuccess(result.data));
770
- } else {
771
- const { code, message } = result.data;
772
- resolve(CommandResultFailure(code, message));
773
- }
774
- }
775
- } catch (error) {
776
- console.error(
777
- `AndroidPlatform cmd: ${cmd}, msg: ${msg} error: ${error}`
778
- );
779
- const { code, message } = error;
780
- resolve(CommandResultFailure(code, message));
781
- }
782
- });
783
- }
784
- async callWebSpatialProtocol(command, query, target, features) {
785
- if (command === "createSpatialized2DElement" && supportsLiveWindowProxyMode()) {
786
- const elementId2 = uuid();
787
- const windowProxy2 = window.open(
788
- `webspatial://${command}?${appendQueryParam(query, "id", elementId2)}`,
789
- target,
790
- features
791
- );
792
- if (!windowProxy2) {
793
- return CommandResultFailure(
794
- "WindowOpenFailed",
795
- `Unable to open child window for ${command}`
796
- );
797
- }
798
- try {
799
- ;
800
- windowProxy2.__SpatialId = elementId2;
801
- } catch {
802
- }
803
- const windowReady = await waitForWindowDocument(windowProxy2);
804
- if (!windowReady) {
805
- return CommandResultFailure(
806
- "WindowProxyUnavailable",
807
- `Timed out waiting for ${command} child window to become scriptable`
808
- );
809
- }
810
- return CommandResultSuccess({ windowProxy: windowProxy2, id: elementId2 });
811
- }
812
- const jsbCommand = this.mapProtocolToJSBCommand(command);
813
- if (!jsbCommand) {
814
- console.warn(`[AndroidPlatform] Unknown protocol command: ${command}`);
815
- return CommandResultFailure(
816
- "UnknownCommand",
817
- `Unknown command: ${command}`
818
- );
819
- }
820
- const elementId = uuid();
821
- const params = { id: elementId };
822
- if (query) {
823
- const searchParams = new URLSearchParams(query);
824
- searchParams.forEach((value, key) => {
825
- try {
826
- params[key] = JSON.parse(decodeURIComponent(value));
827
- } catch {
828
- params[key] = decodeURIComponent(value);
829
- }
830
- });
831
- }
832
- const result = await this.callJSB(jsbCommand, JSON.stringify(params));
833
- if (!result.success) {
834
- return result;
835
- }
836
- const nativeId = result.data?.id || elementId;
837
- const windowProxy = createFakeWindowProxy(nativeId);
838
- return CommandResultSuccess({ windowProxy, id: nativeId });
839
- }
840
- /**
841
- * Maps webspatial:// protocol commands to JSB command names.
842
- */
843
- mapProtocolToJSBCommand(command) {
844
- const commandMap = {
845
- createSpatialized2DElement: "CreateSpatialized2DElement",
846
- createSpatializedStatic3DElement: "CreateSpatializedStatic3DElement",
847
- createSpatializedDynamic3DElement: "CreateSpatializedDynamic3DElement",
848
- createSpatialScene: "CreateSpatialScene"
849
- };
850
- return commandMap[command] || null;
851
- }
852
- callWebSpatialProtocolSync(command, query, target, features) {
853
- if (command === "createSpatialized2DElement" && supportsLiveWindowProxyMode()) {
854
- const elementId2 = uuid();
855
- const windowProxy2 = window.open(
856
- `webspatial://${command}?${appendQueryParam(query, "id", elementId2)}`,
857
- target,
858
- features
859
- );
860
- if (!windowProxy2) {
861
- return CommandResultFailure(
862
- "WindowOpenFailed",
863
- `Unable to open child window for ${command}`
864
- );
865
- }
866
- try {
867
- ;
868
- windowProxy2.__SpatialId = elementId2;
869
- } catch {
870
- }
871
- return CommandResultSuccess({ windowProxy: windowProxy2, id: elementId2 });
872
- }
873
- const elementId = uuid();
874
- const windowProxy = createFakeWindowProxy(elementId);
875
- try {
876
- window.open(
877
- `webspatial://${command}?id=${elementId}&${query || ""}`,
878
- target,
879
- features
880
- );
881
- } catch (e) {
882
- console.warn("[AndroidPlatform] window.open failed:", e);
883
- }
884
- return CommandResultSuccess({ windowProxy, id: elementId });
885
- }
886
- };
887
- }
888
- });
889
-
890
- // src/platform-adapter/vision-os/VisionOSPlatform.ts
891
- var VisionOSPlatform_exports = {};
892
- __export(VisionOSPlatform_exports, {
893
- VisionOSPlatform: () => VisionOSPlatform
894
- });
895
- var VisionOSPlatform;
896
- var init_VisionOSPlatform = __esm({
897
- "src/platform-adapter/vision-os/VisionOSPlatform.ts"() {
898
- "use strict";
899
- init_CommandResultUtils();
900
- VisionOSPlatform = class {
901
- async callJSB(cmd, msg) {
902
- try {
903
- const result = await window.webkit.messageHandlers.bridge.postMessage(
904
- `${cmd}::${msg}`
905
- );
906
- return CommandResultSuccess(result);
907
- } catch (error) {
908
- const { code, message } = JSON.parse(error.message);
909
- return CommandResultFailure(code, message);
910
- }
911
- }
912
- callWebSpatialProtocol(command, query, target, features) {
913
- const { spatialId: id, windowProxy } = this.openWindow(
914
- command,
915
- query,
916
- target,
917
- features
918
- );
919
- return Promise.resolve(
920
- CommandResultSuccess({ windowProxy, id })
921
- );
922
- }
923
- callWebSpatialProtocolSync(command, query, target, features) {
924
- const { spatialId: id = "", windowProxy } = this.openWindow(
925
- command,
926
- query,
927
- target,
928
- features
929
- );
930
- return CommandResultSuccess({ windowProxy, id });
931
- }
932
- openWindow(command, query, target, features) {
933
- const windowProxy = window.open(
934
- `webspatial://${command}?${query || ""}`,
935
- target,
936
- features
937
- );
938
- const ua = windowProxy?.navigator.userAgent;
939
- const spatialId = ua?.match(
940
- /\b([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})\b/gi
941
- )?.[0];
942
- return { spatialId, windowProxy };
943
- }
944
- };
945
- }
946
- });
947
-
948
- // src/ssr-polyfill.ts
949
- var isSSR = typeof window === "undefined";
950
- var isSSREnv = () => isSSR;
951
-
952
- // src/platform-adapter/ssr/SSRPlatform.ts
953
- var SSRPlatform = class {
954
- callJSB(cmd, msg) {
955
- return Promise.resolve({
956
- success: true,
957
- data: void 0,
958
- errorCode: void 0,
959
- errorMessage: void 0
960
- });
961
- }
962
- callWebSpatialProtocol(schema, query, target, features) {
963
- return Promise.resolve({
964
- success: true,
965
- data: void 0,
966
- errorCode: void 0,
967
- errorMessage: void 0
968
- });
969
- }
970
- callWebSpatialProtocolSync(schema, query, target, features, resultCallback) {
971
- return {
972
- success: true,
973
- data: void 0,
974
- errorCode: void 0,
975
- errorMessage: void 0
976
- };
977
- }
978
- };
979
-
980
- // src/platform-adapter/index.ts
981
- function getWebSpatialVersion(ua) {
982
- const match = ua.match(/WebSpatial\/(\d+)\.(\d+)\.(\d+)/);
983
- if (!match) {
984
- return null;
985
- }
986
- return [Number(match[1]), Number(match[2]), Number(match[3])];
987
- }
988
- function isVersionGreater(a, b) {
989
- if (!a) {
990
- return false;
991
- }
992
- for (let index = 0; index < 3; index += 1) {
993
- const diff = a[index] - b[index];
994
- if (diff > 0) {
995
- return true;
996
- }
997
- if (diff < 0) {
998
- return false;
999
- }
1000
- }
1001
- return false;
1002
- }
1003
- function createPlatform() {
1004
- if (isSSREnv()) {
1005
- return new SSRPlatform();
1006
- }
1007
- const userAgent = window.navigator.userAgent;
1008
- const webSpatialVersion = getWebSpatialVersion(userAgent);
1009
- if (window.navigator.userAgent.includes("Puppeteer")) {
1010
- const PuppeteerPlatform2 = (init_PuppeteerPlatform(), __toCommonJS(PuppeteerPlatform_exports)).PuppeteerPlatform;
1011
- return new PuppeteerPlatform2();
1012
- } else if (userAgent.includes("PicoWebApp") && isVersionGreater(webSpatialVersion, [0, 0, 1])) {
1013
- const XRPlatform2 = (init_XRPlatform(), __toCommonJS(XRPlatform_exports)).XRPlatform;
1014
- return new XRPlatform2();
1015
- } else if (userAgent.includes("Android") || userAgent.includes("Linux")) {
1016
- const AndroidPlatform2 = (init_AndroidPlatform(), __toCommonJS(AndroidPlatform_exports)).AndroidPlatform;
1017
- return new AndroidPlatform2();
1018
- } else {
1019
- const VisionOSPlatform2 = (init_VisionOSPlatform(), __toCommonJS(VisionOSPlatform_exports)).VisionOSPlatform;
1020
- return new VisionOSPlatform2();
1021
- }
1022
- }
1023
-
1024
- // src/utils.ts
1025
- function parseBorderRadius(borderProperty, width) {
1026
- if (borderProperty === "") {
1027
- return 0;
1028
- }
1029
- if (borderProperty.endsWith("%")) {
1030
- return width * parseFloat(borderProperty) / 100;
1031
- }
1032
- return parseFloat(borderProperty);
1033
- }
1034
- function parseCornerRadius(computedStyle) {
1035
- const width = parseFloat(computedStyle.getPropertyValue("width"));
1036
- const topLeftPropertyValue = computedStyle.getPropertyValue(
1037
- "border-top-left-radius"
1038
- );
1039
- const topRightPropertyValue = computedStyle.getPropertyValue(
1040
- "border-top-right-radius"
1041
- );
1042
- const bottomLeftPropertyValue = computedStyle.getPropertyValue(
1043
- "border-bottom-left-radius"
1044
- );
1045
- const bottomRightPropertyValue = computedStyle.getPropertyValue(
1046
- "border-bottom-right-radius"
1047
- );
1048
- const cornerRadius = {
1049
- topLeading: parseBorderRadius(topLeftPropertyValue, width),
1050
- bottomLeading: parseBorderRadius(bottomLeftPropertyValue, width),
1051
- topTrailing: parseBorderRadius(topRightPropertyValue, width),
1052
- bottomTrailing: parseBorderRadius(bottomRightPropertyValue, width)
1053
- };
1054
- return cornerRadius;
1055
- }
1056
- function composeSRT(position, rotation, scale) {
1057
- const { x: px, y: py, z: pz } = position;
1058
- const { x: rx, y: ry, z: rz } = rotation;
1059
- const { x: sx, y: sy, z: sz } = scale;
1060
- let m = new DOMMatrix();
1061
- m = m.translate(px, py, pz);
1062
- m = m.rotate(rx, ry, rz);
1063
- m = m.scale(sx, sy, sz);
1064
- return m;
1065
- }
1066
-
1067
- // src/JSBCommand.ts
1068
- var platform = createPlatform();
1069
- var JSBCommand = class {
1070
- commandType = "";
1071
- async execute() {
1072
- const param = this.getParams();
1073
- const msg = param ? JSON.stringify(param) : "";
1074
- return platform.callJSB(this.commandType, msg);
1075
- }
1076
- };
1077
- var UpdateEntityPropertiesCommand = class extends JSBCommand {
1078
- constructor(entity, properties) {
1079
- super();
1080
- this.entity = entity;
1081
- this.properties = properties;
1082
- }
1083
- commandType = "UpdateEntityProperties";
1084
- getParams() {
1085
- const transform = composeSRT(
1086
- this.properties.position ?? this.entity.position,
1087
- this.properties.rotation ?? this.entity.rotation,
1088
- this.properties.scale ?? this.entity.scale
1089
- ).toFloat64Array();
1090
- return {
1091
- entityId: this.entity.id,
1092
- transform
1093
- };
1094
- }
1095
- };
1096
- var UpdateEntityEventCommand = class extends JSBCommand {
1097
- constructor(entity, type, isEnable) {
1098
- super();
1099
- this.entity = entity;
1100
- this.type = type;
1101
- this.isEnable = isEnable;
1102
- }
1103
- commandType = "UpdateEntityEvent";
1104
- getParams() {
1105
- return {
1106
- type: this.type,
1107
- entityId: this.entity.id,
1108
- isEnable: this.isEnable
1109
- };
1110
- }
1111
- };
1112
- var UpdateSpatialSceneProperties = class extends JSBCommand {
1113
- properties;
1114
- commandType = "UpdateSpatialSceneProperties";
1115
- constructor(properties) {
1116
- super();
1117
- this.properties = properties;
1118
- }
1119
- getParams() {
1120
- return this.properties;
1121
- }
1122
- };
1123
- var UpdateSceneConfig = class extends JSBCommand {
1124
- config;
1125
- commandType = "UpdateSceneConfig";
1126
- constructor(config) {
1127
- super();
1128
- this.config = config;
1129
- }
1130
- getParams() {
1131
- return { config: this.config };
1132
- }
1133
- };
1134
- var FocusScene = class extends JSBCommand {
1135
- constructor(id) {
1136
- super();
1137
- this.id = id;
1138
- }
1139
- commandType = "FocusScene";
1140
- getParams() {
1141
- return { id: this.id };
1142
- }
1143
- };
1144
- var GetSpatialSceneState = class extends JSBCommand {
1145
- commandType = "GetSpatialSceneState";
1146
- constructor() {
1147
- super();
1148
- }
1149
- getParams() {
1150
- return {};
1151
- }
1152
- };
1153
- var SpatializedElementCommand = class extends JSBCommand {
1154
- constructor(spatialObject) {
1155
- super();
1156
- this.spatialObject = spatialObject;
1157
- }
1158
- getParams() {
1159
- const extraParams = this.getExtraParams();
1160
- return { id: this.spatialObject.id, ...extraParams };
1161
- }
1162
- };
1163
- var UpdateSpatialized2DElementProperties = class extends SpatializedElementCommand {
1164
- properties;
1165
- commandType = "UpdateSpatialized2DElementProperties";
1166
- constructor(spatialObject, properties) {
1167
- super(spatialObject);
1168
- this.properties = properties;
1169
- }
1170
- getExtraParams() {
1171
- return this.properties;
1172
- }
1173
- };
1174
- var UpdateSpatializedDynamic3DElementProperties = class extends SpatializedElementCommand {
1175
- properties;
1176
- commandType = "UpdateSpatializedDynamic3DElementProperties";
1177
- constructor(spatialObject, properties) {
1178
- super(spatialObject);
1179
- this.properties = properties;
1180
- }
1181
- getExtraParams() {
1182
- return {
1183
- id: this.spatialObject.id,
1184
- ...this.properties
1185
- };
1186
- }
1187
- };
1188
- var UpdateUnlitMaterialProperties = class extends SpatializedElementCommand {
1189
- properties;
1190
- commandType = "UpdateUnlitMaterialProperties";
1191
- constructor(spatialObject, properties) {
1192
- super(spatialObject);
1193
- this.properties = properties;
1194
- }
1195
- getExtraParams() {
1196
- return this.properties;
1197
- }
1198
- };
1199
- var UpdateSpatializedElementTransform = class extends SpatializedElementCommand {
1200
- matrix;
1201
- commandType = "UpdateSpatializedElementTransform";
1202
- constructor(spatialObject, matrix) {
1203
- super(spatialObject);
1204
- this.matrix = matrix;
1205
- }
1206
- getExtraParams() {
1207
- return { matrix: Array.from(this.matrix.toFloat64Array()) };
1208
- }
1209
- };
1210
- var UpdateSpatializedStatic3DElementProperties = class extends SpatializedElementCommand {
1211
- properties;
1212
- commandType = "UpdateSpatializedStatic3DElementProperties";
1213
- constructor(spatialObject, properties) {
1214
- super(spatialObject);
1215
- this.properties = properties;
1216
- }
1217
- getExtraParams() {
1218
- return this.properties;
1219
- }
1220
- };
1221
- var AddSpatializedElementToSpatialized2DElement = class extends SpatializedElementCommand {
1222
- commandType = "AddSpatializedElementToSpatialized2DElement";
1223
- spatializedElement;
1224
- constructor(spatialObject, spatializedElement) {
1225
- super(spatialObject);
1226
- this.spatializedElement = spatializedElement;
1227
- }
1228
- getExtraParams() {
1229
- return { spatializedElementId: this.spatializedElement.id };
1230
- }
1231
- };
1232
- var AddSpatializedElementToSpatialScene = class extends JSBCommand {
1233
- commandType = "AddSpatializedElementToSpatialScene";
1234
- spatializedElement;
1235
- constructor(spatializedElement) {
1236
- super();
1237
- this.spatializedElement = spatializedElement;
1238
- }
1239
- getParams() {
1240
- return {
1241
- spatializedElementId: this.spatializedElement.id
1242
- };
1243
- }
1244
- };
1245
- var CreateSpatializedStatic3DElementCommand = class extends JSBCommand {
1246
- constructor(modelURL) {
1247
- super();
1248
- this.modelURL = modelURL;
1249
- this.modelURL = modelURL;
1250
- }
1251
- commandType = "CreateSpatializedStatic3DElement";
1252
- getParams() {
1253
- return { modelURL: this.modelURL };
1254
- }
1255
- };
1256
- var CreateSpatializedDynamic3DElementCommand = class extends JSBCommand {
1257
- getParams() {
1258
- return { test: true };
1259
- }
1260
- commandType = "CreateSpatializedDynamic3DElement";
1261
- };
1262
- var CreateSpatialEntityCommand = class extends JSBCommand {
1263
- constructor(name) {
1264
- super();
1265
- this.name = name;
1266
- }
1267
- getParams() {
1268
- return { name: this.name };
1269
- }
1270
- commandType = "CreateSpatialEntity";
1271
- };
1272
- var CreateModelComponentCommand = class extends JSBCommand {
1273
- constructor(options) {
1274
- super();
1275
- this.options = options;
1276
- }
1277
- getParams() {
1278
- let geometryId = this.options.mesh.id;
1279
- let materialIds = this.options.materials.map((material) => material.id);
1280
- return { geometryId, materialIds };
1281
- }
1282
- commandType = "CreateModelComponent";
1283
- };
1284
- var CreateSpatialModelEntityCommand = class extends JSBCommand {
1285
- constructor(options) {
1286
- super();
1287
- this.options = options;
1288
- }
1289
- getParams() {
1290
- return this.options;
1291
- }
1292
- commandType = "CreateSpatialModelEntity";
1293
- };
1294
- var CreateModelAssetCommand = class extends JSBCommand {
1295
- constructor(options) {
1296
- super();
1297
- this.options = options;
1298
- }
1299
- getParams() {
1300
- return { url: this.options.url };
1301
- }
1302
- commandType = "CreateModelAsset";
1303
- };
1304
- var CreateSpatialGeometryCommand = class extends JSBCommand {
1305
- constructor(type, options = {}) {
1306
- super();
1307
- this.type = type;
1308
- this.options = options;
1309
- }
1310
- getParams() {
1311
- return { type: this.type, ...this.options };
1312
- }
1313
- commandType = "CreateGeometry";
1314
- };
1315
- var CreateSpatialUnlitMaterialCommand = class extends JSBCommand {
1316
- constructor(options) {
1317
- super();
1318
- this.options = options;
1319
- }
1320
- getParams() {
1321
- return this.options;
1322
- }
1323
- commandType = "CreateUnlitMaterial";
1324
- };
1325
- var AddComponentToEntityCommand = class extends JSBCommand {
1326
- constructor(entity, comp) {
1327
- super();
1328
- this.entity = entity;
1329
- this.comp = comp;
1330
- }
1331
- getParams() {
1332
- return {
1333
- entityId: this.entity.id,
1334
- componentId: this.comp.id
1335
- };
1336
- }
1337
- commandType = "AddComponentToEntity";
1338
- };
1339
- var SetParentForEntityCommand = class extends JSBCommand {
1340
- // childId, parentId
1341
- constructor(childId, parentId) {
1342
- super();
1343
- this.childId = childId;
1344
- this.parentId = parentId;
1345
- }
1346
- getParams() {
1347
- return {
1348
- childId: this.childId,
1349
- parentId: this.parentId
1350
- };
1351
- }
1352
- commandType = "SetParentToEntity";
1353
- };
1354
- var ConvertFromEntityToEntityCommand = class extends JSBCommand {
1355
- constructor(fromEntityId, toEntityId, fromPosition) {
1356
- super();
1357
- this.fromEntityId = fromEntityId;
1358
- this.toEntityId = toEntityId;
1359
- this.fromPosition = fromPosition;
1360
- }
1361
- getParams() {
1362
- return {
1363
- fromEntityId: this.fromEntityId,
1364
- toEntityId: this.toEntityId,
1365
- position: this.fromPosition
1366
- };
1367
- }
1368
- commandType = "ConvertFromEntityToEntity";
1369
- };
1370
- var ConvertFromEntityToSceneCommand = class extends JSBCommand {
1371
- constructor(fromEntityId, position) {
1372
- super();
1373
- this.fromEntityId = fromEntityId;
1374
- this.position = position;
1375
- }
1376
- getParams() {
1377
- return {
1378
- fromEntityId: this.fromEntityId,
1379
- position: this.position
1380
- };
1381
- }
1382
- commandType = "ConvertFromEntityToScene";
1383
- };
1384
- var ConvertFromSceneToEntityCommand = class extends JSBCommand {
1385
- // let entityId: String
1386
- // let position:Vec3
1387
- constructor(entityId, position) {
1388
- super();
1389
- this.entityId = entityId;
1390
- this.position = position;
1391
- }
1392
- getParams() {
1393
- return {
1394
- entityId: this.entityId,
1395
- position: this.position
1396
- };
1397
- }
1398
- commandType = "ConvertFromSceneToEntity";
1399
- };
1400
- var InspectCommand = class extends JSBCommand {
1401
- constructor(id = "") {
1402
- super();
1403
- this.id = id;
1404
- }
1405
- commandType = "Inspect";
1406
- getParams() {
1407
- return this.id ? { id: this.id } : { id: "" };
1408
- }
1409
- };
1410
- var DestroyCommand = class extends JSBCommand {
1411
- constructor(id) {
1412
- super();
1413
- this.id = id;
1414
- }
1415
- commandType = "Destroy";
1416
- getParams() {
1417
- return { id: this.id };
1418
- }
1419
- };
1420
- var WebSpatialProtocolCommand = class extends JSBCommand {
1421
- target;
1422
- features;
1423
- async execute() {
1424
- const query = this.getQuery();
1425
- return platform.callWebSpatialProtocol(
1426
- this.commandType,
1427
- query,
1428
- this.target,
1429
- this.features
1430
- );
1431
- }
1432
- executeSync() {
1433
- const query = this.getQuery();
1434
- return platform.callWebSpatialProtocolSync(
1435
- this.commandType,
1436
- query,
1437
- this.target,
1438
- this.features
1439
- );
1440
- }
1441
- getQuery() {
1442
- let query = void 0;
1443
- const params = this.getParams();
1444
- if (params) {
1445
- query = Object.keys(params).map((key) => {
1446
- const value = params[key];
1447
- const finalValue = typeof value === "object" ? JSON.stringify(value) : value;
1448
- return `${key}=${encodeURIComponent(finalValue)}`;
1449
- }).join("&");
1450
- }
1451
- return query;
1452
- }
1453
- };
1454
- var createSpatialized2DElementCommand = class extends WebSpatialProtocolCommand {
1455
- commandType = "createSpatialized2DElement";
1456
- constructor() {
1457
- super();
1458
- }
1459
- getParams() {
1460
- return {};
1461
- }
1462
- };
1463
- var createSpatialSceneCommand = class extends WebSpatialProtocolCommand {
1464
- constructor(url, config, target, features) {
1465
- super();
1466
- this.url = url;
1467
- this.config = config;
1468
- this.target = target;
1469
- this.features = features;
1470
- }
1471
- commandType = "createSpatialScene";
1472
- getParams() {
1473
- return {
1474
- url: this.url,
1475
- config: this.config
1476
- };
1477
- }
1478
- };
1479
- var CreateAttachmentEntityCommand = class extends WebSpatialProtocolCommand {
1480
- constructor(options) {
1481
- super();
1482
- this.options = options;
1483
- }
1484
- commandType = "createAttachment";
1485
- getParams() {
1486
- return {
1487
- parentEntityId: this.options.parentEntityId,
1488
- position: this.options.position ?? [0, 0, 0],
1489
- size: this.options.size
1490
- };
1491
- }
1492
- };
1493
- var UpdateAttachmentEntityCommand = class extends JSBCommand {
1494
- constructor(attachmentId, options) {
1495
- super();
1496
- this.attachmentId = attachmentId;
1497
- this.options = options;
1498
- }
1499
- commandType = "UpdateAttachmentEntity";
1500
- getParams() {
1501
- return {
1502
- id: this.attachmentId,
1503
- ...this.options
1504
- };
1505
- }
1506
- };
1507
-
1508
- // src/SpatialObject.ts
1509
- var SpatialObject = class {
1510
- /** @hidden */
1511
- constructor(id) {
1512
- this.id = id;
1513
- }
1514
- name;
1515
- isDestroyed = false;
1516
- async inspect() {
1517
- const ret = await new InspectCommand(this.id).execute();
1518
- if (ret.success) {
1519
- return ret.data;
1520
- }
1521
- throw new Error(ret.errorMessage);
1522
- }
1523
- async destroy() {
1524
- if (this.isDestroyed) {
1525
- return;
1526
- }
1527
- const ret = await new DestroyCommand(this.id).execute();
1528
- if (ret.success) {
1529
- this.onDestroy();
1530
- this.isDestroyed = true;
1531
- return ret.data;
1532
- } else if (this.isDestroyed) {
1533
- return;
1534
- }
1535
- throw new Error(ret.errorMessage);
1536
- }
1537
- // override this method to do some cleanup
1538
- onDestroy() {
1539
- }
1540
- };
1541
-
1542
- // src/SpatialScene.ts
1543
- var instance;
1544
- var SpatialScene = class _SpatialScene extends SpatialObject {
1545
- /**
1546
- * Gets the singleton instance of the SpatialScene.
1547
- * Creates a new instance if one doesn't exist yet.
1548
- * @returns The singleton SpatialScene instance
1549
- */
1550
- static getInstance() {
1551
- if (!instance) {
1552
- instance = new _SpatialScene("");
1553
- }
1554
- return instance;
1555
- }
1556
- /**
1557
- * Updates the properties of the spatial scene.
1558
- * This can include background settings, lighting, and other scene-wide properties.
1559
- * @param properties Partial set of properties to update
1560
- * @returns Promise resolving when the update is complete
1561
- */
1562
- async updateSpatialProperties(properties) {
1563
- return new UpdateSpatialSceneProperties(properties).execute();
1564
- }
1565
- /**
1566
- * Adds a spatialized element to the scene.
1567
- * This makes the element visible and interactive in the spatial environment.
1568
- * @param element The SpatializedElement to add to the scene
1569
- * @returns Promise resolving when the element is added
1570
- */
1571
- async addSpatializedElement(element) {
1572
- return new AddSpatializedElementToSpatialScene(element).execute();
1573
- }
1574
- /**
1575
- * Updates the scene creation configuration.
1576
- * This allows changing scene parameters after initial creation.
1577
- * @param config The new scene creation configuration
1578
- * @returns Promise resolving when the update is complete
1579
- */
1580
- async updateSceneCreationConfig(config) {
1581
- return new UpdateSceneConfig(config).execute();
1582
- }
1583
- /**
1584
- * Gets the current state of the spatial scene.
1585
- * This includes information about active elements and scene configuration.
1586
- * @returns Promise resolving to the current SpatialSceneState
1587
- */
1588
- async getState() {
1589
- return (await new GetSpatialSceneState().execute()).data.name;
1590
- }
1591
- };
1592
-
1593
- // src/types/types.ts
1594
- var SpatializedElementType = /* @__PURE__ */ ((SpatializedElementType2) => {
1595
- SpatializedElementType2[SpatializedElementType2["Spatialized2DElement"] = 0] = "Spatialized2DElement";
1596
- SpatializedElementType2[SpatializedElementType2["SpatializedStatic3DElement"] = 1] = "SpatializedStatic3DElement";
1597
- SpatializedElementType2[SpatializedElementType2["SpatializedDynamic3DElement"] = 2] = "SpatializedDynamic3DElement";
1598
- return SpatializedElementType2;
1599
- })(SpatializedElementType || {});
1600
- var BaseplateVisibilityValues = [
1601
- "automatic",
1602
- "visible",
1603
- "hidden"
1604
- ];
1605
- function isValidBaseplateVisibilityType(type) {
1606
- return BaseplateVisibilityValues.includes(type);
1607
- }
1608
- var WorldScalingValues = ["automatic", "dynamic"];
1609
- function isValidWorldScalingType(type) {
1610
- return WorldScalingValues.includes(type);
1611
- }
1612
- var WorldAlignmentValues = [
1613
- "adaptive",
1614
- "automatic",
1615
- "gravityAligned"
1616
- ];
1617
- function isValidWorldAlignmentType(type) {
1618
- return WorldAlignmentValues.includes(type);
1619
- }
1620
- var SpatialSceneValues = ["window", "volume"];
1621
- function isValidSpatialSceneType(type) {
1622
- return SpatialSceneValues.includes(type);
1623
- }
1624
- function isValidSceneUnit(val) {
1625
- if (typeof val === "number") {
1626
- return val >= 0;
1627
- }
1628
- if (typeof val === "string") {
1629
- if (val.endsWith("px")) {
1630
- if (isNaN(Number(val.slice(0, -2)))) {
1631
- return false;
1632
- }
1633
- return Number(val.slice(0, -2)) >= 0;
1634
- }
1635
- if (val.endsWith("m")) {
1636
- if (isNaN(Number(val.slice(0, -1)))) {
1637
- return false;
1638
- }
1639
- return Number(val.slice(0, -1)) >= 0;
1640
- }
1641
- }
1642
- return false;
1643
- }
1644
- var SpatialSceneState = /* @__PURE__ */ ((SpatialSceneState2) => {
1645
- SpatialSceneState2["idle"] = "idle";
1646
- SpatialSceneState2["pending"] = "pending";
1647
- SpatialSceneState2["willVisible"] = "willVisible";
1648
- SpatialSceneState2["visible"] = "visible";
1649
- SpatialSceneState2["fail"] = "fail";
1650
- return SpatialSceneState2;
1651
- })(SpatialSceneState || {});
1652
- var CubeInfo = class {
1653
- constructor(size, origin) {
1654
- this.size = size;
1655
- this.origin = origin;
1656
- this.size = size;
1657
- this.origin = origin;
1658
- }
1659
- get x() {
1660
- return this.origin.x;
1661
- }
1662
- get y() {
1663
- return this.origin.y;
1664
- }
1665
- get z() {
1666
- return this.origin.z;
1667
- }
1668
- get width() {
1669
- return this.size.width;
1670
- }
1671
- get height() {
1672
- return this.size.height;
1673
- }
1674
- get depth() {
1675
- return this.size.depth;
1676
- }
1677
- get left() {
1678
- return this.x;
1679
- }
1680
- get top() {
1681
- return this.y;
1682
- }
1683
- get right() {
1684
- return this.x + this.width;
1685
- }
1686
- get bottom() {
1687
- return this.y + this.height;
1688
- }
1689
- get back() {
1690
- return this.z;
1691
- }
1692
- get front() {
1693
- return this.z + this.depth;
1694
- }
1695
- };
1696
-
1697
- // src/scene-polyfill.ts
1698
- var defaultSceneConfig = {
1699
- defaultSize: {
1700
- width: 1280,
1701
- height: 720
1702
- }
1703
- };
1704
- var defaultSceneConfigVolume = {
1705
- defaultSize: {
1706
- width: 0.94,
1707
- height: 0.94,
1708
- depth: 0.94
1709
- }
1710
- };
1711
- var INTERNAL_SCHEMA_PREFIX = "webspatial://";
1712
- var SceneManager = class _SceneManager {
1713
- originalOpen;
1714
- static instance;
1715
- static getInstance() {
1716
- if (!_SceneManager.instance) {
1717
- _SceneManager.instance = new _SceneManager();
1718
- }
1719
- return _SceneManager.instance;
1720
- }
1721
- init(window2) {
1722
- this.originalOpen = window2.open.bind(window2);
1723
- window2.open = this.open;
1724
- }
1725
- configMap = {};
1726
- // name=>config
1727
- getConfig(name) {
1728
- if (name === void 0 || !this.configMap[name]) return void 0;
1729
- return this.configMap[name];
1730
- }
1731
- // Ensure URL is absolute; only convert when a relative path is provided
1732
- // - Keep external and special schemes untouched (http, https, data, blob, about, file, mailto, etc.)
1733
- // - Handle protocol-relative URLs (//example.com/path)
1734
- // - Resolve relative paths against document.baseURI (respects <base href>)
1735
- ensureAbsoluteUrl(raw) {
1736
- if (!raw) return raw;
1737
- if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(raw)) {
1738
- return raw;
1739
- }
1740
- if (raw.startsWith("//")) {
1741
- return `${window.location.protocol}${raw}`;
1742
- }
1743
- try {
1744
- return new URL(raw, document.baseURI).toString();
1745
- } catch {
1746
- return raw;
1747
- }
1748
- }
1749
- open = (url, target, features) => {
1750
- if (url?.startsWith(INTERNAL_SCHEMA_PREFIX)) {
1751
- return this.originalOpen(url, target, features);
1752
- }
1753
- url = this.ensureAbsoluteUrl(url);
1754
- if (target === "_self" || target === "_parent" || target === "_top") {
1755
- const newWindow = this.originalOpen(url, target, features);
1756
- return newWindow;
1757
- }
1758
- const cfg = target ? this.getConfig(target) : void 0;
1759
- const cmd = new createSpatialSceneCommand(url, cfg, target, features);
1760
- const result = cmd.executeSync();
1761
- const id = result.data?.id;
1762
- if (id) {
1763
- let focusCmd = new FocusScene(id);
1764
- focusCmd.execute();
1765
- }
1766
- return result.data?.windowProxy;
1767
- };
1768
- initScene(name, callback, options) {
1769
- const sceneType = options?.type ?? "window";
1770
- const defaultConfig = getSceneDefaultConfig(sceneType);
1771
- const rawReturnVal = callback({ ...defaultConfig });
1772
- const [formattedConfig, errors] = formatSceneConfig(rawReturnVal, sceneType);
1773
- if (errors.length > 0) {
1774
- console.warn(`initScene ${name} with errors: ${errors.join(", ")}`);
1775
- }
1776
- this.configMap[name] = {
1777
- ...formattedConfig,
1778
- type: sceneType
1779
- };
1780
- }
1781
- };
1782
- function pxToMeter(px) {
1783
- return px / 1360;
1784
- }
1785
- function meterToPx(meter) {
1786
- return meter * 1360;
1787
- }
1788
- function formatToNumber(str, targetUnit, defaultUnit) {
1789
- if (typeof str === "number") {
1790
- if (defaultUnit === "px" && targetUnit === "px" || defaultUnit === "m" && targetUnit === "m") {
1791
- return str;
1792
- }
1793
- if (defaultUnit === "px" && targetUnit === "m") {
1794
- return pxToMeter(str);
1795
- } else if (defaultUnit === "m" && targetUnit === "px") {
1796
- return meterToPx(str);
1797
- }
1798
- return str;
1799
- }
1800
- if (targetUnit === "m") {
1801
- if (str.endsWith("m")) {
1802
- return Number(str.slice(0, -1));
1803
- } else if (str.endsWith("px")) {
1804
- return pxToMeter(Number(str.slice(0, -2)));
1805
- } else {
1806
- throw new Error("formatToNumber: invalid str");
1807
- }
1808
- } else if (targetUnit === "px") {
1809
- if (str.endsWith("px")) {
1810
- return Number(str.slice(0, -2));
1811
- } else if (str.endsWith("m")) {
1812
- return meterToPx(Number(str.slice(0, -1)));
1813
- } else {
1814
- throw new Error("formatToNumber: invalid str");
1815
- }
1816
- } else {
1817
- throw new Error("formatToNumber: invalid targetUnit");
1818
- }
1819
- }
1820
- function formatSceneConfig(config, sceneType) {
1821
- const defaultSceneConfig2 = getSceneDefaultConfig(sceneType);
1822
- const errors = [];
1823
- const isWindow = sceneType === "window";
1824
- if (!isValidSpatialSceneType(sceneType)) {
1825
- errors.push(`sceneType`);
1826
- }
1827
- if (config.defaultSize) {
1828
- const iterKeys = ["width", "height", "depth"];
1829
- for (let k of iterKeys) {
1830
- if (!(k in config.defaultSize)) continue;
1831
- if (isValidSceneUnit(config.defaultSize[k])) {
1832
- ;
1833
- config.defaultSize[k] = formatToNumber(
1834
- config.defaultSize[k],
1835
- isWindow ? "px" : "m",
1836
- isWindow ? "px" : "m"
1837
- );
1838
- } else {
1839
- ;
1840
- config.defaultSize[k] = defaultSceneConfig2.defaultSize[k];
1841
- errors.push(`defaultSize.${k}`);
1842
- }
1843
- }
1844
- }
1845
- if (config.resizability) {
1846
- const iterKeys = ["minWidth", "minHeight", "maxWidth", "maxHeight"];
1847
- for (let k of iterKeys) {
1848
- if (!(k in config.resizability)) continue;
1849
- if (isValidSceneUnit(config.resizability[k])) {
1850
- ;
1851
- config.resizability[k] = formatToNumber(
1852
- config.resizability[k],
1853
- "px",
1854
- isWindow ? "px" : "m"
1855
- );
1856
- } else {
1857
- ;
1858
- config.resizability[k] = void 0;
1859
- errors.push(`resizability.${k}`);
1860
- }
1861
- }
1862
- }
1863
- if (config.worldScaling) {
1864
- if (!isValidWorldScalingType(config.worldScaling)) {
1865
- config.worldScaling = "automatic";
1866
- errors.push("worldScaling");
1867
- }
1868
- }
1869
- if (config.worldAlignment) {
1870
- if (!isValidWorldAlignmentType(config.worldAlignment)) {
1871
- config.worldAlignment = "automatic";
1872
- errors.push("worldAlignment");
1873
- }
1874
- }
1875
- if (config.baseplateVisibility) {
1876
- if (!isValidBaseplateVisibilityType(config.baseplateVisibility)) {
1877
- config.baseplateVisibility = "automatic";
1878
- errors.push("baseplateVisibility");
1879
- }
1880
- }
1881
- return [config, errors];
1882
- }
1883
- function initScene(name, callback, options) {
1884
- return SceneManager.getInstance().initScene(name, callback, options);
1885
- }
1886
- function hijackWindowOpen(window2) {
1887
- SceneManager.getInstance().init(window2);
1888
- }
1889
- function hijackWindowATag(openedWindow) {
1890
- openedWindow.document.onclick = function(e) {
1891
- let element = e.target;
1892
- let found = false;
1893
- while (!found) {
1894
- if (element && element.tagName == "A") {
1895
- if (handleATag(e)) {
1896
- return false;
1897
- }
1898
- return true;
1899
- }
1900
- if (element && element.parentElement) {
1901
- element = element.parentElement;
1902
- } else {
1903
- break;
1904
- }
1905
- }
1906
- };
1907
- }
1908
- function handleATag(event) {
1909
- const targetElement = event.target;
1910
- if (targetElement.tagName === "A") {
1911
- const link = targetElement;
1912
- const target = link.target;
1913
- const url = link.href;
1914
- if (target && target !== "_self") {
1915
- event.preventDefault();
1916
- window.open(url, target);
1917
- return true;
1918
- }
1919
- }
1920
- }
1921
- function getSceneDefaultConfig(sceneType) {
1922
- return sceneType === "window" ? defaultSceneConfig : defaultSceneConfigVolume;
1923
- }
1924
- async function injectScenePolyfill() {
1925
- if (!window.opener) return;
1926
- const state = await SpatialScene.getInstance().getState();
1927
- if (state !== "pending" /* pending */) return;
1928
- function onContentLoaded(callback) {
1929
- if (document.readyState === "interactive" || document.readyState === "complete") {
1930
- callback();
1931
- } else {
1932
- document.addEventListener("DOMContentLoaded", callback);
1933
- }
1934
- }
1935
- onContentLoaded(async () => {
1936
- let provideDefaultSceneConfig = getSceneDefaultConfig(
1937
- window.xrCurrentSceneType ?? "window"
1938
- );
1939
- let cfg = provideDefaultSceneConfig;
1940
- if (typeof window.xrCurrentSceneDefaults === "function") {
1941
- try {
1942
- cfg = await window.xrCurrentSceneDefaults?.(provideDefaultSceneConfig);
1943
- } catch (error) {
1944
- console.error(error);
1945
- }
1946
- }
1947
- await new Promise((resolve, reject) => {
1948
- setTimeout(() => {
1949
- resolve(null);
1950
- }, 1e3);
1951
- });
1952
- const sceneType = window.xrCurrentSceneType ?? "window";
1953
- const [formattedConfig, errors] = formatSceneConfig(cfg, sceneType);
1954
- if (errors.length > 0) {
1955
- console.warn(
1956
- `window.xrCurrentSceneDefaults with errors: ${errors.join(", ")}`
1957
- );
1958
- }
1959
- await SpatialScene.getInstance().updateSceneCreationConfig({
1960
- ...formattedConfig,
1961
- type: sceneType
1962
- });
1963
- });
1964
- }
1965
- function injectSceneHook() {
1966
- hijackWindowOpen(window);
1967
- hijackWindowATag(window);
1968
- injectScenePolyfill();
1969
- }
1970
-
1971
- // src/SpatializedElement.ts
1972
- init_SpatialWebEvent();
1973
-
1974
- // src/SpatialWebEventCreator.ts
1975
- function createSpatialEvent(type, detail) {
1976
- return new CustomEvent(type, {
1977
- bubbles: true,
1978
- cancelable: false,
1979
- detail
1980
- });
1981
- }
1982
-
1983
- // src/SpatializedElement.ts
1984
- var SpatializedElement = class extends SpatialObject {
1985
- /**
1986
- * Creates a new spatialized element with the specified ID.
1987
- * Registers the element to receive spatial events.
1988
- * @param id Unique identifier for this element
1989
- */
1990
- constructor(id) {
1991
- super(id);
1992
- this.id = id;
1993
- SpatialWebEvent.addEventReceiver(id, this.onReceiveEvent.bind(this));
1994
- }
1995
- /**
1996
- * Updates the transformation matrix of this element in 3D space.
1997
- * This affects the position, rotation, and scale of the element.
1998
- * @param matrix The new transformation matrix
1999
- * @returns Promise resolving when the transform is updated
2000
- */
2001
- async updateTransform(matrix) {
2002
- return new UpdateSpatializedElementTransform(this, matrix).execute();
2003
- }
2004
- /**
2005
- * Information about the element's bounding cube.
2006
- * Used for spatial calculations and hit testing.
2007
- */
2008
- _cubeInfo;
2009
- /**
2010
- * Gets the current cube information for this element.
2011
- * @returns The current CubeInfo or undefined if not set
2012
- */
2013
- get cubeInfo() {
2014
- return this._cubeInfo;
2015
- }
2016
- /**
2017
- * The current transformation matrix of this element.
2018
- */
2019
- _transform;
2020
- /**
2021
- * The inverse of the current transformation matrix.
2022
- * Used for converting world coordinates to local coordinates.
2023
- */
2024
- _transformInv;
2025
- /**
2026
- * Gets the current transformation matrix.
2027
- * @returns The current transformation matrix or undefined if not set
2028
- */
2029
- get transform() {
2030
- return this._transform;
2031
- }
2032
- /**
2033
- * Gets the inverse of the current transformation matrix.
2034
- * @returns The inverse transformation matrix or undefined if not set
2035
- */
2036
- get transformInv() {
2037
- return this._transformInv;
2038
- }
2039
- /**
2040
- * Processes events received from the WebSpatial environment.
2041
- * Handles various spatial events like transforms, gestures, and interactions.
2042
- * @param data The event data received from the WebSpatial system
2043
- */
2044
- onReceiveEvent(data) {
2045
- const { type } = data;
2046
- if (type === "objectdestroy" /* objectdestroy */) {
2047
- this.isDestroyed = true;
2048
- } else if (type === "cubeInfo" /* cubeInfo */) {
2049
- const cubeInfoMsg = data;
2050
- this._cubeInfo = new CubeInfo(cubeInfoMsg.size, cubeInfoMsg.origin);
2051
- } else if (type === "transform" /* transform */) {
2052
- this._transform = new DOMMatrix([
2053
- data.detail.column0[0],
2054
- data.detail.column0[1],
2055
- data.detail.column0[2],
2056
- 0,
2057
- data.detail.column1[0],
2058
- data.detail.column1[1],
2059
- data.detail.column1[2],
2060
- 0,
2061
- data.detail.column2[0],
2062
- data.detail.column2[1],
2063
- data.detail.column2[2],
2064
- 0,
2065
- data.detail.column3[0],
2066
- data.detail.column3[1],
2067
- data.detail.column3[2],
2068
- 1
2069
- ]);
2070
- this._transformInv = this._transform.inverse();
2071
- } else if (type === "spatialtap" /* spatialtap */) {
2072
- const event = createSpatialEvent(
2073
- "spatialtap" /* spatialtap */,
2074
- data.detail
2075
- );
2076
- this._onSpatialTap?.(event);
2077
- } else if (type === "spatialdragstart" /* spatialdragstart */) {
2078
- const dragStartEvent = createSpatialEvent(
2079
- "spatialdragstart" /* spatialdragstart */,
2080
- data.detail
2081
- );
2082
- this._onSpatialDragStart?.(dragStartEvent);
2083
- } else if (type === "spatialdrag" /* spatialdrag */) {
2084
- const event = createSpatialEvent(
2085
- "spatialdrag" /* spatialdrag */,
2086
- data.detail
2087
- );
2088
- this._onSpatialDrag?.(event);
2089
- } else if (type === "spatialdragend" /* spatialdragend */) {
2090
- const event = createSpatialEvent(
2091
- "spatialdragend" /* spatialdragend */,
2092
- data.detail
2093
- );
2094
- this._onSpatialDragEnd?.(event);
2095
- } else if (type === "spatialrotate" /* spatialrotate */) {
2096
- const event = createSpatialEvent(
2097
- "spatialrotate" /* spatialrotate */,
2098
- data.detail
2099
- );
2100
- this._onSpatialRotate?.(event);
2101
- } else if (type === "spatialrotateend" /* spatialrotateend */) {
2102
- const event = createSpatialEvent(
2103
- "spatialrotateend" /* spatialrotateend */,
2104
- data.detail
2105
- );
2106
- this._onSpatialRotateEnd?.(event);
2107
- } else if (type === "spatialmagnify" /* spatialmagnify */) {
2108
- const event = createSpatialEvent(
2109
- "spatialmagnify" /* spatialmagnify */,
2110
- data.detail
2111
- );
2112
- this._onSpatialMagnify?.(event);
2113
- } else if (type === "spatialmagnifyend" /* spatialmagnifyend */) {
2114
- const event = createSpatialEvent(
2115
- "spatialmagnifyend" /* spatialmagnifyend */,
2116
- data.detail
2117
- );
2118
- this._onSpatialMagnifyEnd?.(event);
2119
- }
2120
- }
2121
- _onSpatialTap;
2122
- set onSpatialTap(value) {
2123
- this._onSpatialTap = value;
2124
- this.updateProperties({
2125
- enableTapGesture: value !== void 0
2126
- });
2127
- }
2128
- _onSpatialDragStart;
2129
- set onSpatialDragStart(value) {
2130
- this._onSpatialDragStart = value;
2131
- this.updateProperties({
2132
- enableDragStartGesture: this._onSpatialDragStart !== void 0
2133
- });
2134
- }
2135
- _onSpatialDrag;
2136
- set onSpatialDrag(value) {
2137
- this._onSpatialDrag = value;
2138
- this.updateProperties({
2139
- enableDragGesture: this._onSpatialDrag !== void 0
2140
- });
2141
- }
2142
- _onSpatialDragEnd;
2143
- set onSpatialDragEnd(value) {
2144
- this._onSpatialDragEnd = value;
2145
- this.updateProperties({
2146
- enableDragEndGesture: value !== void 0
2147
- });
2148
- }
2149
- _onSpatialRotate;
2150
- set onSpatialRotate(value) {
2151
- this._onSpatialRotate = value;
2152
- this.updateProperties({
2153
- enableRotateGesture: this._onSpatialRotate !== void 0
2154
- });
2155
- }
2156
- _onSpatialRotateEnd;
2157
- set onSpatialRotateEnd(value) {
2158
- this._onSpatialRotateEnd = value;
2159
- this.updateProperties({
2160
- enableRotateEndGesture: value !== void 0
2161
- });
2162
- }
2163
- _onSpatialMagnify;
2164
- set onSpatialMagnify(value) {
2165
- this._onSpatialMagnify = value;
2166
- this.updateProperties({
2167
- enableMagnifyGesture: value !== void 0
2168
- });
2169
- }
2170
- _onSpatialMagnifyEnd;
2171
- set onSpatialMagnifyEnd(value) {
2172
- this._onSpatialMagnifyEnd = value;
2173
- this.updateProperties({
2174
- enableMagnifyEndGesture: value !== void 0
2175
- });
2176
- }
2177
- /**
2178
- * Cleans up resources when this element is destroyed.
2179
- * Removes event receivers to prevent memory leaks.
2180
- */
2181
- onDestroy() {
2182
- SpatialWebEvent.removeEventReceiver(this.id);
2183
- }
2184
- };
2185
-
2186
- // src/Spatialized2DElement.ts
2187
- var Spatialized2DElement = class extends SpatializedElement {
2188
- /**
2189
- * Creates a new spatialized 2D element.
2190
- * @param id Unique identifier for this element
2191
- * @param windowProxy Reference to the window object containing the 2D content
2192
- */
2193
- constructor(id, windowProxy) {
2194
- super(id);
2195
- this.windowProxy = windowProxy;
2196
- hijackWindowATag(windowProxy);
2197
- }
2198
- /**
2199
- * Updates the properties of this 2D element.
2200
- * This can include size, position, background, and other visual properties.
2201
- * @param properties Partial set of properties to update
2202
- * @returns Promise resolving when the update is complete
2203
- */
2204
- async updateProperties(properties) {
2205
- return new UpdateSpatialized2DElementProperties(this, properties).execute();
2206
- }
2207
- /**
2208
- * Adds a child spatialized element to this 2D element.
2209
- * This allows for creating hierarchical structures of spatial elements.
2210
- * @param element The child element to add
2211
- * @returns Promise resolving when the element is added
2212
- */
2213
- async addSpatializedElement(element) {
2214
- return new AddSpatializedElementToSpatialized2DElement(
2215
- this,
2216
- element
2217
- ).execute();
2218
- }
2219
- };
2220
-
2221
- // src/SpatializedStatic3DElement.ts
2222
- var SpatializedStatic3DElement = class extends SpatializedElement {
2223
- /**
2224
- * Creates a new spatialized static 3D element with the specified ID and URL.
2225
- * Registers the element to receive spatial events.
2226
- * @param id Unique identifier for this element
2227
- * @param modelURL URL of the 3D model
2228
- */
2229
- constructor(id, modelURL) {
2230
- super(id);
2231
- this.modelURL = modelURL;
2232
- }
2233
- /**
2234
- * Promise resolver for the ready state.
2235
- * Used to resolve the ready promise when the model is loaded.
2236
- */
2237
- _readyResolve;
2238
- /**
2239
- * Caches the last model URL to detect changes.
2240
- * Used to reset the ready promise when the model URL changes.
2241
- */
2242
- modelURL;
2243
- /**
2244
- * Creates a new promise for tracking the ready state of the model.
2245
- * @returns Promise that resolves when the model is loaded (true) or fails to load (false)
2246
- */
2247
- createReadyPromise() {
2248
- return new Promise((resolve) => {
2249
- this._readyResolve = resolve;
2250
- });
2251
- }
2252
- /**
2253
- * Promise that resolves when the model is loaded.
2254
- * Resolves to true on successful load, false on failure.
2255
- */
2256
- ready = this.createReadyPromise();
2257
- /**
2258
- * Updates the properties of this static 3D element.
2259
- * Handles special case for modelURL changes by resetting the ready promise.
2260
- * @param properties Partial set of properties to update
2261
- * @returns Promise resolving when the update is complete
2262
- */
2263
- async updateProperties(properties) {
2264
- if (properties.modelURL !== void 0) {
2265
- if (this.modelURL !== properties.modelURL) {
2266
- this.modelURL = properties.modelURL;
2267
- this.ready = this.createReadyPromise();
2268
- }
2269
- }
2270
- return new UpdateSpatializedStatic3DElementProperties(
2271
- this,
2272
- properties
2273
- ).execute();
2274
- }
2275
- /**
2276
- * Processes events received from the WebSpatial environment.
2277
- * Handles model loading events in addition to base spatial events.
2278
- * @param data The event data received from the WebSpatial system
2279
- */
2280
- onReceiveEvent(data) {
2281
- if (data.type === "modelloaded" /* modelloaded */) {
2282
- this._onLoadCallback?.();
2283
- this._readyResolve?.(true);
2284
- } else if (data.type === "modelloadfailed" /* modelloadfailed */) {
2285
- this._onLoadFailureCallback?.();
2286
- this._readyResolve?.(false);
2287
- } else {
2288
- super.onReceiveEvent(data);
2289
- }
2290
- }
2291
- /**
2292
- * Callback function for successful model loading.
2293
- */
2294
- _onLoadCallback;
2295
- /**
2296
- * Sets the callback function for successful model loading.
2297
- * @param callback Function to call when the model is loaded successfully
2298
- */
2299
- set onLoadCallback(callback) {
2300
- this._onLoadCallback = callback;
2301
- }
2302
- /**
2303
- * Callback function for model loading failure.
2304
- */
2305
- _onLoadFailureCallback;
2306
- /**
2307
- * Sets the callback function for model loading failure.
2308
- * @param callback Function to call when the model fails to load
2309
- */
2310
- set onLoadFailureCallback(callback) {
2311
- this._onLoadFailureCallback = callback;
2312
- }
2313
- updateModelTransform(transform) {
2314
- const modelTransform = Array.from(transform.toFloat64Array());
2315
- this.updateProperties({ modelTransform });
2316
- }
2317
- };
2318
-
2319
- // src/SpatializedDynamic3DElement.ts
2320
- var SpatializedDynamic3DElement = class extends SpatializedElement {
2321
- children = [];
2322
- constructor(id) {
2323
- super(id);
2324
- }
2325
- async addEntity(entity) {
2326
- const ans = new SetParentForEntityCommand(entity.id, this.id).execute();
2327
- this.children.push(entity);
2328
- entity.parent = this;
2329
- return ans;
2330
- }
2331
- async updateProperties(properties) {
2332
- return new UpdateSpatializedDynamic3DElementProperties(
2333
- this,
2334
- properties
2335
- ).execute();
2336
- }
2337
- };
2338
-
2339
- // src/SpatializedElementCreator.ts
2340
- async function createSpatialized2DElement() {
2341
- const result = await new createSpatialized2DElementCommand().execute();
2342
- if (!result.success) {
2343
- throw new Error("createSpatialized2DElement failed");
2344
- } else {
2345
- const { id, windowProxy } = result.data;
2346
- windowProxy.document.head.innerHTML = `<meta name="viewport" content="width=device-width, initial-scale=1">
2347
- <base href="${document.baseURI}">`;
2348
- return new Spatialized2DElement(id, windowProxy);
2349
- }
2350
- }
2351
- async function createSpatializedStatic3DElement(modelURL) {
2352
- const result = await new CreateSpatializedStatic3DElementCommand(
2353
- modelURL
2354
- ).execute();
2355
- if (!result.success) {
2356
- throw new Error("createSpatializedStatic3DElement failed");
2357
- } else {
2358
- const { id } = result.data;
2359
- return new SpatializedStatic3DElement(id, modelURL);
2360
- }
2361
- }
2362
- async function createSpatializedDynamic3DElement() {
2363
- const result = await new CreateSpatializedDynamic3DElementCommand().execute();
2364
- if (!result.success) {
2365
- throw new Error("createSpatializedDynamic3DElement failed");
2366
- } else {
2367
- const { id } = result.data;
2368
- return new SpatializedDynamic3DElement(id);
2369
- }
2370
- }
2371
-
2372
- // src/reality/Attachment.ts
2373
- var Attachment = class extends SpatialObject {
2374
- constructor(id, windowProxy, options) {
2375
- super(id);
2376
- this.windowProxy = windowProxy;
2377
- this.options = options;
2378
- }
2379
- getContainer() {
2380
- return this.windowProxy.document.body;
2381
- }
2382
- getWindowProxy() {
2383
- return this.windowProxy;
2384
- }
2385
- async update(options) {
2386
- if (this.isDestroyed) return;
2387
- if (options.position) this.options.position = options.position;
2388
- if (options.size) this.options.size = options.size;
2389
- return new UpdateAttachmentEntityCommand(this.id, options).execute();
2390
- }
2391
- };
2392
- async function createAttachmentEntity(options) {
2393
- const result = await new CreateAttachmentEntityCommand(options).execute();
2394
- if (!result.success) {
2395
- throw new Error("createAttachmentEntity failed: " + result?.errorMessage);
2396
- }
2397
- const { id, windowProxy } = result.data;
2398
- return new Attachment(id, windowProxy, options);
2399
- }
2400
-
2401
- // src/reality/entity/SpatialEntity.ts
2402
- init_SpatialWebEvent();
2403
- var SpatialEntity = class _SpatialEntity extends SpatialObject {
2404
- constructor(id, userData) {
2405
- super(id);
2406
- this.userData = userData;
2407
- SpatialWebEvent.addEventReceiver(id, this.onReceiveEvent);
2408
- }
2409
- position = { x: 0, y: 0, z: 0 };
2410
- rotation = { x: 0, y: 0, z: 0 };
2411
- scale = { x: 1, y: 1, z: 1 };
2412
- events = {};
2413
- children = [];
2414
- parent = null;
2415
- async addComponent(component) {
2416
- return new AddComponentToEntityCommand(this, component).execute();
2417
- }
2418
- async setPosition(position) {
2419
- return this.updateTransform({ position });
2420
- }
2421
- async setRotation(rotation) {
2422
- return this.updateTransform({ rotation });
2423
- }
2424
- async setScale(scale) {
2425
- return this.updateTransform({ scale });
2426
- }
2427
- async addEntity(ent) {
2428
- const ans = await new SetParentForEntityCommand(ent.id, this.id).execute();
2429
- this.children.push(ent);
2430
- ent.parent = this;
2431
- return ans;
2432
- }
2433
- async removeFromParent() {
2434
- const ans = await new SetParentForEntityCommand(
2435
- this.id,
2436
- void 0
2437
- ).execute();
2438
- if (this.parent) {
2439
- this.parent.children = this.parent.children.filter(
2440
- (child) => child.id !== this.id
2441
- );
2442
- this.parent = null;
2443
- }
2444
- return ans;
2445
- }
2446
- async updateTransform(properties) {
2447
- this.position = properties.position ?? this.position;
2448
- this.rotation = properties.rotation ?? this.rotation;
2449
- this.scale = properties.scale ?? this.scale;
2450
- return new UpdateEntityPropertiesCommand(this, properties).execute();
2451
- }
2452
- async addEvent(type, callback) {
2453
- if (this.events[type]) {
2454
- this.events[type] = callback;
2455
- } else {
2456
- try {
2457
- await this.updateEntityEvent(type, true);
2458
- this.events[type] = callback;
2459
- } catch (error) {
2460
- console.error("addEvent failed", type);
2461
- }
2462
- }
2463
- }
2464
- async removeEvent(eventName) {
2465
- if (this.events[eventName]) {
2466
- delete this.events[eventName];
2467
- try {
2468
- await this.updateEntityEvent(eventName, false);
2469
- } catch (error) {
2470
- console.error("removeEvent failed", eventName);
2471
- }
2472
- }
2473
- }
2474
- async updateEntityEvent(eventName, isEnable) {
2475
- return new UpdateEntityEventCommand(this, eventName, isEnable).execute();
2476
- }
2477
- onReceiveEvent = (data) => {
2478
- const { type } = data;
2479
- if (type === "objectdestroy" /* objectdestroy */) {
2480
- this.isDestroyed = true;
2481
- } else if (type === "spatialtap" /* spatialtap */) {
2482
- const evt = createSpatialEvent(
2483
- "spatialtap" /* spatialtap */,
2484
- data.detail
2485
- );
2486
- this.dispatchEvent(evt);
2487
- } else if (type === "spatialdragstart" /* spatialdragstart */) {
2488
- const evt = createSpatialEvent(
2489
- "spatialdragstart" /* spatialdragstart */,
2490
- data.detail
2491
- );
2492
- this.dispatchEvent(evt);
2493
- } else if (type === "spatialdrag" /* spatialdrag */) {
2494
- const evt = createSpatialEvent(
2495
- "spatialdrag" /* spatialdrag */,
2496
- data.detail
2497
- );
2498
- this.dispatchEvent(evt);
2499
- } else if (type === "spatialdragend" /* spatialdragend */) {
2500
- const evt = createSpatialEvent(
2501
- "spatialdragend" /* spatialdragend */,
2502
- data.detail
2503
- );
2504
- this.dispatchEvent(evt);
2505
- } else if (type === "spatialrotate" /* spatialrotate */) {
2506
- const evt = createSpatialEvent(
2507
- "spatialrotate" /* spatialrotate */,
2508
- data.detail
2509
- );
2510
- this.dispatchEvent(evt);
2511
- } else if (type === "spatialrotateend" /* spatialrotateend */) {
2512
- const evt = createSpatialEvent(
2513
- "spatialrotateend" /* spatialrotateend */,
2514
- data.detail
2515
- );
2516
- this.dispatchEvent(evt);
2517
- } else if (type === "spatialmagnify" /* spatialmagnify */) {
2518
- const evt = createSpatialEvent(
2519
- "spatialmagnify" /* spatialmagnify */,
2520
- data.detail
2521
- );
2522
- this.dispatchEvent(evt);
2523
- } else if (type === "spatialmagnifyend" /* spatialmagnifyend */) {
2524
- const evt = createSpatialEvent(
2525
- "spatialmagnifyend" /* spatialmagnifyend */,
2526
- data.detail
2527
- );
2528
- this.dispatchEvent(evt);
2529
- }
2530
- };
2531
- dispatchEvent(evt) {
2532
- if (!evt.__origin) {
2533
- Object.defineProperty(evt, "__origin", { value: this, enumerable: false });
2534
- }
2535
- this.events[evt.type]?.(evt);
2536
- if (evt.bubbles && !evt.cancelBubble) {
2537
- if (this.parent && this.parent instanceof _SpatialEntity) {
2538
- this.parent.dispatchEvent(evt);
2539
- }
2540
- }
2541
- }
2542
- onDestroy() {
2543
- SpatialWebEvent.removeEventReceiver(this.id);
2544
- this.children.forEach((child) => {
2545
- child.parent = null;
2546
- });
2547
- this.children = [];
2548
- if (this.parent) {
2549
- this.parent.children = this.parent.children.filter(
2550
- (child) => child.id !== this.id
2551
- );
2552
- this.parent = null;
2553
- }
2554
- }
2555
- // onUpdate(properties: SpatialEntityProperties) {
2556
- // this.position = properties.position
2557
- // this.rotation = properties.rotation
2558
- // this.scale = properties.scale
2559
- // }
2560
- async convertFromEntityToEntity(fromEntityId, toEntityId, position) {
2561
- return new ConvertFromEntityToEntityCommand(
2562
- fromEntityId,
2563
- toEntityId,
2564
- position
2565
- ).execute();
2566
- }
2567
- async convertFromEntityToScene(fromEntityId, position) {
2568
- return new ConvertFromEntityToSceneCommand(fromEntityId, position).execute();
2569
- }
2570
- async convertFromSceneToEntity(entityId, position) {
2571
- return new ConvertFromSceneToEntityCommand(entityId, position).execute();
2572
- }
2573
- };
2574
-
2575
- // src/reality/entity/SpatialModelEntity.ts
2576
- var SpatialModelEntity = class extends SpatialEntity {
2577
- constructor(id, options, userData) {
2578
- super(id, userData);
2579
- this.id = id;
2580
- this.options = options;
2581
- this.userData = userData;
2582
- }
2583
- };
2584
-
2585
- // src/reality/component/SpatialComponent.ts
2586
- init_SpatialWebEvent();
2587
- var SpatialComponent = class extends SpatialObject {
2588
- constructor(id) {
2589
- super(id);
2590
- SpatialWebEvent.addEventReceiver(id, this.onReceiveEvent);
2591
- }
2592
- onReceiveEvent = (data) => {
2593
- const { type } = data;
2594
- if (type === "objectdestroy" /* objectdestroy */) {
2595
- this.isDestroyed = true;
2596
- }
2597
- };
2598
- };
2599
-
2600
- // src/reality/component/ModelComponent.ts
2601
- var ModelComponent = class extends SpatialComponent {
2602
- constructor(id, options) {
2603
- super(id);
2604
- this.options = options;
2605
- }
2606
- };
2607
-
2608
- // src/reality/material/SpatialMaterial.ts
2609
- var SpatialMaterial = class extends SpatialObject {
2610
- constructor(id, type) {
2611
- super(id);
2612
- this.id = id;
2613
- this.type = type;
2614
- this.type = type;
2615
- }
2616
- };
2617
-
2618
- // src/reality/material/SpatialUnlitMaterial.ts
2619
- var SpatialUnlitMaterial = class extends SpatialMaterial {
2620
- constructor(id, options) {
2621
- super(id, "unlit");
2622
- this.id = id;
2623
- this.options = options;
2624
- }
2625
- updateProperties(properties) {
2626
- return new UpdateUnlitMaterialProperties(this, properties).execute();
2627
- }
2628
- };
2629
-
2630
- // src/reality/resource/SpatialModelAsset.ts
2631
- var SpatialModelAsset = class extends SpatialObject {
2632
- constructor(id, options) {
2633
- super(id);
2634
- this.id = id;
2635
- this.options = options;
2636
- }
2637
- };
2638
-
2639
- // src/reality/realityCreator.ts
2640
- async function createSpatialEntity(userData) {
2641
- const result = await new CreateSpatialEntityCommand(userData?.name).execute();
2642
- if (!result.success) {
2643
- throw new Error("createSpatialEntity failed:" + result?.errorMessage);
2644
- } else {
2645
- const { id } = result.data;
2646
- return new SpatialEntity(id, userData);
2647
- }
2648
- }
2649
- async function createSpatialGeometry(ctor, options) {
2650
- const result = await new CreateSpatialGeometryCommand(
2651
- ctor.type,
2652
- options
2653
- ).execute();
2654
- if (!result.success) {
2655
- throw new Error("createSpatialGeometry failed:" + result?.errorMessage);
2656
- } else {
2657
- const { id } = result.data;
2658
- return new ctor(id, options);
2659
- }
2660
- }
2661
- async function createSpatialUnlitMaterial(options) {
2662
- const result = await new CreateSpatialUnlitMaterialCommand(options).execute();
2663
- if (!result.success) {
2664
- throw new Error("createSpatialUnlitMaterial failed:" + result?.errorMessage);
2665
- } else {
2666
- const { id } = result.data;
2667
- return new SpatialUnlitMaterial(id, options);
2668
- }
2669
- }
2670
- async function createModelComponent(options) {
2671
- const result = await new CreateModelComponentCommand(options).execute();
2672
- if (!result.success) {
2673
- throw new Error("createModelComponent failed:" + result?.errorMessage);
2674
- } else {
2675
- const { id } = result.data;
2676
- return new ModelComponent(id, options);
2677
- }
2678
- }
2679
- async function createSpatialModelEntity(options, userData) {
2680
- const result = await new CreateSpatialModelEntityCommand(options).execute();
2681
- if (!result.success) {
2682
- throw new Error("createSpatialModelEntity failed:" + result?.errorMessage);
2683
- } else {
2684
- const { id } = result.data;
2685
- return new SpatialModelEntity(id, options, userData);
2686
- }
2687
- }
2688
- async function createModelAsset(options) {
2689
- const result = await new CreateModelAssetCommand(options).execute();
2690
- if (!result.success) {
2691
- throw new Error("createModelAsset failed:" + result?.errorMessage);
2692
- } else {
2693
- const { id } = result.data;
2694
- return new SpatialModelAsset(id, options);
2695
- }
2696
- }
2697
-
2698
- // src/reality/geometry/SpatialGeometry.ts
2699
- var SpatialGeometry = class extends SpatialObject {
2700
- constructor(id, options) {
2701
- super(id);
2702
- this.id = id;
2703
- this.options = options;
2704
- }
2705
- static type;
2706
- };
2707
-
2708
- // src/reality/geometry/SpatialBoxGeometry.ts
2709
- var SpatialBoxGeometry = class extends SpatialGeometry {
2710
- constructor(id, options) {
2711
- super(id, options);
2712
- this.id = id;
2713
- this.options = options;
2714
- }
2715
- static type = "BoxGeometry";
2716
- };
2717
-
2718
- // src/reality/geometry/SpatialSphereGeometry.ts
2719
- var SpatialSphereGeometry = class extends SpatialGeometry {
2720
- constructor(id, options) {
2721
- super(id, options);
2722
- this.id = id;
2723
- this.options = options;
2724
- }
2725
- static type = "SphereGeometry";
2726
- };
2727
-
2728
- // src/reality/geometry/SpatialCylinderGeometry.ts
2729
- var SpatialCylinderGeometry = class extends SpatialGeometry {
2730
- constructor(id, options) {
2731
- super(id, options);
2732
- this.id = id;
2733
- this.options = options;
2734
- }
2735
- static type = "CylinderGeometry";
2736
- };
2737
-
2738
- // src/reality/geometry/SpatialPlaneGeometry.ts
2739
- var SpatialPlaneGeometry = class extends SpatialGeometry {
2740
- constructor(id, options) {
2741
- super(id, options);
2742
- this.id = id;
2743
- this.options = options;
2744
- }
2745
- static type = "PlaneGeometry";
2746
- };
2747
-
2748
- // src/reality/geometry/SpatialConeGeometry.ts
2749
- var SpatialConeGeometry = class extends SpatialGeometry {
2750
- constructor(id, options) {
2751
- super(id, options);
2752
- this.id = id;
2753
- this.options = options;
2754
- }
2755
- static type = "ConeGeometry";
2756
- };
2757
-
2758
- // src/SpatialSession.ts
2759
- var SpatialSession = class {
2760
- /**
2761
- * Gets the singleton instance of the spatial scene.
2762
- * The spatial scene is the root container for all spatial elements.
2763
- * @returns The SpatialScene singleton instance
2764
- */
2765
- getSpatialScene() {
2766
- return SpatialScene.getInstance();
2767
- }
2768
- /**
2769
- * Creates a new 2D element that can be spatialized in the 3D environment.
2770
- * 2D elements represent HTML content that can be positioned in 3D space.
2771
- * @returns Promise resolving to a new Spatialized2DElement instance
2772
- */
2773
- createSpatialized2DElement() {
2774
- return createSpatialized2DElement();
2775
- }
2776
- /**
2777
- * Creates a new static 3D element with an optional model URL.
2778
- * Static 3D elements represent pre-built 3D models that can be loaded from a URL.
2779
- * @param modelURL Optional URL to the 3D model to load
2780
- * @returns Promise resolving to a new SpatializedStatic3DElement instance
2781
- */
2782
- createSpatializedStatic3DElement(modelURL) {
2783
- return createSpatializedStatic3DElement(modelURL);
2784
- }
2785
- /**
2786
- * Initializes the spatial scene with custom configuration.
2787
- * This is a reference to the initScene function from scene-polyfill.
2788
- */
2789
- initScene = initScene;
2790
- /**
2791
- * Creates a new dynamic 3D element that can be manipulated at runtime.
2792
- * Dynamic 3D elements allow for programmatic creation and modification of 3D content.
2793
- * @returns Promise resolving to a new SpatializedDynamic3DElement instance
2794
- */
2795
- createSpatializedDynamic3DElement() {
2796
- return createSpatializedDynamic3DElement();
2797
- }
2798
- /**
2799
- * Creates a new spatial entity with an optional name.
2800
- * Entities are the basic building blocks for creating custom 3D content.
2801
- * @param name Optional name for the entity
2802
- * @returns Promise resolving to a new SpatialEntity instance
2803
- */
2804
- createEntity(userData) {
2805
- return createSpatialEntity(userData);
2806
- }
2807
- /**
2808
- * Creates a box geometry with optional configuration.
2809
- * @param options Configuration options for the box geometry
2810
- * @returns Promise resolving to a new SpatialBoxGeometry instance
2811
- */
2812
- createBoxGeometry(options = {}) {
2813
- return createSpatialGeometry(SpatialBoxGeometry, options);
2814
- }
2815
- /**
2816
- * Creates a plane geometry with optional configuration.
2817
- * @param options Configuration options for the plane geometry
2818
- * @returns Promise resolving to a new SpatialPlaneGeometry instance
2819
- */
2820
- createPlaneGeometry(options = {}) {
2821
- return createSpatialGeometry(SpatialPlaneGeometry, options);
2822
- }
2823
- /**
2824
- * Creates a sphere geometry with optional configuration.
2825
- * @param options Configuration options for the sphere geometry
2826
- * @returns Promise resolving to a new SpatialSphereGeometry instance
2827
- */
2828
- createSphereGeometry(options = {}) {
2829
- return createSpatialGeometry(SpatialSphereGeometry, options);
2830
- }
2831
- /**
2832
- * Creates a cone geometry with the specified configuration.
2833
- * @param options Configuration options for the cone geometry
2834
- * @returns Promise resolving to a new SpatialConeGeometry instance
2835
- */
2836
- createConeGeometry(options) {
2837
- return createSpatialGeometry(SpatialConeGeometry, options);
2838
- }
2839
- /**
2840
- * Creates a cylinder geometry with the specified configuration.
2841
- * @param options Configuration options for the cylinder geometry
2842
- * @returns Promise resolving to a new SpatialCylinderGeometry instance
2843
- */
2844
- createCylinderGeometry(options) {
2845
- return createSpatialGeometry(SpatialCylinderGeometry, options);
2846
- }
2847
- /**
2848
- * Creates a model component with the specified configuration.
2849
- * Model components are used to add 3D model rendering capabilities to entities.
2850
- * @param options Configuration options for the model component
2851
- * @returns Promise resolving to a new ModelComponent instance
2852
- */
2853
- createModelComponent(options) {
2854
- return createModelComponent(options);
2855
- }
2856
- /**
2857
- * Creates an unlit material with the specified configuration.
2858
- * Unlit materials don't respond to lighting in the scene.
2859
- * @param options Configuration options for the unlit material
2860
- * @returns Promise resolving to a new SpatialUnlitMaterial instance
2861
- */
2862
- createUnlitMaterial(options) {
2863
- return createSpatialUnlitMaterial(options);
2864
- }
2865
- /**
2866
- * Creates a model asset with the specified configuration.
2867
- * Model assets represent 3D model resources that can be used by entities.
2868
- * @param options Configuration options for the model asset
2869
- * @returns Promise resolving to a new SpatialModelAsset instance
2870
- */
2871
- createModelAsset(options) {
2872
- return createModelAsset(options);
2873
- }
2874
- /**
2875
- * Creates a spatial model entity with the specified configuration.
2876
- * This is a convenience method for creating an entity with a model component.
2877
- * @param options Configuration options for the spatial model entity
2878
- * @returns Promise resolving to a new SpatialModelEntity instance
2879
- */
2880
- createSpatialModelEntity(options, userData) {
2881
- return createSpatialModelEntity(options, userData);
2882
- }
2883
- /**
2884
- * Creates an attachment entity that renders 2D HTML content as a child
2885
- * of a 3D entity in the scene graph.
2886
- * @param options Configuration options including parent entity ID, position, and size
2887
- * @returns Promise resolving to a new Attachment instance
2888
- */
2889
- createAttachmentEntity(options) {
2890
- return createAttachmentEntity(options);
2891
- }
2892
- };
2893
-
2894
- // src/Spatial.ts
2895
- init_SpatialWebEvent();
2896
- var Spatial = class {
2897
- /**
2898
- * Requests a spatial session object from the browser.
2899
- * This is the primary method to initialize spatial functionality.
2900
- * @returns The SpatialSession instance or null if not available in the current browser
2901
- * [TODO] discuss implications of this not being async
2902
- */
2903
- requestSession() {
2904
- if (this.runInSpatialWeb()) {
2905
- SpatialWebEvent.init();
2906
- return new SpatialSession();
2907
- } else {
2908
- return null;
2909
- }
2910
- }
2911
- /**
2912
- * Checks if the current page is running in a spatial web environment.
2913
- * This method detects if the application is running in a WebSpatial-compatible browser.
2914
- * @returns True if running in a spatial web environment, false otherwise
2915
- */
2916
- runInSpatialWeb() {
2917
- if (navigator.userAgent.indexOf("WebSpatial/") > 0) {
2918
- return true;
2919
- }
2920
- return false;
2921
- }
2922
- /** @deprecated
2923
- * Checks if WebSpatial is supported in the current environment.
2924
- * Verifies compatibility between native and client versions.
2925
- * @returns True if web spatial is supported by this webpage
2926
- */
2927
- isSupported() {
2928
- return true;
2929
- }
2930
- /** @deprecated
2931
- * Gets the native WebSpatial version from the browser environment.
2932
- * The version format follows semantic versioning (x.x.x).
2933
- * @returns Native version string in format "x.x.x"
2934
- */
2935
- getNativeVersion() {
2936
- if (window.__WebSpatialData && window.__WebSpatialData.getNativeVersion) {
2937
- return window.__WebSpatialData.getNativeVersion();
2938
- }
2939
- return window.WebSpatailNativeVersion === "PACKAGE_VERSION" ? this.getClientVersion() : window.WebSpatailNativeVersion;
2940
- }
2941
- /** @deprecated
2942
- * Gets the client SDK version.
2943
- * The version format follows semantic versioning (x.x.x).
2944
- * @returns Client SDK version string in format "x.x.x"
2945
- */
2946
- getClientVersion() {
2947
- return "1.2.1";
2948
- }
2949
- };
2950
-
2951
- // src/spatial-window-polyfill.ts
2952
- var spatial = new Spatial();
2953
- var session = void 0;
2954
- var SpatialGlobalCustomVars = {
2955
- backgroundMaterial: "--xr-background-material"
2956
- };
2957
- var htmlBackgroundMaterial = "";
2958
- function setCurrentWindowStyle(backgroundMaterial) {
2959
- if (backgroundMaterial !== htmlBackgroundMaterial) {
2960
- session?.getSpatialScene()?.updateSpatialProperties({
2961
- material: backgroundMaterial
2962
- });
2963
- htmlBackgroundMaterial = backgroundMaterial;
2964
- }
2965
- }
2966
- function checkHtmlBackgroundMaterial() {
2967
- const computedStyle = getComputedStyle(document.documentElement);
2968
- const backgroundMaterial = computedStyle.getPropertyValue(
2969
- SpatialGlobalCustomVars.backgroundMaterial
2970
- );
2971
- setCurrentWindowStyle(backgroundMaterial || "none");
2972
- }
2973
- var htmlCornerRadius = {
2974
- topLeading: 0,
2975
- bottomLeading: 0,
2976
- topTrailing: 0,
2977
- bottomTrailing: 0
2978
- };
2979
- function checkCornerRadius() {
2980
- const computedStyle = getComputedStyle(document.documentElement);
2981
- const cornerRadius = parseCornerRadius(computedStyle);
2982
- setCornerRadius(cornerRadius);
2983
- }
2984
- function setCornerRadius(cornerRadius) {
2985
- if (htmlCornerRadius.topLeading !== cornerRadius.topLeading || htmlCornerRadius.bottomLeading !== cornerRadius.bottomLeading || htmlCornerRadius.topTrailing !== cornerRadius.topTrailing || htmlCornerRadius.bottomTrailing !== cornerRadius.bottomTrailing) {
2986
- session?.getSpatialScene()?.updateSpatialProperties({
2987
- cornerRadius
2988
- });
2989
- htmlCornerRadius.topLeading = cornerRadius.topLeading;
2990
- htmlCornerRadius.bottomLeading = cornerRadius.bottomLeading;
2991
- htmlCornerRadius.topTrailing = cornerRadius.topTrailing;
2992
- htmlCornerRadius.bottomTrailing = cornerRadius.bottomTrailing;
2993
- }
2994
- }
2995
- function setOpacity(opacity) {
2996
- session?.getSpatialScene().updateSpatialProperties({
2997
- opacity
2998
- });
2999
- }
3000
- function checkOpacity() {
3001
- const computedStyle = getComputedStyle(document.documentElement);
3002
- const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
3003
- setOpacity(opacity);
3004
- }
3005
- function hijackDocumentElementStyle() {
3006
- const rawDocumentStyle = document.documentElement.style;
3007
- const styleProxy = new Proxy(rawDocumentStyle, {
3008
- set: function(target, key, value) {
3009
- const ret = Reflect.set(target, key, value);
3010
- if (key === SpatialGlobalCustomVars.backgroundMaterial) {
3011
- setCurrentWindowStyle(value);
3012
- }
3013
- if (key === "border-radius" || key === "borderRadius" || key === "border-top-left-radius" || key === "borderTopLeftRadius" || key === "border-top-right-radius" || key === "borderTopRightRadius" || key === "border-bottom-left-radius" || key === "borderBottomLeftRadius" || key === "border-bottom-right-radius" || key === "borderBottomRightRadius") {
3014
- checkCornerRadius();
3015
- }
3016
- if (key === "opacity") {
3017
- checkOpacity();
3018
- }
3019
- return ret;
3020
- },
3021
- get: function(target, prop) {
3022
- if (typeof target[prop] === "function") {
3023
- return function(...args) {
3024
- if (prop === "setProperty") {
3025
- const [property, value] = args;
3026
- if (property === SpatialGlobalCustomVars.backgroundMaterial) {
3027
- setCurrentWindowStyle(value);
3028
- }
3029
- } else if (prop === "removeProperty") {
3030
- const [property] = args;
3031
- if (property === SpatialGlobalCustomVars.backgroundMaterial) {
3032
- setCurrentWindowStyle("none");
3033
- }
3034
- }
3035
- return target[prop](
3036
- ...args
3037
- );
3038
- };
3039
- }
3040
- return Reflect.get(target, prop);
3041
- }
3042
- });
3043
- Object.defineProperty(document.documentElement, "style", {
3044
- get: function() {
3045
- return styleProxy;
3046
- }
3047
- });
3048
- }
3049
- function monitorExternalStyleChange() {
3050
- const headObserver = new MutationObserver(checkCSSProperties);
3051
- headObserver.observe(document.head, { childList: true, subtree: true });
3052
- }
3053
- function checkCSSProperties() {
3054
- checkHtmlBackgroundMaterial();
3055
- checkCornerRadius();
3056
- checkOpacity();
3057
- }
3058
- function monitorHTMLAttributeChange() {
3059
- const observer = new MutationObserver((mutations) => {
3060
- mutations.forEach((mutation) => {
3061
- if (mutation.type === "attributes" && mutation.attributeName) {
3062
- checkCSSProperties();
3063
- }
3064
- });
3065
- });
3066
- observer.observe(document.documentElement, {
3067
- attributes: true,
3068
- attributeFilter: ["style", "class"]
3069
- });
3070
- }
3071
- async function spatialWindowPolyfill() {
3072
- if (!spatial.runInSpatialWeb()) {
3073
- return;
3074
- }
3075
- session = await spatial.requestSession();
3076
- if (document.readyState === "complete") {
3077
- checkCSSProperties();
3078
- } else {
3079
- window.addEventListener("load", () => {
3080
- checkCSSProperties();
3081
- });
3082
- }
3083
- hijackDocumentElementStyle();
3084
- monitorExternalStyleChange();
3085
- monitorHTMLAttributeChange();
3086
- }
3087
-
3088
- // src/index.ts
3089
- if (!isSSREnv() && navigator.userAgent.indexOf("WebSpatial/") > 0) {
3090
- injectSceneHook();
3091
- spatialWindowPolyfill();
3092
- }
3093
- export {
3094
- Attachment,
3095
- BaseplateVisibilityValues,
3096
- CubeInfo,
3097
- ModelComponent,
3098
- Spatial,
3099
- SpatialBoxGeometry,
3100
- SpatialComponent,
3101
- SpatialConeGeometry,
3102
- SpatialCylinderGeometry,
3103
- SpatialEntity,
3104
- SpatialGeometry,
3105
- SpatialMaterial,
3106
- SpatialModelAsset,
3107
- SpatialModelEntity,
3108
- SpatialObject,
3109
- SpatialPlaneGeometry,
3110
- SpatialScene,
3111
- SpatialSceneState,
3112
- SpatialSceneValues,
3113
- SpatialSession,
3114
- SpatialSphereGeometry,
3115
- SpatialUnlitMaterial,
3116
- Spatialized2DElement,
3117
- SpatializedDynamic3DElement,
3118
- SpatializedElement,
3119
- SpatializedElementType,
3120
- SpatializedStatic3DElement,
3121
- WorldAlignmentValues,
3122
- WorldScalingValues,
3123
- createAttachmentEntity,
3124
- isSSREnv,
3125
- isValidBaseplateVisibilityType,
3126
- isValidSceneUnit,
3127
- isValidSpatialSceneType,
3128
- isValidWorldAlignmentType,
3129
- isValidWorldScalingType
3130
- };
3131
- //# sourceMappingURL=index.js.map