コード例 #1
0
    private void ImportTextures()
    {
        //Add imported textures
        string importPath    = Application.dataPath + "/Import/" + "Textures";
        int    importCounter = 0;

        string[]         files      = Directory.GetFiles(importPath);
        TextureItemModel blankModel = new TextureItemModel();

        blankModel.DisplayName = "No Texture";
        GlobalSettingsModel.EditorModel.AllTextureItemsModel.Add(blankModel);

        foreach (string file in files)
        {
            if (file.EndsWith(".png") || file.EndsWith(".jpg"))
            {
                string name = file.Substring(file.LastIndexOf('\\') + 1);
                name = name.Substring(0, name.Length - 4);

                Texture2D texture   = new Texture2D(2, 2);
                byte[]    byteArray = File.ReadAllBytes(file);
                if (texture.LoadImage(byteArray))
                {
                    int    index       = name.LastIndexOf("_");
                    string textureName = name.Substring(0, index);

                    texture.name = textureName;
                    TextureItemModel model;
                    if (GlobalSettingsModel.EditorModel.AllTextureItemsModel.Contains(textureName))
                    {
                        model = GlobalSettingsModel.EditorModel.AllTextureItemsModel.Get(textureName);
                    }
                    else
                    {
                        model             = new TextureItemModel();
                        model.DisplayName = textureName;
                        GlobalSettingsModel.EditorModel.AllTextureItemsModel.Add(model);
                        importCounter++;
                    }

                    if (name.EndsWith("_color") || name.EndsWith("_Color") || name.EndsWith("_diff") || name.EndsWith("_Diff") || name.EndsWith("_albedo") || name.EndsWith("_Albedo") || name.EndsWith("_Diffuse") || name.EndsWith("_diffuse"))
                    {
                        model.SetColorMap(texture);
                    }

                    if (name.EndsWith("_normal") || name.EndsWith("_Normal") || name.EndsWith("_nor") || name.EndsWith("_Normal"))
                    {
                        model.SetNormalMap(texture);
                    }

                    /*
                     * if (name.EndsWith("_roughness") || name.EndsWith("_Roughness"))
                     * {
                     *  Texture2D newTexture = new Texture2D(texture.width, texture.height);
                     *  for (int i = 0; i < texture.width; i++)
                     *  {
                     *      for (int j = 0; j < texture.height; j++)
                     *      {
                     *          Color grey = texture.GetPixel(i, j);
                     *          Color alpha = newTexture.GetPixel(i, j);
                     *          alpha.a = 1f - grey.r;
                     *          newTexture.SetPixel(i, j, alpha);
                     *      }
                     *  }
                     *  newTexture.Apply();
                     *  byte[] bytes = newTexture.EncodeToJPG(100);
                     *  File.WriteAllBytes("F:\\Users\\Flo\\Desktop\\Studium\\BachelorArbeit\\bachelor-thesis\\Unity\\BachelorApplication\\Assets\\Import\\Textures\\" + "_metallic" + ".jpg", bytes);
                     *  if (GlobalSettingsModel.EditorModel.AllTextureItemsModel.Contains(textureName))
                     *  {
                     *      var model = GlobalSettingsModel.EditorModel.AllTextureItemsModel.Get(textureName);
                     *      model.MetallicMap = newTexture;
                     *  }
                     *  else
                     *  {
                     *      TextureItemModel model = new TextureItemModel();
                     *      model.MetallicMap = newTexture;
                     *      model.DisplayName = textureName;
                     *      GlobalSettingsModel.EditorModel.AllTextureItemsModel.Add(model);
                     *      importCounter++;
                     *  }
                     *  DestroyImmediate(newTexture);
                     * }*/
                }
                //DestroyImmediate(texture);
            }
        }

        Debug.Log(importCounter + " textures imported");
    }
コード例 #2
0
 public TextureSceneObjectModel(TextureItemModel itemModel) : base(itemModel)
 {
 }