Ground height map from UInt8Array corrupted points

I’m experimenting with BABYLON.MeshBuilder.CreateGroundFromHeightMap and I have a question and an issue.


Question: I don’t have clear what minHeight and maxHeight are doing exactly. For what I’ve been testing, those two parameters refer to the mimumn and maximum values the ground will have after being created, regardless the actual height map values I’m sending to CreateGroundFromHeightMap . It will adjust the mesh vertices to that range (0 minimum and 1 maximum by default).

Is that correct?


Issue (more important): Depending on the amplitude of the height points I’m sending to CreateGroundFromHeightMap , if some of them are lower than a threshold, the vertex is corrupted or whatever, and it appears upside into the air.

Here is an example working fine:

Then, if I increment the amplitude of the height map, one of the vertices appear in the sky. I believe this is the vertex with lowest value (if I’m not wrong by some tests I’ve done).

Incrementing more the amplitude, more and more vertices are corrupted.

Some idea what’s coming on here?

I would try “Uint8ClampedArray” in “data” definition, if it does not disturb data visualization (flat point).

const data = new Uint8ClampedArray(mapWidth * mapHeight * 4);

If you check the numbers in “value”, at least 6 calculations of “value” are out of the Uint8Array definition (are < = -1), and therefore, they are not assigned (consistent with numbers of points gone). So “value” is set to whatever JS gives, instead an error, not related to calculation. Set to Uint8ClampedArray, since you can’t assign values less than -1, by definition, Or use an heightOffset higher [from 21 (still gives some negatives but are > -1 and < 0) or more to whatever you consider], to assure all calculations of “value” are considered.

    let value = noise2D(x, y) * amplitude + heightOffset + 1; by the way, if you change the last term (…+1)

You are absolutelly right, that’s why it is unsigned int :sweat_smile:

Thanks for the explanation!

1 Like