createMaterial_ method

dynamic createMaterial_(
  1. dynamic materialName
)

Implementation

createMaterial_(materialName) async {
  // Create material

  var scope = this;
  var mat = this.materialsInfo[materialName];
  var params = {"name": materialName, "side": this.side};

  var resolveURL = (baseUrl, url) {
    if (!(url is String) || url == '') return '';

    // Absolute URL
    var _reg = RegExp(r"^https?:\/\/", caseSensitive: false);
    if (_reg.hasMatch(url)) return url;

    return baseUrl + url;
  };

  var setMapForType = (mapType, value) async {
    if (params[mapType] != null) return; // Keep the first encountered texture

    var texParams = scope.getTextureParams(value, params);

    var map = await scope.loadTexture(
        resolveURL(scope.baseUrl, texParams["url"]), null, null, null, null);

    map.repeat.copy(texParams["scale"]);
    map.offset.copy(texParams["offset"]);

    map.wrapS = scope.wrap;
    map.wrapT = scope.wrap;

    params[mapType] = map;
  };

  for (var prop in mat.keys) {
    var value = mat[prop];
    var n;

    if (value == '') continue;

    switch (prop.toLowerCase()) {

      // Ns is material specular exponent

      case 'kd':

        // Diffuse color (color under white light) using RGB values

        params["color"] = new Color(1, 1, 1).fromArray(value);

        break;

      case 'ks':

        // Specular color (color when light is reflected from shiny surface) using RGB values
        params["specular"] = new Color(1, 1, 1).fromArray(value);

        break;

      case 'ke':

        // Emissive using RGB values
        params["emissive"] = new Color(1, 1, 1).fromArray(value);

        break;

      case 'map_kd':

        // Diffuse texture map

        await setMapForType('map', value);

        break;

      case 'map_ks':

        // Specular map

        await setMapForType('specularMap', value);

        break;

      case 'map_ke':

        // Emissive map

        await setMapForType('emissiveMap', value);

        break;

      case 'norm':
        await setMapForType('normalMap', value);

        break;

      case 'map_bump':
      case 'bump':

        // Bump texture map

        await setMapForType('bumpMap', value);

        break;

      case 'map_d':

        // Alpha map

        await setMapForType('alphaMap', value);
        params["transparent"] = true;

        break;

      case 'ns':

        // The specular exponent (defines the focus of the specular highlight)
        // A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000.

        params["shininess"] = parseFloat(value);

        break;

      case 'd':
        n = parseFloat(value);

        if (n < 1) {
          params["opacity"] = n;
          params["transparent"] = true;
        }

        break;

      case 'tr':
        n = parseFloat(value);

        if (this.options != null && this.options["invertTrProperty"])
          n = 1 - n;

        if (n > 0) {
          params["opacity"] = 1 - n;
          params["transparent"] = true;
        }

        break;

      default:
        break;
    }
  }

  this.materials[materialName] = new MeshPhongMaterial(params);
  return this.materials[materialName];
}