0

I have a function in a class here which 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 way too often because it checks for !mesh.

The idea was to prevent this by adding some kind of "pending" like so: Add a tile which 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 which 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!

1 Answer 1

0

Never mind... the provided solution works. The problem was that i did forget to spread the updated tile in this line:

this.tiles.add(tileKey, { ...updatedTile, mesh: new Mesh('pending') })

so the object was just not correct.

Cheers!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.