Best way to resuing an asset and updating it's positions

I am loading a gltf asset (Car) and I want to reuse it and update it’s positions by incoming status updates.
There will be around 100 cars which will receive position updates.

I load my asset like this:

    SceneLoader.ImportMesh(
      "",
      "assets/models/",
      "car.gltf",
      this.scene,
      (newMeshes) => {
        for (let i=0; i<newMeshes.length; i++){
          this.container.meshes.push(newMeshes[i]);
        }
        //this.container.removeAllFromScene();

Then I initiate instances like this:

const entries = this.container.instantiateModelsToScene(name => "Car1", true);

And then I update the position:

    this.scene.meshes.forEach((mesh) => {
      if (mesh.name === "Car1") {
        mesh.position.set(shuttleStatus.pose.x,shuttleStatus.pose.z,shuttleStatus.pose.y);
      }
    });

But this loop may be not the best solution s there are many meshes called “Car1”.
Is there some better option like just moving the root object?

Maybe you can use the id instead of the name or even use the tags for each vehicle.

In such cases I usually just store a reference to each object of a specific type in an array, then iterate through that array, rather than all scene meshes. I have always assumed this would be more efficient, but I don’t know for sure :slight_smile:

cars: Mesh[] = [];

// ... instantiate car mesh

cars.push(carMesh);

// change each car's properties
cars.forEach(car => {
  const position = Vector3.Zero();
  car.position = position;
});
3 Likes

Ok, I thought there may be a better approach, like putting them in some sort of “container” and then just move this single container around.

You can parent them all to a single TransformNode Transform Node | Babylon.js Documentation (babylonjs.com) but then you can’t update them individually, just all of them together.

Thank you, I think that’s the best solution.
Because when we take the example of a car, I don’t want to move the door, window, etc. separate from each other. I want to move the whole car at once.