-
Notifications
You must be signed in to change notification settings - Fork 19
/
scn.js
158 lines (143 loc) · 4.72 KB
/
scn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as THREE from 'three';
import metaversefile from 'metaversefile';
const {useApp, createApp, createAppAsync, addTrackedApp, removeTrackedApp, useCleanup} = metaversefile;
function typeContentToUrl(type, content) {
if (typeof content === 'object') {
content = JSON.stringify(content);
}
const dataUrlPrefix = 'data:' + type + ',';
return '/@proxy/' + dataUrlPrefix + encodeURIComponent(content).replace(/\%/g, '%25')//.replace(/\\//g, '%2F');
}
function getObjectUrl(object) {
let {start_url, type, content} = object;
let u;
if (start_url) {
// make path relative to the .scn file
u = /^\\.\\//.test(start_url) ? (new URL(import.meta.url).pathname.replace(/(\\/)[^\\/]*$/, '$1') + start_url.replace(/^\\.\\//, '')) : start_url;
} else if (type && content) {
u = typeContentToUrl(type, content);
} else {
throw new Error('invalid scene object: ' + JSON.stringify(object));
}
return u;
}
function mergeComponents(a, b) {
const result = a.map(({
key,
value,
}) => ({
key,
value,
}));
for (let i = 0; i < b.length; i++) {
const bComponent = b[i];
const {key, value} = bComponent;
let aComponent = result.find(c => c.key === key);
if (!aComponent) {
aComponent = {
key,
value,
};
result.push(aComponent);
} else {
aComponent.value = value;
}
}
return result;
}
export default e => {
const app = useApp();
const srcUrl = ${this.srcUrl};
const mode = app.getComponent('mode') ?? 'attached';
const paused = app.getComponent('paused') ?? false;
const objectComponents = app.getComponent('objectComponents') ?? [];
// console.log('scn got mode', app.getComponent('mode'), 'attached');
const loadApp = (() => {
switch (mode) {
case 'detached': {
return async (url, position, quaternion, scale, components) => {
const components2 = {};
for (const {key, value} of components) {
components2[key] = value;
}
for (const {key, value} of objectComponents) {
components2[key] = value;
}
if (components2.mode === undefined) {
components2.mode = 'detached';
}
if (components2.paused === undefined) {
components2.paused = paused;
}
const subApp = await createAppAsync({
start_url: url,
position,
quaternion,
scale,
parent: app,
components: components2,
});
// app.add(subApp);
// console.log('scn app add subapp', app, subApp, subApp.parent);
// subApp.updateMatrixWorld();
app.addEventListener('componentsupdate', e => {
const {keys} = e;
if (keys.includes('paused')) {
const paused = app.getComponent('paused') ?? false;
subApp.setComponent('paused', paused);
}
});
};
}
case 'attached': {
return async (url, position, quaternion, scale, components) => {
components = mergeComponents(components, objectComponents);
await addTrackedApp(url, position, quaternion, scale, components);
};
}
default: {
throw new Error('unknown mode: ' + mode);
}
}
})();
let live = true;
e.waitUntil((async () => {
const res = await fetch(srcUrl);
const j = await res.json();
const {objects} = j;
const buckets = {};
for (const object of objects) {
const lp = object.loadPriority ?? 0;
let a = buckets[lp];
if (!a) {
a = [];
buckets[lp] = a;
}
a.push(object);
}
const sKeys = Object.keys(buckets).sort((a, b) => a - b);
for (let i=0; i<sKeys.length; i++) {
const lp = sKeys[i];
await Promise.all(buckets[lp].map(async object => {
if (live) {
let {position = [0, 0, 0], quaternion = [0, 0, 0, 1], scale = [1, 1, 1], components = []} = object;
position = new THREE.Vector3().fromArray(position);
quaternion = new THREE.Quaternion().fromArray(quaternion);
scale = new THREE.Vector3().fromArray(scale);
const url = getObjectUrl(object);
await loadApp(url, position, quaternion, scale, components);
}
}));
}
})());
useCleanup(() => {
live = false;
});
app.hasSubApps = true;
return true;
};
export const contentId = ${this.contentId};
export const name = ${this.name};
export const description = ${this.description};
export const type = 'scn';
export const components = ${this.components};