i have a function in a class here wich runs every frame (in babylonjs). its purpose is to load 3d tiles and set the mesh.
if (!mesh) {
// this.loadTile is async!
const newTile = this.loadTile(updatedTile)
if (newTile) {
this.tiles.add(tileKey, newTile)
}
} else {
// do some other magic
this.tiles.add(tileKey, updatedTile)
}
each frame (or update) i check if there is a mesh for that specific tile. if not it should load the tile. loading it is async. now – since this is async and it takes longer then one update cycle it will run loadTile wayyyyy too often because it checks for !mesh.
the idea was to prevent this by adding some kind of “pending” like so:
add a tile wich has a “pending” mesh, and when the async loadTile finishes it would replace the “pending” mesh:
if (!mesh) {
this.tiles.add(tileKey, { updatedTile, mesh: new Mesh('pending') })
// this.loadTile is async!
this.loadTile(updatedTile).then((newTile) => {
if (newTile) {
this.tiles.add(tileKey, newTile)
}
})
} else if (mesh.name !== 'pending') {
// do some other magic
this.tiles.add(tileKey, updatedTile)
}
but for some reason it never reaches the “else if”.
since i am inside of a function wich updates the viewport i cannot make it async.
what would be a way to prevent the function to repeat the loadTile endlessly 😉 ?
Thanks a lot!