loadSubtree function

Future<void> loadSubtree(
  1. Node node, {
  2. required AsyncPrefabLoader load,
  3. FsceneComponentRegistry? registry,
  4. AssetBundle? bundle,
})

Instantiates a lazy placeholder node's prefab content under it.

load resolves the referenced prefab document (the same loader loadScene uses). No-op if node is not a lazy placeholder or is already loaded.

Implementation

Future<void> loadSubtree(
  Node node, {
  required AsyncPrefabLoader load,
  FsceneComponentRegistry? registry,
  AssetBundle? bundle,
}) async {
  final spec = lazyInstanceOf(node);
  if (spec == null) {
    debugPrint('fscene: loadSubtree on a node that is not a lazy placeholder');
    return;
  }
  if (node.children.isNotEmpty) return; // already loaded

  // Expand the instance in isolation, in engine space (no handedness mirror)
  // and at identity: the placeholder already carries the placement, and the
  // realized content inherits the placeholder's frame from its ancestors.
  final host = SceneDocument(stage: StageMetadata(handedness: Handedness.left));
  host.addNode(NodeSpec(id: host.newId(), instance: _eager(spec)), root: true);
  final composed = await composeSceneAsync(host, load: load);
  final realized = await realizeSceneAsync(
    composed,
    registry: registry,
    bundle: bundle,
  );
  for (final child in List<Node>.of(realized.children)) {
    realized.remove(child);
    node.add(child);
  }
}