loadModel method

  1. @override
Future<void> loadModel({
  1. String? modelPath,
  2. String? modelUrl,
  3. required Map<String, ByteData> resources,
  4. List<String>? preselectedEntities,
  5. List<double>? selectionColor,
  6. List<PatchColor>? patchColors,
})
override

Method to load the model from assets or network. resources is empty for .glb models and populated for .gltf models.

Implementation

@override
Future<void> loadModel({
  String? modelPath,
  String? modelUrl,
  required Map<String, ByteData> resources,
  List<String>? preselectedEntities,
  List<double>? selectionColor,
  List<PatchColor>? patchColors, // Add patchColors
}) async {
  Uint8List modelBytes;
  String modelName;

  if (modelPath != null) {
    ByteData modelData = await rootBundle.load(modelPath);
    modelBytes = modelData.buffer.asUint8List();
    modelName = modelPath.split('/').last;
  } else if (modelUrl != null) {
    final response = await http.get(Uri.parse(modelUrl));
    if (response.statusCode != 200) {
      throw Exception('Failed to load model: $modelUrl, status: ${response.statusCode}');
    }
    modelBytes = response.bodyBytes;
    modelName = modelUrl.split('/').last;
  } else {
    throw ArgumentError('Must provide either modelPath or modelUrl');
  }

  final resourceMap = resources.map(
        (key, value) => MapEntry(key, value.buffer.asUint8List()),
  );

  // Convert patchColors to a serializable format
  final patchColorsMap = patchColors?.map((patch) => {
    'name': patch.name,
    'color': patch.color,
  }).toList();

  final args = {
    'modelBytes': modelBytes,
    'name': modelName,
    'resources': resourceMap,
    'preselectedEntities': preselectedEntities,
    'selectionColor': selectionColor,
    'patchColors': patchColorsMap, // Pass patchColors
  };

  await _methodChannel.invokeMethod('loadModel', args);
}