Advertisement
Guest User

MegascansImporter.cs

a guest
May 20th, 2018
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 54.08 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Experimental;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8.  
  9. namespace Quixel
  10. {
  11.     public class MegascansJsonImport
  12.     {
  13.         public static void ImportFromJSON(string jsonPath, bool usePath = true)
  14.         {
  15.             //in which we basically just reconstruct the .qxl file from a json file... because then i don't have to rewrite anything in the importer LAWL.
  16.             string jArray = usePath ? File.ReadAllText(jsonPath) : jsonPath;
  17.             Newtonsoft.Json.Linq.JArray testArray = Newtonsoft.Json.Linq.JArray.Parse(jArray);
  18.             List<Newtonsoft.Json.Linq.JObject> objectList = new List<Newtonsoft.Json.Linq.JObject>();
  19.             for (int i = 0; i < testArray.Count; ++i)
  20.             {
  21.                 objectList.Add(testArray[i].ToObject<Newtonsoft.Json.Linq.JObject>());
  22.             }
  23.             for (int i = 0; i < objectList.Count; ++i)
  24.             {
  25.                 //create a new text file...
  26.                 string qxl = "MegascansBundle";
  27.                 string path = (string)objectList[i]["path"];
  28.                 qxl += System.Environment.NewLine + (string)objectList[i]["id"];
  29.                 qxl += System.Environment.NewLine + path;
  30.                 qxl += System.Environment.NewLine + (string)objectList[i]["category"];
  31.                 qxl += System.Environment.NewLine + (string)objectList[i]["resolution"];
  32.  
  33.                 bool plants = false;
  34.                 if (path.Contains("3dplant"))
  35.                 {
  36.                     plants = true;
  37.                 }
  38.  
  39.                 Newtonsoft.Json.Linq.JArray comps = (Newtonsoft.Json.Linq.JArray)objectList[i]["components"];
  40.                 for (int j = 0; j < comps.Count; ++j)
  41.                 {
  42.                     string p = (string)comps[j]["path"];
  43.                     if (p != null)
  44.                     {
  45.                         p = p.Replace(path + Path.DirectorySeparatorChar, "");
  46.                         qxl += System.Environment.NewLine + p;
  47.                         if (plants)
  48.                         {
  49.                             string[] pb = p.Split('_');
  50.                             pb[0] = "Billboard";
  51.                             string bb = "Textures" + Path.DirectorySeparatorChar + "Billboard" + Path.DirectorySeparatorChar + "Billboard";
  52.                             for (int k = 1; k < pb.Length; ++k)
  53.                             {
  54.                                 bb += "_" + pb[k];
  55.                             }
  56.                             qxl += System.Environment.NewLine + bb;
  57.                         }
  58.                     }
  59.                 }
  60.                 comps = (Newtonsoft.Json.Linq.JArray)objectList[i]["meshList"];
  61.                 for (int j = 0; j < comps.Count; ++j)
  62.                 {
  63.                     string p = (string)comps[j]["path"];
  64.                     if (p != null)
  65.                     {
  66.                         p = p.Replace(path + Path.DirectorySeparatorChar, "");
  67.                         qxl += System.Environment.NewLine + p;
  68.                     }
  69.                 }
  70.                 string[] textArray = qxl.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.None);
  71.                 MegascansImporter.ImportQXL(textArray);
  72.             }
  73.         }
  74.     }
  75.  
  76.     [InitializeOnLoad]
  77.     public class MegascansImporter : Editor
  78.     {
  79.         static private string path;
  80.         static private string defPath;
  81.         static private string assetPath;
  82.  
  83.         static private string texturePath;
  84.         static private string meshesPath;
  85.         static private string materialsPath;
  86.         static private string prefabPath;
  87.         static private List<string> meshesPaths = new List<string>();
  88.         static private List<string> texturePaths = new List<string>();
  89.  
  90.         static private int texPack;
  91.         static private int dispType;
  92.         static private int shaderType;
  93.  
  94.         static private string globalPrefix;
  95.         static private string userPrefix = EditorPrefs.GetString("QuixelDefaultPrefix");
  96.  
  97.         static private bool foliage;
  98.         static private bool plant;
  99.  
  100.         static private Material finalMat;
  101.         static private Material billboardMat;
  102.  
  103.         public static bool ImportQXL(string[] textArray)
  104.         {
  105.             bool success = true;
  106.             Debug.Log("Reading .qxl file");
  107.             path = ConstructPath(textArray);
  108.             if (textArray[0].Contains("MegascansBundle"))
  109.             {
  110.                 Debug.Log("Valid bundle file...");
  111.             }
  112.             else
  113.             {
  114.                 Debug.LogError("Invalid bundle file.");
  115.             }
  116.             textArray[2] = FixSlashes(textArray[2]);
  117.             plant = false;
  118.             if (textArray[2].ToLower().Contains("3dplant"))
  119.             {
  120.                 plant = true;
  121.             }
  122.             foliage = false;
  123.             if (textArray[3].ToLower().Contains("foliage"))
  124.             {
  125.                 foliage = true;
  126.             }
  127.             AssetDatabase.Refresh();
  128.             List<string> absolutePaths = new List<string>();
  129.             List<string> names = new List<string>();
  130.             texturePath = FixSlashes(Path.Combine(path, "Textures"));
  131.             materialsPath = FixSlashes(Path.Combine(path, "Materials"));
  132.             meshesPath = FixSlashes(Path.Combine(path, "Models"));
  133.             prefabPath = FixSlashes(Path.Combine(path, "Prefabs"));
  134.             if (!AssetDatabase.IsValidFolder(texturePath))
  135.             {
  136.                 texturePath = AssetDatabase.CreateFolder(path, "Textures");
  137.                 texturePath = AssetDatabase.GUIDToAssetPath(texturePath);
  138.             }
  139.             if (!AssetDatabase.IsValidFolder(materialsPath))
  140.             {
  141.                 materialsPath = AssetDatabase.CreateFolder(path, "Materials");
  142.                 materialsPath = AssetDatabase.GUIDToAssetPath(materialsPath);
  143.             }
  144.             string[] getName = path.Split(Path.DirectorySeparatorChar);
  145.             string matName = getName[getName.Length - 1];
  146.             globalPrefix = "QXL_" + userPrefix + matName;
  147.             bool containsMesh = false;
  148.             for (int i = 4; i < textArray.Length; ++i)
  149.             {
  150.                 string absolutePath = FixSlashes(Path.Combine(textArray[2], textArray[i]));
  151.                 if ((absolutePath.ToLower().Contains(".fbx") || absolutePath.ToLower().Contains(".obj")))
  152.                 {
  153.                     if (!AssetDatabase.IsValidFolder(meshesPath))
  154.                     {
  155.                         meshesPath = AssetDatabase.CreateFolder(path, "Models");
  156.                         meshesPath = AssetDatabase.GUIDToAssetPath(meshesPath);
  157.                     }
  158.                     if (!AssetDatabase.IsValidFolder(prefabPath))
  159.                     {
  160.                         prefabPath = AssetDatabase.CreateFolder(path, "Prefabs");
  161.                         prefabPath = AssetDatabase.GUIDToAssetPath(prefabPath);
  162.                     }
  163.                     ImportMesh(absolutePath, meshesPath, textArray[i]);
  164.                     containsMesh = true;
  165.                 }
  166.                 if (absolutePath.ToLower().Contains(".exr"))
  167.                 {
  168.                     ImportEXR(absolutePath, textArray[i]);
  169.                 }
  170.                 if (absolutePath.ToLower().Contains(".jpg"))
  171.                 {
  172.                     absolutePaths.Add(absolutePath);
  173.                     names.Add(textArray[i]);
  174.                 }
  175.             }
  176.             if (shaderType == 0)
  177.             {
  178.                 PackTexturesHD(absolutePaths, names);
  179.             }
  180.             if (shaderType > 0)
  181.             {
  182.                 PackTexturesLW(absolutePaths, names);
  183.             }
  184.  
  185.             success = CreateMaterial();
  186.             if (plant)
  187.             {
  188.                 success = CreateMaterial(true);
  189.             }
  190.             if (containsMesh)
  191.             {
  192.                 if (plant)
  193.                 {
  194.                     success = CreatePlantPrefabs();
  195.                 }
  196.                 else
  197.                 {
  198.                     success = CreatePrefabs();
  199.                 }
  200.             }
  201.             AssetDatabase.SaveAssets();
  202.             AssetDatabase.Refresh();
  203.             EditorUtility.ClearProgressBar();
  204.             return success;
  205.         }
  206.  
  207.         static void GetPresets()
  208.         {
  209.             path = EditorPrefs.GetString("QuixelDefaultPath");
  210.             dispType = EditorPrefs.GetInt("QuixelDefaultDisplacement");
  211.             texPack = EditorPrefs.GetInt("QuixelDefaultTexPacking");
  212.             shaderType = EditorPrefs.GetInt("QuixelDefaultShader");
  213.             userPrefix = EditorPrefs.GetString("QuixelDefaultPrefix");
  214.             if (userPrefix.Length > 0)
  215.             {
  216.                 userPrefix += "_";
  217.             }
  218.         }
  219.  
  220.         static void FixVertColors(GameObject go)
  221.         {
  222.             MeshFilter mf = go.transform.GetComponent<MeshFilter>();
  223.             if( !mf )
  224.             {
  225.                 mf = go.transform.GetChild(0).GetComponent<MeshFilter>();
  226.             }
  227.             if( !mf )
  228.             {
  229.                 return;
  230.             }
  231.             Color[] vCols = mf.sharedMesh.colors;
  232.             for (int i = 0; i < vCols.Length; ++i)
  233.             {
  234.                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Fixing Vertex Colors...", (float)i / (vCols.Length - 1));
  235.                 vCols[i] = new Color(vCols[i].r, vCols[i].r, vCols[i].r, vCols[i].r);
  236.             }
  237.             mf.sharedMesh.colors = vCols;
  238.         }
  239.  
  240.         static bool CreatePlantPrefabs()
  241.         {
  242.             string[] p = new string[] { meshesPath };
  243.             string[] s = AssetDatabase.FindAssets("t:Model", p);
  244.             List<string> meshes = new List<string>();
  245.             for (int i = 0; i < s.Length; ++i)
  246.             {
  247.                 meshes.Add(AssetDatabase.GUIDToAssetPath(s[i]));
  248.             }
  249.             List<List<string>> variants = new List<List<string>>();
  250.             for (int i = 0; i < meshes.Count; ++i)
  251.             {
  252.                 List<string> v = new List<string>();
  253.                 for (int j = 0; j < meshes.Count; ++j)
  254.                 {
  255.                     string meshA = CleanUp(meshes[j], true);
  256.                     string meshB = CleanUp(meshes[i], true);
  257.                     if ((meshA.Length == meshB.Length) && meshA.Contains(meshB))
  258.                     {
  259.                         v.Add(meshes[j]);
  260.                     }
  261.                 }
  262.                 bool exists = false;
  263.                 for (int j = 0; j < variants.Count; ++j)
  264.                 {
  265.                     if (variants[j][0].ToLower().Contains(v[0].ToLower()))
  266.                     {
  267.                         exists = true;
  268.                     }
  269.                 }
  270.                 if (!exists)
  271.                 {
  272.                     variants.Add(v);
  273.                 }
  274.             }
  275.  
  276.             for (int i = 0; i < variants.Count; ++i)
  277.             {
  278.                 GameObject g = new GameObject();
  279.                 g.AddComponent<LODGroup>();
  280.                 string[] gns = variants[i][0].Split('/');
  281.                 string gn = CleanUp(gns[gns.Length - 1], true);
  282.                 g.name = gn;
  283.  
  284.                 for (int j = 0; j < variants[i].Count; ++j)
  285.                 {
  286.                     EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Gathering Variants...", (float)j / (variants.Count - 1));
  287.                     Object m = AssetDatabase.LoadAssetAtPath(variants[i][j], typeof(Object));
  288.                     GameObject mo = Object.Instantiate(m) as GameObject;
  289.                     mo.transform.parent = g.transform;
  290.                     mo.name = mo.name.Replace("(Clone)", "");
  291.                 }
  292.  
  293.                 LOD[] lods = new LOD[g.transform.childCount];
  294.                 float lodHeight = 1.0f;
  295.                 for (int j = 0; j < g.transform.childCount; ++j)
  296.                 {
  297.                     EditorUtility.DisplayProgressBar("Creating Prefabs", "Creating LODs...", (float)j / (g.transform.childCount - 1));
  298.                     Renderer[] r = new Renderer[1];
  299.                     r[0] = g.transform.GetChild(j).GetComponent<Renderer>();
  300.                     if( ! r[0])
  301.                     {
  302.                         r[0] = g.transform.GetChild(j).GetChild(0).GetComponent<Renderer>();
  303.                     }
  304.                     r[0].material = finalMat;
  305.                     if (j == g.transform.childCount - 1)
  306.                     {
  307.                         r[0].material = billboardMat;
  308.                     }
  309.                     if (j < 1)
  310.                     {
  311.                         lodHeight *= 0.25f;
  312.                     }
  313.                     else
  314.                     {
  315.                         lodHeight *= 0.5f;
  316.                     }
  317.                     lods[j] = new LOD(lodHeight, r);
  318.                 }
  319.                 g.GetComponent<LODGroup>().SetLODs(lods);
  320.                 g.GetComponent<LODGroup>().RecalculateBounds();
  321.                 g.GetComponent<LODGroup>().fadeMode = LODFadeMode.CrossFade;
  322.                 g.GetComponent<LODGroup>().animateCrossFading = true;
  323.  
  324.                 string prefabName = prefabPath + "/" + globalPrefix + i.ToString() + ".prefab";
  325.                 prefabName = prefabName.Replace("(Clone)", "");
  326.                 Object pf = AssetDatabase.LoadAssetAtPath(prefabName, typeof(Object));
  327.                 if (!pf)
  328.                 {
  329.                     PrefabUtility.CreatePrefab(prefabName, g);
  330.                 }
  331.                 else
  332.                 {
  333.                     PrefabUtility.ReplacePrefab(g, pf, ReplacePrefabOptions.ReplaceNameBased);
  334.                 }
  335.                 DestroyImmediate(g);
  336.             }
  337.             return true;
  338.         }
  339.  
  340.         static bool CreatePrefabs(bool hasBillboard = false)
  341.         {
  342.             List<GameObject> ol = new List<GameObject>();
  343.             if (!AssetDatabase.IsValidFolder(meshesPath))
  344.             {
  345.                 Debug.LogWarning("Error 102: Could not read mesh folder...");
  346.                 return false;
  347.             }
  348.             string[] p = new string[] { meshesPath };
  349.             string[] s = AssetDatabase.FindAssets("t:Model", p);
  350.             for (int i = 0; i < s.Length; ++i)
  351.             {
  352.                 string sap = AssetDatabase.GUIDToAssetPath(s[i]);
  353.                 Object o = AssetDatabase.LoadAssetAtPath<Object>(sap);
  354.                 ol.Add(Object.Instantiate(o) as GameObject);
  355.             }
  356.  
  357.             List<List<Transform>> transforms = new List<List<Transform>>();
  358.             for (int i = 0; i < ol.Count; ++i)
  359.             {
  360.                 List<Transform> t = new List<Transform>();
  361.                 if (ol[i].transform.childCount > 0)
  362.                 {
  363.                     for (int j = 0; j < ol[i].transform.childCount; ++j)
  364.                     {
  365.                         EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Gathering Variants...", (float)j / (ol[i].transform.childCount - 1));
  366.                         t.Add(ol[i].transform.GetChild(j));
  367.                     }
  368.                 }
  369.                 else
  370.                 {
  371.                     t.Add(ol[i].transform);
  372.                 }
  373.                 transforms.Add(t);
  374.             }
  375.  
  376.             if ((transforms.Count < 1) || (transforms[0].Count < 1))
  377.             {
  378.                 return false;
  379.             }
  380.  
  381.             List<GameObject> gos = new List<GameObject>();
  382.             for (int i = 0; i < transforms[0].Count; ++i)
  383.             {
  384.                 GameObject go = new GameObject();
  385.                 go.AddComponent<LODGroup>();
  386.                 string[] ns = transforms[0][i].name.Split('/');
  387.                 go.name = CleanUp(ns[ns.Length - 1], true);
  388.  
  389.                 gos.Add(go);
  390.             }
  391.  
  392.             for (int i = 0; i < gos.Count; ++i)
  393.             {
  394.                 for (int j = 0; j < transforms.Count; ++j)
  395.                 {
  396.                     if (transforms[j].Count > 0)
  397.                     {
  398.                         transforms[j][i].parent = gos[i].transform;
  399.                         transforms[j][i].transform.localPosition = Vector3.zero;
  400.                     }
  401.                 }
  402.             }
  403.  
  404.             for (int i = 0; i < gos.Count; ++i)
  405.             {
  406.                 LOD[] lods = new LOD[gos[i].transform.childCount];
  407.                 float lodHeight = 1.0f;
  408.                 for (int j = 0; j < gos[i].transform.childCount; ++j)
  409.                 {
  410.                     EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Creating LODs...", (float)j / (gos[i].transform.childCount - 1));
  411.                     Renderer[] r = new Renderer[1];
  412.                     r[0] = gos[i].transform.GetChild(j).GetComponent<Renderer>();
  413.                     r[0].material = finalMat;
  414.                     if (hasBillboard && (j == gos[i].transform.childCount - 1))
  415.                     {
  416.                         r[0].material = billboardMat;
  417.                     }
  418.                     if (gos[i].transform.childCount > 2)
  419.                     {
  420.                         if (j < (gos[i].transform.childCount / 2))
  421.                         {
  422.                             lodHeight *= 0.75f;
  423.                         }
  424.                         if (j >= (gos[i].transform.childCount / 2))
  425.                         {
  426.                             lodHeight *= 0.5f;
  427.                         }
  428.                     }
  429.                     else
  430.                     {
  431.                         if (j < 1)
  432.                         {
  433.                             lodHeight *= 0.75f;
  434.                         }
  435.                         else
  436.                         {
  437.                             lodHeight *= 0.15f;
  438.                         }
  439.                     }
  440.                     lods[j] = new LOD(lodHeight, r);
  441.  
  442.                     //this should only add LODs to the first 2 meshes.
  443.                     if (j < 2)
  444.                     {
  445.                         if (!hasBillboard)
  446.                         {
  447.                             gos[i].transform.GetChild(j).gameObject.AddComponent<MeshCollider>().sharedMesh = gos[i].transform.GetChild(gos[i].transform.childCount - 1).GetComponent<MeshFilter>().sharedMesh;
  448.                         }
  449.                     }
  450.                 }
  451.                 gos[i].GetComponent<LODGroup>().SetLODs(lods);
  452.                 gos[i].GetComponent<LODGroup>().RecalculateBounds();
  453.                 gos[i].GetComponent<LODGroup>().fadeMode = LODFadeMode.CrossFade;
  454.                 gos[i].GetComponent<LODGroup>().animateCrossFading = true;
  455.                 gos[i].isStatic = true;
  456.  
  457.                 string prefabName = prefabPath + "/" + globalPrefix + i.ToString() + ".prefab";
  458.                 prefabName = prefabName.Replace("(Clone)", "");
  459.                 Object pf = AssetDatabase.LoadAssetAtPath(prefabName, typeof(Object));
  460.                 if (!pf)
  461.                 {
  462.                     PrefabUtility.CreatePrefab(prefabName, gos[i]);
  463.                 }
  464.                 else
  465.                 {
  466.                     PrefabUtility.ReplacePrefab(gos[i], pf, ReplacePrefabOptions.ReplaceNameBased);
  467.                 }
  468.             }
  469.  
  470.             for (int i = 0; i < ol.Count; ++i)
  471.             {
  472.                 DestroyImmediate(ol[i]);
  473.             }
  474.             for (int i = 0; i < gos.Count; ++i)
  475.             {
  476.                 DestroyImmediate(gos[i]);
  477.             }
  478.             return true;
  479.         }
  480.  
  481.         static bool CreateMaterial(bool isBillboard = false)
  482.         {
  483.             if (!AssetDatabase.IsValidFolder(texturePath))
  484.             {
  485.                 Debug.LogWarning("Error 101: Could not read from texture folder...");
  486.                 return false;
  487.             }
  488.             string rp = CleanUp(materialsPath + "/" + globalPrefix);
  489.             if (isBillboard)
  490.             {
  491.                 rp += "_Billboard";
  492.             }
  493.             rp += ".mat";
  494.             Material mat = (Material)AssetDatabase.LoadAssetAtPath(rp, typeof(Material));
  495.             if (!mat)
  496.             {
  497.                 mat = new Material(Shader.Find("Standard"));
  498.                 AssetDatabase.CreateAsset(mat, rp);
  499.                 AssetDatabase.Refresh();
  500.             }
  501.             if (shaderType < 1)
  502.             {
  503.                 mat.shader = Shader.Find("HDRenderPipeline/Lit");
  504.                 mat.SetInt("_DisplacementMode", dispType);
  505.             }
  506.             if (shaderType > 0)
  507.             {
  508.                 mat.shader = Shader.Find("LightweightPipeline/Standard (Physically Based)");
  509.             }
  510.             if (shaderType > 1)
  511.             {
  512.                 mat.shader = Shader.Find("Standard");
  513.             }
  514.  
  515.             string[] texPath = new string[] { texturePath };
  516.             string[] texPaths = AssetDatabase.FindAssets("t:Texture2D", texPath);
  517.             for (int i = 0; i < texPaths.Length; ++i)
  518.             {
  519.                 string p = AssetDatabase.GUIDToAssetPath(texPaths[i]);
  520.                 BillboardTexSort(p);
  521.             }
  522.             AssetDatabase.Refresh();
  523.             return true;
  524.         }
  525.  
  526.         static void BillboardTexSort(string p)
  527.         {
  528.             string[] path = new string[] { materialsPath };
  529.             string[] mats = AssetDatabase.FindAssets("t:Material", path);
  530.             for (int i = 0; i < mats.Length; ++i)
  531.             {
  532.                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Material", "Assigning Textures...", (float)i / (mats.Length - 1));
  533.                 Material mat = (Material)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(mats[i]), typeof(Material));
  534.                 if (mat.name.ToLower().Contains("billboard") && p.ToLower().Contains("billboard"))
  535.                 {
  536.                     if (shaderType == 0)
  537.                     {
  538.                         HDMaterialAssign(mat, p);
  539.                     }
  540.                     if (shaderType > 0)
  541.                     {
  542.                         LWMaterialAssign(mat, p);
  543.                     }
  544.                     billboardMat = mat;
  545.                 }
  546.                 if (!mat.name.ToLower().Contains("billboard") && !p.ToLower().Contains("billboard"))
  547.                 {
  548.                     if (shaderType == 0)
  549.                     {
  550.                         HDMaterialAssign(mat, p);
  551.                     }
  552.                     if (shaderType > 0)
  553.                     {
  554.                         LWMaterialAssign(mat, p);
  555.                     }
  556.                     finalMat = mat;
  557.                 }
  558.                 //UnityEditor.Experimental.Rendering.HDPipeline.HDEditorUtils.ResetMaterialKeywords(mat);
  559.             }
  560.         }
  561.  
  562.         static void HDMaterialAssign(Material mat, string p)
  563.         {
  564.             if (p.Contains("Albedo"))
  565.             {
  566.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  567.                 mat.SetTexture("_BaseColorMap", tex);
  568.                 if (p.Contains("Alpha"))
  569.                 {
  570.                     mat.SetInt("_AlphaCutoffEnable", 1);
  571.                     mat.SetFloat("_AlphaCutoff", 0.333f);
  572.                     mat.SetInt("_DoubleSidedEnable", 1);
  573.                 }
  574.             }
  575.             if (p.Contains("Masks"))
  576.             {
  577.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  578.                 tImp.sRGBTexture = false;
  579.                 AssetDatabase.ImportAsset(p);
  580.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  581.                 mat.SetTexture("_MaskMap", tex);
  582.             }
  583.             if (p.Contains("Specular"))
  584.             {
  585.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  586.                 mat.SetTexture("_SpecularColorMap", tex);
  587.             }
  588.             if (p.Contains("Normal"))
  589.             {
  590.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  591.                 tImp.textureType = TextureImporterType.NormalMap;
  592.                 AssetDatabase.ImportAsset(p);
  593.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  594.                 mat.SetTexture("_NormalMap", tex);
  595.             }
  596.             if (p.Contains("Fuzz"))
  597.             {
  598.                 mat.SetInt("_MaterialID", 0);
  599.                 mat.SetInt("_DiffusionProfile", 2);
  600.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  601.                 tImp.sRGBTexture = false;
  602.                 AssetDatabase.ImportAsset(p);
  603.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  604.                 mat.SetTexture("_SubsurfaceMaskMap", tex);
  605.             }
  606.             if (p.Contains("Translucency"))
  607.             {
  608.                 mat.SetInt("_MaterialID", 0);
  609.                 mat.SetInt("_DiffusionProfile", 1);
  610.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  611.                 tImp.sRGBTexture = false;
  612.                 AssetDatabase.ImportAsset(p);
  613.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  614.                 mat.SetTexture("_SubsurfaceMaskMap", tex);
  615.                 mat.SetTexture("_ThicknessMap", tex);
  616.                 if (foliage)
  617.                 {
  618.                     mat.SetInt("_DiffusionProfile", 2);
  619.                     mat.SetFloat("_CoatMask", 0.0f);
  620.                     mat.SetInt("_EnableWind", 1);
  621.                 }
  622.             }
  623.             int dispType = MegascansImporterWindow.GetDispType();
  624.             if (dispType > 0)
  625.             {
  626.                 mat.shader = Shader.Find("HDRenderPipeline/LitTesselaton");
  627.                 if (p.Contains("Displacement"))
  628.                 {
  629.                     Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  630.                     mat.SetTexture("_HeightMap", tex);
  631.                 }
  632.             }
  633.         }
  634.  
  635.         static void LWMaterialAssign(Material mat, string p)
  636.         {            
  637.             if (p.Contains("Albedo"))
  638.             {
  639.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  640.                 mat.SetTexture("_MainTex", tex);
  641.                 if (p.Contains("Alpha"))
  642.                 {
  643.                     mat.SetInt("_Mode", 1);
  644.                 }
  645.             }
  646.             if (p.Contains("Metallic") && (texPack == 0))
  647.             {
  648.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  649.                 tImp.sRGBTexture = false;
  650.                 AssetDatabase.ImportAsset(p);
  651.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  652.                 mat.SetTexture("_MetallicGlossMap", tex);
  653.             }
  654.             if (p.Contains("Specular") && (texPack == 1))
  655.             {
  656.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  657.                 mat.SetTexture("_SpecGlossMap", tex);
  658.             }
  659.             if (p.Contains("Normal"))
  660.             {
  661.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  662.                 tImp.textureType = TextureImporterType.NormalMap;
  663.                 AssetDatabase.ImportAsset(p);
  664.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  665.                 mat.SetTexture("_BumpMap", tex);
  666.             }
  667.             if (p.Contains("AO"))
  668.             {
  669.                 TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
  670.                 tImp.sRGBTexture = false;
  671.                 AssetDatabase.ImportAsset(p);
  672.                 Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
  673.                 mat.SetTexture("_OcclusionMap", tex);
  674.             }
  675.         }
  676.  
  677.         static bool IterationCounter( int currentIter, int maxIter, int iterCheck)
  678.         {
  679.             if(currentIter % (maxIter / iterCheck) == 0 && currentIter != 0)
  680.             {
  681.                 return true;
  682.             }
  683.             return false;
  684.         }
  685.  
  686.         static void PackTexturesHD(List<string> absPaths, List<string> names)
  687.         {
  688.             bool metallicFound = false;
  689.             bool glossFound = false;
  690.             bool AOFound = false;
  691.             for (int i = 0; i < absPaths.Count; ++i)
  692.             {
  693.                 string p = CleanUp(texturePath + "/" + globalPrefix + "_");
  694.                 if (absPaths[i].ToLower().Contains("billboard") && plant)
  695.                 {
  696.                     p += "Billboard_";
  697.                 }
  698.                 //create albedo
  699.                 if (absPaths[i].Contains("Albedo"))
  700.                 {
  701.                     Texture2D t = ImportJPG(absPaths[i]);
  702.                     Color[] pixels = t.GetPixels();
  703.                     Color[] oPixels = new Color[pixels.Length];
  704.                     bool hasAlpha = false;
  705.                     p += "Albedo";
  706.                     for (int j = 0; j < absPaths.Count; ++j)
  707.                     {
  708.                         if (absPaths[j].Contains("Opacity"))
  709.                         {
  710.                             if ((!absPaths[i].ToLower().Contains("billboard") && !absPaths[j].ToLower().Contains("billboard")) ||
  711.                                 (absPaths[i].ToLower().Contains("billboard") && absPaths[j].ToLower().Contains("billboard")))
  712.                             {
  713.                                 Texture2D o = ImportJPG(absPaths[j]);
  714.                                 oPixels = o.GetPixels();
  715.                                 hasAlpha = true;
  716.                             }
  717.                         }
  718.                     }
  719.                     for (int j = 0; j < pixels.Length; ++j)
  720.                     {
  721.                         if (IterationCounter(j, pixels.Length-1, 100))
  722.                         {
  723.                             EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Albedo...", (float)j / (pixels.Length - 1));
  724.                         }
  725.                         float o = 1.0f;
  726.                         if(hasAlpha)
  727.                         {
  728.                             o = oPixels[j].r;
  729.                         }
  730.                         pixels[j].a = o;
  731.                     }
  732.                     if (hasAlpha)
  733.                     {
  734.                         p += "_Alpha";
  735.                     }
  736.                     Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
  737.                     nt.SetPixels(pixels);
  738.                     p += ".png";
  739.                     File.WriteAllBytes(p, nt.EncodeToPNG());
  740.                     AssetDatabase.ImportAsset(p);
  741.                     texturePaths.Add(p);
  742.                 }
  743.  
  744.                 //create masks
  745.                 if (absPaths[i].Contains("Gloss") || absPaths[i].Contains("Roughness"))
  746.                 {
  747.                     Texture2D t = ImportJPG(absPaths[i]);
  748.                     Color[] pixels = t.GetPixels();
  749.                     //if gloss or roughness...
  750.                     if (absPaths[i].Contains("Gloss"))
  751.                     {
  752.                         glossFound = true;
  753.                         for (int j = 0; j < pixels.Length; ++j)
  754.                         {
  755.                             if (IterationCounter(j, pixels.Length - 1, 100))
  756.                             {
  757.                                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Gloss...", (float)j / (pixels.Length - 1));
  758.                             }
  759.                             float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
  760.                             pixels[j].a = gloss;
  761.                         }
  762.                     }
  763.                     if (absPaths[i].Contains("Roughness"))
  764.                     {
  765.                         if (glossFound)
  766.                         {
  767.                             continue;
  768.                         }
  769.                         for (int j = 0; j < pixels.Length; ++j)
  770.                         {
  771.                             if (IterationCounter(j, pixels.Length - 1, 100))
  772.                             {
  773.                                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Gloss...", (float)j / (pixels.Length - 1));
  774.                             }
  775.                             float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
  776.                             pixels[j].a = 1.0f - gloss;
  777.                         }
  778.                     }
  779.                     //do metallic...
  780.                     for (int j = 0; j < absPaths.Count; ++j)
  781.                     {
  782.                         if (absPaths[j].Contains("Metal"))
  783.                         {
  784.                             metallicFound = true;
  785.                             Color[] mPixels = ImportJPG(absPaths[j]).GetPixels();
  786.                             for (int k = 0; k < pixels.Length; ++k)
  787.                             {
  788.                                 if (IterationCounter(k, pixels.Length - 1, 100))
  789.                                 {
  790.                                     EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Metallic...", (float)j / (pixels.Length - 1));
  791.                                 }
  792.                                 float metal = (mPixels[j].r + mPixels[j].g + mPixels[j].b) / 3.0f;
  793.                                 pixels[j].r = metal;
  794.                             }
  795.                         }
  796.                         if (absPaths[j].Contains("AO"))
  797.                         {
  798.                             AOFound = true;
  799.                             Color[] aoPixels = ImportJPG(absPaths[j]).GetPixels();
  800.                             for (int k = 0; k < pixels.Length; ++k)
  801.                             {
  802.                                 if (IterationCounter(k, pixels.Length - 1, 100))
  803.                                 {
  804.                                     EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask AO...", (float)j / (pixels.Length - 1));
  805.                                 }
  806.                                 float ao = (aoPixels[j].r + aoPixels[j].g + aoPixels[j].b) / 3.0f;
  807.                                 pixels[j].g = ao;
  808.                             }
  809.                         }
  810.                     }
  811.  
  812.                     if (!metallicFound)
  813.                     {
  814.                         for (int j = 0; j < pixels.Length; ++j)
  815.                         {
  816.                             if (IterationCounter(j, pixels.Length - 1, 100))
  817.                             {
  818.                                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Metallic...", (float)j / (pixels.Length - 1));
  819.                             }
  820.                             pixels[j].r = 1.0f;
  821.                         }
  822.                     }
  823.                     if (!AOFound)
  824.                     {
  825.                         for (int j = 0; j < pixels.Length; ++j)
  826.                         {
  827.                             if (IterationCounter(j, pixels.Length - 1, 100))
  828.                             {
  829.                                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Metallic...", (float)j / (pixels.Length - 1));
  830.                             }
  831.                             pixels[j].g = 1.0f;
  832.                         }
  833.                     }
  834.                     for (int j = 0; j < pixels.Length; ++j)
  835.                     {
  836.                         pixels[j].b = 0.0f;
  837.                     }
  838.  
  839.                     Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
  840.                     nt.SetPixels(pixels);
  841.                     p += "Masks" + ".png";
  842.                     File.WriteAllBytes(p, nt.EncodeToPNG());
  843.                     AssetDatabase.ImportAsset(p);
  844.                     texturePaths.Add(p);
  845.                 }
  846.  
  847.                 if ((absPaths[i].Contains("Normal")) && (!absPaths[i].Contains("Bump")))
  848.                 {
  849.                     Texture2D normal = ImportJPG(absPaths[i]);
  850.                     p += "Normal" + ".png";
  851.                     File.WriteAllBytes(p, normal.EncodeToPNG());
  852.                     AssetDatabase.ImportAsset(p);
  853.                     texturePaths.Add(p);
  854.                 }
  855.                 if (absPaths[i].Contains("Specular"))
  856.                 {
  857.                     if (texPack > 0)
  858.                     {
  859.                         Texture2D spec = ImportJPG(absPaths[i]);
  860.                         p += "Specular" + ".png";
  861.                         File.WriteAllBytes(p, spec.EncodeToPNG());
  862.                         AssetDatabase.ImportAsset(p);
  863.                         texturePaths.Add(p);
  864.                     }
  865.                 }
  866.                 if (absPaths[i].Contains("Fuzz"))
  867.                 {
  868.                     Texture2D fuzz = ImportJPG(absPaths[i]);
  869.                     p += "Fuzz" + ".png";
  870.                     File.WriteAllBytes(p, fuzz.EncodeToPNG());
  871.                     AssetDatabase.ImportAsset(p);
  872.                     texturePaths.Add(p);
  873.                 }
  874.                 if (absPaths[i].Contains("Translucency"))
  875.                 {
  876.                     Texture2D trans = ImportJPG(absPaths[i]);
  877.                     Color[] pixels = trans.GetPixels();
  878.                     for (int j = 0; j < pixels.Length; ++j)
  879.                     {
  880.                         if (IterationCounter(j, pixels.Length - 1, 100))
  881.                         {
  882.                             EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Translucency...", (float)j / (pixels.Length - 1));
  883.                         }
  884.                         float transP = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
  885.                         pixels[j].r = transP;
  886.                         pixels[j].g = transP;
  887.                         pixels[j].b = transP;
  888.                     }
  889.                     trans.SetPixels(pixels);
  890.                     p += "Translucency" + ".png";
  891.                     File.WriteAllBytes(p, trans.EncodeToPNG());
  892.                     AssetDatabase.ImportAsset(p);
  893.                     texturePaths.Add(p);
  894.                 }
  895.             }
  896.             AssetDatabase.Refresh();
  897.         }
  898.  
  899.         static void PackTexturesLW(List<string> absPaths, List<string> names)
  900.         {
  901.             bool metallicFound = false;
  902.             bool glossFound = false;
  903.             for (int i = 0; i < absPaths.Count; ++i)
  904.             {
  905.                 string p = CleanUp(texturePath + "/" + globalPrefix + "_");
  906.                 if (absPaths[i].ToLower().Contains("billboard") && plant)
  907.                 {
  908.                     p += "Billboard_";
  909.                 }
  910.                 //create albedo
  911.                 if (absPaths[i].Contains("Albedo"))
  912.                 {
  913.                     p += "Albedo";
  914.                     Texture2D t = ImportJPG(absPaths[i]);
  915.                     Color[] pixels = t.GetPixels();
  916.                     Color[] oPixels = new Color[pixels.Length];
  917.                     bool hasAlpha = false;
  918.                     for (int j = 0; j < absPaths.Count; ++j)
  919.                     {
  920.                         if (absPaths[j].Contains("Opacity"))
  921.                         {
  922.                             if ((!absPaths[i].ToLower().Contains("billboard") && !absPaths[j].ToLower().Contains("billboard")) ||
  923.                                 (absPaths[i].ToLower().Contains("billboard") && absPaths[j].ToLower().Contains("billboard")))
  924.                             {
  925.                                 Texture2D o = ImportJPG(absPaths[j]);
  926.                                 oPixels = o.GetPixels();
  927.                                 hasAlpha = true;
  928.                             }
  929.                         }
  930.                     }
  931.                     for (int j = 0; j < pixels.Length; ++j)
  932.                     {
  933.                         float o = 1.0f;
  934.                         if (hasAlpha)
  935.                         {
  936.                             o = oPixels[j].r;
  937.                         }
  938.                         if (IterationCounter(j, pixels.Length - 1, 100))
  939.                         {
  940.                             EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Albedo...", (float)j / (pixels.Length - 1));
  941.                         }
  942.                         pixels[j].a = o;
  943.                     }
  944.                     if (hasAlpha)
  945.                     {
  946.                         p += "_Alpha";
  947.                     }
  948.                     Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
  949.                     nt.SetPixels(pixels);
  950.                     p += ".png";
  951.                     File.WriteAllBytes(p, nt.EncodeToPNG());
  952.                     AssetDatabase.ImportAsset(p);
  953.                     texturePaths.Add(p);
  954.                 }
  955.  
  956.                 if (absPaths[i].Contains("AO"))
  957.                 {
  958.                     Texture2D t = ImportJPG(absPaths[i]);
  959.                     p += "AO" + ".png";
  960.                     File.WriteAllBytes(p, t.EncodeToPNG());
  961.                     AssetDatabase.ImportAsset(p);
  962.                     texturePaths.Add(p);
  963.                 }
  964.  
  965.                 //create masks
  966.                 if (absPaths[i].Contains("Gloss") || absPaths[i].Contains("Roughness"))
  967.                 {
  968.                     Texture2D t = ImportJPG(absPaths[i]);
  969.                     Color[] pixels = t.GetPixels();
  970.                     //if gloss or roughness...
  971.                     if (absPaths[i].Contains("Gloss"))
  972.                     {
  973.                         glossFound = true;
  974.                         for (int j = 0; j < pixels.Length; ++j)
  975.                         {
  976.                             if (IterationCounter(j, pixels.Length - 1, 100))
  977.                             {
  978.                                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Packing Gloss...", (float)j / (pixels.Length - 1));
  979.                             }
  980.                             float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
  981.                             pixels[j].a = gloss;
  982.                         }
  983.                     }
  984.                     if (absPaths[i].Contains("Roughness"))
  985.                     {
  986.                         if (glossFound)
  987.                         {
  988.                             continue;
  989.                         }
  990.                         for (int j = 0; j < pixels.Length; ++j)
  991.                         {
  992.                             if (IterationCounter(j, pixels.Length - 1, 100))
  993.                             {
  994.                                 EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Packing Gloss...", (float)j / (pixels.Length - 1));
  995.                             }
  996.                             float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
  997.                             pixels[j].a = 1.0f - gloss;
  998.                         }
  999.                     }
  1000.                     //do metallic...
  1001.                     for (int j = 0; j < absPaths.Count; ++j)
  1002.                     {
  1003.                         if (absPaths[j].Contains("Metal"))
  1004.                         {
  1005.                             metallicFound = true;
  1006.                             Color[] mPixels = ImportJPG(absPaths[j]).GetPixels();
  1007.                             for (int k = 0; k < pixels.Length; ++k)
  1008.                             {
  1009.                                 if (IterationCounter(j, pixels.Length - 1, 100))
  1010.                                 {
  1011.                                     EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Metallic with Gloss...", (float)j / (pixels.Length - 1));
  1012.                                 }
  1013.                                 float metal = (mPixels[j].r + mPixels[j].g + mPixels[j].b) / 3.0f;
  1014.                                 pixels[j].r = metal;
  1015.                                 pixels[j].g = metal;
  1016.                                 pixels[j].b = metal;
  1017.                             }
  1018.                         }
  1019.                         if (absPaths[i].Contains("Specular") && texPack > 0)
  1020.                         {
  1021.                             Color[] sPixels = ImportJPG(absPaths[j]).GetPixels();
  1022.                             for (int k = 0; k < pixels.Length; ++k)
  1023.                             {
  1024.                                 if (IterationCounter(j, pixels.Length - 1, 100))
  1025.                                 {
  1026.                                     EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Specular with Gloss...", (float)j / (pixels.Length - 1));
  1027.                                 }
  1028.                                 pixels[j].r = sPixels[j].r;
  1029.                                 pixels[j].g = sPixels[j].g;
  1030.                                 pixels[j].b = sPixels[j].b;
  1031.                             }
  1032.                         }
  1033.                     }
  1034.  
  1035.                     if (!metallicFound)
  1036.                     {
  1037.                         for (int j = 0; j < pixels.Length; ++j)
  1038.                         {
  1039.                             pixels[j].r = 0.0f;
  1040.                             pixels[j].g = 0.0f;
  1041.                             pixels[j].b = 0.0f;
  1042.                         }
  1043.                     }
  1044.                     Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
  1045.                     nt.SetPixels(pixels);
  1046.                     p += "Metallic" + ".png";
  1047.                     if (texPack > 0)
  1048.                     {
  1049.                         p.Replace("Metallic", "Specular");
  1050.                     }
  1051.                     File.WriteAllBytes(p, nt.EncodeToPNG());
  1052.                     AssetDatabase.ImportAsset(p);
  1053.                     texturePaths.Add(p);
  1054.                 }
  1055.  
  1056.                 if ((absPaths[i].Contains("Normal")) && (!absPaths[i].Contains("Bump")))
  1057.                 {
  1058.                     p += "Normal" + ".png";
  1059.                     Texture2D normal = ImportJPG(absPaths[i]);
  1060.                     File.WriteAllBytes(p, normal.EncodeToPNG());
  1061.                     AssetDatabase.ImportAsset(p);
  1062.                     texturePaths.Add(p);
  1063.                 }
  1064.             }
  1065.             AssetDatabase.Refresh();
  1066.         }
  1067.  
  1068.         static Texture2D ImportJPG(string absPath)
  1069.         {
  1070.             byte[] texData = File.ReadAllBytes(absPath);
  1071.             Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBAFloat, true);
  1072.             tex.LoadImage(texData);
  1073.             return tex;
  1074.         }
  1075.  
  1076.         static void ImportEXR(string absolutePath, string name)
  1077.         {
  1078.             string relativePath = texturePath + Path.PathSeparator + globalPrefix + "_" + "Displacement";
  1079.             File.Copy(absolutePath, CleanUp(relativePath) + ".exr", true);
  1080.         }
  1081.  
  1082.         static void ImportMesh(string absolutePath, string relativePath, string name)
  1083.         {
  1084.             string[] n = name.Split(Path.PathSeparator);
  1085.             string n2 = CleanUp(n[n.Length - 1], true);
  1086.             string newPath = FixSlashes(Path.Combine(meshesPath, globalPrefix));
  1087.             if (plant)
  1088.             {
  1089.                 newPath += "_" + n2;
  1090.             }
  1091.             string absPath = absolutePath;
  1092.             int ld = FindLOD(absPath);
  1093.  
  1094.             newPath += "_" + "LOD" + ld.ToString();
  1095.  
  1096.             if (absolutePath.ToLower().Contains(".fbx"))
  1097.             {
  1098.                 newPath += ".fbx";
  1099.             }
  1100.             else
  1101.             {
  1102.                 newPath += ".obj";
  1103.             }
  1104.  
  1105.             if (ld == 6)
  1106.             {
  1107.                 //import the super highpoly mesh.
  1108.             }
  1109.             else
  1110.             {
  1111.                 for (int i = ld; i < 6; ++i)
  1112.                 {
  1113.                     string nld = "_LOD" + i.ToString();
  1114.                     if (File.Exists(absPath))
  1115.                     {
  1116.                         File.Copy(absPath, newPath, true);
  1117.                         absPath = absPath.Replace(nld, "_LOD" + (i + 1).ToString());
  1118.                         newPath = newPath.Replace(nld, "_LOD" + (i + 1).ToString());
  1119.                         meshesPaths.Add(CleanUp(newPath));
  1120.                     }
  1121.                 }
  1122.             }
  1123.         }
  1124.  
  1125.         static List<int> CheckRes(List<string> paths)
  1126.         {
  1127.             List<int> resolutions = new List<int>();
  1128.             for (int i = 0; i < paths.Count; ++i)
  1129.             {
  1130.                 int res = 0;
  1131.                 if (path.Contains("1k"))
  1132.                 {
  1133.                     res = 1024;
  1134.                 }
  1135.                 if (path.Contains("2k"))
  1136.                 {
  1137.                     res = 2048;
  1138.                 }
  1139.                 if (path.Contains("4k"))
  1140.                 {
  1141.                     res = 4096;
  1142.                 }
  1143.                 if (path.Contains("8k"))
  1144.                 {
  1145.                     res = 8192;
  1146.                 }
  1147.                 resolutions.Add(res);
  1148.             }
  1149.             return resolutions;
  1150.         }
  1151.  
  1152.         static int CheckRes(string path)
  1153.         {
  1154.             int res = 0;
  1155.             if (path.Contains("1k"))
  1156.             {
  1157.                 res = 1024;
  1158.             }
  1159.             if (path.Contains("2k"))
  1160.             {
  1161.                 res = 2048;
  1162.             }
  1163.             if (path.Contains("4k"))
  1164.             {
  1165.                 res = 4096;
  1166.             }
  1167.             if (path.Contains("8k"))
  1168.             {
  1169.                 res = 8192;
  1170.             }
  1171.             return res;
  1172.         }
  1173.  
  1174.         static int FindLOD(string path)
  1175.         {
  1176.             path = path.Replace(".fbx", "");
  1177.             path = path.Replace(".obj", "");
  1178.             path = path.Replace(".FBX", "");
  1179.             path = path.Replace(".OBJ", "");
  1180.             int ld = 6;
  1181.             if (path.EndsWith("0"))
  1182.             {
  1183.                 ld = 0;
  1184.             }
  1185.             if (path.EndsWith("1"))
  1186.             {
  1187.                 ld = 1;
  1188.             }
  1189.             else if (path.EndsWith("2"))
  1190.             {
  1191.                 ld = 2;
  1192.             }
  1193.             else if (path.EndsWith("3"))
  1194.             {
  1195.                 ld = 3;
  1196.             }
  1197.             else if (path.EndsWith("4"))
  1198.             {
  1199.                 ld = 4;
  1200.             }
  1201.             else if (path.EndsWith("5"))
  1202.             {
  1203.                 ld = 5;
  1204.             }
  1205.             return ld;
  1206.         }
  1207.  
  1208.         static string FixSlashes(string txt)
  1209.         {
  1210.             //txt = txt.Replace(Path.DirectorySeparatorChar, "/");
  1211.             //txt = txt.Replace(Path.DirectorySeparatorChar, "/");
  1212.             return txt;
  1213.         }
  1214.  
  1215.         static string CleanUp(string path, bool nukeLODs = false)
  1216.         {
  1217.             //Nuke the "LOD" from mesh strings.
  1218.             if (nukeLODs)
  1219.             {
  1220.                 path = path.Replace("_LOD0", "");
  1221.                 path = path.Replace("_LOD1", "");
  1222.                 path = path.Replace("_LOD2", "");
  1223.                 path = path.Replace("_LOD3", "");
  1224.                 path = path.Replace("_LOD4", "");
  1225.                 path = path.Replace("_LOD5", "");
  1226.                 path = path.Replace("_LOD6", "");
  1227.             }
  1228.  
  1229.             path = path.Replace(".fbx", "");
  1230.             path = path.Replace(".FBX", "");
  1231.             path = path.Replace(".obj", "");
  1232.             path = path.Replace(".OBJ", "");
  1233.  
  1234.             path = path.Replace(".jpg", "");
  1235.             path = path.Replace(".exr", "");
  1236.  
  1237.             //Nuke the package key from strings.
  1238.             //path = path.Replace(key, "");
  1239.             return path;
  1240.         }
  1241.  
  1242.         public static string ConstructPath(string[] textArray)
  1243.         {
  1244.             //i fucked something up in here, fix when get back!!!!!!
  1245.             GetPresets();
  1246.  
  1247.             string defPath = path;
  1248.  
  1249.             defPath = FixSlashes(defPath);
  1250.             string[] pathParts = defPath.Split(Path.DirectorySeparatorChar);
  1251.             defPath = "Assets";
  1252.             for (int i = 0; i < pathParts.Length; ++i)
  1253.             {
  1254.                 if (pathParts[i].Length > 0)
  1255.                 {
  1256.                     if (AssetDatabase.IsValidFolder(defPath))
  1257.                     {
  1258.                         string tempPath = Path.Combine(defPath, pathParts[i]);
  1259.                         tempPath = FixSlashes(tempPath);
  1260.                         if (!AssetDatabase.IsValidFolder(tempPath))
  1261.                         {
  1262.                             string newPath = AssetDatabase.CreateFolder(defPath, pathParts[i]);
  1263.                             defPath = AssetDatabase.GUIDToAssetPath(newPath);
  1264.                         }
  1265.                         else
  1266.                         {
  1267.                             defPath = FixSlashes(tempPath);
  1268.                         }
  1269.                     }
  1270.                 }
  1271.             }
  1272.  
  1273.             string assetPath = FixSlashes(textArray[2]);
  1274.             string[] assetPathParts = assetPath.Split(Path.DirectorySeparatorChar);
  1275.             string assetType = "Surface";
  1276.             string tp = FixSlashes(textArray[2]);
  1277.             if(tp.ToLower().Contains("3d"))
  1278.             {
  1279.                 assetType = "3D_Asset";
  1280.             }
  1281.             if(tp.ToLower().Contains("debris")||
  1282.                 tp.ToLower().Contains("dbrs")||
  1283.                 tp.ToLower().Contains("scatter")||
  1284.                 tp.ToLower().Contains("sctr"))
  1285.             {
  1286.                 assetType = "3D_Scatter_Assets";
  1287.             }
  1288.             if(tp.ToLower().Contains("plant"))
  1289.             {
  1290.                 assetType = "3D_Plants";
  1291.             }
  1292.             assetPath = FixSlashes(Path.Combine(defPath, assetType));
  1293.             if (!AssetDatabase.IsValidFolder(assetPath))
  1294.             {
  1295.                 assetPath = AssetDatabase.CreateFolder(defPath, assetType);
  1296.                 assetPath = AssetDatabase.GUIDToAssetPath(assetPath);
  1297.             }
  1298.             defPath = assetPath;
  1299.  
  1300.             assetPath = FixSlashes(Path.Combine(defPath, assetPathParts[assetPathParts.Length - 1]));
  1301.             if (!AssetDatabase.IsValidFolder(assetPath))
  1302.             {
  1303.                 assetPath = AssetDatabase.CreateFolder(defPath, assetPathParts[assetPathParts.Length - 1]);
  1304.                 assetPath = AssetDatabase.GUIDToAssetPath(assetPath);
  1305.             }
  1306.  
  1307.             int shaderType = EditorPrefs.GetInt("QuixelDefaultShader");
  1308.             if (shaderType == 3)
  1309.             {
  1310.                 //attempt to auto-detect a settings file for Lightweight or HD pipelines
  1311.                 if (AssetDatabase.IsValidFolder("Assets/Settings"))
  1312.                 {
  1313.                     if (AssetDatabase.LoadAssetAtPath("Assets/Settings/HDRenderPipelineAsset.asset", typeof(ScriptableObject)))
  1314.                     {
  1315.                         shaderType = 0;
  1316.                     }
  1317.                     else if (AssetDatabase.LoadAssetAtPath("Assets/Settings/Lightweight_RenderPipeline.asset", typeof(ScriptableObject)))
  1318.                     {
  1319.                         shaderType = 1;
  1320.                     }
  1321.                     else
  1322.                     {
  1323.                         shaderType = 2;
  1324.                     }
  1325.                 }
  1326.             }
  1327.  
  1328.             return FixSlashes(assetPath);
  1329.         }
  1330.     }
  1331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement