Exemplo n.º 1
0
        /// <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;

            activeLOD = (string)objectList["activeLOD"];
            string minLOD = (string)objectList["minLOD"];

            assetName = (string)objectList["name"];
            type      = (string)objectList["type"];

            isAlembic              = false;
            plant                  = false;
            highPoly               = false;
            hasBillboardLOD        = false;
            hasBillboardLODOnly    = false;
            mapName                = "";
            folderNamingConvention = (string)objectList["folderNamingConvention"];

            //get mesh components from the current object.
            JArray meshComps = (JArray)objectList["meshList"];

            //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, activeLOD);

            if (type.ToLower().Contains("3dplant"))
            {
                plant = true;
                if (minLOD == activeLOD)
                {
                    hasBillboardLODOnly = true;
                }
            }

            try {
                LoadPreferences();
                shaderType = MegascansUtilities.GetShaderType();
                MegascansUtilities.CalculateNumberOfOperations(objectList, dispType, texPack, shaderType, 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);
                }
            } catch (Exception ex) {
                Debug.Log("Error setting import path.");
                Debug.Log(ex.ToString());
                MegascansUtilities.HideProgressBar();
            }

            try {
                //process textures
                ProcessTextures(objectList);
                if (finalMat == null && !(plant && hasBillboardLODOnly))
                {
                    Debug.Log("Could not import the textures and create the material.");
                    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?";
                    highPoly = EditorUtility.DisplayDialog("WARNING!", msg, "Yes", "No");
                }
                try {
                    //process meshes and prefabs
                    PrefabData prefData = new PrefabData(path, assetName, folderNamingConvention, lodFadeMode, highPoly, addAssetToScene, setupCollision, hasBillboardLOD, isAlembic, false, false, finalMat, 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();
            Resources.UnloadUnusedAssets();
            GC.Collect();
            return(path);
        }
Exemplo n.º 2
0
        /// <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);
        }