示例#1
0
        void ImportAllTextures(Material mat, JArray texturesList)
        {
            try {
                List <string> typesOfTexturesAvailable = new List <string>();
                for (int i = 0; i < texturesList.Count; i++)
                {
                    typesOfTexturesAvailable.Add((string)texturesList[i]["type"]);
                }

                string    destTexPath;
                Texture2D tex;

                for (int i = 0; i < texturesList.Count; i++)
                {
                    mapName = (string)texturesList[i]["type"];
                    MegascansUtilities.UpdateProgressBar(1.0f, "Processing Asset " + assetName, "Importing texture: " + mapName);

                    if ((string)texturesList[i]["type"] == "albedo" || ((string)texturesList[i]["type"] == "diffuse" && !typesOfTexturesAvailable.Contains("albedo")))
                    {
                        destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                        MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath);
                        tex = texPrcsr.ImportTexture();

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

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

                        if (MegascansUtilities.AlbedoHasOpacity((JObject)texturesList[i]["channelsData"]))
                        {
                            float alphaCutoff = 0.33f;
                            texPrcsr.AdjustAlphaCutoff();

                            if (shaderType > 0)
                            {
                                mat.SetFloat("_AlphaClip", 1);
                                mat.SetFloat("_Cutoff", 0.1f);
                                mat.SetFloat("_Mode", 1);
                                mat.SetFloat("_Cull", 0);
                                mat.EnableKeyword("_ALPHATEST_ON");
                            }
                            else
                            {
                                mat.SetInt("_AlphaCutoffEnable", 1);
                                mat.SetFloat("_AlphaCutoff", alphaCutoff);
                                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");
                            }
                        }
                    }
                    else if ((string)texturesList[i]["type"] == "specular")
                    {
                        if (texPack > 0)
                        {
                            destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                            MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath);
                            tex = texPrcsr.ImportTexture();

                            mat.SetTexture("_SpecGlossMap", tex);
                            mat.SetTexture("_SpecularColorMap", 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.EnableKeyword("_METALLICSPECGLOSSMAP");
                            mat.EnableKeyword("_SPECGLOSSMAP");
                            mat.EnableKeyword("_SPECULAR_SETUP");
                            mat.EnableKeyword("_SPECULARCOLORMAP");
                            mat.EnableKeyword("_MATERIAL_FEATURE_SPECULAR_COLOR");
                        }
                    }
                    else if ((string)texturesList[i]["type"] == "masks")
                    {
                        if (texPack < 1 || shaderType < 1)
                        {
                            destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                            MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath, false, false);
                            tex = texPrcsr.ImportTexture();

                            mat.SetTexture("_MaskMap", tex);
                            mat.SetTexture("_MetallicGlossMap", tex);
                            mat.EnableKeyword("_MASKMAP");
                            mat.SetFloat("_MaterialID", 1);
                            mat.EnableKeyword("_METALLICSPECGLOSSMAP");
                            mat.EnableKeyword("_METALLICGLOSSMAP");

                            bool hasMetalness;
                            bool hasAO;
                            bool hasGloss;

                            MegascansUtilities.MaskMapComponents((JObject)texturesList[i]["channelsData"], out hasMetalness, out hasAO, out hasGloss);

                            if (!hasMetalness)
                            {
                                mat.SetFloat("_Metallic", 1.0f);
                            }

                            if (hasAO)
                            {
                                mat.SetTexture("_OcclusionMap", tex);
                                mat.EnableKeyword("_OCCLUSIONMAP");
                            }
                        }
                    }
                    else if ((string)texturesList[i]["type"] == "normal")
                    {
                        string normalMapPath = (string)texturesList[i]["path"];
                        if (activeLOD == "high" && !normalMapPath.Contains("NormalBump"))
                        {
                            for (int x = 0; x < 10; x++)
                            {
                                string n = normalMapPath.Replace("_LOD" + x.ToString(), "Bump");
                                if (File.Exists(n))
                                {
                                    normalMapPath = n;
                                    break;
                                }
                            }
                            if (normalMapPath.Contains("NormalBump"))
                            {
                                continue;
                            }
                        }

                        destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                        MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor(normalMapPath, destTexPath, true, false);
                        tex = texPrcsr.ImportTexture();
                        mat.SetTexture("_BumpMap", tex);
                        mat.SetTexture("_NormalMap", tex);
                        mat.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
                        mat.EnableKeyword("_NORMALMAP");
                    }
                    else if ((string)texturesList[i]["type"] == "ao" && texPack > 0)
                    {
                        destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                        MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath, false, false);
                        tex = texPrcsr.ImportTexture();
                        mat.SetTexture("_OcclusionMap", tex);
                        mat.EnableKeyword("_OCCLUSIONMAP");
                    }
                    else if ((string)texturesList[i]["type"] == "displacement")
                    {
                        if (dispType > 0)
                        {
                            destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                            MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath, false, false);
                            tex = texPrcsr.ImportTexture();
                            mat.SetTexture("_HeightMap", tex);
                            mat.SetTexture("_ParallaxMap", tex);
                            mat.EnableKeyword("_DISPLACEMENT_LOCK_TILING_SCALE");
                            if (shaderType == 0)
                            {
                                mat.EnableKeyword("_HEIGHTMAP");
                            }
                            if (dispType == 1)
                            {
                                mat.EnableKeyword("_VERTEX_DISPLACEMENT");
                                mat.EnableKeyword("_VERTEX_DISPLACEMENT_LOCK_OBJECT_SCALE");
                            }
                            else if (dispType == 2)
                            {
                                mat.EnableKeyword("_PARALLAXMAP");
                                mat.EnableKeyword("_PIXEL_DISPLACEMENT");
                                mat.EnableKeyword("_PIXEL_DISPLACEMENT_LOCK_OBJECT_SCALE");
                            }
                        }
                    }
                    else if ((string)texturesList[i]["type"] == "translucency")
                    {
                        destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                        MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath);
                        tex = texPrcsr.ImportTexture();

                        mat.SetTexture("_SubsurfaceMaskMap", tex);
                        mat.EnableKeyword("_SUBSURFACE_MASK_MAP");
                        mat.SetInt("_DiffusionProfile", 1);
                        mat.SetFloat("_EnableSubsurfaceScattering", 1);
                        if (!typesOfTexturesAvailable.Contains("transmission"))
                        {
                            mat.SetTexture("_ThicknessMap", tex);
                            mat.EnableKeyword("_THICKNESSMAP");
                        }
                        if (plant)
                        {
                            mat.SetInt("_DiffusionProfile", 2);
                            mat.SetFloat("_CoatMask", 0.0f);
                            mat.SetInt("_EnableWind", 1);
                            mat.EnableKeyword("_VERTEX_WIND");
                        }
                        MegascansMaterialUtils.AddSSSSettings(mat, shaderType);
                    }
                    else if ((string)texturesList[i]["type"] == "transmission")
                    {
                        destTexPath = Path.Combine(texPath, (string)texturesList[i]["nameOverride"]);
                        MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor((string)texturesList[i]["path"], destTexPath, false, false);
                        tex = texPrcsr.ImportTexture();

                        mat.SetTexture("_ThicknessMap", tex);
                        mat.EnableKeyword("_THICKNESSMAP");
                        mat.SetInt("_DiffusionProfile", 2);
                        MegascansMaterialUtils.AddSSSSettings(mat, shaderType);
                    }
                    else if (importAllTextures)
                    {
                        mapName = (string)texturesList[i]["type"];
                        string mapPath        = (string)texturesList[i]["path"];
                        string otherTexFolder = MegascansUtilities.ValidateFolderCreate(texPath, "Others");
                        destTexPath = Path.Combine(otherTexFolder, (string)texturesList[i]["nameOverride"]);
                        MegascansTextureProcessor texPrcsr = new MegascansTextureProcessor(mapPath, destTexPath);
                        tex = texPrcsr.ImportTexture();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Log("Exception::MegascansImporter::ImportAllTextures:: " + ex.ToString());
                MegascansUtilities.HideProgressBar();
            }
        }