GroundMesh updateCoordinateHeights() problem


I’ve created a GroundMesh, after that I am doing some updates to the PositionKind vertices.

Afterwards I am calling the following:

terrain.updateVerticesData(VertexBuffer.PositionKind, positionsVertices);

The vertices do get updated, but when I’m trying to get the height of the GroundMesh at a certain position using terrain.getHeightAtCoordinates(x, z) I’m not getting the updated value.

Seems like I need some sort of callback to wait for the terrain to be updated first.

I have tried with terrain.onAfterWorldMatrixUpdateObservable.addOnce and this is not really working for me.

Any ideas?


As I can see it in the code, it should work. Would you have a repro in the Playground?

Hey @Evgeni_Popov actually I think I know the reason why it’s not working:

I am trying to create an irregular polygon with a custom shape (in 3D) I actually want to overlay the shape on a GroundMesh that has elevation.

Unfortunately I can’t find a way to make the polygon vertices to get the height at the position of the terrain.

This polygon is generated based on a geojson file, so its basically a flat boundary that I want to overlay on a terrain with elevation. I used the MeshBuilder.ExtrudedPolygon factory. But I also need to get somehow the internal points of the boundary polygon.

Not sure if you might have an idea how to do this, as soon as I can I will write it on a playground.

@Evgeni_Popov here’s an example of what I would like to do. The boundary should overlay across the ground terrain without traspassing.

The easiest way is probably to use CSG to get the intersection between the polygon and a box with the same dimensions as the terrain:

@Evgeni_Popov thanks, I understand what you did, but this is not what I want, I think I didn’t explain myself correctly. I have updated the playground to show a better example:

Notice the boundary is now in the “center” of the mountain, I actually want it to overlay the mountain. So if the height of the mountain at certain position x,y is for example 20, then the boundary takes that height and applies it to itself. That way the boundary can overlay on the ground without traspassing the mountain.

Ok, I understand better!

You can get the vertices of your polygon and compute the y coordinate by calling getHeightAtCoordinates. However, you will have to subdivide your polygon if you want to avoid too many artifacts:

Subdivision Result Num faces
0 image 10
1 image 40
4 image 250
8 image 810


However, the number of subdivisions (the increaseVertices call in the PG) will depend on where you put the boundary on the terrain, and you will still see z-fighting artifacts depending on the camera position/rotation (you can improve things by changing the yOffset value in the PG).

I don’t know what you plan to do, but maybe using a decal texture can fit your need.

1 Like

@Evgeni_Popov thank you so much, I ended up using mesh subdivision as suggested and it’s working great. For my use case trying texture decals won’t work because I have to do some more manipulations to the object.

Thanks again!

1 Like