parse method

  1. @override
dynamic parse(
  1. dynamic json, [
  2. String? path,
  3. Function? onLoad,
  4. Function? onError,
])
override

Implementation

@override
parse(json, [String? path, Function? onLoad, Function? onError]) {
  var content;
  var extensions = {};
  var plugins = {};

  if (json is String) {
    content = json;
  } else {
    var magic = LoaderUtils.decodeText(Uint8List.view(json.buffer, 0, 4));

    if (magic == binaryExtensionHeaderMagic) {
      // try {

      extensions[gltfExtensions["KHR_BINARY_GLTF"]] = GLTFBinaryExtension(json.buffer);

      // } catch ( error ) {

      //   if ( onError != null ) onError( error );
      //   return;

      // }

      content = extensions[gltfExtensions["KHR_BINARY_GLTF"]].content;
    } else {
      content = LoaderUtils.decodeText(json);
    }
  }

  Map<String, dynamic> decoded = convert.jsonDecode(content);

  if (decoded["asset"] == null || num.parse(decoded["asset"]["version"]) < 2.0) {
    if (onError != null) onError('THREE.GLTFLoader: Unsupported asset. glTF versions >= 2.0 are supported.');
    return;
  }

  var parser = GLTFParser(decoded, {
    "path": path ?? (resourcePath ?? ''),
    "crossOrigin": crossOrigin,
    "requestHeader": requestHeader,
    "manager": manager,
    "ktx2Loader": ktx2Loader,
    "meshoptDecoder": meshoptDecoder
  });

  parser.fileLoader.setRequestHeader(requestHeader);

  for (var i = 0; i < pluginCallbacks.length; i++) {
    var plugin = pluginCallbacks[i](parser);
    plugins[plugin.name] = plugin;

    // Workaround to avoid determining as unknown extension
    // in addUnknownExtensionsToUserData().
    // Remove this workaround if we move all the existing
    // extension handlers to plugin system
    extensions[plugin.name] = true;
  }

  if (decoded["extensionsUsed"] != null) {
    for (var i = 0; i < decoded["extensionsUsed"].length; ++i) {
      var extensionName = decoded["extensionsUsed"][i];
      var extensionsRequired = decoded["extensionsRequired"] ?? [];

      if (extensionName == gltfExtensions["KHR_MATERIALS_UNLIT"]) {
        extensions[extensionName] = GLTFMaterialsUnlitExtension();
      } else if (extensionName == gltfExtensions["KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS"]) {
        extensions[extensionName] = GLTFMaterialsPbrSpecularGlossinessExtension();
      } else if (extensionName == gltfExtensions["KHR_DRACO_MESH_COMPRESSION"]) {
        extensions[extensionName] = GLTFDracoMeshCompressionExtension(decoded, dracoLoader);
      } else if (extensionName == gltfExtensions["MSFT_TEXTURE_DDS"]) {
        extensions[extensionName] = GLTFTextureDDSExtension(ddsLoader);
      } else if (extensionName == gltfExtensions["KHR_TEXTURE_TRANSFORM"]) {
        extensions[extensionName] = GLTFTextureTransformExtension();
      } else if (extensionName == gltfExtensions["KHR_MESH_QUANTIZATION"]) {
        extensions[extensionName] = GLTFMeshQuantizationExtension();
      } else {
        if (extensionsRequired.indexOf(extensionName) >= 0 && plugins[extensionName] == null) {
          print('THREE.GLTFLoader: Unknown extension $extensionName.');
        }
      }

      // switch ( extensionName ) {
      //   case EXTENSIONS["KHR_MATERIALS_UNLIT"]:
      //     extensions[ extensionName ] = new GLTFMaterialsUnlitExtension();
      //     break;
      //   case EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS:
      //     extensions[ extensionName ] = new GLTFMaterialsPbrSpecularGlossinessExtension();
      //     break;
      //   case EXTENSIONS.KHR_DRACO_MESH_COMPRESSION:
      //     extensions[ extensionName ] = new GLTFDracoMeshCompressionExtension( json, this.dracoLoader );
      //     break;
      //   case EXTENSIONS.MSFT_TEXTURE_DDS:
      //     extensions[ extensionName ] = new GLTFTextureDDSExtension( this.ddsLoader );
      //     break;
      //   case EXTENSIONS.KHR_TEXTURE_TRANSFORM:
      //     extensions[ extensionName ] = new GLTFTextureTransformExtension();
      //     break;
      //   case EXTENSIONS.KHR_MESH_QUANTIZATION:
      //     extensions[ extensionName ] = new GLTFMeshQuantizationExtension();
      //     break;
      //   default:
      //     if ( extensionsRequired.indexOf( extensionName ) >= 0 && plugins[ extensionName ] == null ) {
      //       print( 'THREE.GLTFLoader: Unknown extension ${extensionName}.' );
      //     }
      // }

    }
  }

  parser.setExtensions(extensions);
  parser.setPlugins(plugins);
  parser.parse(onLoad, onError);
}