Unable To Create Physics Aggregate on Ground Map while using offscreen canvas: Solved

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

Welcome to the community!

CreateGroundFromHeightMap is not guaranteed to finish building the vertex data on the same frame, so I think that’s what might be causing the “no valid mesh” error. Can you try doing the aggregate creation like this?

// Ground
  const ground = BABYLON.MeshBuilder.CreateGroundFromHeightMap("heightmap", '../resources/img.png', {
    width: 1024,
    height: 1024,
    minHeight: 0,
    maxHeight: 256,
    subdivisions: 256
    onReady: (groundMesh) => {
       const groundAggregate = new BABYLON.PhysicsAggregate(groundMesh, BABYLON.PhysicsShapeType.MESH, { mass: 0 }, scene);
    }
  }, scene);
1 Like

Thanks for looking into this Carol.
I think there’s supposed to be a comma after

subdivisions: 256

If not I’m getting unexpected identifier ‘onReady’

After adding a comma I’m getting the same error as before.

PhysicsAggregate._addSizeOptions (physicsAggregate.ts:237:1)
new PhysicsAggregate (physicsAggregate.ts:149:1)

Correction:
I implemented that possible fix into the wrong place. Doing additional testing now as that seems to have been the fix.