/// <summary> /// This function attempts to create a folder name /// </summary> void GetAssetFolderName(JObject objectList) { assetName = (string)objectList["name"]; id = (string)objectList["id"]; assetResolution = ""; try { string namingConvention = (string)objectList["namingConvention"]; finalName = namingConvention; if (namingConvention != "" && namingConvention != null) { if (namingConvention.Contains("$id")) { finalName = finalName.Replace("$id", id); } if (namingConvention.Contains("$name")) { finalName = finalName.Replace("$name", assetName); } //Add support for $matName when it is finalized. if (namingConvention.Contains("$matName")) { finalName = finalName.Replace("$matName", ""); } if (namingConvention.Contains("$type")) { finalName = finalName.Replace("$type", (string)objectList["type"]); } if (namingConvention.Contains("$resolution")) { assetResolution = (string)objectList["resolution"]; } return; } } catch (Exception ex) { Debug.Log(ex); MegascansUtilities.HideProgressBar(); } finalName = finalName.Replace("__", "_").Replace(".", ""); finalName = MegascansUtilities.FixSpaces(new string[] { assetName.Replace(" ", "_"), id }); }
/// <summary> /// This function attempts to create a folder name /// </summary> void GetAssetFolderName(JObject objectList) { try { assetResolution = ""; string namingConvention = (string)objectList["namingConvention"]; finalName = namingConvention; if (namingConvention != "" && namingConvention != null) { if (namingConvention.Contains("$id")) { finalName = finalName.Replace("$id", (string)objectList["id"]); } if (namingConvention.Contains("$name")) { finalName = finalName.Replace("$name", (string)objectList["name"]); } if (namingConvention.Contains("$type")) { finalName = finalName.Replace("$type", (string)objectList["type"]); } if (namingConvention.Contains("$resolution")) { assetResolution = (string)objectList["resolution"]; } return; } } catch (Exception ex) { Debug.Log(ex); MegascansUtilities.HideProgressBar(); } //then create a unique subfolder for the asset. assetName = (string)objectList["name"]; id = (string)objectList["id"]; finalName = MegascansUtilities.FixSpaces(new string[] { assetName.Replace(" ", "_"), id }); Debug.Log("Final name:" + finalName); }
/// <summary> /// Takes an imported JSON object, and breaks it into relevant components and data. /// Then calls relevant functions for actual import of asset. /// </summary> /// <param name="objectList"></param> public string ImportMegascansAssets(JObject objectList) { var startTime = System.DateTime.Now; string activeLOD = (string)objectList["activeLOD"]; string minLOD = (string)objectList["minLOD"]; isAlembic = false; plant = false; highPoly = false; hasBillboardLOD = false; hasBillboardLODOnly = false; mapName = ""; //get texture components from the current object. JArray textureComps = (JArray)objectList["components"]; //get mesh components from the current object. JArray meshComps = (JArray)objectList["meshList"]; //Map name overrides. mapNames = (JObject)objectList["mapNameOverride"]; //run a check to see if we're using Unity 5 or below, and then if we're trying to import a high poly mesh. if so, let the user know we are aborting the import. if (meshComps.Count > 0) { isAlembic = Path.GetExtension((string)meshComps[0]["path"]) == ".abc"; } hasBillboardLOD = MegascansMeshUtils.ContainsLowestLOD((JArray)objectList["lodList"], minLOD); assetType = (int)objectList["meshVersion"]; type = (string)objectList["type"]; if (type.ToLower().Contains("3dplant")) { plant = true; if (minLOD == activeLOD) { hasBillboardLODOnly = true; } } try { LoadPreferences(); MegascansUtilities.CalculateNumberOfOperations(objectList, dispType, texPack, shaderType, generateTerrainNormal, hasBillboardLODOnly); path = ConstructPath(objectList); if (path == null || path == "") { Debug.Log("Asset: " + (string)objectList["name"] + " already exist in the Project. Please delete/rename the existing folder and re-import this asset."); AssetDatabase.Refresh(); return(null); } GetShaderType(); } catch (Exception ex) { Debug.Log("Error setting import path."); Debug.Log(ex.ToString()); MegascansUtilities.HideProgressBar(); } try { //process textures ProcessTextures(textureComps); if (finalMat == null && !(plant && hasBillboardLODOnly)) { Debug.Log("The import path is incorrect. Asset import aborted."); return(null); } else { if (type.ToLower().Contains("surface") && applyToSelection) { foreach (MeshRenderer render in MegascansUtilities.GetSelectedMeshRenderers()) { render.material = finalMat; } } } } catch (Exception ex) { Debug.Log("Error importing textures."); Debug.Log(ex.ToString()); MegascansUtilities.HideProgressBar(); } //process meshes if (meshComps == null && !type.Contains("surface")) { Debug.LogError("No meshes found. Please double check your export settings."); Debug.Log("Import failed."); return(null); } if (meshComps.Count > 0) { if (activeLOD == "high") { //detect if we're trying to import a high poly mesh... string msg = "You are about to import a high poly mesh. \nThese meshes are usually millions of polygons and can cause instability to your project. \nWould you like to proceed?"; if (EditorUtility.DisplayDialog("WARNING!", msg, "Yes", "No")) { #if UNITY_EDITOR_WIN hpMat = new Material(finalMat.shader); hpMat.CopyPropertiesFromMaterial(finalMat); hpMat.SetTexture("_NormalMap", null); hpMat.SetTexture("_BumpMap", null); hpMat.DisableKeyword("_NORMALMAP_TANGENT_SPACE"); hpMat.DisableKeyword("_NORMALMAP"); hpMat.name = MegascansUtilities.FixSpaces(new string[] { hpMat.name, "HighPoly" }); string hpMatDir = MegascansUtilities.FixSpaces(new string[] { matPath, "HighPoly.mat" }); AssetDatabase.CreateAsset(hpMat, hpMatDir); #endif highPoly = true; } } try { //process meshes and prefabs PrefabData prefData = new PrefabData(path, finalName, lodFadeMode, highPoly, addAssetToScene, setupCollision, hasBillboardLOD, isAlembic, false, finalMat, hpMat, billboardMat, new List <string>(), new List <List <string> >()); MegascansMeshUtils.ProcessMeshes(objectList, path, highPoly, plant, prefData); } catch (Exception ex) { Debug.Log("Error importing meshes."); Debug.Log(ex.ToString()); MegascansUtilities.HideProgressBar(); } } var endTime = System.DateTime.Now; var totalTime = endTime - startTime; Debug.Log("Asset Import Time: " + totalTime); AssetDatabase.Refresh(); MegascansUtilities.HideProgressBar(); //Application.GarbageCollectUnusedAssets(); return(path); }
/// <summary> /// Process textures from Megascans asset import. /// </summary> /// <param name="textureComponents"></param> /// <returns></returns> void ProcessTextures(JArray textureComponents) { //create a subdirectory for textures. texPath = MegascansUtilities.ValidateFolderCreate(path, "Textures"); texPath += "/" + finalName.Replace(" ", "_").Replace("$lod", "").Replace("$variation", "").Replace("$resolution", assetResolution); matPath = MegascansUtilities.ValidateFolderCreate(path, "Materials"); matPath += "/" + finalName.Replace(" ", "_").Replace("$mapName", "").Replace("$resolution", "").Replace("$lod", "").Replace("$variation", ""); //Attempt to store all the paths we might need to get our textures. //It's quicker to do this, than create an array and loop through it continually using a lot of if-statements later on. string albedo = null; string opacity = null; string normals = null; string metallic = null; string specular = null; string AO = null; string gloss = null; string displacement = null; string roughness = null; string translucency = null; //Search the JSON array for each texture type, leave it null if it doesn't exist. This is important as we use the null check later. for (int i = 0; i < textureComponents.Count; ++i) { albedo = (string)textureComponents[i]["type"] == "albedo" ? (string)textureComponents[i]["path"] : albedo; albedo = (albedo == null && (string)textureComponents[i]["type"] == "diffuse") ? (string)textureComponents[i]["path"] : albedo; opacity = (string)textureComponents[i]["type"] == "opacity" ? (string)textureComponents[i]["path"] : opacity; normals = (string)textureComponents[i]["type"] == "normal" ? (string)textureComponents[i]["path"] : normals; metallic = (string)textureComponents[i]["type"] == "metalness" ? (string)textureComponents[i]["path"] : metallic; specular = (string)textureComponents[i]["type"] == "specular" ? (string)textureComponents[i]["path"] : specular; AO = (string)textureComponents[i]["type"] == "ao" ? (string)textureComponents[i]["path"] : AO; gloss = (string)textureComponents[i]["type"] == "gloss" ? (string)textureComponents[i]["path"] : gloss; displacement = (string)textureComponents[i]["type"] == "displacement" ? (string)textureComponents[i]["path"] : displacement; roughness = (string)textureComponents[i]["type"] == "roughness" ? (string)textureComponents[i]["path"] : roughness; translucency = (string)textureComponents[i]["type"] == "translucency" ? (string)textureComponents[i]["path"] : translucency; } //make sure we never try to import the high poly normalmap... if (normals != null) { for (int i = 0; i < 6; ++i) { string ld = "_LOD" + i.ToString(); string n = normals.Replace("Bump", ld); if (File.Exists(n)) { normals = n; break; } } } if (!(assetType > 1 && plant && hasBillboardLODOnly)) { finalMat = ReadWriteAllTextures(albedo, opacity, normals, metallic, specular, AO, gloss, displacement, roughness, translucency); } if (assetType > 1 && plant && hasBillboardLOD) { #if UNITY_EDITOR_WIN string[] pathParts = albedo.Split('\\'); #endif #if UNITY_EDITOR_OSX string[] pathParts = albedo.Split('/'); #endif string[] nameParts = pathParts[pathParts.Length - 1].Split('_'); albedo = albedo == null ? null : albedo.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); opacity = opacity == null ? null : opacity.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); normals = normals == null ? null : normals.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); metallic = metallic == null ? null : metallic.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); specular = specular == null ? null : specular.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); AO = AO == null ? null : AO.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); gloss = gloss == null ? null : gloss.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); displacement = displacement == null ? null : displacement.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); roughness = roughness == null ? null : roughness.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); translucency = translucency == null ? null : translucency.Replace("Atlas", "Billboard").Replace(nameParts[0], "Billboard"); texPath = MegascansUtilities.FixSpaces(new string[] { texPath, "Billboard" }); matPath = MegascansUtilities.FixSpaces(new string[] { matPath, "Billboard" }); billboardMat = ReadWriteAllTextures(albedo, opacity, normals, metallic, specular, AO, gloss, displacement, roughness, translucency); } }
/// <summary> /// Process textures from Megascans asset import. /// </summary> /// <param name="textureComponents"></param> /// <returns></returns> void ProcessTextures(JArray textureComponents, JArray texBBData) { //create a subdirectory for textures. string tempTexName = finalName.Replace(" ", "_").Replace("$lod", "").Replace("$variation", "").Replace("$resolution", assetResolution); texPath = Path.Combine(MegascansUtilities.ValidateFolderCreate(path, "Textures"), tempTexName); string tempMatName = finalName.Replace(" ", "_").Replace("$mapName", "").Replace("$resolution", "").Replace("$lod", "").Replace("$variation", ""); matPath = Path.Combine(MegascansUtilities.ValidateFolderCreate(path, "Materials"), tempMatName); //Attempt to store all the paths we might need to get our textures. //It's quicker to do this, than create an array and loop through it continually using a lot of if-statements later on. string albedo = null; string opacity = null; string normals = null; string metallic = null; string specular = null; string AO = null; string gloss = null; string displacement = null; string roughness = null; string translucency = null; string transmission = null; //Search the JSON array for each texture type, leave it null if it doesn't exist. This is important as we use the null check later. for (int i = 0; i < textureComponents.Count; ++i) { albedo = (string)textureComponents[i]["type"] == "albedo" ? (string)textureComponents[i]["path"] : albedo; albedo = (albedo == null && (string)textureComponents[i]["type"] == "diffuse") ? (string)textureComponents[i]["path"] : albedo; opacity = (string)textureComponents[i]["type"] == "opacity" ? (string)textureComponents[i]["path"] : opacity; normals = (string)textureComponents[i]["type"] == "normal" ? (string)textureComponents[i]["path"] : normals; metallic = (string)textureComponents[i]["type"] == "metalness" ? (string)textureComponents[i]["path"] : metallic; specular = (string)textureComponents[i]["type"] == "specular" ? (string)textureComponents[i]["path"] : specular; AO = (string)textureComponents[i]["type"] == "ao" ? (string)textureComponents[i]["path"] : AO; gloss = (string)textureComponents[i]["type"] == "gloss" ? (string)textureComponents[i]["path"] : gloss; displacement = (string)textureComponents[i]["type"] == "displacement" ? (string)textureComponents[i]["path"] : displacement; roughness = (string)textureComponents[i]["type"] == "roughness" ? (string)textureComponents[i]["path"] : roughness; translucency = (string)textureComponents[i]["type"] == "translucency" ? (string)textureComponents[i]["path"] : translucency; transmission = (string)textureComponents[i]["type"] == "transmission" ? (string)textureComponents[i]["path"] : transmission; } //make sure we never try to import the high poly normalmap... if (normals != null) { for (int i = 0; i < 6; ++i) { string ld = "_LOD" + i.ToString(); string n = normals.Replace("Bump", ld); if (File.Exists(n)) { normals = n; break; } } } if (!(assetType > 1 && plant && hasBillboardLODOnly)) { finalMat = ReadWriteAllTextures(albedo, opacity, normals, metallic, specular, AO, gloss, displacement, roughness, translucency, transmission); } if (importAllTextures) { for (int i = 0; i < textureComponents.Count; ++i) { string type = (string)textureComponents[i]["type"]; if (!importedTextures.Contains(type)) { ImportTexture(type, (string)textureComponents[i]["path"]); } } } if (assetType > 1 && plant && hasBillboardLOD) { //Search the JSON array for each texture type, leave it null if it doesn't exist. This is important as we use the null check later. for (int i = 0; i < texBBData.Count; ++i) { albedo = (string)texBBData[i]["type"] == "albedo" ? (string)texBBData[i]["path"] : albedo; albedo = (albedo == null && (string)texBBData[i]["type"] == "diffuse") ? (string)texBBData[i]["path"] : albedo; opacity = (string)texBBData[i]["type"] == "opacity" ? (string)texBBData[i]["path"] : opacity; normals = (string)texBBData[i]["type"] == "normal" ? (string)texBBData[i]["path"] : normals; metallic = (string)texBBData[i]["type"] == "metalness" ? (string)texBBData[i]["path"] : metallic; specular = (string)texBBData[i]["type"] == "specular" ? (string)texBBData[i]["path"] : specular; AO = (string)texBBData[i]["type"] == "ao" ? (string)texBBData[i]["path"] : AO; gloss = (string)texBBData[i]["type"] == "gloss" ? (string)texBBData[i]["path"] : gloss; displacement = (string)texBBData[i]["type"] == "displacement" ? (string)texBBData[i]["path"] : displacement; roughness = (string)texBBData[i]["type"] == "roughness" ? (string)texBBData[i]["path"] : roughness; translucency = (string)texBBData[i]["type"] == "translucency" ? (string)texBBData[i]["path"] : translucency; transmission = (string)texBBData[i]["type"] == "transmission" ? (string)texBBData[i]["path"] : transmission; } texPath = MegascansUtilities.FixSpaces(new string[] { texPath, "Billboard" }); matPath = MegascansUtilities.FixSpaces(new string[] { matPath, "Billboard" }); billboardMat = ReadWriteAllTextures(albedo, opacity, normals, metallic, specular, AO, gloss, displacement, roughness, translucency, transmission); if (importAllTextures) { for (int i = 0; i < texBBData.Count; ++i) { string type = (string)texBBData[i]["type"]; if (!importedTextures.Contains(type)) { ImportTexture(type, (string)texBBData[i]["path"]); } } } } }