getHeightAtCoordinates no longer works in latest bjs v5 and v6

It can be reproduced in the following PG. With version 4.2.1, a correct height value is returned. But with bjs version set to v5 and v6, the function returned NaN.

I’ve tried adding a 1sec timeout in the scene.onReadyObservable function and it seems to collect the information correctly.
The scene is initialized prior to loading the “CreateGroundFromHeightMap” function.

Here you´ve two options:

    // Check if Ground is Ready
    ground.onMeshReadyObservable.add(() =>{ 
        console.log(ground.getHeightAtCoordinates(0,0));
        var height = ground.getHeightAtCoordinates(0,0);
            alert("OnReady Alert: " + height);
    });

    // Adding a Timeout
    scene.onReadyObservable.add(() => {
        setTimeout(()=> {
            console.log(ground.getHeightAtCoordinates(0,0));
            var height = ground.getHeightAtCoordinates(0,0);
            alert("Timeout Alert: " + height);
        }, 100);
    });

Hope it helps

Thanks @DRLeria

But does it implies the onReadyObservable behavior is changed in the newer versions? It used to be called when GroundMesh is ready in v4. But now it is called before its ready.

The other interesting thing is if I add a Box mesh, onReadyObservable seems to be called after both meshes are ready and CreateGroundFromHeightMap can return the correct value.

I couldn’t tell you honestly.

In any case, you can always obtain the information once you have loaded the HeightMap with its corresponding texture.

You have to use the onReady callback of CreateGroundFromHeightMap to make sure that the mesh is ready to be queried (it is a parameter of the options object).

If scene.onReadyObservable works in 4.2 and not in 5/6, it’s only by luck (or bad luck!) :slight_smile:

2 Likes

Exactly
The case would be similar when importing an external file (glb, gltf…).

Using the onReady callback to detect if the HeightMap has been loaded correctly is the best option.

Maybe it is better to clarify in the documentation for onReadyObservable.

Something like: It does not guarantee meshes are ready if these meshes involve asynchronous asset loading?