parseImages method

dynamic parseImages(
  1. dynamic json,
  2. dynamic onLoad
)

Implementation

parseImages(json, onLoad) async {
  var scope = this;
  var images = {};

  late ImageLoader loader;

  loadImage(url) async {
    scope.manager.itemStart(url);

    return await loader.loadAsync(url, () {
      scope.manager.itemEnd(url);
    });
  }

  deserializeImage(image) async {
    if (image is String) {
      var url = image;

      var path =
          RegExp("^(//)|([a-z]+:(//)?)", caseSensitive: false).hasMatch(url) ? url : (scope.resourcePath ?? "") + url;

      return await loadImage(path);
    } else {
      if (image.data) {
        return {"data": getTypedArray(image.type, image.data), "width": image.width, "height": image.height};
      } else {
        return null;
      }
    }
  }

  if (json != null && json.length > 0) {
    var manager = LoadingManager(onLoad, null, null);

    loader = ImageLoader(manager);
    loader.setCrossOrigin(crossOrigin);

    for (var i = 0, il = json.length; i < il; i++) {
      Map<String, dynamic> image = json[i];
      var url = image["url"];

      if (url is List) {
        // load array of images e.g CubeTexture

        List imageArray = [];

        for (var j = 0, jl = url.length; j < jl; j++) {
          var currentUrl = url[j];

          var deserializedImage = await deserializeImage(currentUrl);

          if (deserializedImage != null) {
            imageArray.add(deserializedImage);

            // if ( deserializedImage is HTMLImageElement ) {

            // 	imageArray.push( deserializedImage );

            // } else {

            // 	// special case: handle array of data textures for cube textures

            // 	imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );

            // }

          }
        }

        images[image["uuid"]] = Source(imageArray);
      } else {
        // load single image

        var deserializedImage = await deserializeImage(image["url"]);

        if (deserializedImage != null) {
          images[image["uuid"]] = Source(deserializedImage);
        }
      }
    }
  }

  return images;
}