Ground getHeightAtCoordinates returns NaN values when subdivisions is != 100

Using the Skybox example, I tried to change the value of the subdivision, to check the results. Unfortunately, whenever the value is different from 100, the method getHeightAtCoordinates from the ground sometimes returns NaN values.

var ground = BABYLON.Mesh.CreateGroundFromHeightMap(“ground”, “textures/ground/heightMap.png”, 200, 200, 100, 0,10, scene, false);

I checked out this Topic but the solution is not in the post and the link they refer is not working.

Any Idea why I get this NaN values and how to solve it?

Apparently when you call getHeightAtCoordinates before the terrain geometry has been created, it doesn’t initialize properly, so you should wait for the scene to be ready, or you can call updateCoordinateHeights before calling getHeightAtCoordinates

1 Like

Hi @Gijs,

Yes, I’m assuring that the ground is loaded, I’m doing this inside the ground.onReady = function () {}.
I find a temporal solution, but I do not fully understand the consequences.

In the ground.onReady = function () {} I call getHeightAtCoordinates before I call the ground.optimize(). It was my first line before following the online example.

Solution: Call the optimization function only after you use no longer need getHeightAtCoordinates.

1 Like

I think I found the problem: the optimize function changes the ground’s _subdivisionsX and _subdivisionsY to the provided chunksCount parameter, which is the number of submeshes to create — while those variables actually represent the number of quads the ground is subdivided into.

Could you try if setting _subdivisionsX and _subdivisionsY to their original value after you called the optimize function solved your problem?

Just to clarify, with getHeightAtCoordinates I’m looking for the Y coordinate at a point (x,z) so the mesh I want to place is over the ground.

Then, you want me to set up _subdivisionsX and _subdivisionsY to their original value. Initially, this is only one value: subdivisions. Originally is: subdivitionsValue^-2? or I should get it to save it and set it up back?

I meant the first one, which you provide in the ground constructor, in other code:

let sx = ground._subdivisionsX;
let sy = ground._subdivisionsY;


ground._subdivisionsX = sx;
ground._subdivisionsY = sy;

Unfortunately, this solution is not working. I don’t know if this make sense to you, but when added the code you suggested, the ground was no visible anymore. although the other elements were placed correctly. So maybe it recalculates the surface??

My mistake, the optimize function requires at least one parameter (chuncksCount), in my PG test it works correctly then