Sending certain objects from a WebWorker
scope/thread back to the main scope/thread is not allowed. More information on this can be found here:
aMessage
To deliver an object to the worker, it should be in the data field in the event received by the DedicatedWorkerGlobalScope.onmessage handler. This object can be any value or JavaScript object handled by the structured clone algorithm, even those with cyclical references.
transferList (Optional)
An optional array of Transferable objects that transfers ownership. Once transferred, the object becomes unusable (neutered) where it originated and can only be accessed by the receiving worker.
Only MessagePort and ArrayBuffer objects are transferable. ·Null` is not a valid value for transferList.
Read more about the structured clone algorithm here:
In the section on Things that don't work with structured clones, you'll find the explanation for your error.
I encountered a similar issue and decided against using web workers. If you have a workaround, I'd love to hear about it.
Note: One potential solution is creating a THREE.BufferGeometry
and then sending the buffer attributes as transferable objects back to the main thread. It's technically feasible but requires custom code to implement.
UPDATE
Responding to your comment, here's an update:
If you convert a THREE.Geometry
to a THREE.BufferGeometry
using the fromGeometry
method, you cannot directly send the buffer geometry as transferable. However, you can send the buffer arrays from the buffer attributes (THREE.BufferAttribute
) as transferables and reconstruct the buffer attributes and geometry in the main thread using the buffer arrays received from your worker.
Although untested, this approach should be theoretically possible. The performance improvement remains uncertain, but it's worth experimenting with.
var bufferGeometry = THREE.BufferGeometry().fromGeometry( geometry );
// Access buffer attributes holding buffer arrays.
// Collect them by name (ie. color, normal, position, etc )
var attribute = bufferGeometry.getAttribute( name );
var bufferArray = attribute.array;
// Index attribute access:
var indexAttribute = bufferGeometry.getIndex();
var indexArray = indexAttribute.array;