-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
192 lines (172 loc) · 5.22 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as THREE from 'three';
// import easing from './easing.js';
import metaversefile from 'metaversefile';
const {useApp, useFrame, useWorld, useActivate, useLoaders, usePhysics, addTrackedApp, useDefaultModules, useCleanup} = metaversefile;
const baseUrl = import.meta.url.replace(/(\/)[^\/\\]*$/, '$1');
const localVector = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const fruitFileNames = [
'Egg_Fruit_dream.glb',
'Lavender_Berry_dream.glb',
'Long_Apple_dream.glb',
'Red_Shroom_dream.glb',
'Slime_Fruit_dream.glb',
'Squid_Squash_dream.glb',
];
export default () => {
const app = useApp();
const world = useWorld();
const physics = usePhysics();
app.name = 'infinifruit';
const _loadFruit = async fileName => {
const app = await world.appManager.addTrackedApp(
`${baseUrl}fruit/`,
undefined, // position = new THREE.Vector3(),
undefined, // quaternion = new THREE.Quaternion(),
undefined, // scale = new THREE.Vector3(1, 1, 1),
[
{
key: 'fileName',
value: fileName,
},
],
);
return app;
/* const m = await metaversefile.import(`${baseUrl}fruit/`);
const app = metaversefile.createApp();
app.setComponent('fileName', fileName);
await metaversefile.addModule(app, m);
return app; */
// return await metaversefile.load(u);
};
const _loadGlb = async fileName => {
const u = `${baseUrl}${fileName}`;
let o = await new Promise((accept, reject) => {
const {gltfLoader} = useLoaders();
gltfLoader.load(u, accept, function onprogress() {}, reject);
});
o = o.scene;
// window.fruit = o;
return o;
};
const subApps = [];
let physicsIds = [];
(async () => {
const emptyFruitFileNames = [];
const os = await Promise.all(emptyFruitFileNames.map(fruitFileName => _loadFruit(fruitFileName))
.concat([
/* (async () => {
const u = `${baseUrl}fruit.glb`;
let o = await new Promise((accept, reject) => {
const {gltfLoader} = useLoaders();
gltfLoader.load(u, accept, function onprogress() {}, reject);
});
window.fruit = o;
return o;
})(), */
_loadGlb('plant.glb'),
_loadGlb('plant2.glb'),
])
);
const [
// eggFruit,
// lavenderBerry,
// longApple,
// redShroom,
// slimeFruit,
// squidSquash,
plant,
plant2,
] = os;
const fruits = [
// eggFruit,
// lavenderBerry,
// longApple,
// redShroom,
// slimeFruit,
// squidSquash,
];
/* for (const fruit of fruits) {
fruit.scale.multiplyScalar(0.2);
} */
plant.scale.multiplyScalar(5);
plant2.scale.multiplyScalar(0.02);
app.add(plant);
app.add(plant2);
// app.add(longApple);
/* for (const o of os) {
app.add(o);
} */
app.updateMatrixWorld();
subApps.push(...fruits);
// window.fruits = fruits;
/* // visibility
for (const f of fruits) {
f.visible = false;
}
eggFruit.visible = true; */
/* activateCb = e => {
console.log('activate infinifruit', e);
}; */
let loadSpec = null;
frameCb = (timestamp, timeDiff) => {
/* // console.log('use frame', timestamp, timeDiff);
localQuaternion.setFromAxisAngle(localVector.set(0, 0, 1), Math.sin(timestamp / 1000) * 0.1);
for (const o of os) {
o.quaternion.copy(localQuaternion);
} */
if (subApps.length === 0 && !loadSpec) {
(async () => {
loadSpec = {
startTime: null,
endTime: null,
};
const fruitFileName = fruitFileNames[Math.floor(Math.random() * fruitFileNames.length)];
const fruit = await _loadFruit(fruitFileName);
loadSpec.startTime = performance.now();
loadSpec.endTime = loadSpec.startTime + 1000;
subApps.push(fruit);
const _clearApps = () => {
subApps.splice(subApps.indexOf(fruit), 1);
fruit.removeEventListener('activate', _clearApps);
fruit.removeEventListener('grabupdate', _clearApps);
};
fruit.addEventListener('activate', _clearApps);
fruit.addEventListener('grabupdate', e => {
if (e.grab) {
_clearApps();
}
});
// loadSpec = null;
})();
}
for (const subApp of subApps) {
subApp.position.copy(app.position);
subApp.quaternion.copy(app.quaternion);
if (loadSpec && loadSpec.startTime !== null) {
const f = (timestamp - loadSpec.startTime) / (loadSpec.endTime - loadSpec.startTime);
if (f < 1) {
subApp.scale.setScalar(f);
} else {
subApp.scale.setScalar(1);
loadSpec = null;
}
} else {
subApp.scale.setScalar(1);
}
subApp.updateMatrixWorld();
subApp.visible = subApp.scale.x > 0;
}
};
})();
let frameCb = null;
useFrame(({timestamp, timeDiff}) => {
frameCb && frameCb(timestamp, timeDiff);
});
useCleanup(() => {
for (const physicsId of physicsIds) {
physics.removeGeometry(physicsId);
}
});
return app;
};