Example #1
0
        private void LoadLocalImage(Material mat, string floderPath, LoadedTextureType texType)
        {
            //递归查找贴图
            string texturePath = ZipUtility.FindFileFullName(floderPath, mat.name + texType.ToString());

            if (string.IsNullOrEmpty(texturePath))
            {
                return;
            }

            // Debug.LogFormat("<color=#ff0000>找到贴图:{0}</color>", texturePath);
            FileInfo textureFI = new FileInfo(texturePath);

            byte[] texBytes;
            //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(16, 16);
            //if (textureFI.Extension.Equals(".tga", StringComparison.OrdinalIgnoreCase))
            //{
            //    //TGA tgaMap = new TGA(texturePath);
            //    TGA2PNG.OpenTGAFile(GetImageByte(texturePath), ref bitmap);
            //    //texturePath = $"{textureFI.Directory}\\{textureFI.Name}.png";
            //    MemoryStream ms = new MemoryStream();
            //    //tgaMap.ToBitmap().Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            //    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            //    texBytes = ms.ToArray();
            //    //ms.Dispose();
            //}
            //else
            {
                texBytes = File.ReadAllBytes(texturePath);
            }

            //UnityWebRequest request = UnityWebRequestTexture.GetTexture($"file:///{texturePath}");
            //await request.SendWebRequest();
            //Texture2D tex2D = DownloadHandlerTexture.GetContent(request);

            Texture2D tex2D = new Texture2D(16, 16);

            tex2D.LoadImage(texBytes);
            tex2D.Compress(false);
            //Debug.Log("材质球 - " + mat.name);

            switch (texType)
            {
            case LoadedTextureType._Albedo:
                if (mat.HasProperty("_MainTex"))
                {
                    mat.SetTexture("_MainTex", tex2D);
                }
                break;

            case LoadedTextureType._AlbedoTransparency:
                if (mat.HasProperty("_MainTex"))
                {
                    mat.SetTexture("_MainTex", tex2D);
                }
                break;

            case LoadedTextureType._Metallic:
                if (mat.HasProperty("_MetallicGlossMap"))
                {
                    mat.color = Color.white;
                    mat.SetTexture("_MetallicGlossMap", tex2D);
                    //if (mat.HasProperty("_GlossMapScale"))
                    //{
                    //    //float glossValue = mat.GetFloat("_Glossiness");
                    //    //float setGlossValue = glossValue <= 0 ? 0.5f : glossValue;
                    //    mat.SetFloat("_GlossMapScale", 1f);
                    //}
                }
                break;

            case LoadedTextureType._MetallicSmoothness:
                if (mat.HasProperty("_MetallicGlossMap"))
                {
                    mat.color = Color.white;
                    mat.SetTexture("_MetallicGlossMap", tex2D);
                    //if (mat.HasProperty("_GlossMapScale"))
                    //{
                    //    //float glossValue = mat.GetFloat("_Glossiness");
                    //    //float setGlossValue = glossValue <= 0 ? 0.5f : glossValue;
                    //    mat.SetFloat("_GlossMapScale", 1f);
                    //}
                }
                break;

            case LoadedTextureType._Normal:
                if (mat.HasProperty("_BumpMap"))
                {
                    mat.SetTexture("_BumpMap", tex2D);
                }
                break;

            case LoadedTextureType._ao:
                if (mat.HasProperty("_OcclusionMap"))
                {
                    mat.SetTexture("_OcclusionMap", tex2D);
                }
                break;

            default:
                break;
            }
        }