parse method

dynamic parse(
  1. dynamic text, [
  2. String? path,
  3. Function? onLoad,
  4. Function? onError,
])
override
  • Parses a MTL file. *
    • @param {String} text - Content of MTL file
      • @return {MaterialCreator}
      • @see setPath setResourcePath
      • @note In order for relative texture references to resolve correctly
      • you must call setResourcePath() explicitly prior to parse.

Implementation

parse(text, [String? path, Function? onLoad, Function? onError]) {
  var lines = text.split('\n');
  var info = {};
  var delimiter_pattern = RegExp(r"\s+");
  var materialsInfo = {};

  for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    line = line.trim();

    if (line.length == 0 || line[0] == '#') {
      // Blank line or comment ignore
      continue;
    }

    var pos = line.indexOf(' ');

    var key = (pos >= 0) ? line.substring(0, pos) : line;
    key = key.toLowerCase();

    String value = (pos >= 0) ? line.substring(pos + 1) : '';
    value = value.trim();

    if (key == 'newmtl') {
      // New material

      info = {"name": value};
      materialsInfo[value] = info;
    } else {
      if (key == 'ka' || key == 'kd' || key == 'ks' || key == 'ke') {
        var ss = value.split(delimiter_pattern);
        info[key] = [parseFloat(ss[0]), parseFloat(ss[1]), parseFloat(ss[2])];
      } else {
        info[key] = value;
      }
    }
  }

  var materialCreator = new MaterialCreator(
      this.resourcePath != "" ? this.resourcePath : path,
      this.materialOptions);
  materialCreator.setCrossOrigin(this.crossOrigin);
  materialCreator.setManager(this.manager);
  materialCreator.setMaterials(materialsInfo);
  return materialCreator;
}