FlutterSceneVrmBinding.fromRootNode constructor

FlutterSceneVrmBinding.fromRootNode(
  1. Node root, {
  2. required VrmModel model,
  3. FlutterSceneVrmBindingOptions? options,
  4. FlutterSceneSkinReduction? skinReduction,
})

Creates a binding for a Flutter Scene GLB root.

skinReduction is the joint subset the import applied to skins larger than the renderer's palette (FlutterSceneResolvedImport.skinReduction); vertex data this binding packs itself follows the same subset.

Implementation

FlutterSceneVrmBinding.fromRootNode(
  this.root, {
  required this.model,
  FlutterSceneVrmBindingOptions? options,
  FlutterSceneSkinReduction? skinReduction,
}) {
  options ??= FlutterSceneVrmBindingOptions();
  for (final entry
      in (skinReduction?.jointSlotRemapsByNode ?? const {}).entries) {
    final node = model.gltf.nodes.elementAtOrNull(entry.key);
    final skin = model.gltf.skins.elementAtOrNull(node?.skin ?? -1);
    final kept = entry.value.where((slot) => slot >= 0).length;
    _capabilityWarnings.add(
      VrmDiagnostic(
        severity: const VrmInfo(),
        code: 'flutterScene.skinReduced',
        message:
            'Skin ${skin?.index} lists ${skin?.joints.length} joints; node '
            '${entry.key} animates through the $kept joints its mesh weights.',
        gltfNodeIndex: entry.key,
      ),
    );
  }
  _capabilityWarnings.addAll(
    VrmFirstPersonController(model).geometrySplitWarnings(),
  );
  for (final material in model.gltf.materials) {
    final warning =
        model.vrm0MtoonFallbackWarning(material.index) ??
        material.mtoonFallbackWarning();
    if (warning != null) _capabilityWarnings.add(warning);
    final transparentZWriteWarning = model
        .vrm0TransparentZWriteFallbackWarning(material.index);
    if (transparentZWriteWarning != null) {
      _capabilityWarnings.add(transparentZWriteWarning);
    }
    for (final texture in _uvAccessedMaterialTextures(material)) {
      final texCoord = texture.textureTransform?.texCoord ?? texture.texCoord;
      if (texCoord != 0 && texCoord != 1) {
        _warnOnce(
          code: 'flutterScene.unsupportedTextureCoordinateSet',
          message:
              'Flutter Scene imports TEXCOORD_0 and TEXCOORD_1; material ${material.index} requests TEXCOORD_$texCoord.',
          gltfMaterialIndex: material.index,
        );
      }
    }
  }
  for (final entry in options.nodeIndexPaths.entries) {
    if (entry.key < 0 || entry.key >= model.gltf.nodes.length) {
      _warnOnce(
        code: 'flutterScene.invalidNodeIndexPath',
        message:
            'Flutter Scene node index path targets a glTF node outside the parsed model.',
        gltfNodeIndex: entry.key,
      );
    } else if (root.getChildByIndexPath(entry.value) == null) {
      _warnOnce(
        code: 'flutterScene.invalidNodeIndexPath',
        message:
            'Flutter Scene node index path could not be resolved from the supplied root node.',
        gltfNodeIndex: entry.key,
      );
    }
  }
  final sceneNodes = _sceneNodesByGltfIndex(root, model, options);
  final claimedSceneNodes = <scene.Node>{};
  sceneNodes.removeWhere((nodeIndex, sceneNode) {
    if (claimedSceneNodes.add(sceneNode)) return false;
    _warnOnce(
      code: 'flutterScene.duplicateNodeBinding',
      message:
          'Flutter Scene node index paths mapped more than one glTF node index to the same scene node.',
      gltfNodeIndex: nodeIndex,
    );
    return true;
  });
  for (final entry in sceneNodes.entries) {
    if (identical(entry.value, root)) {
      _rootGltfNodeIndex = entry.key;
      break;
    }
  }
  final rootGltfNodeIndex = _rootGltfNodeIndex;
  if (rootGltfNodeIndex == null) {
    _importRootLocalTransform = root.localTransform.clone();
  } else {
    _rootNodeLocalTransform =
        model.gltf.nodes[rootGltfNodeIndex].restTransform;
    final inverseRest = vm.Matrix4.tryInvert(
      _toSceneMatrix(_rootNodeLocalTransform!),
    );
    if (inverseRest == null) {
      _importRootLocalTransform = vm.Matrix4.identity();
      _warnOnce(
        code: 'flutterScene.nonInvertibleGltfRoot',
        message:
            'The glTF scene-root transform is not invertible; its importer transform could not be separated from model-root motion.',
        gltfNodeIndex: rootGltfNodeIndex,
      );
    } else {
      _importRootLocalTransform = root.localTransform.clone()
        ..multiply(inverseRest);
    }
  }
  final coreFromSceneTransform = vm.Matrix4.tryInvert(
    _importRootLocalTransform,
  );
  _coreFromSceneTransform = coreFromSceneTransform ?? vm.Matrix4.identity();
  if (coreFromSceneTransform == null) {
    _warnOnce(
      code: 'flutterScene.nonInvertibleImportRoot',
      message:
          'Flutter Scene import root transform is not invertible; core world transforms cannot be converted from renderer coordinates.',
    );
  }
  for (final node in model.gltf.nodes) {
    if (sceneNodes.containsKey(node.index)) continue;
    _warnOnce(
      code: 'flutterScene.missingNodeBinding',
      message:
          'Flutter Scene node traversal did not produce a node for this glTF node index.',
      gltfNodeIndex: node.index,
    );
  }
  _nodeBindings = {
    for (final entry in sceneNodes.entries)
      entry.key: entry.key == rootGltfNodeIndex
          ? _FlutterSceneRootNodeBinding(this, entry.value)
          : _FlutterSceneNodeBinding(entry.value, _coreFromSceneTransform),
  };
  final morphTargetDataFactory = MorphTargetDataFactory(model.gltf);
  _meshBindings = {
    for (final entry in sceneNodes.entries)
      if (entry.value.mesh != null)
        entry.key: _FlutterSceneMeshBinding(
          this,
          entry.key,
          entry.value,
          morphTargetDataFactory,
          skinReduction?.remapForNode(entry.key),
        ),
  };
  _materialBindings = {
    for (final entry in _sceneMaterials(sceneNodes, model).entries)
      entry.key: _FlutterSceneMaterialBinding(this, entry.key, entry.value),
  };
  for (final material in model.gltf.materials) {
    final occurrences = _materialBindings[material.index]?._materials;
    if (material.alphaMode == GltfAlphaMode.mask &&
        (occurrences == null ||
            occurrences.isEmpty ||
            // This adapter is pinned to the renderer's material seam.
            // ignore: invalid_use_of_internal_member
            occurrences.any((value) => !value.depthAlphaMasked))) {
      _warnOnce(
        code: 'flutterScene.maskAuxiliaryPassFallback',
        message:
            'The imported MASK material does not support alpha cutoff in auxiliary depth and shadow passes.',
        gltfMaterialIndex: material.index,
      );
    }
  }
  for (final entry in _materialBindings.entries) {
    if (entry.key < 0 || entry.key >= model.gltf.materials.length) continue;
    final material = model.gltf.materials[entry.key];
    if (material.mtoon == null &&
        model.vrm0MtoonFallbackWarning(entry.key) == null) {
      continue;
    }
    entry.value.applyFallback(material);
  }
}