I seem to have found a weird interaction when using havok with an offscreen canvas.
This following code works properly in both playground and when running on the main thread:
async function testScene(engine) {
const scene = new BABYLON.Scene(engine);
const havokInstance = await HavokPhysics({ locateFile: () => 'vendors/HavokPhysics.wasm'});
const hk = new BABYLON.HavokPlugin(true, havokInstance);
scene.enablePhysics(new BABYLON.Vector3(0, -9.8, 0), hk);
const camera = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, 0, 0), scene);
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter: 2, segments: 32 }, scene);
sphere.position.y = 200;
// Ground
const ground = BABYLON.MeshBuilder.CreateGroundFromHeightMap("heightmap", '../resources/img.png', {
width: 1024,
height: 1024,
minHeight: 0,
maxHeight: 256,
subdivisions: 256
}, scene);
//Create Aggregates
const sphereAggregate = new BABYLON.PhysicsAggregate(sphere, BABYLON.PhysicsShapeType.SPHERE, { mass: 10, restitution: 0.1 }, scene);
const groundAggregate = new BABYLON.PhysicsAggregate(ground, BABYLON.PhysicsShapeType.MESH, { mass: 0 }, scene);
return scene;
}
However, when I try running it in a webworker I get “No valid mesh was provided for mesh or convex hull ship parameter. Please provide a mesh with a valid geometry (number of vertices greater than 0).”
If I comment out the line creating the ground aggregate it works fine and I can I see the mesh and sphere. This also indicates that the sphere aggregate is being created properly.
Edit:
Babylon V6.34.1
Chromium v144.0.5735.289
Troubleshooting List:
Updated to Babylon V6.36.1 No Change
Tested a different heightmap image. No Change
Solution:
Delay adding physics aggregate until Mesh has been fully built using onReady per carol’s solution