First of all - I'm a beginner in three.js and I just want to share my experience.
My task was to convert geometry into something and transfer it to another WebWorker(you cannot transfer all objects between workers, only structured cloneable datatypes. So I opened documentation and tried to use .toJSON and then BufferGeometryLoader(and many other approaches).
const box = new BoxBufferGeometry();
const geomJSON = box.toJSON();
const geometriesLoader = new THREE.BufferGeometryLoader();
const parsedGeom = geometriesLoader.parse(geomJSON);
const mesh = new Mesh(parsedGeom);
this.scene.add(mesh);
Also, I tried other types of loaders, JSON.stringify
, etc. But only packing meshes with geometries in the GLTF file and serializing it using callback seems to work(not very handy).
I asked holy Internet for help but he didn't hear me. And documentation says only:
.toJSON () : Object
Convert the buffer geometry to three.js JSON Object/Scene format.
Three.js version: 0.135.0 and 0.136.0
So, I thought I will die, but then I accidentally found that function: .toNonIndexed()
.
const box = new BoxBufferGeometry();
const geomJSON = box.toNonIndexed().toJSON();
const geometriesLoader = new THREE.BufferGeometryLoader();
const parsedGeom = geometriesLoader.parse(geomJSON);
const mesh = new Mesh(parsedGeom);
this.scene.add(mesh);
I am not sure what it does, but at least it works now, and I can see my geometry after serialization. I am not sure that it doesn't cause other problems though.
If you are more experienced in three.js, then please, give me and other noobs more information about this issue and the purpose of .toJSON
. Thanks.