threeToCannon web worker question

Hi, I want to use web worker,I use three-to-cannon threeToCannon(mesh, { type: ShapeType.Mesh });,
it’s return shape. I send postMessage({shape:shape,position:position,quaternion:quaternion}) to worker.js. worker.js code is let physBox = new CANNON.Body({ mass: 0, position:position, quaternion:quaternion, shape: shape, });
It’s catch error “Uncaught TypeError: shape.calculateWorldAABB is not a function”
Could you help me? Thanks a lot!

Note that the Web Worker postMessage API can transfer only simple value types, not classes, functions, or instances. See The structured clone algorithm - Web APIs | MDN. To transfer a shape, vector, or quaternion, you’ll need to first serialize it into some other form.

You can find some example serialization code for cannon.js objects here, if it’s helpful: aframe-physics-system/protocol.js at 59100ac83033cf337d26925de1f92854c2988b75 · n5ro/aframe-physics-system · GitHub.

Thanks a lot! I will try it.

@neciszhang have you solve it? i have the same problem

just want to plug this in here but we released our cannon-es worker api for vanilla use use-cannon/packages/cannon-worker-api at master · pmndrs/use-cannon · GitHub this comes from pmndrs, the same collective that maintains cannon itself.

1 Like