sampling height for 4 wheels on cliffs etc, so for thousand cars being driven on terrain, and 4 points on each car, total become 4000, and sampling 4k points per frame is dropping frames. and i cant even run it on separate thread as it i guess runs on main thread only
Sampling the most detailed height of the terrain four thousand times a frame is going to be a performance hit no matter how you slice it. It means we have to load the highest resolution tile in the hierarchy for every point you query, which could mean a lot of tile lookups (especially if it’s a tileset with subtrees like the Google Photorealistic 3D Tiles tileset). All of this loading does happen on a separate thread though, so you shouldn’t be forced to do everything on the main thread.
For four thousand queries per frame, there’s some other approaches you can take:
- Do a line trace down at the terrain. This will only give you the height of the currently visible tile at that point, and it won’t work for tiles that aren’t loaded yet (like those that are offscreen), but it will be much faster as there’s no loading involved.
- Use the
EarthGravitationalModel1996Gridclass. When used with the appropriate grid file, this will give you the height of any position in the world with low resolution (15 arcminutes). - Reduce the amount of work you’re doing. You could reduce it by a factor of four by only doing a single lookup for the car instead of one for each wheel. You might use a level of detail system, where cars closer to the camera use more lookups to be more accurate, and cars farther away use fewer lookups because any errors won’t be noticeable. You could also defer the work between multiple frames - if you have 1000 cars, you run queries for only 25% of them each frame, so you’ve completed them all after four frames but the work per frame is reduced.
You’ll probably want to rely on some combination of these techniques to achieve what you’re looking for. Maybe using the EGM grid for cars off screen, the line trace for cars on screen, and a full query for cars up close, or something like that.