void ImportTexture(string type, string path)
 {
     if (File.Exists(path))
     {
         string texMapName  = (string)mapNames[Regex.Replace(type, @"\b([a-z])", m => m.Value.ToUpper())];
         string tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName) : texPath + texMapName;
         string importPath  = tempTexName + ".png";
         MegascansImageUtils.CreateTexture(path, importPath);
         MegascansImageUtils.TextureImportSetup(importPath, false, false);
     }
 }
Exemple #2
0
        /// <summary>
        /// Previous version of the importer would loop through a list of texture paths, and use a bunch of if-statements and do things accordingly.
        /// This version just takes in every texture path and if it's not null, does the thing. Less looping, better overall performance.
        /// </summary>
        /// <param name="albedo"></param>
        /// <param name="opacity"></param>
        /// <param name="normals"></param>
        /// <param name="metallic"></param>
        /// <param name="specular"></param>
        /// <param name="AO"></param>
        /// <param name="gloss"></param>
        /// <param name="displacement"></param>
        /// <param name="roughness"></param>
        /// <param name="translucency"></param>
        Material ReadWriteAllTextures(string albedo, string opacity, string normals, string metallic, string specular, string AO, string gloss, string displacement, string roughness, string translucency)
        {
            try {
                Material mat = CreateMaterial();

                if (mat == null)
                {
                    return(null);
                }

                //create a new work thread for each texture to be processed.
                //Pack the opacity into the alpha channel of albedo if it exists.
                string texMapName = (string)mapNames["Albedo"];
                tempTexName = texPath.Contains("$mapName")? texPath.Replace("$mapName", texMapName): texPath + texMapName;
                string p = tempTexName + ".png";
                mapName = opacity != null ? "Albedo + Alpha" : "Albedo";
                MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                Texture2D tex = MegascansImageUtils.PackTextures(albedo, opacity, p);

                mat.SetTexture("_MainTex", tex);
                mat.SetTexture("_BaseColorMap", tex);

                if (shaderType == 1)
                {
                    mat.SetTexture("_BaseMap", tex);
                    mat.SetColor("_BaseColor", Color.white);
                }

                if (opacity != null)
                {
                    if (shaderType > 0)
                    {
                        mat.SetFloat("_AlphaClip", 1);
                        mat.SetFloat("_Mode", 1);
                        mat.SetFloat("_Cull", 1);
                        mat.EnableKeyword("_ALPHATEST_ON");
                    }
                    else
                    {
                        mat.SetInt("_AlphaCutoffEnable", 1);
                        mat.SetFloat("_AlphaCutoff", 0.333f);
                        mat.SetInt("_DoubleSidedEnable", 1);

                        mat.SetOverrideTag("RenderType", "TransparentCutout");
                        mat.SetInt("_ZTestGBuffer", (int)UnityEngine.Rendering.CompareFunction.Equal);
                        mat.SetInt("_CullMode", (int)UnityEngine.Rendering.CullMode.Off);
                        mat.SetInt("_CullModeForward", (int)UnityEngine.Rendering.CullMode.Back);
                        mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                        mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                        mat.SetInt("_ZWrite", 1);
                        mat.renderQueue = 2450;
                        mat.SetInt("_ZTestGBuffer", (int)UnityEngine.Rendering.CompareFunction.Equal);

                        mat.EnableKeyword("_ALPHATEST_ON");
                        mat.EnableKeyword("_DOUBLESIDED_ON");
                        mat.DisableKeyword("_BLENDMODE_ALPHA");
                        mat.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
                    }
                }

                //test to see if gloss is absent but roughness is present...
                bool useRoughness = (gloss == null && roughness != null);
                if (texPack < 1 || shaderType < 1)
                {
                    mapName = "Masks";
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", "Masks"): texPath + "Masks";
                    p           = tempTexName + ".png";
                    mat.SetFloat("_Metallic", 1.0f);
                    tex = MegascansImageUtils.PackTextures(metallic, AO, displacement, useRoughness ? roughness : gloss, p, useRoughness);
                    mat.SetTexture("_MaskMap", tex);
                    mat.EnableKeyword("_MASKMAP");
                    mat.SetFloat("_MaterialID", 1);
                    mat.SetTexture("_MetallicGlossMap", tex);
                    mat.EnableKeyword("_METALLICSPECGLOSSMAP");
                    mat.EnableKeyword("_METALLICGLOSSMAP");
                }

                //do we need to process a specular map?
                if (texPack > 0 && specular != null)
                {
                    texMapName = (string)mapNames["Specular"];
                    mapName    = "Specular + Gloss";
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName): texPath + texMapName;
                    p           = tempTexName + ".png";
                    tex         = MegascansImageUtils.PackTextures(specular, useRoughness ? roughness : gloss, p, useRoughness);
                    mat.SetTexture("_SpecGlossMap", tex);
                    mat.SetColor("_SpecColor", new UnityEngine.Color(1.0f, 1.0f, 1.0f));
                    mat.SetColor("_SpecularColor", new UnityEngine.Color(1.0f, 1.0f, 1.0f));
                    mat.SetFloat("_WorkflowMode", 0);
                    mat.SetFloat("_MaterialID", 4);
                    mat.SetTexture("_SpecularColorMap", tex);
                    mat.EnableKeyword("_METALLICSPECGLOSSMAP");
                    mat.EnableKeyword("_SPECGLOSSMAP");
                    mat.EnableKeyword("_SPECULAR_SETUP");
                    mat.EnableKeyword("_SPECULARCOLORMAP");
                    mat.EnableKeyword("_MATERIAL_FEATURE_SPECULAR_COLOR");
                }

                //handle any textures which can just be converted in place.
                if (normals != null)
                {
                    texMapName = (string)mapNames["Normal"];
                    mapName    = "Normals";
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName): texPath + texMapName;
                    p           = tempTexName + ".png";
                    MegascansImageUtils.CreateTexture(normals, p);
                    tex = MegascansImageUtils.TextureImportSetup(p, true, false);
                    mat.SetTexture("_BumpMap", tex);
                    mat.SetTexture("_NormalMap", tex);
                    mat.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
                    mat.EnableKeyword("_NORMALMAP");

                    mapName     = "Normals_Terrain";
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName + "_Terrain"): texPath + texMapName + "_Terrain";
                    p           = tempTexName + ".png";

                    if (generateTerrainNormal && type.ToLower().Contains("surface"))
                    {
                        MegascansImageUtils.ImportTerrainNormal(normals, p);
                    }
                }

                if (displacement != null && dispType > 0)
                {
                    texMapName = (string)mapNames["Displacement"];
                    mapName    = "Displacement";
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName): texPath + texMapName;
                    p           = tempTexName + ".png";
                    MegascansImageUtils.CreateTexture(displacement, p);
                    tex = MegascansImageUtils.TextureImportSetup(p, false, false);
                    mat.SetTexture("_HeightMap", tex);
                    mat.SetTexture("_ParallaxMap", tex);
                    mat.EnableKeyword("_DISPLACEMENT_LOCK_TILING_SCALE");
                    if (dispType == 1)
                    {
                        mat.EnableKeyword("_VERTEX_DISPLACEMENT");
                        mat.EnableKeyword("_VERTEX_DISPLACEMENT_LOCK_OBJECT_SCALE");
                    }
                    if (dispType == 2)
                    {
                        mat.EnableKeyword("_PIXEL_DISPLACEMENT");
                        mat.EnableKeyword("_PIXEL_DISPLACEMENT_LOCK_OBJECT_SCALE");
                    }
                }

                //occlusion may or may not need to be packed, depending on the shader used.
                if (shaderType > 1 && AO != null)
                {
                    texMapName = (string)mapNames["AO"];
                    mapName    = "AO";
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName): texPath + texMapName;
                    p           = tempTexName + ".png";
                    MegascansImageUtils.CreateTexture(AO, p);
                    tex = MegascansImageUtils.TextureImportSetup(p, false, false);
                    mat.SetTexture("_OcclusionMap", tex);
                    mat.EnableKeyword("_OCCLUSIONMAP");
                }

                if (translucency != null)
                {
                    texMapName = (string)mapNames["Translucency"];
                    mapName    = "Translucency";
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);
                    tempTexName = texPath.Contains("$mapName") ? texPath.Replace("$mapName", texMapName): texPath + texMapName;
                    p           = tempTexName + ".png";
                    tex         = MegascansImageUtils.PackTextures(translucency, translucency, translucency, null, p);
                    mat.SetInt("_MaterialID", 0);
                    mat.SetInt("_DiffusionProfile", 1);
                    mat.SetFloat("_EnableSubsurfaceScattering", 1);
                    mat.SetTexture("_SubsurfaceMaskMap", tex);
                    mat.SetTexture("_ThicknessMap", tex);
                    if (plant)
                    {
                        mat.SetInt("_DiffusionProfile", 2);
                        mat.SetFloat("_CoatMask", 0.0f);
                        mat.SetInt("_EnableWind", 1);
                        mat.EnableKeyword("_VERTEX_WIND");
                    }
                    mat.EnableKeyword("_SUBSURFACE_MASK_MAP");
                    mat.EnableKeyword("_THICKNESSMAP");
                    mat.EnableKeyword("_MATERIAL_FEATURE_TRANSMISSION");
                }
                return(mat);
            } catch (Exception ex) {
                Debug.Log("Exception: " + ex.ToString());
                MegascansUtilities.HideProgressBar();
                return(null);
            }
        }