im using below code to add entities to the editor, from a local json object, but there are about 10000~ entities, and editor.entities.create and entity.set commands get a lot slower after like 2-3k, starts like 100 entity/sec then gets slower about 5 entity/sec, is there a way to create all entities in one call, or editing scene of editor directly, maybe via some api?
i though about creating a gltf file containing whole scene, upload as a asset then add the template, but couldn’t find a easy way to create gltf file, that could work too i guess
fetch("http://localhost:8080/scene.json")
.then((res) => res.json())
.then(async (/** @type {{position: number[], rotation: number[], mesh: string, parent: string}[]} */ body) => {
/** @type {Record<string, pc.Entity>} */
let entities = { PersistentLevel: editor.entities.create({parent: editor.entities.root}), };
setInterval(() => {
console.log("remaining", Object.keys(body))
}, 2000)
// while loop as scene.json is linear instead of a tree structure
while (Object.keys(body).length !== 0) {
for (let [entity_name, entity_data] of Object.entries(body)) {
if (!entities[entity_data.parent]) continue;
// dont lock up the event loop
await new Promise((resolve) => setTimeout(resolve, 1));
// Create a new Entity
entities[entity_name] = editor.entities.create({ parent: entities[entity_data.parent] });
entities[entity_name].set('name', entity_name.split('.').at(-1));
entities[entity_name].set('position', entity_data.position);
entities[entity_name].set('rotation', entity_data.rotation);
if (entity_data.mesh){
if (!editor.assets.findOne(asset => asset.get('name') === entity_data.mesh.split('.')[0].split('/').at(-1))) {
throw new Error("cant find mesh" + entity_data.mesh)
}
entities[entity_name].addComponent('render', {
type: 'asset',
asset: editor.assets.findOne(asset => asset.get('type') === "render" && asset.get('name') === entity_data.mesh.split('.')[0].split('/').at(-1)).get('id')
});
}
// delete from body so dont create entity again
delete body[entity_name];
}
}
});