/**
   * Assigns the texture.
   *
   * @param pathToModel
   * @param modelFile
   * @param scene
   * @param m
   * @param sceneMaterialID
   * @param mesh
   */
  private void assignMaterial(
      String pathToModel,
      File modelFile,
      Scene3ds scene,
      Mesh3ds m,
      int sceneMaterialID,
      MTTriangleMesh mesh) {
    if (scene.materials() > 0) {
      if (m.faceMats() > 0) {
        // Just take the first material in the mesh, it could have more but we dont support more
        // than 1 material for a mesh
        // materialIndexForMesh = m.faceMat(0).matIndex();

        Material3ds mat = scene.material(sceneMaterialID);
        String materialName = mat.name();
        if (debug)
          logger.debug(
              "Material name for mesh \"" + mesh.getName() + ":-> \"" + materialName + "\"");
        materialName.trim();
        materialName.toLowerCase();

        // Try to load texture
        try {
          PImage cachedImage = textureCache.get(materialName);
          if (cachedImage != null) {
            mesh.setTexture(cachedImage);
            mesh.setTextureEnabled(true);
            if (debug) logger.debug("->Loaded texture from CACHE : \"" + materialName + "\"");
            return;
          }

          if (modelFile.exists()) { // If model is loaded from local file system
            String modelFolder =
                modelFile
                    .getParent(); // pathToModel.substring(),
                                  // pathToModel.lastIndexOf(File.pathSeparator)
            File modelFolderFile = new File(modelFolder);
            if (modelFolderFile.exists() && modelFolderFile.isDirectory())
              modelFolder = modelFolderFile.getAbsolutePath();
            else {
              modelFolder = "";
            }

            String[] suffix =
                new String[] {
                  "jpg", "JPG", "tga", "TGA", "bmp", "BMP", "png", "PNG", "tiff", "TIFF"
                };
            for (int j = 0; j < suffix.length; j++) {
              String suffixString = suffix[j];
              // Try to load and set texture to mesh
              String texturePath =
                  modelFolder + MTApplication.separator + materialName + "." + suffixString;
              File textureFile = new File(texturePath);
              if (textureFile.exists()) {
                boolean success = textureFile.renameTo(new File(texturePath));
                if (!success) {
                  // File was not successfully renamed
                  logger.debug("failed to RENAME file: " + textureFile.getAbsolutePath());
                }
                PImage texture = null;
                if (MT4jSettings.getInstance().isOpenGlMode()) { // TODO check if render thread
                  PImage img = pa.loadImage(texturePath);
                  if (Tools3D.isPowerOfTwoDimension(img)) {
                    texture =
                        new GLTexture(
                            pa,
                            img,
                            new GLTextureSettings(
                                TEXTURE_TARGET.TEXTURE_2D,
                                SHRINKAGE_FILTER.Trilinear,
                                EXPANSION_FILTER.Bilinear,
                                WRAP_MODE.REPEAT,
                                WRAP_MODE.REPEAT));
                  } else {
                    texture =
                        new GLTexture(
                            pa,
                            img,
                            new GLTextureSettings(
                                TEXTURE_TARGET.RECTANGULAR,
                                SHRINKAGE_FILTER.Trilinear,
                                EXPANSION_FILTER.Bilinear,
                                WRAP_MODE.REPEAT,
                                WRAP_MODE.REPEAT));
                    // ((GLTexture)texture).setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps,
                    // EXPANSION_FILTER.Bilinear);
                  }
                } else {
                  texture = pa.loadImage(texturePath);
                }
                mesh.setTexture(texture);
                mesh.setTextureEnabled(true);

                textureCache.put(materialName, texture);
                if (debug) logger.debug("->Loaded material texture: \"" + materialName + "\"");
                break;
              }
              if (j + 1 == suffix.length) {
                logger.error("Couldnt load material texture: \"" + materialName + "\"");
              }
            }
          } else { // Probably loading from jar file
            PImage texture = null;
            String[] suffix =
                new String[] {
                  "jpg", "JPG", "tga", "TGA", "bmp", "BMP", "png", "PNG", "tiff", "TIFF"
                };
            for (String suffixString : suffix) {
              String modelFolder =
                  pathToModel.substring(0, pathToModel.lastIndexOf(MTApplication.separator));
              String texturePath =
                  modelFolder + MTApplication.separator + materialName + "." + suffixString;
              if (MT4jSettings.getInstance().isOpenGlMode()) {
                PImage img = pa.loadImage(texturePath);
                if (Tools3D.isPowerOfTwoDimension(img)) {
                  texture =
                      new GLTexture(
                          pa,
                          img,
                          new GLTextureSettings(
                              TEXTURE_TARGET.TEXTURE_2D,
                              SHRINKAGE_FILTER.Trilinear,
                              EXPANSION_FILTER.Bilinear,
                              WRAP_MODE.REPEAT,
                              WRAP_MODE.REPEAT));
                } else {
                  texture =
                      new GLTexture(
                          pa,
                          img,
                          new GLTextureSettings(
                              TEXTURE_TARGET.RECTANGULAR,
                              SHRINKAGE_FILTER.Trilinear,
                              EXPANSION_FILTER.Bilinear,
                              WRAP_MODE.REPEAT,
                              WRAP_MODE.REPEAT));
                  // ((GLTexture)texture).setFilter(SHRINKAGE_FILTER.BilinearNoMipMaps,
                  // EXPANSION_FILTER.Bilinear);
                }
              } else {
                texture = pa.loadImage(texturePath);
              }
              mesh.setTexture(texture);
              mesh.setTextureEnabled(true);

              textureCache.put(materialName, texture);
              if (debug) logger.debug("->Loaded material texture: \"" + materialName + "\"");
              break;
            }
          }
        } catch (Exception e) {
          logger.error(e.getMessage());
        }
      } // if (m.faceMats() > 0)
    } // if (scene.materials() > 0)
  }