From what I gather, the issue is as follows:
- You are looking to load a file as a geometry (such as obj or stl).
- You aim to load this file within a WebWorker.
- Once loaded, you wish to send the geometry back to the main script.
- To achieve this, you are considering sending the file back as JSON since direct object transfer is not feasible.
- Following this, you plan to convert the JSON data into a geometry on the main thread.
The downside of the above approach is that converting from JSON to a geometry involves another loading operation, similar to what JSONLoader does. In essence, it defeats the purpose of using a separate worker thread.
My suggested method involves loading the file into flat arrays of vertices and normals, which are then sent back to the main thread for incorporation into a BufferGeometry. Additionally, utilizing transferable objects can enhance processing speed.
// worker.js
var vertices = new Float32Array(faces * 3 * 3);
var normals = new Float32Array(faces * 3 * 3);
// Load your file into these arrays.
var message = {
status: 'complete',
vertices: vertices,
normals: normals
};
postMessage(message, [message.vertices.buffer, message.normals.buffer]);
// app.js
onmessage = function (event) {
var vertices = event.data.vertices;
var normals = event.data.normals;
var geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(vertices, 3));
geometry.addAttribute('normal', new THREE.BufferAttribute(normals, 3));
var material = new THREE.MeshPhongMaterial();
var mesh = new THREE.Mesh(geometry, material);
// Perform further actions as needed.
};