releaseUnused method

(int, int) releaseUnused()

Lets go of every model, material file and map the world's entities do not draw with — the switched-off ones' are kept — and of the shapes' meshes and materials nothing draws: what a level left behind leaves the GPU. Call once the next level is in the world; whatever is drawn again later is loaded again. Returns how many models and material files went.

Implementation

(int models, int materials) releaseUnused() {
  final world = this.world();
  _refresh(world);
  final modelPaths = <String>{};
  final materialPaths = <String>{};
  for (final archetype in world.queryArchetypes(const [ModelComponent])) {
    final column = archetype.getColumn(ModelComponent)!;
    for (var row = 0; row < archetype.length; row++) {
      final model = column[row] as ModelComponent;
      modelPaths.add(model.model);
      materialPaths.addAll(model.materials.values);
    }
  }
  for (final archetype in world.queryArchetypes(const [
    Primitive3DComponent,
  ])) {
    final column = archetype.getColumn(Primitive3DComponent)!;
    for (var row = 0; row < archetype.length; row++) {
      materialPaths.add((column[row] as Primitive3DComponent).material);
    }
  }
  // What the kits asked for as they built.
  for (final proxy in _providerProxies) {
    modelPaths.addAll(proxy.modelsAsked);
    materialPaths.addAll(proxy.materialsAsked);
  }
  final gone = (
    models.retainOnly(modelPaths),
    materials.retainOnly(materialPaths),
  );
  shapes.releaseUnused();
  return gone;
}