Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using UnityEditor.Experimental;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace Quixel
- {
- public class MegascansJsonImport
- {
- public static void ImportFromJSON(string jsonPath, bool usePath = true)
- {
- //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.
- string jArray = usePath ? File.ReadAllText(jsonPath) : jsonPath;
- Newtonsoft.Json.Linq.JArray testArray = Newtonsoft.Json.Linq.JArray.Parse(jArray);
- List<Newtonsoft.Json.Linq.JObject> objectList = new List<Newtonsoft.Json.Linq.JObject>();
- for (int i = 0; i < testArray.Count; ++i)
- {
- objectList.Add(testArray[i].ToObject<Newtonsoft.Json.Linq.JObject>());
- }
- for (int i = 0; i < objectList.Count; ++i)
- {
- //create a new text file...
- string qxl = "MegascansBundle";
- string path = (string)objectList[i]["path"];
- qxl += System.Environment.NewLine + (string)objectList[i]["id"];
- qxl += System.Environment.NewLine + path;
- qxl += System.Environment.NewLine + (string)objectList[i]["category"];
- qxl += System.Environment.NewLine + (string)objectList[i]["resolution"];
- bool plants = false;
- if (path.Contains("3dplant"))
- {
- plants = true;
- }
- Newtonsoft.Json.Linq.JArray comps = (Newtonsoft.Json.Linq.JArray)objectList[i]["components"];
- for (int j = 0; j < comps.Count; ++j)
- {
- string p = (string)comps[j]["path"];
- if (p != null)
- {
- p = p.Replace(path + Path.DirectorySeparatorChar, "");
- qxl += System.Environment.NewLine + p;
- if (plants)
- {
- string[] pb = p.Split('_');
- pb[0] = "Billboard";
- string bb = "Textures" + Path.DirectorySeparatorChar + "Billboard" + Path.DirectorySeparatorChar + "Billboard";
- for (int k = 1; k < pb.Length; ++k)
- {
- bb += "_" + pb[k];
- }
- qxl += System.Environment.NewLine + bb;
- }
- }
- }
- comps = (Newtonsoft.Json.Linq.JArray)objectList[i]["meshList"];
- for (int j = 0; j < comps.Count; ++j)
- {
- string p = (string)comps[j]["path"];
- if (p != null)
- {
- p = p.Replace(path + Path.DirectorySeparatorChar, "");
- qxl += System.Environment.NewLine + p;
- }
- }
- string[] textArray = qxl.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.None);
- MegascansImporter.ImportQXL(textArray);
- }
- }
- }
- [InitializeOnLoad]
- public class MegascansImporter : Editor
- {
- static private string path;
- static private string defPath;
- static private string assetPath;
- static private string texturePath;
- static private string meshesPath;
- static private string materialsPath;
- static private string prefabPath;
- static private List<string> meshesPaths = new List<string>();
- static private List<string> texturePaths = new List<string>();
- static private int texPack;
- static private int dispType;
- static private int shaderType;
- static private string globalPrefix;
- static private string userPrefix = EditorPrefs.GetString("QuixelDefaultPrefix");
- static private bool foliage;
- static private bool plant;
- static private Material finalMat;
- static private Material billboardMat;
- public static bool ImportQXL(string[] textArray)
- {
- bool success = true;
- Debug.Log("Reading .qxl file");
- path = ConstructPath(textArray);
- if (textArray[0].Contains("MegascansBundle"))
- {
- Debug.Log("Valid bundle file...");
- }
- else
- {
- Debug.LogError("Invalid bundle file.");
- }
- textArray[2] = FixSlashes(textArray[2]);
- plant = false;
- if (textArray[2].ToLower().Contains("3dplant"))
- {
- plant = true;
- }
- foliage = false;
- if (textArray[3].ToLower().Contains("foliage"))
- {
- foliage = true;
- }
- AssetDatabase.Refresh();
- List<string> absolutePaths = new List<string>();
- List<string> names = new List<string>();
- texturePath = FixSlashes(Path.Combine(path, "Textures"));
- materialsPath = FixSlashes(Path.Combine(path, "Materials"));
- meshesPath = FixSlashes(Path.Combine(path, "Models"));
- prefabPath = FixSlashes(Path.Combine(path, "Prefabs"));
- if (!AssetDatabase.IsValidFolder(texturePath))
- {
- texturePath = AssetDatabase.CreateFolder(path, "Textures");
- texturePath = AssetDatabase.GUIDToAssetPath(texturePath);
- }
- if (!AssetDatabase.IsValidFolder(materialsPath))
- {
- materialsPath = AssetDatabase.CreateFolder(path, "Materials");
- materialsPath = AssetDatabase.GUIDToAssetPath(materialsPath);
- }
- string[] getName = path.Split(Path.DirectorySeparatorChar);
- string matName = getName[getName.Length - 1];
- globalPrefix = "QXL_" + userPrefix + matName;
- bool containsMesh = false;
- for (int i = 4; i < textArray.Length; ++i)
- {
- string absolutePath = FixSlashes(Path.Combine(textArray[2], textArray[i]));
- if ((absolutePath.ToLower().Contains(".fbx") || absolutePath.ToLower().Contains(".obj")))
- {
- if (!AssetDatabase.IsValidFolder(meshesPath))
- {
- meshesPath = AssetDatabase.CreateFolder(path, "Models");
- meshesPath = AssetDatabase.GUIDToAssetPath(meshesPath);
- }
- if (!AssetDatabase.IsValidFolder(prefabPath))
- {
- prefabPath = AssetDatabase.CreateFolder(path, "Prefabs");
- prefabPath = AssetDatabase.GUIDToAssetPath(prefabPath);
- }
- ImportMesh(absolutePath, meshesPath, textArray[i]);
- containsMesh = true;
- }
- if (absolutePath.ToLower().Contains(".exr"))
- {
- ImportEXR(absolutePath, textArray[i]);
- }
- if (absolutePath.ToLower().Contains(".jpg"))
- {
- absolutePaths.Add(absolutePath);
- names.Add(textArray[i]);
- }
- }
- if (shaderType == 0)
- {
- PackTexturesHD(absolutePaths, names);
- }
- if (shaderType > 0)
- {
- PackTexturesLW(absolutePaths, names);
- }
- success = CreateMaterial();
- if (plant)
- {
- success = CreateMaterial(true);
- }
- if (containsMesh)
- {
- if (plant)
- {
- success = CreatePlantPrefabs();
- }
- else
- {
- success = CreatePrefabs();
- }
- }
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
- EditorUtility.ClearProgressBar();
- return success;
- }
- static void GetPresets()
- {
- path = EditorPrefs.GetString("QuixelDefaultPath");
- dispType = EditorPrefs.GetInt("QuixelDefaultDisplacement");
- texPack = EditorPrefs.GetInt("QuixelDefaultTexPacking");
- shaderType = EditorPrefs.GetInt("QuixelDefaultShader");
- userPrefix = EditorPrefs.GetString("QuixelDefaultPrefix");
- if (userPrefix.Length > 0)
- {
- userPrefix += "_";
- }
- }
- static void FixVertColors(GameObject go)
- {
- MeshFilter mf = go.transform.GetComponent<MeshFilter>();
- if( !mf )
- {
- mf = go.transform.GetChild(0).GetComponent<MeshFilter>();
- }
- if( !mf )
- {
- return;
- }
- Color[] vCols = mf.sharedMesh.colors;
- for (int i = 0; i < vCols.Length; ++i)
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Fixing Vertex Colors...", (float)i / (vCols.Length - 1));
- vCols[i] = new Color(vCols[i].r, vCols[i].r, vCols[i].r, vCols[i].r);
- }
- mf.sharedMesh.colors = vCols;
- }
- static bool CreatePlantPrefabs()
- {
- string[] p = new string[] { meshesPath };
- string[] s = AssetDatabase.FindAssets("t:Model", p);
- List<string> meshes = new List<string>();
- for (int i = 0; i < s.Length; ++i)
- {
- meshes.Add(AssetDatabase.GUIDToAssetPath(s[i]));
- }
- List<List<string>> variants = new List<List<string>>();
- for (int i = 0; i < meshes.Count; ++i)
- {
- List<string> v = new List<string>();
- for (int j = 0; j < meshes.Count; ++j)
- {
- string meshA = CleanUp(meshes[j], true);
- string meshB = CleanUp(meshes[i], true);
- if ((meshA.Length == meshB.Length) && meshA.Contains(meshB))
- {
- v.Add(meshes[j]);
- }
- }
- bool exists = false;
- for (int j = 0; j < variants.Count; ++j)
- {
- if (variants[j][0].ToLower().Contains(v[0].ToLower()))
- {
- exists = true;
- }
- }
- if (!exists)
- {
- variants.Add(v);
- }
- }
- for (int i = 0; i < variants.Count; ++i)
- {
- GameObject g = new GameObject();
- g.AddComponent<LODGroup>();
- string[] gns = variants[i][0].Split('/');
- string gn = CleanUp(gns[gns.Length - 1], true);
- g.name = gn;
- for (int j = 0; j < variants[i].Count; ++j)
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Gathering Variants...", (float)j / (variants.Count - 1));
- Object m = AssetDatabase.LoadAssetAtPath(variants[i][j], typeof(Object));
- GameObject mo = Object.Instantiate(m) as GameObject;
- mo.transform.parent = g.transform;
- mo.name = mo.name.Replace("(Clone)", "");
- }
- LOD[] lods = new LOD[g.transform.childCount];
- float lodHeight = 1.0f;
- for (int j = 0; j < g.transform.childCount; ++j)
- {
- EditorUtility.DisplayProgressBar("Creating Prefabs", "Creating LODs...", (float)j / (g.transform.childCount - 1));
- Renderer[] r = new Renderer[1];
- r[0] = g.transform.GetChild(j).GetComponent<Renderer>();
- if( ! r[0])
- {
- r[0] = g.transform.GetChild(j).GetChild(0).GetComponent<Renderer>();
- }
- r[0].material = finalMat;
- if (j == g.transform.childCount - 1)
- {
- r[0].material = billboardMat;
- }
- if (j < 1)
- {
- lodHeight *= 0.25f;
- }
- else
- {
- lodHeight *= 0.5f;
- }
- lods[j] = new LOD(lodHeight, r);
- }
- g.GetComponent<LODGroup>().SetLODs(lods);
- g.GetComponent<LODGroup>().RecalculateBounds();
- g.GetComponent<LODGroup>().fadeMode = LODFadeMode.CrossFade;
- g.GetComponent<LODGroup>().animateCrossFading = true;
- string prefabName = prefabPath + "/" + globalPrefix + i.ToString() + ".prefab";
- prefabName = prefabName.Replace("(Clone)", "");
- Object pf = AssetDatabase.LoadAssetAtPath(prefabName, typeof(Object));
- if (!pf)
- {
- PrefabUtility.CreatePrefab(prefabName, g);
- }
- else
- {
- PrefabUtility.ReplacePrefab(g, pf, ReplacePrefabOptions.ReplaceNameBased);
- }
- DestroyImmediate(g);
- }
- return true;
- }
- static bool CreatePrefabs(bool hasBillboard = false)
- {
- List<GameObject> ol = new List<GameObject>();
- if (!AssetDatabase.IsValidFolder(meshesPath))
- {
- Debug.LogWarning("Error 102: Could not read mesh folder...");
- return false;
- }
- string[] p = new string[] { meshesPath };
- string[] s = AssetDatabase.FindAssets("t:Model", p);
- for (int i = 0; i < s.Length; ++i)
- {
- string sap = AssetDatabase.GUIDToAssetPath(s[i]);
- Object o = AssetDatabase.LoadAssetAtPath<Object>(sap);
- ol.Add(Object.Instantiate(o) as GameObject);
- }
- List<List<Transform>> transforms = new List<List<Transform>>();
- for (int i = 0; i < ol.Count; ++i)
- {
- List<Transform> t = new List<Transform>();
- if (ol[i].transform.childCount > 0)
- {
- for (int j = 0; j < ol[i].transform.childCount; ++j)
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Gathering Variants...", (float)j / (ol[i].transform.childCount - 1));
- t.Add(ol[i].transform.GetChild(j));
- }
- }
- else
- {
- t.Add(ol[i].transform);
- }
- transforms.Add(t);
- }
- if ((transforms.Count < 1) || (transforms[0].Count < 1))
- {
- return false;
- }
- List<GameObject> gos = new List<GameObject>();
- for (int i = 0; i < transforms[0].Count; ++i)
- {
- GameObject go = new GameObject();
- go.AddComponent<LODGroup>();
- string[] ns = transforms[0][i].name.Split('/');
- go.name = CleanUp(ns[ns.Length - 1], true);
- gos.Add(go);
- }
- for (int i = 0; i < gos.Count; ++i)
- {
- for (int j = 0; j < transforms.Count; ++j)
- {
- if (transforms[j].Count > 0)
- {
- transforms[j][i].parent = gos[i].transform;
- transforms[j][i].transform.localPosition = Vector3.zero;
- }
- }
- }
- for (int i = 0; i < gos.Count; ++i)
- {
- LOD[] lods = new LOD[gos[i].transform.childCount];
- float lodHeight = 1.0f;
- for (int j = 0; j < gos[i].transform.childCount; ++j)
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Prefabs", "Creating LODs...", (float)j / (gos[i].transform.childCount - 1));
- Renderer[] r = new Renderer[1];
- r[0] = gos[i].transform.GetChild(j).GetComponent<Renderer>();
- r[0].material = finalMat;
- if (hasBillboard && (j == gos[i].transform.childCount - 1))
- {
- r[0].material = billboardMat;
- }
- if (gos[i].transform.childCount > 2)
- {
- if (j < (gos[i].transform.childCount / 2))
- {
- lodHeight *= 0.75f;
- }
- if (j >= (gos[i].transform.childCount / 2))
- {
- lodHeight *= 0.5f;
- }
- }
- else
- {
- if (j < 1)
- {
- lodHeight *= 0.75f;
- }
- else
- {
- lodHeight *= 0.15f;
- }
- }
- lods[j] = new LOD(lodHeight, r);
- //this should only add LODs to the first 2 meshes.
- if (j < 2)
- {
- if (!hasBillboard)
- {
- gos[i].transform.GetChild(j).gameObject.AddComponent<MeshCollider>().sharedMesh = gos[i].transform.GetChild(gos[i].transform.childCount - 1).GetComponent<MeshFilter>().sharedMesh;
- }
- }
- }
- gos[i].GetComponent<LODGroup>().SetLODs(lods);
- gos[i].GetComponent<LODGroup>().RecalculateBounds();
- gos[i].GetComponent<LODGroup>().fadeMode = LODFadeMode.CrossFade;
- gos[i].GetComponent<LODGroup>().animateCrossFading = true;
- gos[i].isStatic = true;
- string prefabName = prefabPath + "/" + globalPrefix + i.ToString() + ".prefab";
- prefabName = prefabName.Replace("(Clone)", "");
- Object pf = AssetDatabase.LoadAssetAtPath(prefabName, typeof(Object));
- if (!pf)
- {
- PrefabUtility.CreatePrefab(prefabName, gos[i]);
- }
- else
- {
- PrefabUtility.ReplacePrefab(gos[i], pf, ReplacePrefabOptions.ReplaceNameBased);
- }
- }
- for (int i = 0; i < ol.Count; ++i)
- {
- DestroyImmediate(ol[i]);
- }
- for (int i = 0; i < gos.Count; ++i)
- {
- DestroyImmediate(gos[i]);
- }
- return true;
- }
- static bool CreateMaterial(bool isBillboard = false)
- {
- if (!AssetDatabase.IsValidFolder(texturePath))
- {
- Debug.LogWarning("Error 101: Could not read from texture folder...");
- return false;
- }
- string rp = CleanUp(materialsPath + "/" + globalPrefix);
- if (isBillboard)
- {
- rp += "_Billboard";
- }
- rp += ".mat";
- Material mat = (Material)AssetDatabase.LoadAssetAtPath(rp, typeof(Material));
- if (!mat)
- {
- mat = new Material(Shader.Find("Standard"));
- AssetDatabase.CreateAsset(mat, rp);
- AssetDatabase.Refresh();
- }
- if (shaderType < 1)
- {
- mat.shader = Shader.Find("HDRenderPipeline/Lit");
- mat.SetInt("_DisplacementMode", dispType);
- }
- if (shaderType > 0)
- {
- mat.shader = Shader.Find("LightweightPipeline/Standard (Physically Based)");
- }
- if (shaderType > 1)
- {
- mat.shader = Shader.Find("Standard");
- }
- string[] texPath = new string[] { texturePath };
- string[] texPaths = AssetDatabase.FindAssets("t:Texture2D", texPath);
- for (int i = 0; i < texPaths.Length; ++i)
- {
- string p = AssetDatabase.GUIDToAssetPath(texPaths[i]);
- BillboardTexSort(p);
- }
- AssetDatabase.Refresh();
- return true;
- }
- static void BillboardTexSort(string p)
- {
- string[] path = new string[] { materialsPath };
- string[] mats = AssetDatabase.FindAssets("t:Material", path);
- for (int i = 0; i < mats.Length; ++i)
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Creating Material", "Assigning Textures...", (float)i / (mats.Length - 1));
- Material mat = (Material)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(mats[i]), typeof(Material));
- if (mat.name.ToLower().Contains("billboard") && p.ToLower().Contains("billboard"))
- {
- if (shaderType == 0)
- {
- HDMaterialAssign(mat, p);
- }
- if (shaderType > 0)
- {
- LWMaterialAssign(mat, p);
- }
- billboardMat = mat;
- }
- if (!mat.name.ToLower().Contains("billboard") && !p.ToLower().Contains("billboard"))
- {
- if (shaderType == 0)
- {
- HDMaterialAssign(mat, p);
- }
- if (shaderType > 0)
- {
- LWMaterialAssign(mat, p);
- }
- finalMat = mat;
- }
- //UnityEditor.Experimental.Rendering.HDPipeline.HDEditorUtils.ResetMaterialKeywords(mat);
- }
- }
- static void HDMaterialAssign(Material mat, string p)
- {
- if (p.Contains("Albedo"))
- {
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_BaseColorMap", tex);
- if (p.Contains("Alpha"))
- {
- mat.SetInt("_AlphaCutoffEnable", 1);
- mat.SetFloat("_AlphaCutoff", 0.333f);
- mat.SetInt("_DoubleSidedEnable", 1);
- }
- }
- if (p.Contains("Masks"))
- {
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.sRGBTexture = false;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_MaskMap", tex);
- }
- if (p.Contains("Specular"))
- {
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_SpecularColorMap", tex);
- }
- if (p.Contains("Normal"))
- {
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.textureType = TextureImporterType.NormalMap;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_NormalMap", tex);
- }
- if (p.Contains("Fuzz"))
- {
- mat.SetInt("_MaterialID", 0);
- mat.SetInt("_DiffusionProfile", 2);
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.sRGBTexture = false;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_SubsurfaceMaskMap", tex);
- }
- if (p.Contains("Translucency"))
- {
- mat.SetInt("_MaterialID", 0);
- mat.SetInt("_DiffusionProfile", 1);
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.sRGBTexture = false;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_SubsurfaceMaskMap", tex);
- mat.SetTexture("_ThicknessMap", tex);
- if (foliage)
- {
- mat.SetInt("_DiffusionProfile", 2);
- mat.SetFloat("_CoatMask", 0.0f);
- mat.SetInt("_EnableWind", 1);
- }
- }
- int dispType = MegascansImporterWindow.GetDispType();
- if (dispType > 0)
- {
- mat.shader = Shader.Find("HDRenderPipeline/LitTesselaton");
- if (p.Contains("Displacement"))
- {
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_HeightMap", tex);
- }
- }
- }
- static void LWMaterialAssign(Material mat, string p)
- {
- if (p.Contains("Albedo"))
- {
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_MainTex", tex);
- if (p.Contains("Alpha"))
- {
- mat.SetInt("_Mode", 1);
- }
- }
- if (p.Contains("Metallic") && (texPack == 0))
- {
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.sRGBTexture = false;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_MetallicGlossMap", tex);
- }
- if (p.Contains("Specular") && (texPack == 1))
- {
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_SpecGlossMap", tex);
- }
- if (p.Contains("Normal"))
- {
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.textureType = TextureImporterType.NormalMap;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_BumpMap", tex);
- }
- if (p.Contains("AO"))
- {
- TextureImporter tImp = AssetImporter.GetAtPath(p) as TextureImporter;
- tImp.sRGBTexture = false;
- AssetDatabase.ImportAsset(p);
- Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(p, typeof(Texture2D));
- mat.SetTexture("_OcclusionMap", tex);
- }
- }
- static bool IterationCounter( int currentIter, int maxIter, int iterCheck)
- {
- if(currentIter % (maxIter / iterCheck) == 0 && currentIter != 0)
- {
- return true;
- }
- return false;
- }
- static void PackTexturesHD(List<string> absPaths, List<string> names)
- {
- bool metallicFound = false;
- bool glossFound = false;
- bool AOFound = false;
- for (int i = 0; i < absPaths.Count; ++i)
- {
- string p = CleanUp(texturePath + "/" + globalPrefix + "_");
- if (absPaths[i].ToLower().Contains("billboard") && plant)
- {
- p += "Billboard_";
- }
- //create albedo
- if (absPaths[i].Contains("Albedo"))
- {
- Texture2D t = ImportJPG(absPaths[i]);
- Color[] pixels = t.GetPixels();
- Color[] oPixels = new Color[pixels.Length];
- bool hasAlpha = false;
- p += "Albedo";
- for (int j = 0; j < absPaths.Count; ++j)
- {
- if (absPaths[j].Contains("Opacity"))
- {
- if ((!absPaths[i].ToLower().Contains("billboard") && !absPaths[j].ToLower().Contains("billboard")) ||
- (absPaths[i].ToLower().Contains("billboard") && absPaths[j].ToLower().Contains("billboard")))
- {
- Texture2D o = ImportJPG(absPaths[j]);
- oPixels = o.GetPixels();
- hasAlpha = true;
- }
- }
- }
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length-1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Albedo...", (float)j / (pixels.Length - 1));
- }
- float o = 1.0f;
- if(hasAlpha)
- {
- o = oPixels[j].r;
- }
- pixels[j].a = o;
- }
- if (hasAlpha)
- {
- p += "_Alpha";
- }
- Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
- nt.SetPixels(pixels);
- p += ".png";
- File.WriteAllBytes(p, nt.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- //create masks
- if (absPaths[i].Contains("Gloss") || absPaths[i].Contains("Roughness"))
- {
- Texture2D t = ImportJPG(absPaths[i]);
- Color[] pixels = t.GetPixels();
- //if gloss or roughness...
- if (absPaths[i].Contains("Gloss"))
- {
- glossFound = true;
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Gloss...", (float)j / (pixels.Length - 1));
- }
- float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
- pixels[j].a = gloss;
- }
- }
- if (absPaths[i].Contains("Roughness"))
- {
- if (glossFound)
- {
- continue;
- }
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Gloss...", (float)j / (pixels.Length - 1));
- }
- float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
- pixels[j].a = 1.0f - gloss;
- }
- }
- //do metallic...
- for (int j = 0; j < absPaths.Count; ++j)
- {
- if (absPaths[j].Contains("Metal"))
- {
- metallicFound = true;
- Color[] mPixels = ImportJPG(absPaths[j]).GetPixels();
- for (int k = 0; k < pixels.Length; ++k)
- {
- if (IterationCounter(k, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Metallic...", (float)j / (pixels.Length - 1));
- }
- float metal = (mPixels[j].r + mPixels[j].g + mPixels[j].b) / 3.0f;
- pixels[j].r = metal;
- }
- }
- if (absPaths[j].Contains("AO"))
- {
- AOFound = true;
- Color[] aoPixels = ImportJPG(absPaths[j]).GetPixels();
- for (int k = 0; k < pixels.Length; ++k)
- {
- if (IterationCounter(k, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask AO...", (float)j / (pixels.Length - 1));
- }
- float ao = (aoPixels[j].r + aoPixels[j].g + aoPixels[j].b) / 3.0f;
- pixels[j].g = ao;
- }
- }
- }
- if (!metallicFound)
- {
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Metallic...", (float)j / (pixels.Length - 1));
- }
- pixels[j].r = 1.0f;
- }
- }
- if (!AOFound)
- {
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Mask Metallic...", (float)j / (pixels.Length - 1));
- }
- pixels[j].g = 1.0f;
- }
- }
- for (int j = 0; j < pixels.Length; ++j)
- {
- pixels[j].b = 0.0f;
- }
- Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
- nt.SetPixels(pixels);
- p += "Masks" + ".png";
- File.WriteAllBytes(p, nt.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- if ((absPaths[i].Contains("Normal")) && (!absPaths[i].Contains("Bump")))
- {
- Texture2D normal = ImportJPG(absPaths[i]);
- p += "Normal" + ".png";
- File.WriteAllBytes(p, normal.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- if (absPaths[i].Contains("Specular"))
- {
- if (texPack > 0)
- {
- Texture2D spec = ImportJPG(absPaths[i]);
- p += "Specular" + ".png";
- File.WriteAllBytes(p, spec.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- }
- if (absPaths[i].Contains("Fuzz"))
- {
- Texture2D fuzz = ImportJPG(absPaths[i]);
- p += "Fuzz" + ".png";
- File.WriteAllBytes(p, fuzz.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- if (absPaths[i].Contains("Translucency"))
- {
- Texture2D trans = ImportJPG(absPaths[i]);
- Color[] pixels = trans.GetPixels();
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Translucency...", (float)j / (pixels.Length - 1));
- }
- float transP = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
- pixels[j].r = transP;
- pixels[j].g = transP;
- pixels[j].b = transP;
- }
- trans.SetPixels(pixels);
- p += "Translucency" + ".png";
- File.WriteAllBytes(p, trans.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- }
- AssetDatabase.Refresh();
- }
- static void PackTexturesLW(List<string> absPaths, List<string> names)
- {
- bool metallicFound = false;
- bool glossFound = false;
- for (int i = 0; i < absPaths.Count; ++i)
- {
- string p = CleanUp(texturePath + "/" + globalPrefix + "_");
- if (absPaths[i].ToLower().Contains("billboard") && plant)
- {
- p += "Billboard_";
- }
- //create albedo
- if (absPaths[i].Contains("Albedo"))
- {
- p += "Albedo";
- Texture2D t = ImportJPG(absPaths[i]);
- Color[] pixels = t.GetPixels();
- Color[] oPixels = new Color[pixels.Length];
- bool hasAlpha = false;
- for (int j = 0; j < absPaths.Count; ++j)
- {
- if (absPaths[j].Contains("Opacity"))
- {
- if ((!absPaths[i].ToLower().Contains("billboard") && !absPaths[j].ToLower().Contains("billboard")) ||
- (absPaths[i].ToLower().Contains("billboard") && absPaths[j].ToLower().Contains("billboard")))
- {
- Texture2D o = ImportJPG(absPaths[j]);
- oPixels = o.GetPixels();
- hasAlpha = true;
- }
- }
- }
- for (int j = 0; j < pixels.Length; ++j)
- {
- float o = 1.0f;
- if (hasAlpha)
- {
- o = oPixels[j].r;
- }
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Albedo...", (float)j / (pixels.Length - 1));
- }
- pixels[j].a = o;
- }
- if (hasAlpha)
- {
- p += "_Alpha";
- }
- Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
- nt.SetPixels(pixels);
- p += ".png";
- File.WriteAllBytes(p, nt.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- if (absPaths[i].Contains("AO"))
- {
- Texture2D t = ImportJPG(absPaths[i]);
- p += "AO" + ".png";
- File.WriteAllBytes(p, t.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- //create masks
- if (absPaths[i].Contains("Gloss") || absPaths[i].Contains("Roughness"))
- {
- Texture2D t = ImportJPG(absPaths[i]);
- Color[] pixels = t.GetPixels();
- //if gloss or roughness...
- if (absPaths[i].Contains("Gloss"))
- {
- glossFound = true;
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Packing Gloss...", (float)j / (pixels.Length - 1));
- }
- float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
- pixels[j].a = gloss;
- }
- }
- if (absPaths[i].Contains("Roughness"))
- {
- if (glossFound)
- {
- continue;
- }
- for (int j = 0; j < pixels.Length; ++j)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Packing Gloss...", (float)j / (pixels.Length - 1));
- }
- float gloss = (pixels[j].r + pixels[j].g + pixels[j].b) / 3.0f;
- pixels[j].a = 1.0f - gloss;
- }
- }
- //do metallic...
- for (int j = 0; j < absPaths.Count; ++j)
- {
- if (absPaths[j].Contains("Metal"))
- {
- metallicFound = true;
- Color[] mPixels = ImportJPG(absPaths[j]).GetPixels();
- for (int k = 0; k < pixels.Length; ++k)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Metallic with Gloss...", (float)j / (pixels.Length - 1));
- }
- float metal = (mPixels[j].r + mPixels[j].g + mPixels[j].b) / 3.0f;
- pixels[j].r = metal;
- pixels[j].g = metal;
- pixels[j].b = metal;
- }
- }
- if (absPaths[i].Contains("Specular") && texPack > 0)
- {
- Color[] sPixels = ImportJPG(absPaths[j]).GetPixels();
- for (int k = 0; k < pixels.Length; ++k)
- {
- if (IterationCounter(j, pixels.Length - 1, 100))
- {
- EditorUtility.DisplayProgressBar(globalPrefix + " " + "Converting Textures", "Creating Specular with Gloss...", (float)j / (pixels.Length - 1));
- }
- pixels[j].r = sPixels[j].r;
- pixels[j].g = sPixels[j].g;
- pixels[j].b = sPixels[j].b;
- }
- }
- }
- if (!metallicFound)
- {
- for (int j = 0; j < pixels.Length; ++j)
- {
- pixels[j].r = 0.0f;
- pixels[j].g = 0.0f;
- pixels[j].b = 0.0f;
- }
- }
- Texture2D nt = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, true);
- nt.SetPixels(pixels);
- p += "Metallic" + ".png";
- if (texPack > 0)
- {
- p.Replace("Metallic", "Specular");
- }
- File.WriteAllBytes(p, nt.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- if ((absPaths[i].Contains("Normal")) && (!absPaths[i].Contains("Bump")))
- {
- p += "Normal" + ".png";
- Texture2D normal = ImportJPG(absPaths[i]);
- File.WriteAllBytes(p, normal.EncodeToPNG());
- AssetDatabase.ImportAsset(p);
- texturePaths.Add(p);
- }
- }
- AssetDatabase.Refresh();
- }
- static Texture2D ImportJPG(string absPath)
- {
- byte[] texData = File.ReadAllBytes(absPath);
- Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBAFloat, true);
- tex.LoadImage(texData);
- return tex;
- }
- static void ImportEXR(string absolutePath, string name)
- {
- string relativePath = texturePath + Path.PathSeparator + globalPrefix + "_" + "Displacement";
- File.Copy(absolutePath, CleanUp(relativePath) + ".exr", true);
- }
- static void ImportMesh(string absolutePath, string relativePath, string name)
- {
- string[] n = name.Split(Path.PathSeparator);
- string n2 = CleanUp(n[n.Length - 1], true);
- string newPath = FixSlashes(Path.Combine(meshesPath, globalPrefix));
- if (plant)
- {
- newPath += "_" + n2;
- }
- string absPath = absolutePath;
- int ld = FindLOD(absPath);
- newPath += "_" + "LOD" + ld.ToString();
- if (absolutePath.ToLower().Contains(".fbx"))
- {
- newPath += ".fbx";
- }
- else
- {
- newPath += ".obj";
- }
- if (ld == 6)
- {
- //import the super highpoly mesh.
- }
- else
- {
- for (int i = ld; i < 6; ++i)
- {
- string nld = "_LOD" + i.ToString();
- if (File.Exists(absPath))
- {
- File.Copy(absPath, newPath, true);
- absPath = absPath.Replace(nld, "_LOD" + (i + 1).ToString());
- newPath = newPath.Replace(nld, "_LOD" + (i + 1).ToString());
- meshesPaths.Add(CleanUp(newPath));
- }
- }
- }
- }
- static List<int> CheckRes(List<string> paths)
- {
- List<int> resolutions = new List<int>();
- for (int i = 0; i < paths.Count; ++i)
- {
- int res = 0;
- if (path.Contains("1k"))
- {
- res = 1024;
- }
- if (path.Contains("2k"))
- {
- res = 2048;
- }
- if (path.Contains("4k"))
- {
- res = 4096;
- }
- if (path.Contains("8k"))
- {
- res = 8192;
- }
- resolutions.Add(res);
- }
- return resolutions;
- }
- static int CheckRes(string path)
- {
- int res = 0;
- if (path.Contains("1k"))
- {
- res = 1024;
- }
- if (path.Contains("2k"))
- {
- res = 2048;
- }
- if (path.Contains("4k"))
- {
- res = 4096;
- }
- if (path.Contains("8k"))
- {
- res = 8192;
- }
- return res;
- }
- static int FindLOD(string path)
- {
- path = path.Replace(".fbx", "");
- path = path.Replace(".obj", "");
- path = path.Replace(".FBX", "");
- path = path.Replace(".OBJ", "");
- int ld = 6;
- if (path.EndsWith("0"))
- {
- ld = 0;
- }
- if (path.EndsWith("1"))
- {
- ld = 1;
- }
- else if (path.EndsWith("2"))
- {
- ld = 2;
- }
- else if (path.EndsWith("3"))
- {
- ld = 3;
- }
- else if (path.EndsWith("4"))
- {
- ld = 4;
- }
- else if (path.EndsWith("5"))
- {
- ld = 5;
- }
- return ld;
- }
- static string FixSlashes(string txt)
- {
- //txt = txt.Replace(Path.DirectorySeparatorChar, "/");
- //txt = txt.Replace(Path.DirectorySeparatorChar, "/");
- return txt;
- }
- static string CleanUp(string path, bool nukeLODs = false)
- {
- //Nuke the "LOD" from mesh strings.
- if (nukeLODs)
- {
- path = path.Replace("_LOD0", "");
- path = path.Replace("_LOD1", "");
- path = path.Replace("_LOD2", "");
- path = path.Replace("_LOD3", "");
- path = path.Replace("_LOD4", "");
- path = path.Replace("_LOD5", "");
- path = path.Replace("_LOD6", "");
- }
- path = path.Replace(".fbx", "");
- path = path.Replace(".FBX", "");
- path = path.Replace(".obj", "");
- path = path.Replace(".OBJ", "");
- path = path.Replace(".jpg", "");
- path = path.Replace(".exr", "");
- //Nuke the package key from strings.
- //path = path.Replace(key, "");
- return path;
- }
- public static string ConstructPath(string[] textArray)
- {
- //i fucked something up in here, fix when get back!!!!!!
- GetPresets();
- string defPath = path;
- defPath = FixSlashes(defPath);
- string[] pathParts = defPath.Split(Path.DirectorySeparatorChar);
- defPath = "Assets";
- for (int i = 0; i < pathParts.Length; ++i)
- {
- if (pathParts[i].Length > 0)
- {
- if (AssetDatabase.IsValidFolder(defPath))
- {
- string tempPath = Path.Combine(defPath, pathParts[i]);
- tempPath = FixSlashes(tempPath);
- if (!AssetDatabase.IsValidFolder(tempPath))
- {
- string newPath = AssetDatabase.CreateFolder(defPath, pathParts[i]);
- defPath = AssetDatabase.GUIDToAssetPath(newPath);
- }
- else
- {
- defPath = FixSlashes(tempPath);
- }
- }
- }
- }
- string assetPath = FixSlashes(textArray[2]);
- string[] assetPathParts = assetPath.Split(Path.DirectorySeparatorChar);
- string assetType = "Surface";
- string tp = FixSlashes(textArray[2]);
- if(tp.ToLower().Contains("3d"))
- {
- assetType = "3D_Asset";
- }
- if(tp.ToLower().Contains("debris")||
- tp.ToLower().Contains("dbrs")||
- tp.ToLower().Contains("scatter")||
- tp.ToLower().Contains("sctr"))
- {
- assetType = "3D_Scatter_Assets";
- }
- if(tp.ToLower().Contains("plant"))
- {
- assetType = "3D_Plants";
- }
- assetPath = FixSlashes(Path.Combine(defPath, assetType));
- if (!AssetDatabase.IsValidFolder(assetPath))
- {
- assetPath = AssetDatabase.CreateFolder(defPath, assetType);
- assetPath = AssetDatabase.GUIDToAssetPath(assetPath);
- }
- defPath = assetPath;
- assetPath = FixSlashes(Path.Combine(defPath, assetPathParts[assetPathParts.Length - 1]));
- if (!AssetDatabase.IsValidFolder(assetPath))
- {
- assetPath = AssetDatabase.CreateFolder(defPath, assetPathParts[assetPathParts.Length - 1]);
- assetPath = AssetDatabase.GUIDToAssetPath(assetPath);
- }
- int shaderType = EditorPrefs.GetInt("QuixelDefaultShader");
- if (shaderType == 3)
- {
- //attempt to auto-detect a settings file for Lightweight or HD pipelines
- if (AssetDatabase.IsValidFolder("Assets/Settings"))
- {
- if (AssetDatabase.LoadAssetAtPath("Assets/Settings/HDRenderPipelineAsset.asset", typeof(ScriptableObject)))
- {
- shaderType = 0;
- }
- else if (AssetDatabase.LoadAssetAtPath("Assets/Settings/Lightweight_RenderPipeline.asset", typeof(ScriptableObject)))
- {
- shaderType = 1;
- }
- else
- {
- shaderType = 2;
- }
- }
- }
- return FixSlashes(assetPath);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement