createMaterial_ method

dynamic createMaterial_(
  1. String materialName
)

Implementation

createMaterial_(String materialName) async {
  // Create material

  final scope = this;
  final mat = materialsInfo[materialName];
  final Map<String,dynamic> params = {"name": materialName, "side": side};

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

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

    return baseUrl + url;
  }

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

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

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

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

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

    params[mapType] = map;
  }

  for (final prop in mat.keys) {
    final value = mat[prop];
    double 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"] = Color.fromList(value);

        break;

      case 'ks':

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

        break;

      case 'ke':

        // Emissive using RGB values
        params["emissive"] = Color.fromList(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"] = double.parse(value);

        break;

      case 'd':
        n = double.parse(value);

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

        break;

      case 'tr':
        n = double.parse(value);

        if (options != null) {
          if ((options!.containsKey("invertTrProperty")) && options!["invertTrProperty"]) {
            n = 1 - n;
          }
        }
        if (n > 0) {
          params["opacity"] = 1 - n;
          params["transparent"] = true;
        }

        break;

      default:
        break;
    }
  }

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